-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path6-BreakpointGraph.py
More file actions
142 lines (96 loc) · 3.51 KB
/
6-BreakpointGraph.py
File metadata and controls
142 lines (96 loc) · 3.51 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# Breakpoints are a simple way to stop the graph to ask for approval before moving to the next nodes
from langchain_openai import ChatOpenAI
# to build graph
from langgraph.graph import StateGraph, START, MessagesState
from langchain_core.messages import HumanMessage, SystemMessage
from langgraph.prebuilt import tools_condition, ToolNode
# To create image
from utils.graph_img_generation import save_and_show_graph
# for printing messages
from langchain_core.messages import HumanMessage
# for adding checkpoint in memory
from langgraph.checkpoint.memory import MemorySaver
from config.secret_keys import OPENAI_API_KEY
from config.config import get_llm
# defining the LLM
llm = get_llm()
# defining a memory location
memory = MemorySaver()
# defining the tools
def multiply(a: int, b: int) -> int:
"""Multiply a and b.
Args:
a: first int
b: second int
"""
return a * b
def add(a: int, b: int) -> int:
"""Adds a and b.
Args:
a: first int
b: second int
"""
return a + b
def subtract(a: int, b: int) -> int:
"""Subtract a and b.
Args:
a: first int
b: second int
"""
return a - b
def divide(a: int, b: int) -> float:
"""Divide a and b.
Args:
a: first int
b: second int
"""
return a / b
tools = [add, subtract, multiply, divide]
llm_with_tools = llm.bind_tools(tools, parallel_tool_calls=False)
# System message
sys_msg = SystemMessage(content="You are a helpful assistant tasked with performing arithmetic on a set of inputs.")
# Node
def assistant(state: MessagesState):
return {"messages": [llm_with_tools.invoke([sys_msg] + state["messages"])]}
# Build the graph
builder = StateGraph(MessagesState)
# Define nodes: these do the work
builder.add_node("assistant", assistant)
builder.add_node("tools", ToolNode(tools))
# Define edges: these determine how the control flow moves
builder.add_edge(START, "assistant")
builder.add_conditional_edges(
"assistant",
# If the latest message (result) from assistant is a tool call -> tools_condition routes to tools
# If the latest message (result) from assistant is a not a tool call -> tools_condition routes to END
tools_condition,
)
builder.add_edge("tools", "assistant")
# Compile graph with memory
breakpoint_graph = builder.compile(interrupt_before=["tools"], checkpointer=memory)
# Use the utility function to save and optionally show the graph
save_and_show_graph(breakpoint_graph, filename="6-BreakpointGraph", show_image=False)
# Specify a thread AKA session
config = {"configurable": {"thread_id": "1"}}
# Start the conversation loop
while True:
# Take user input
user_msg = input("Enter your message (or type 'exit' to quit): ")
# Break the loop if the user types 'exit'
if user_msg.lower() == 'exit':
print("Exiting the session...")
break
# Input
initial_input = {"messages": HumanMessage(content=user_msg)}
# Run the graph until the first interruption
for event in breakpoint_graph.stream(initial_input, config, stream_mode="values"):
event['messages'][-1].pretty_print()
# Get user feedback
user_approval = input("Do you want to call the tool? (yes/no): ")
# Check approval
if user_approval.lower() == "yes":
# If approved, continue the graph execution
for event in breakpoint_graph.stream(None, config, stream_mode="values"):
event['messages'][-1].pretty_print()
else:
print("Operation cancelled by user.")