Jobanpreet commited on
Commit
c80f682
·
verified ·
1 Parent(s): e9b42f3

Delete advance_post.py

Browse files
Files changed (1) hide show
  1. advance_post.py +0 -74
advance_post.py DELETED
@@ -1,74 +0,0 @@
1
- import requests
2
- from langchain.output_parsers import ResponseSchema, StructuredOutputParser
3
- from langchain.prompts import PromptTemplate
4
- from langchain_community.document_loaders import WebBaseLoader
5
- from langchain.prompts import ChatPromptTemplate
6
- from langchain_core.output_parsers import StrOutputParser
7
- import nest_asyncio
8
-
9
-
10
- def google_search(linkedin_post,model , google_api_key, search_engine_id , num_results_per_query=[3,2,1]):
11
-
12
- response_schemas = [
13
- ResponseSchema(name="answer", description="These are the top three relevant questions from the LinkedIn post" , type="list")]
14
- output_parser = StructuredOutputParser.from_response_schemas(response_schemas)
15
- format_instructions = output_parser.get_format_instructions()
16
-
17
- template = """
18
- 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.:
19
- LinkedIn post: {post}
20
- {format_instructions}
21
-
22
- """
23
- prompt = PromptTemplate(
24
- template=template,
25
- input_variables=["post"],
26
- partial_variables={"format_instructions": format_instructions},
27
- )
28
-
29
- chain = prompt | model | output_parser
30
- result=chain.invoke({"post": linkedin_post})
31
- questions=result['answer']
32
- print(questions)
33
-
34
- all_links = []
35
- for query, num_results in zip(questions, num_results_per_query):
36
- url = f"https://www.googleapis.com/customsearch/v1?key={google_api_key}&cx={search_engine_id}&q={query}&tbm=nws&num={num_results}"
37
- headers = {'Cookie': 'NID=513=KqMRZpKGj6WedOM42XZfrWSUunISFtrQ1twN2s6GEO_lIwb4SzNBCoRHw1Z6lmrRjuSHMxW2wIm1kL20piObJbroQQR5Sr3YSuCTXqH9UstqwzvSaUgS6P40fPvq9OKeDxWg3O8UGTYX_7g8xR76ox80aUZ4oy14DCjgwNInLDc'}
38
- response = requests.get(url, headers=headers)
39
- search_results = response.json()
40
- links = [item['link'] for item in search_results.get('items', [])]
41
- all_links.extend(links)
42
-
43
-
44
- return all_links
45
-
46
-
47
- nest_asyncio.apply()
48
- def advanced_post(all_links ,model ,linkedinpost):
49
- loader = WebBaseLoader(all_links,encoding="utf-8")
50
- loader.requests_per_second = 1
51
- docs = loader.load()
52
- 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.
53
- 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.
54
-
55
- Linkedin post:{post}
56
- Documents: {content}"""
57
-
58
- prompt = ChatPromptTemplate.from_template(template)
59
- chain= prompt | model | StrOutputParser()
60
- result=chain.invoke({'post':linkedinpost , 'content':docs})
61
- return result , docs
62
-
63
-
64
-
65
-
66
-
67
-
68
-
69
-
70
-
71
-
72
-
73
-
74
-