Jobanpreet commited on
Commit
7761c23
·
verified ·
1 Parent(s): d7b05de

Delete advance_post.py

Browse files
Files changed (1) hide show
  1. advance_post.py +0 -79
advance_post.py DELETED
@@ -1,79 +0,0 @@
1
- import requests
2
- from langchain.output_parsers import ResponseSchema, StructuredOutputParser
3
- from langchain.prompts import PromptTemplate
4
- from langchain_openai import ChatOpenAI
5
- from langchain_community.document_loaders import WebBaseLoader
6
- from langchain.prompts import ChatPromptTemplate
7
- from langchain_core.output_parsers import StrOutputParser
8
- from langchain_groq import ChatGroq
9
-
10
- import nest_asyncio
11
-
12
- def google_search(linkedin_post,openai_api_key, google_api_key, search_engine_id , num_results_per_query=[3,2,1]):
13
-
14
- response_schemas = [
15
- ResponseSchema(name="answer", description="These are the top three relevant questions from the LinkedIn post" , type="list")]
16
- output_parser = StructuredOutputParser.from_response_schemas(response_schemas)
17
- format_instructions = output_parser.get_format_instructions()
18
-
19
- template = """
20
- You are a helpful question extractor bot. You are provided with LinkedIn post and your task is to extract the top three relevant questions from the post which are related to the topics of the post only.:
21
- LinkedIn post: {post}
22
- {format_instructions}
23
-
24
- """
25
- prompt = PromptTemplate(
26
- template=template,
27
- input_variables=["post"],
28
- partial_variables={"format_instructions": format_instructions},
29
- )
30
- model=ChatOpenAI(api_key=openai_api_key, model="gpt-4-turbo-preview", temperature=0)
31
- #model = ChatGroq(temperature=0, groq_api_key="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", model_name="mixtral-8x7b-32768")
32
- chain = prompt | model | output_parser
33
- result=chain.invoke({"post": linkedin_post})
34
- questions=result['answer']
35
- print(questions)
36
-
37
- all_links = []
38
- for query, num_results in zip(questions, num_results_per_query):
39
- url = f"https://www.googleapis.com/customsearch/v1?key={google_api_key}&cx={search_engine_id}&q={query}&tbm=nws&num={num_results}"
40
- headers = {'Cookie': 'NID=513=KqMRZpKGj6WedOM42XZfrWSUunISFtrQ1twN2s6GEO_lIwb4SzNBCoRHw1Z6lmrRjuSHMxW2wIm1kL20piObJbroQQR5Sr3YSuCTXqH9UstqwzvSaUgS6P40fPvq9OKeDxWg3O8UGTYX_7g8xR76ox80aUZ4oy14DCjgwNInLDc'}
41
- response = requests.get(url, headers=headers)
42
- search_results = response.json()
43
- links = [item['link'] for item in search_results.get('items', [])]
44
- all_links.extend(links)
45
-
46
-
47
- return all_links
48
-
49
-
50
- nest_asyncio.apply()
51
- def advanced_post(all_links ,openai_api_key ,linkedinpost):
52
- loader = WebBaseLoader(all_links,encoding="utf-8")
53
- loader.requests_per_second = 1
54
- docs = loader.load()
55
- template="""You are a helpful linkedin post creator . You are provided with LinkedIn post and documents related to the post extracted from different articles from the internet.
56
- Your task is to create a new linkedin post but content should be taken from the documents according to the semantic similarity of the post content with document content.
57
-
58
- Linkedin post:{post}
59
- Documents: {content}"""
60
-
61
- prompt = ChatPromptTemplate.from_template(template)
62
- model=ChatOpenAI(temperature=0 ,api_key=openai_api_key , model="gpt-4-turbo-preview")
63
- #model = ChatGroq(temperature=0, groq_api_key="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", model_name="mixtral-8x7b-32768")
64
- chain= prompt | model | StrOutputParser()
65
- result=chain.invoke({'post':linkedinpost , 'content':docs})
66
- return result , docs
67
-
68
-
69
-
70
-
71
-
72
-
73
-
74
-
75
-
76
-
77
-
78
-
79
-