DrishtiSharma commited on
Commit
b5a0276
·
verified ·
1 Parent(s): 6ec949a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -44
app.py CHANGED
@@ -1,13 +1,12 @@
1
  import os
2
  import streamlit as st
3
  import json
4
- import time
5
  from langchain_openai import ChatOpenAI
6
  from langchain_core.tools import tool
7
  from langchain_community.tools.tavily_search import TavilySearchResults
8
  from langgraph.graph import StateGraph, END
9
  from typing import TypedDict, Annotated, Sequence
10
- from langchain_core.messages import AIMessage, HumanMessage
11
  import operator
12
  import networkx as nx
13
  import matplotlib.pyplot as plt
@@ -38,17 +37,18 @@ def search(query: str):
38
 
39
  tools = [search, multiply]
40
  tool_map = {tool.name: tool for tool in tools}
 
41
  model_with_tools = model.bind_tools(tools)
42
 
43
  # Define Agent State class
44
  class AgentState(TypedDict):
45
- messages: Annotated[Sequence[HumanMessage], operator.add]
46
 
47
  # Define workflow nodes
48
  def invoke_model(state):
49
  messages = state['messages']
50
  question = messages[-1]
51
- return {"messages": [model_with_tools.invoke(question.content)]}
52
 
53
  def invoke_tool(state):
54
  tool_calls = state['messages'][-1].additional_kwargs.get("tool_calls", [])
@@ -63,15 +63,14 @@ def invoke_tool(state):
63
  selected_tool = tool_details.get("function").get("name")
64
  st.sidebar.write(f"Selected tool: {selected_tool}")
65
 
66
- # Add human-in-the-loop for all tools
67
- if st.session_state['human_loop']:
68
- response = st.sidebar.radio(f"Proceed with tool '{selected_tool}'?", ["Yes", "No"], index=0)
69
- if response == "No":
70
- raise ValueError(f"Execution of '{selected_tool}' was canceled.")
71
-
72
- # Invoke tool and return response as AIMessage
73
  response = tool_map[selected_tool].invoke(json.loads(tool_details.get("function").get("arguments")))
74
- return {"messages": [AIMessage(content=str(response))]}
75
 
76
  def router(state):
77
  tool_calls = state['messages'][-1].additional_kwargs.get("tool_calls", [])
@@ -90,7 +89,7 @@ graph.set_entry_point("agent")
90
  compiled_app = graph.compile()
91
 
92
  # Function to render graph with NetworkX
93
- def render_graph_nx():
94
  G = nx.DiGraph()
95
  G.add_edge("agent", "tool", label="invoke tool")
96
  G.add_edge("agent", "end", label="end condition")
@@ -106,43 +105,29 @@ def render_graph_nx():
106
 
107
  # Streamlit UI
108
  st.title("LLM Tool Workflow Demo")
109
- st.write("Compare results **with and without human intervention** in the workflow.")
110
 
111
- # Sidebar for configuration
112
  st.sidebar.header("Configuration")
113
- st.session_state['human_loop'] = st.sidebar.checkbox("Enable Human-in-the-Loop", value=False)
114
 
115
  # Input prompt
116
  prompt = st.text_input("Enter your question:", "What is 24 * 365?")
117
  if st.button("Run Workflow"):
118
  st.subheader("Execution Results")
119
-
120
- # Run workflow without and with human-in-the-loop
121
- for human_loop in [False, True]:
122
- st.markdown(f"### {'With' if human_loop else 'Without'} Human-in-the-Loop")
123
- st.session_state['human_loop'] = human_loop
124
  intermediate_outputs = []
