-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
55 lines (45 loc) · 1.81 KB
/
app.py
File metadata and controls
55 lines (45 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# For usage in terminal
from pprint import pprint
from langchain_core.runnables.config import RunnableConfig
from langgraph.types import Command
from .src.chatbot.graph import graph
def handle_human_review(event: dict, config: RunnableConfig):
print(
"================================= Human Review ================================="
)
print("Verifying tool call: ", event["__interrupt__"][0].value["question"])
print("Tool Call:")
pprint(event["__interrupt__"][0].value["tool_call"])
action_input = input("Human Review - Action (feedback/continue): ")
if action_input not in ["feedback", "continue"]:
raise RuntimeError("Invalid action. Please enter 'feedback', or 'continue'.")
data_input = input("Human Review - Data (optional): ")
command: Command = Command(
resume={
"action": action_input,
"data": data_input if data_input else None,
}
)
stream_graph_updates(command, config)
def stream_graph_updates(graph_input: dict | Command, config: RunnableConfig):
events = graph.stream(
graph_input,
config,
stream_mode="values",
)
for event in events:
if "__interrupt__" in event:
handle_human_review(event, config)
elif "messages" in event:
event["messages"][-1].pretty_print()
snapshot = graph.get_state(config)
print("Sensitive: ", snapshot.values.get("sensitive", False))
config: RunnableConfig = {"configurable": {"thread_id": "1"}}
if __name__ == "__main__":
while True:
user_input = input("User: ")
if user_input.lower() in ["quit", "exit", "q"]:
print("Goodbye!")
break
graph_input = {"messages": [{"role": "user", "content": user_input}]}
stream_graph_updates(graph_input, config)