-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagent_in_graph_with_memory.py
182 lines (121 loc) · 4.82 KB
/
agent_in_graph_with_memory.py
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# ===============================================
# Agent with Memory in Graph
# ===============================================
# Let's build a ReAct agent, a general agent architecture.
# act - let the model call specific tools
# observe - pass the tool output back to the model
# reason - let the model reason about the tool output
# to decide what to do next
# (e.g., call another tool or just respond directly)
# -----------------------------------------------
# Let's load our environment variables
from dotenv import load_dotenv
load_dotenv()
# -----------------------------------------------
# Let's build few tools
# -----------------------------------------------
from langchain_openai import ChatOpenAI
def multiply(a: int, b: int) -> int:
"""
Multiplies 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 divide(a: int, b: int) -> int:
"""
Divides a and b.
Args:
a: first int
b: second int
"""
return a / b
tools = [multiply, add, divide]
llm = ChatOpenAI(model="gpt-4o-mini")
# OpenAI model specifically defaults to parallel tool calling for efficiency
# So we will set parallel_tool_calls to False
llm_with_tools = llm.bind_tools(tools, parallel_tool_calls=False)
# -----------------------------------------------
# Define assistant function
# -----------------------------------------------
from langgraph.graph import MessagesState
from langchain_core.messages import HumanMessage, SystemMessage
sys_msg = SystemMessage(content="You are a helpful assistant tasked with performing arithmetic on a set of inputs.")
# Node function
def assistant(state: MessagesState):
return {"messages" : [llm_with_tools.invoke([sys_msg] + state["messages"])]}
# -----------------------------------------------
# Let's define a graph
# -----------------------------------------------
from langgraph.graph import StateGraph, START
from langgraph.prebuilt import tools_condition
from langgraph.prebuilt import ToolNode
# Graph
builder = StateGraph(MessagesState)
# Add nodes
builder.add_node("assistant", assistant)
builder.add_node("tools", ToolNode(tools))
# Add edges
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
react_graph = builder.compile()
# -----------------------------------------------
# Display the graph
# -----------------------------------------------
from IPython.display import Image, display
display(Image(react_graph.get_graph().draw_mermaid_png()))
# ===============================================
# Memory in Graph
# ===============================================
# -----------------------------------------------
# Let's run Agent as before
# -----------------------------------------------
messages = [HumanMessage(content="Add 6 and 4")]
messages = react_graph.invoke({"messages": messages})
for msg in messages["messages"]:
msg.pretty_print()
# Let's ask the agent to multiply
messages = [HumanMessage(content="Multiply the result by 3")]
messages = react_graph.invoke({"messages": messages})
for msg in messages["messages"]:
msg.pretty_print()
# -----------------------------------------------
# Agent is not able to recall the result from previous step
# Let's a checkpointer to automatically save the graph state after each step
# -----------------------------------------------
from langgraph.checkpoint.memory import MemorySaver
memory_saver = MemorySaver()
react_graph_memory = builder.compile(checkpointer=memory_saver)
# -----------------------------------------------
# Let's use thread_id to store our collection of graph states.
# -----------------------------------------------
# Specify thread_id
config = {"configurable": {"thread_id": "1"}}
# Specify an input
messages = [HumanMessage(content="Add 6 and 4")]
# Run the graph with memory saver
messages = react_graph_memory.invoke({"messages": messages}, config)
for msg in messages["messages"]:
msg.pretty_print()
# Now let's send another message to use result from previous step
messages = [HumanMessage(content="Multiply the result by 3")]
messages = react_graph_memory.invoke({"messages": messages}, config)
for msg in messages["messages"]:
msg.pretty_print()
# -----------------------------------------------