125
- start_time = time.time()
126
-
127
- try:
128
- # Collect intermediate outputs
129
- for s in compiled_app.stream({"messages": [HumanMessage(content=prompt)]}):
130
- intermediate_outputs.append(s)
131
-
132
- # Extract and display the final response
133
- final_output = intermediate_outputs[-1]
134
- messages = final_output.get('messages', [])
135
- if messages:
136
- response_content = messages[-1].content
137
- st.write("Response:", response_content)
138
- else:
139
- st.write("Response: No content generated.")
140
- except Exception as e:
141
- st.error(f"Error: {e}")
142
-
143
- st.write(f"Execution Time: {time.time() - start_time:.2f} seconds")
144
- st.write("---")
145
-
146
- # Display Workflow Graph
147
  st.subheader("Workflow Graph")
148
- render_graph_nx()
 
1
  import os
2
  import streamlit as st
3
  import json
 
4
  from langchain_openai import ChatOpenAI
5
  from langchain_core.tools import tool
6
  from langchain_community.tools.tavily_search import TavilySearchResults
7
  from langgraph.graph import StateGraph, END
8
  from typing import TypedDict, Annotated, Sequence
9
+ from langchain_core.messages import BaseMessage
10
  import operator
11
  import networkx as nx
12
  import matplotlib.pyplot as plt
 
37
 
38
  tools = [search, multiply]
39
  tool_map = {tool.name: tool for tool in tools}
40
+
41
  model_with_tools = model.bind_tools(tools)
42
 
43
  # Define Agent State class
44
  class AgentState(TypedDict):
45
+ messages: Annotated[Sequence[BaseMessage], operator.add]
46
 
47
  # Define workflow nodes
48
  def invoke_model(state):
49
  messages = state['messages']
50
  question = messages[-1]
51
+ return {"messages": [model_with_tools.invoke(question)]}
52
 
53
  def invoke_tool(state):
54
  tool_calls = state['messages'][-1].additional_kwargs.get("tool_calls", [])
 
63
  selected_tool = tool_details.get("function").get("name")
64
  st.sidebar.write(f"Selected tool: {selected_tool}")
65
 
66
+ if selected_tool == "search":
67
+ if 'human_loop' in st.session_state and st.session_state['human_loop']:
68
+ response = st.sidebar.radio("Proceed with web search?", ["Yes", "No"])
69
+ if response == "No":
70
+ raise ValueError("User canceled the search tool execution.")
71
+
 
72
  response = tool_map[selected_tool].invoke(json.loads(tool_details.get("function").get("arguments")))
73
+ return {"messages": [response]}
74
 
75
  def router(state):
76
  tool_calls = state['messages'][-1].additional_kwargs.get("tool_calls", [])
 
89
  compiled_app = graph.compile()
90
 
91
  # Function to render graph with NetworkX
92
+ def render_graph_nx(graph):
93
  G = nx.DiGraph()
94
  G.add_edge("agent", "tool", label="invoke tool")
95
  G.add_edge("agent", "end", label="end condition")
 
105
 
106
  # Streamlit UI
107
  st.title("LLM Tool Workflow Demo")
108
+ st.write("This app demonstrates LLM-based tool usage with and without human intervention.")
109
 
110
+ # Sidebar for options
111
  st.sidebar.header("Configuration")
112
+ st.session_state['human_loop'] = st.sidebar.checkbox("Enable Human-in-the-Loop (For Search)", value=False)
113
 
114
  # Input prompt
115
  prompt = st.text_input("Enter your question:", "What is 24 * 365?")
116
  if st.button("Run Workflow"):
117
  st.subheader("Execution Results")
118
+ try:
 
 
 
 
119
  intermediate_outputs = []
120
+ for s in compiled_app.stream({"messages": [prompt]}):
121
+ intermediate_outputs.append(s)
122
+ st.write("Response:", list(s.values())[0])
123
+ st.write("---")
124
+
125
+ st.sidebar.write("### Intermediate Outputs")
126
+ for i, output in enumerate(intermediate_outputs):
127
+ st.sidebar.write(f"Step {i+1}: {output}")
128
+ except Exception as e:
129
+ st.error(f"Error occurred: {e}")
130
+
131
+ # Display Graph
 
 
 
 
 
 
 
 
 
 
132
  st.subheader("Workflow Graph")
133
+ render_graph_nx(graph)