Spaces:
Sleeping
Sleeping
Update blog_title_generator.py
Browse files- blog_title_generator.py +26 -43
blog_title_generator.py
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
|
|
|
|
|
| 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
|
|
@@ -9,14 +13,19 @@ 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
|
| 18 |
"""
|
| 19 |
-
Performs a search on
|
| 20 |
|
| 21 |
Args:
|
| 22 |
query (str): The search query to be executed.
|
|
@@ -24,13 +33,19 @@ def blog_titles_agent(llm, state):
|
|
| 24 |
Returns:
|
| 25 |
str: The result of the search query.
|
| 26 |
"""
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
print("Tool Call")
|
| 30 |
-
return
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
tool = [
|
| 34 |
|
| 35 |
llm_with_tools = llm.bind_tools(tool)
|
| 36 |
|
|
@@ -50,7 +65,7 @@ def blog_titles_agent(llm, state):
|
|
| 50 |
Context:
|
| 51 |
- Generate only 3 Blog Titles.
|
| 52 |
- The blog Title should be engaging.
|
| 53 |
-
- Always Use the attached tool
|
| 54 |
|
| 55 |
Constraints:
|
| 56 |
- Dont include any punctuation mark in the Title.
|
|
@@ -86,6 +101,7 @@ def blog_titles_agent(llm, state):
|
|
| 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)
|
|
@@ -97,9 +113,7 @@ def blog_titles_agent(llm, state):
|
|
| 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 |
|
|
@@ -133,35 +147,4 @@ def blog_titles_llm(llm, state):
|
|
| 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 |
-
|
|
|
|
| 1 |
+
|
| 2 |
+
import os
|
| 3 |
import time
|
| 4 |
import json
|
| 5 |
+
from dotenv import load_dotenv
|
| 6 |
from langchain_community.tools import DuckDuckGoSearchRun
|
| 7 |
+
from langchain_community.tools import TavilySearchResults
|
| 8 |
from langgraph.graph import MessagesState
|
| 9 |
from langchain_core.prompts import PromptTemplate
|
| 10 |
from langchain_core.messages import HumanMessage, SystemMessage
|
|
|
|
| 13 |
from langgraph.prebuilt import ToolNode
|
| 14 |
from langgraph.graph.state import CompiledStateGraph
|
| 15 |
|
| 16 |
+
# Secret Key
|
| 17 |
+
load_dotenv(override=True)
|
| 18 |
+
tavily_api_key = os.getenv("TAVILY_API_KEY")
|
| 19 |
+
|
| 20 |
|
| 21 |
def blog_titles_agent(llm, state):
|
| 22 |
|
| 23 |
queries = state["search_queries"]
|
| 24 |
+
|
| 25 |
|
| 26 |
+
def tavily_search(query: str) -> str:
|
| 27 |
"""
|
| 28 |
+
Performs a search on Tavily using the provided query.
|
| 29 |
|
| 30 |
Args:
|
| 31 |
query (str): The search query to be executed.
|
|
|
|
| 33 |
Returns:
|
| 34 |
str: The result of the search query.
|
| 35 |
"""
|
| 36 |
+
tavily = TavilySearchResults(
|
| 37 |
+
max_results=5,
|
| 38 |
+
search_depth="advanced",
|
| 39 |
+
include_answer=True,
|
| 40 |
+
include_raw_content=True,
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
text = tavily.invoke({"query": f"{query}"})
|
| 44 |
print("Tool Call")
|
| 45 |
+
return text
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
tool = [tavily_search]
|
| 49 |
|
| 50 |
llm_with_tools = llm.bind_tools(tool)
|
| 51 |
|
|
|
|
| 65 |
Context:
|
| 66 |
- Generate only 3 Blog Titles.
|
| 67 |
- The blog Title should be engaging.
|
| 68 |
+
- Always Use the attached tool tavily_search to get the latest information about the Topic.
|
| 69 |
|
| 70 |
Constraints:
|
| 71 |
- Dont include any punctuation mark in the Title.
|
|
|
|
| 101 |
time.sleep(5)
|
| 102 |
messages = [HumanMessage(content=f"Search this query: {q}")]
|
| 103 |
messages = react_graph.invoke({"messages": messages})
|
| 104 |
+
print("\n",messages)
|
| 105 |
t = messages['messages'][-1].content
|
| 106 |
t = t.replace("json", "").replace("```", "").strip()
|
| 107 |
t = json.loads(t)
|
|
|
|
| 113 |
|
| 114 |
|
| 115 |
def blog_titles_llm(llm, state):
|
|
|
|
| 116 |
topic = state["topic"]
|
|
|
|
| 117 |
blog_titles_prompt_template = PromptTemplate.from_template("""
|
| 118 |
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.
|
| 119 |
|
|
|
|
| 147 |
titles = content.replace("json", "").replace("```", "").strip() # type: ignore
|
| 148 |
titles = json.loads(titles)
|
| 149 |
print(titles)
|
| 150 |
+
return {"blog_topics": titles}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|