Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from typing import TypedDict, Annotated
|
3 |
+
from langgraph.graph import StateGraph, START, END
|
4 |
+
from langgraph.checkpoint.memory import MemorySaver
|
5 |
+
from langgraph.checkpoint.sqlite import SqliteSaver
|
6 |
+
from langgraph.graph.message import add_messages
|
7 |
+
from langchain_openai import ChatOpenAI
|
8 |
+
from langchain_community.tools.tavily_search import TavilySearchResults
|
9 |
+
from langchain_core.messages import HumanMessage, ToolMessage, AIMessage
|
10 |
+
from langgraph.prebuilt import ToolNode, tools_condition
|
11 |
+
import os
|
12 |
+
|
13 |
+
|
14 |
+
st.title("Checkpoints-Breakpoints")
|
15 |
+
#st.caption("Demonstrating a step-by-step graph workflow with breakpoints and human control.")
|
16 |
+
|
17 |
+
# Environment variables
|
18 |
+
openai_api_key = os.getenv("OPENAI_API_KEY")
|
19 |
+
tavily_api_key = os.getenv("TAVILY_API_KEY")
|
20 |
+
|
21 |
+
if openai_api_key and tavily_api_key:
|
22 |
+
os.environ["OPENAI_API_KEY"] = openai_api_key
|
23 |
+
os.environ["TAVILY_API_KEY"] = tavily_api_key
|
24 |
+
|
25 |
+
# Define the State
|
26 |
+
class State(TypedDict):
|
27 |
+
messages: Annotated[list, add_messages]
|
28 |
+
|
29 |
+
# Instantiate ChatOpenAI and Tools
|
30 |
+
llm = ChatOpenAI(model="gpt-4o-mini")
|
31 |
+
tool = TavilySearchResults(max_results=2)
|
32 |
+
tools = [tool]
|
33 |
+
llm_with_tools = llm.bind_tools(tools)
|
34 |
+
|
35 |
+
# Define the agent function
|
36 |
+
def Agent(state: State):
|
37 |
+
return {"messages": [llm_with_tools.invoke(state["messages"])]}
|
38 |
+
|
39 |
+
# Memory Checkpoints
|
40 |
+
memory = MemorySaver()
|
41 |
+
|
42 |
+
# Build the Graph
|
43 |
+
graph = StateGraph(State)
|
44 |
+
tool_node = ToolNode(tools=[tool])
|
45 |
+
graph.add_node("Agent", Agent)
|
46 |
+
graph.add_node("tools", tool_node)
|
47 |
+
|
48 |
+
graph.add_conditional_edges("Agent", tools_condition)
|
49 |
+
graph.add_edge("tools", "Agent")
|
50 |
+
graph.set_entry_point("Agent")
|
51 |
+
|
52 |
+
app = graph.compile(checkpointer=memory, interrupt_before=["tools"])
|
53 |
+
|
54 |
+
# Display the graph structure
|
55 |
+
st.subheader("Graph Workflow")
|
56 |
+
st.image(app.get_graph().draw_mermaid_png(), caption="Graph Visualization", use_column_width=True)
|
57 |
+
|
58 |
+
# Input Section
|
59 |
+
st.subheader("Run the Workflow")
|
60 |
+
user_input = st.text_input("Enter a message to start the graph:", "Hello, I am from Kolkata")
|
61 |
+
thread_id = st.text_input("Thread ID", "1")
|
62 |
+
|
63 |
+
if st.button("Execute Workflow"):
|
64 |
+
thread = {"configurable": {"thread_id": thread_id}}
|
65 |
+
input_message = {'messages': HumanMessage(content=user_input)}
|
66 |
+
|
67 |
+
st.write("### Execution Outputs")
|
68 |
+
outputs = []
|
69 |
+
for event in app.stream(input_message, thread, stream_mode="values"):
|
70 |
+
latest_message = event["messages"][-1].pretty_print()
|
71 |
+
outputs.append(latest_message)
|
72 |
+
st.code(latest_message)
|
73 |
+
|
74 |
+
# Display all intermediate steps
|
75 |
+
st.subheader("Intermediate Outputs")
|
76 |
+
for i, output in enumerate(outputs, 1):
|
77 |
+
st.write(f"**Step {i}:**")
|
78 |
+
st.code(output)
|
79 |
+
|
80 |
+
# Snapshot State
|
81 |
+
st.subheader("Current State Snapshot")
|
82 |
+
snapshot = app.get_state(thread)
|
83 |
+
current_message = snapshot.values["messages"][-1]
|
84 |
+
st.code(current_message.pretty_print())
|
85 |
+
|
86 |
+
# Manual State Update
|
87 |
+
st.subheader("Manual Update")
|
88 |
+
tool_call_id = current_message.tool_calls[0]["id"]
|
89 |
+
manual_response = st.text_area("Manual Tool Response", "Enter response to continue...")
|
90 |
+
if st.button("Update State"):
|
91 |
+
new_messages = [
|
92 |
+
ToolMessage(content=manual_response, tool_call_id=tool_call_id),
|
93 |
+
AIMessage(content=manual_response),
|
94 |
+
]
|
95 |
+
app.update_state(thread, {"messages": new_messages})
|
96 |
+
st.success("State updated successfully!")
|
97 |
+
st.code(app.get_state(thread).values["messages"][-1].pretty_print())
|
98 |
+
|
99 |
+
else:
|
100 |
+
st.error("API keys are missing! Please set `OPENAI_API_KEY` and `TAVILY_API_KEY` in Hugging Face Spaces Secrets.")
|