AhsanRazi commited on
Commit
76afb48
·
verified ·
1 Parent(s): b1365c6

Update blog_title_generator.py

Browse files
Files changed (1) hide show
  1. blog_title_generator.py +166 -163
blog_title_generator.py CHANGED
@@ -1,164 +1,167 @@
1
- import time
2
- import json
3
- from langchain_community.tools import DuckDuckGoSearchRun
4
- from langgraph.graph import MessagesState
5
- from langchain_core.prompts import PromptTemplate
6
- from langchain_core.messages import HumanMessage, SystemMessage
7
- from langgraph.graph import START, StateGraph
8
- from langgraph.prebuilt import tools_condition
9
- from langgraph.prebuilt import ToolNode
10
- from langgraph.graph.state import CompiledStateGraph
11
-
12
-
13
- def blog_titles_agent(llm, state):
14
-
15
- queries = state["search_queries"]
16
-
17
- def duckduckgo_search(query: str) -> str:
18
- """
19
- Performs a search on DuckDuckGo using the provided query.
20
-
21
- Args:
22
- query (str): The search query to be executed.
23
-
24
- Returns:
25
- str: The result of the search query.
26
- """
27
- search = DuckDuckGoSearchRun()
28
- result = search.invoke(query)
29
- print("Tool Call")
30
- return result
31
-
32
-
33
- tool = [duckduckgo_search]
34
-
35
- llm_with_tools = llm.bind_tools(tool)
36
-
37
- # System message
38
- sys_msg = SystemMessage(content="""
39
- Task: You are an expert Blog Title generator. Generate a blog Title on the Topic given to you by using the examples below as a guide.
40
-
41
- Examples:
42
- - Is It Time To Replace Your Boiler
43
- - Cold Radiators Let’s Look at the Reasons
44
- - Why Invest in a New Central Heating System
45
- - When Is the Right Time to Replace Your Boiler
46
- - Common Boiler Faults That Are Repaired by Gas Engineers
47
- - Air Source Heat Pumps vs. Traditional Boilers: Which Is Right for You
48
- - What Are Smart Thermostats and Are They Worth Installing
49
-
50
- Context:
51
- - Generate only 3 Blog Titles.
52
- - The blog Title should be engaging.
53
- - Always Use the attached tool duckduckgo_search to get the latest information about the Topic.
54
-
55
- Constraints:
56
- - Dont include any punctuation mark in the Title.
57
-
58
- Output Format:
59
- - Give the Blog Titles in the JSON List of String Format.
60
- - Don't include any extra information.
61
-
62
- """)
63
-
64
- # Node
65
- def assistant(state: MessagesState) -> MessagesState:
66
- return {"messages": [llm_with_tools.invoke([sys_msg] + state["messages"])]}
67
-
68
-
69
- builder: StateGraph = StateGraph(MessagesState)
70
-
71
- builder.add_node("assistant", assistant)
72
- builder.add_node("tools", ToolNode(tool))
73
-
74
-
75
- builder.add_edge(START, "assistant")
76
- builder.add_conditional_edges(
77
- "assistant",
78
- tools_condition,
79
- )
80
- builder.add_edge("tools", "assistant")
81
- react_graph: CompiledStateGraph = builder.compile()
82
-
83
-
84
- titles = []
85
- for q in queries:
86
- time.sleep(5)
87
- messages = [HumanMessage(content=f"Search this query: {q}")]
88
- messages = react_graph.invoke({"messages": messages})
89
- t = messages['messages'][-1].content
90
- t = t.replace("json", "").replace("```", "").strip()
91
- t = json.loads(t)
92
- for i in t:
93
- titles.append(i)
94
-
95
- print(titles)
96
- return {"blog_topics": titles}
97
-
98
-
99
- def blog_titles_llm(llm, state):
100
- blog_titles_prompt_template = PromptTemplate.from_template("""
101
- Task: You are an expert Blog Titles generator. Generate a blog Title on the Category given to you by using the examples below as a guide.
102
-
103
-
104
- Examples:
105
- - Is It Time To Replace Your Boiler
106
- - Cold Radiators Let’s Look at the Reasons
107
- - Why Invest in a New Central Heating System
108
- - When Is the Right Time to Replace Your Boiler
109
- - Common Boiler Faults That Are Repaired by Gas Engineers
110
- - Air Source Heat Pumps vs. Traditional Boilers: Which Is Right for You
111
- - What Are Smart Thermostats and Are They Worth Installing
112
-
113
- Context:
114
- - Generate atleast 10 Blog Titles.
115
- - The blog Title should be engaging.
116
-
117
- Constraints:
118
- - Dont include any punctuation mark in the Title.
119
-
120
- Output Format:
121
- - Give the Blog Titles in the JSON List of String Format.
122
- - Don't include any extra information.
123
-
124
- Category = {category}
125
- """)
126
-
127
- prompt = blog_titles_prompt_template.invoke({"category": "Air Conditioning"})
128
- response = llm.invoke(prompt)
129
- content = response.content
130
- titles = content.replace("json", "").replace("```", "").strip() # type: ignore
131
- titles = json.loads(titles)
132
- print(titles)
133
- return {"blog_topics": titles}
134
-
135
-
136
-
137
-
138
-
139
-
140
-
141
-
142
-
143
- # from langchain_community.tools import DuckDuckGoSearchResults
144
- # import time
145
-
146
- # def search_topics(queries):
147
- # titles = []
148
- # search = DuckDuckGoSearchResults(output_format="list")
149
- # for query in queries:
150
- # time.sleep(5)
151
- # response = search.invoke(query)
152
- # for r in response:
153
- # titles.append(r['title'])
154
-
155
- # return titles
156
-
157
- # from langchain_community.tools import DuckDuckGoSearchRun
158
-
159
- # def searched_blog_topics(state):
160
- # queries = state["search_queries"]
161
- # topics = search_topics(queries)
162
- # return {"blog_topics": topics}
163
-
 
 
 
