Spaces:
Sleeping
Sleeping
AI News Agent
Browse files
src/langgraph_agenticai/graph/graph_builder.py
CHANGED
|
@@ -4,6 +4,7 @@ from langchain_core.prompts import ChatPromptTemplate
|
|
| 4 |
from src.langgraph_agenticai.state.state import State
|
| 5 |
from src.langgraph_agenticai.nodes.basic_chatbot_node import BasicChatbotNode
|
| 6 |
from src.langgraph_agenticai.nodes.chatbot_with_tools_node import ChatbotWithToolsNode
|
|
|
|
| 7 |
from src.langgraph_agenticai.tools.search_tool import get_tools, create_tool_node
|
| 8 |
|
| 9 |
class GraphBuilder:
|
|
@@ -55,6 +56,31 @@ class GraphBuilder:
|
|
| 55 |
self.graph_builder.add_edge(START,"chatbot")
|
| 56 |
self.graph_builder.add_conditional_edges("chatbot", tools_condition)
|
| 57 |
self.graph_builder.add_edge("tools", "chatbot")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
|
| 60 |
def setup_graph(self, usecase: str):
|
|
@@ -67,4 +93,8 @@ class GraphBuilder:
|
|
| 67 |
if usecase == "Chatbot with Tool":
|
| 68 |
self.chatbot_with_tools_build_graph()
|
| 69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
return self.graph_builder.compile()
|
|
|
|
| 4 |
from src.langgraph_agenticai.state.state import State
|
| 5 |
from src.langgraph_agenticai.nodes.basic_chatbot_node import BasicChatbotNode
|
| 6 |
from src.langgraph_agenticai.nodes.chatbot_with_tools_node import ChatbotWithToolsNode
|
| 7 |
+
from src.langgraph_agenticai.nodes.chatbot_ai_news_node import ChatbotAINewsNode
|
| 8 |
from src.langgraph_agenticai.tools.search_tool import get_tools, create_tool_node
|
| 9 |
|
| 10 |
class GraphBuilder:
|
|
|
|
| 56 |
self.graph_builder.add_edge(START,"chatbot")
|
| 57 |
self.graph_builder.add_conditional_edges("chatbot", tools_condition)
|
| 58 |
self.graph_builder.add_edge("tools", "chatbot")
|
| 59 |
+
|
| 60 |
+
def chatbot_ai_news_biuld_graph(self):
|
| 61 |
+
"""
|
| 62 |
+
Chatbot which provided AI News
|
| 63 |
+
|
| 64 |
+
"""
|
| 65 |
+
## Define the tool and tool node
|
| 66 |
+
tools = get_tools()
|
| 67 |
+
tool_node = create_tool_node(tools)
|
| 68 |
+
|
| 69 |
+
## Define the LLM
|
| 70 |
+
llm = self.llm
|
| 71 |
+
|
| 72 |
+
## Define Chatbot node
|
| 73 |
+
obj_chatbot_ai_news_node = ChatbotAINewsNode(llm)
|
| 74 |
+
chatbot_node = obj_chatbot_ai_news_node.create_ai_new_chatbot(tools)
|
| 75 |
+
|
| 76 |
+
## Add nodes
|
| 77 |
+
self.graph_builder.add_node("chatbot", chatbot_node)
|
| 78 |
+
self.graph_builder.add_node("tools", tool_node)
|
| 79 |
+
|
| 80 |
+
## define conditional and direct edges
|
| 81 |
+
self.graph_builder.add_edge(START,"chatbot")
|
| 82 |
+
self.graph_builder.add_conditional_edges("chatbot", tools_condition)
|
| 83 |
+
self.graph_builder.add_edge("tools", "chatbot")
|
| 84 |
|
| 85 |
|
| 86 |
def setup_graph(self, usecase: str):
|
|
|
|
| 93 |
if usecase == "Chatbot with Tool":
|
| 94 |
self.chatbot_with_tools_build_graph()
|
| 95 |
|
| 96 |
+
if usecase == "AI News":
|
| 97 |
+
self.chatbot_ai_news_biuld_graph()
|
| 98 |
+
pass
|
| 99 |
+
|
| 100 |
return self.graph_builder.compile()
|
src/langgraph_agenticai/nodes/chatbot_ai_news_node.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from src.langgraph_agenticai.state.state import State
|
| 2 |
+
from langchain_core.prompts import ChatPromptTemplate
|
| 3 |
+
from langchain.agents import create_openai_tools_agent, AgentExecutor
|
| 4 |
+
|
| 5 |
+
class ChatbotAINewsNode:
|
| 6 |
+
|
| 7 |
+
def __init__(self, model):
|
| 8 |
+
self.llm = model
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def create_ai_new_chatbot(self, tools):
|
| 12 |
+
|
| 13 |
+
# AI expert system prompt
|
| 14 |
+
system_prompt = """You are a professional AI news analyst with expertise in:
|
| 15 |
+
- Breaking technology news
|
| 16 |
+
- Emerging AI research trends
|
| 17 |
+
- Industry developments in artificial intelligence
|
| 18 |
+
- Analysis of AI ethics and policy
|
| 19 |
+
|
| 20 |
+
Guidelines:
|
| 21 |
+
1. Provide up-to-date, accurate information
|
| 22 |
+
2. Always verify facts using Tavily search
|
| 23 |
+
3. Include relevant sources from search results
|
| 24 |
+
4. Explain technical terms when needed
|
| 25 |
+
5. Maintain neutral, objective tone
|
| 26 |
+
|
| 27 |
+
Provide only information about the AI News and Trends. For other queries, please respond with the reson that you are only the AI News Expert.
|
| 28 |
+
"""
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
llm_with_tools = self.llm.bind_tools(tools)
|
| 32 |
+
|
| 33 |
+
def chatbot_node(state: State):
|
| 34 |
+
"""
|
| 35 |
+
Chatbot logic for processing the input state and returning a response.
|
| 36 |
+
"""
|
| 37 |
+
return {"messages": [llm_with_tools.invoke([system_prompt] + state["messages"])]}
|
| 38 |
+
|
| 39 |
+
return chatbot_node
|
src/langgraph_agenticai/ui/streamlit_ui/display_result.py
CHANGED
|
@@ -23,7 +23,7 @@ class DisplayResultStreamlit:
|
|
| 23 |
with st.chat_message("assistant"):
|
| 24 |
st.write(value["messages"].content)
|
| 25 |
|
| 26 |
-
elif usecase=="Chatbot with Tool":
|
| 27 |
# Prepare state and invoke the graph
|
| 28 |
initial_state = {"messages": [user_message]}
|
| 29 |
res = graph.invoke(initial_state)
|
|
|
|
| 23 |
with st.chat_message("assistant"):
|
| 24 |
st.write(value["messages"].content)
|
| 25 |
|
| 26 |
+
elif usecase=="Chatbot with Tool" or usecase=="AI News":
|
| 27 |
# Prepare state and invoke the graph
|
| 28 |
initial_state = {"messages": [user_message]}
|
| 29 |
res = graph.invoke(initial_state)
|
src/langgraph_agenticai/ui/streamlit_ui/loadui.py
CHANGED
|
@@ -52,7 +52,7 @@ class LoadStreamlitUI:
|
|
| 52 |
# Use case selection
|
| 53 |
self.user_controls["selected_usecase"] = st.selectbox("Select Usecases", usecase_options)
|
| 54 |
|
| 55 |
-
if self.user_controls["selected_usecase"] =="Chatbot with Tool":
|
| 56 |
# API key input
|
| 57 |
os.environ["TAVILY_API_KEY"] = self.user_controls["TAVILY_API_KEY"] = st.session_state["TAVILY_API_KEY"] = st.text_input("TAVILY API KEY",
|
| 58 |
type="password")
|
|
|
|
| 52 |
# Use case selection
|
| 53 |
self.user_controls["selected_usecase"] = st.selectbox("Select Usecases", usecase_options)
|
| 54 |
|
| 55 |
+
if self.user_controls["selected_usecase"] =="Chatbot with Tool" or self.user_controls["selected_usecase"] =="AI News":
|
| 56 |
# API key input
|
| 57 |
os.environ["TAVILY_API_KEY"] = self.user_controls["TAVILY_API_KEY"] = st.session_state["TAVILY_API_KEY"] = st.text_input("TAVILY API KEY",
|
| 58 |
type="password")
|