Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 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
|
|
@@ -37,7 +38,6 @@ def search(query: str):
|
|
| 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
|
|
@@ -63,12 +63,12 @@ def invoke_tool(state):
|
|
| 63 |
selected_tool = tool_details.get("function").get("name")
|
| 64 |
st.sidebar.write(f"Selected tool: {selected_tool}")
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
response = tool_map[selected_tool].invoke(json.loads(tool_details.get("function").get("arguments")))
|
| 73 |
return {"messages": [response]}
|
| 74 |
|
|
@@ -89,7 +89,7 @@ graph.set_entry_point("agent")
|
|
| 89 |
compiled_app = graph.compile()
|
| 90 |
|
| 91 |
# Function to render graph with NetworkX
|
| 92 |
-
def render_graph_nx(
|
| 93 |
G = nx.DiGraph()
|
| 94 |
G.add_edge("agent", "tool", label="invoke tool")
|
| 95 |
G.add_edge("agent", "end", label="end condition")
|
|
@@ -104,30 +104,44 @@ def render_graph_nx(graph):
|
|
| 104 |
st.pyplot(plt)
|
| 105 |
|
| 106 |
# Streamlit UI
|
| 107 |
-
st.title("
|
| 108 |
-
st.write("
|
| 109 |
|
| 110 |
-
# Sidebar for
|
| 111 |
st.sidebar.header("Configuration")
|
| 112 |
-
st.session_state['human_loop'] = st.sidebar.checkbox("Enable Human-in-the-Loop
|
| 113 |
|
| 114 |
-
# Input
|
| 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 |
-
|
| 123 |
-
|
|
|
|
|
|
|
| 124 |
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
except Exception as e:
|
| 129 |
-
st.error(f"Error
|
|
|
|
| 130 |
|
| 131 |
-
# Display Graph
|
| 132 |
st.subheader("Workflow Graph")
|
| 133 |
-
render_graph_nx(
|
|
|
|
| 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
|
|
|
|
| 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
|
|
|
|
| 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 |
response = tool_map[selected_tool].invoke(json.loads(tool_details.get("function").get("arguments")))
|
| 73 |
return {"messages": [response]}
|
| 74 |
|
|
|
|
| 89 |
compiled_app = graph.compile()
|
| 90 |
|
| 91 |
# Function to render graph with NetworkX
|
| 92 |
+
def render_graph_nx():
|
| 93 |
G = nx.DiGraph()
|
| 94 |
G.add_edge("agent", "tool", label="invoke tool")
|
| 95 |
G.add_edge("agent", "end", label="end condition")
|
|
|
|
| 104 |
st.pyplot(plt)
|
| 105 |
|
| 106 |
# Streamlit UI
|
| 107 |
+
st.title("Blah Blah Demo")
|
| 108 |
+
st.write("Compare results **with and without human intervention** in the workflow.")
|
| 109 |
|
| 110 |
+
# Sidebar for configuration
|
| 111 |
st.sidebar.header("Configuration")
|
| 112 |
+
st.session_state['human_loop'] = st.sidebar.checkbox("Enable Human-in-the-Loop", value=False)
|
| 113 |
|
| 114 |
+
# Input and comparison mode
|
| 115 |
prompt = st.text_input("Enter your question:", "What is 24 * 365?")
|
| 116 |
if st.button("Run Workflow"):
|
| 117 |
st.subheader("Execution Results")
|
| 118 |
+
|
| 119 |
+
# Without human-in-the-loop
|
| 120 |
+
st.markdown("### Without Human-in-the-Loop")
|
| 121 |
+
st.session_state['human_loop'] = False
|
| 122 |
+
start_time = time.time()
|
| 123 |
try:
|
| 124 |
intermediate_outputs = []
|
| 125 |
for s in compiled_app.stream({"messages": [prompt]}):
|
| 126 |
intermediate_outputs.append(s)
|
| 127 |
+
st.write("Response:", intermediate_outputs[-1]['messages'][0])
|
| 128 |
+
except Exception as e:
|
| 129 |
+
st.error(f"Error: {e}")
|
| 130 |
+
st.write(f"Execution Time: {time.time() - start_time:.2f} seconds")
|
| 131 |
|
| 132 |
+
# With human-in-the-loop
|
| 133 |
+
st.markdown("### With Human-in-the-Loop")
|
| 134 |
+
st.session_state['human_loop'] = True
|
| 135 |
+
start_time = time.time()
|
| 136 |
+
try:
|
| 137 |
+
intermediate_outputs = []
|
| 138 |
+
for s in compiled_app.stream({"messages": [prompt]}):
|
| 139 |
+
intermediate_outputs.append(s)
|
| 140 |
+
st.write("Response:", intermediate_outputs[-1]['messages'][0])
|
| 141 |
except Exception as e:
|
| 142 |
+
st.error(f"Error: {e}")
|
| 143 |
+
st.write(f"Execution Time: {time.time() - start_time:.2f} seconds")
|
| 144 |
|
| 145 |
+
# Display Workflow Graph
|
| 146 |
st.subheader("Workflow Graph")
|
| 147 |
+
render_graph_nx()
|