164
 
 
1
+ import time
2
+ import json
3
+ from langchain_community.tools import DuckDuckGoSearchRun
4
+ from langgraph.graph import MessagesState
5
+ from langchain_core.prompts import PromptTemplate
6
+ from langchain_core.messages import HumanMessage, SystemMessage
7
+ from langgraph.graph import START, StateGraph
8
+ from langgraph.prebuilt import tools_condition
9
+ from langgraph.prebuilt import ToolNode
10
+ from langgraph.graph.state import CompiledStateGraph
11
+
12
+
13
+ def blog_titles_agent(llm, state):
14
+
15
+ queries = state["search_queries"]
16
+
17
+ def duckduckgo_search(query: str) -> str:
18
+ """
19
+ Performs a search on DuckDuckGo using the provided query.
20
+
21
+ Args:
22
+ query (str): The search query to be executed.
23
+
24
+ Returns:
25
+ str: The result of the search query.
26
+ """
27
+ search = DuckDuckGoSearchRun()
28
+ result = search.invoke(query)
29
+ print("Tool Call")
30
+ return result
31
+
32
+
33
+ tool = [duckduckgo_search]
34
+
35
+ llm_with_tools = llm.bind_tools(tool)
36
+
37
+ # System message
38
+ sys_msg = SystemMessage(content="""
39
+ Task: You are an expert Blog Title generator. Generate a blog Title on the Topic given to you by using the examples below as a guide.
40
+
41
+ Examples:
42
+ - Is It Time To Replace Your Boiler
43
+ - Cold Radiators Let’s Look at the Reasons
44
+ - Why Invest in a New Central Heating System
45
+ - When Is the Right Time to Replace Your Boiler
46
+ - Common Boiler Faults That Are Repaired by Gas Engineers
47
+ - Air Source Heat Pumps vs. Traditional Boilers: Which Is Right for You
48
+ - What Are Smart Thermostats and Are They Worth Installing
49
+
50
+ Context:
51
+ - Generate only 3 Blog Titles.
52
+ - The blog Title should be engaging.
53
+ - Always Use the attached tool duckduckgo_search to get the latest information about the Topic.
54
+
55
+ Constraints:
56
+ - Dont include any punctuation mark in the Title.
57
+
58
+ Output Format:
59
+ - Give the Blog Titles in the JSON List of String Format.
60
+ - Don't include any extra information.
61
+
62
+ """)
63
+
64
+ # Node
65
+ def assistant(state: MessagesState) -> MessagesState:
66
+ return {"messages": [llm_with_tools.invoke([sys_msg] + state["messages"])]}
67
+
68
+
69
+ builder: StateGraph = StateGraph(MessagesState)
70
+
71
+ builder.add_node("assistant", assistant)
72
+ builder.add_node("tools", ToolNode(tool))
73
+
74
+
75
+ builder.add_edge(START, "assistant")
76
+ builder.add_conditional_edges(
77
+ "assistant",
78
+ tools_condition,
79
+ )
80
+ builder.add_edge("tools", "assistant")
81
+ react_graph: CompiledStateGraph = builder.compile()
82
+
83
+
84
+ titles = []
85
+ for q in queries:
86
+ time.sleep(5)
87
+ messages = [HumanMessage(content=f"Search this query: {q}")]
88
+ messages = react_graph.invoke({"messages": messages})
89
+ t = messages['messages'][-1].content
90
+ t = t.replace("json", "").replace("```", "").strip()
91
+ t = json.loads(t)
92
+ for i in t:
93
+ titles.append(i)
94
+
95
+ print(titles)
96
+ return {"blog_topics": titles}
97
+
98
+
99
+ def blog_titles_llm(llm, state):
100
+
101
+ topic = state["topic"]
102
+
103
+ blog_titles_prompt_template = PromptTemplate.from_template("""
104
+ Task: You are an expert Blog Titles generator. Generate a blog Title on the Category given to you by using the examples below as a guide.
105
+
106
+
107
+ Examples:
108
+ - Is It Time To Replace Your Boiler
109
+ - Cold Radiators Let’s Look at the Reasons
110
+ - Why Invest in a New Central Heating System
111
+ - When Is the Right Time to Replace Your Boiler
112
+ - Common Boiler Faults That Are Repaired by Gas Engineers
113
+ - Air Source Heat Pumps vs. Traditional Boilers: Which Is Right for You
114
+ - What Are Smart Thermostats and Are They Worth Installing
115
+
116
+ Context:
117
+ - Generate atleast 10 Blog Titles.
118
+ - The blog Title should be engaging.
119
+
120
+ Constraints:
121
+ - Dont include any punctuation mark in the Title.
122
+
123
+ Output Format:
124
+ - Give the Blog Titles in the JSON List of String Format.
125
+ - Don't include any extra information.
126
+
127
+ Category = {category}
128
+ """)
129
+
130
+ prompt = blog_titles_prompt_template.invoke({"category": topic})
131
+ response = llm.invoke(prompt)
132
+ content = response.content
133
+ titles = content.replace("json", "").replace("```", "").strip() # type: ignore
134
+ titles = json.loads(titles)
135
+ print(titles)
136
+ return {"blog_topics": titles}
137
+
138
+
139
+
140
+
141
+
142
+
143
+
144
+
145
+
146
+ # from langchain_community.tools import DuckDuckGoSearchResults
147
+ # import time
148
+
149
+ # def search_topics(queries):
150
+ # titles = []
151
+ # search = DuckDuckGoSearchResults(output_format="list")
152
+ # for query in queries:
153
+ # time.sleep(5)
154
+ # response = search.invoke(query)
155
+ # for r in response:
156
+ # titles.append(r['title'])
157
+
158
+ # return titles
159
+
160
+ # from langchain_community.tools import DuckDuckGoSearchRun
161
+
162
+ # def searched_blog_topics(state):
163
+ # queries = state["search_queries"]
164
+ # topics = search_topics(queries)
165
+ # return {"blog_topics": topics}
166
+
167