Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ import os
|
|
| 2 |
import streamlit as st
|
| 3 |
import pandas as pd
|
| 4 |
import matplotlib.pyplot as plt
|
|
|
|
| 5 |
from langchain_community.tools.tavily_search import TavilySearchResults
|
| 6 |
from langchain_openai import ChatOpenAI
|
| 7 |
from langgraph.graph import MessagesState
|
|
@@ -9,7 +10,6 @@ from langgraph.graph import START, StateGraph
|
|
| 9 |
from langgraph.prebuilt import tools_condition
|
| 10 |
from langgraph.prebuilt import ToolNode
|
| 11 |
from langchain_core.messages import HumanMessage, SystemMessage
|
| 12 |
-
import tempfile
|
| 13 |
|
| 14 |
# ------------------- Environment Variable Setup -------------------
|
| 15 |
# Fetch API keys from environment variables
|
|
@@ -23,7 +23,6 @@ if not tavily_api_key:
|
|
| 23 |
raise ValueError("Missing required environment variable: TAVILY_API_KEY")
|
| 24 |
|
| 25 |
# ------------------- Tool Definitions -------------------
|
| 26 |
-
# Tavily Search Tool
|
| 27 |
tavily_tool = TavilySearchResults(max_results=5)
|
| 28 |
|
| 29 |
def multiply(a: int, b: int) -> int:
|
|
@@ -40,10 +39,9 @@ def divide(a: int, b: int) -> float:
|
|
| 40 |
raise ValueError("Division by zero is not allowed.")
|
| 41 |
return a / b
|
| 42 |
|
| 43 |
-
# Combine tools
|
| 44 |
tools = [add, multiply, divide, tavily_tool]
|
| 45 |
|
| 46 |
-
# ------------------- LLM
|
| 47 |
llm = ChatOpenAI(model="gpt-4o-mini")
|
| 48 |
llm_with_tools = llm.bind_tools(tools, parallel_tool_calls=False)
|
| 49 |
sys_msg = SystemMessage(content="You are a helpful assistant tasked with performing arithmetic and search on a set of inputs.")
|
|
@@ -53,7 +51,6 @@ def assistant(state: MessagesState):
|
|
| 53 |
"""Assistant node to invoke LLM with tools."""
|
| 54 |
return {"messages": [llm_with_tools.invoke([sys_msg] + state["messages"])]}
|
| 55 |
|
| 56 |
-
# Define the graph
|
| 57 |
app_graph = StateGraph(MessagesState)
|
| 58 |
app_graph.add_node("assistant", assistant)
|
| 59 |
app_graph.add_node("tools", ToolNode(tools))
|
|
@@ -62,25 +59,28 @@ app_graph.add_conditional_edges("assistant", tools_condition)
|
|
| 62 |
app_graph.add_edge("tools", "assistant")
|
| 63 |
react_graph = app_graph.compile()
|
| 64 |
|
| 65 |
-
# Save graph visualization as an image
|
| 66 |
-
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmpfile:
|
| 67 |
-
graph_image_path = tmpfile.name
|
| 68 |
-
graph = react_graph.get_graph(xray=True)
|
| 69 |
-
graph.draw_mermaid_png()
|
| 70 |
-
with open(tmpfile.name, "wb") as f:
|
| 71 |
-
f.write(graph._repr_png_()) # Save the graph image
|
| 72 |
-
|
| 73 |
-
|
| 74 |
# ------------------- Streamlit Interface -------------------
|
| 75 |
-
st.title("ReAct Agent
|
| 76 |
|
| 77 |
-
# Display the workflow graph
|
| 78 |
st.header("LangGraph Workflow Visualization")
|
| 79 |
-
st.image(graph_image_path, caption="LangGraph Workflow Visualization")
|
| 80 |
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
if st.button("Submit"):
|
| 86 |
if not user_question.strip():
|
|
@@ -91,14 +91,12 @@ if st.button("Submit"):
|
|
| 91 |
messages = [HumanMessage(content=user_question)]
|
| 92 |
response = react_graph.invoke({"messages": messages})
|
| 93 |
|
| 94 |
-
# Display results
|
| 95 |
st.subheader("Responses")
|
| 96 |
for m in response['messages']:
|
| 97 |
st.write(m.content)
|
| 98 |
-
|
| 99 |
st.success("Processing complete!")
|
| 100 |
|
| 101 |
-
# Example
|
| 102 |
st.sidebar.subheader("Example Questions")
|
| 103 |
st.sidebar.write("- Add 3 and 4. Multiply the result by 2. Divide it by 5.")
|
| 104 |
st.sidebar.write("- Tell me how many centuries Virat Kohli scored.")
|
|
|
|
| 2 |
import streamlit as st
|
| 3 |
import pandas as pd
|
| 4 |
import matplotlib.pyplot as plt
|
| 5 |
+
import networkx as nx
|
| 6 |
from langchain_community.tools.tavily_search import TavilySearchResults
|
| 7 |
from langchain_openai import ChatOpenAI
|
| 8 |
from langgraph.graph import MessagesState
|
|
|
|
| 10 |
from langgraph.prebuilt import tools_condition
|
| 11 |
from langgraph.prebuilt import ToolNode
|
| 12 |
from langchain_core.messages import HumanMessage, SystemMessage
|
|
|
|
| 13 |
|
| 14 |
# ------------------- Environment Variable Setup -------------------
|
| 15 |
# Fetch API keys from environment variables
|
|
|
|
| 23 |
raise ValueError("Missing required environment variable: TAVILY_API_KEY")
|
| 24 |
|
| 25 |
# ------------------- Tool Definitions -------------------
|
|
|
|
| 26 |
tavily_tool = TavilySearchResults(max_results=5)
|
| 27 |
|
| 28 |
def multiply(a: int, b: int) -> int:
|
|
|
|
| 39 |
raise ValueError("Division by zero is not allowed.")
|
| 40 |
return a / b
|
| 41 |
|
|
|
|
| 42 |
tools = [add, multiply, divide, tavily_tool]
|
| 43 |
|
| 44 |
+
# ------------------- LLM Setup -------------------
|
| 45 |
llm = ChatOpenAI(model="gpt-4o-mini")
|
| 46 |
llm_with_tools = llm.bind_tools(tools, parallel_tool_calls=False)
|
| 47 |
sys_msg = SystemMessage(content="You are a helpful assistant tasked with performing arithmetic and search on a set of inputs.")
|
|
|
|
| 51 |
"""Assistant node to invoke LLM with tools."""
|
| 52 |
return {"messages": [llm_with_tools.invoke([sys_msg] + state["messages"])]}
|
| 53 |
|
|
|
|
| 54 |
app_graph = StateGraph(MessagesState)
|
| 55 |
app_graph.add_node("assistant", assistant)
|
| 56 |
app_graph.add_node("tools", ToolNode(tools))
|
|
|
|
| 59 |
app_graph.add_edge("tools", "assistant")
|
| 60 |
react_graph = app_graph.compile()
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
# ------------------- Streamlit Interface -------------------
|
| 63 |
+
st.title("ReAct Agent)
|
| 64 |
|
| 65 |
+
# Display the workflow graph using NetworkX
|
| 66 |
st.header("LangGraph Workflow Visualization")
|
|
|
|
| 67 |
|
| 68 |
+
G = nx.DiGraph()
|
| 69 |
+
G.add_edge("START", "assistant")
|
| 70 |
+
G.add_edge("assistant", "tools", label="tools_condition")
|
| 71 |
+
G.add_edge("tools", "assistant", label="loop back")
|
| 72 |
+
|
| 73 |
+
plt.figure(figsize=(10, 6))
|
| 74 |
+
pos = nx.spring_layout(G, seed=42)
|
| 75 |
+
nx.draw(G, pos, with_labels=True, node_size=3000, node_color="lightblue", font_size=10, font_weight="bold")
|
| 76 |
+
nx.draw_networkx_edge_labels(G, pos, edge_labels={
|
| 77 |
+
("assistant", "tools"): "tools_condition",
|
| 78 |
+
("tools", "assistant"): "loop back"
|
| 79 |
+
}, font_color="red")
|
| 80 |
+
st.pyplot(plt)
|
| 81 |
+
|
| 82 |
+
# User input
|
| 83 |
+
user_question = st.text_area("Enter your question:", placeholder="Example: 'Add 3 and 4. Multiply the result by 2. Divide it by 5.'")
|
| 84 |
|
| 85 |
if st.button("Submit"):
|
| 86 |
if not user_question.strip():
|
|
|
|
| 91 |
messages = [HumanMessage(content=user_question)]
|
| 92 |
response = react_graph.invoke({"messages": messages})
|
| 93 |
|
|
|
|
| 94 |
st.subheader("Responses")
|
| 95 |
for m in response['messages']:
|
| 96 |
st.write(m.content)
|
|
|
|
| 97 |
st.success("Processing complete!")
|
| 98 |
|
| 99 |
+
# Example Questions
|
| 100 |
st.sidebar.subheader("Example Questions")
|
| 101 |
st.sidebar.write("- Add 3 and 4. Multiply the result by 2. Divide it by 5.")
|
| 102 |
st.sidebar.write("- Tell me how many centuries Virat Kohli scored.")
|