DrishtiSharma commited on
Commit
c24cad9
·
verified ·
1 Parent(s): acf1e75

Update app.py

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