-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculator_agent.py
More file actions
77 lines (43 loc) · 1.79 KB
/
calculator_agent.py
File metadata and controls
77 lines (43 loc) · 1.79 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
from dotenv import load_dotenv
load_dotenv()
from langchain_openai import ChatOpenAI
# Creating our tools for the agent to use
def add(a : int, b: int) -> int:
"""Adds two numbers."""
return a + b
def subtract(a : int, b: int) -> int:
"""Subtracts the second number from the first."""
return a - b
def multiply(a : int, b: int) -> int:
"""Multiplies two numbers."""
return a * b
def divide(a : int, b: int) -> int:
"""Divides the first number by the second. Raises ValueError if dividing by zero."""
if b == 0:
raise ValueError("Cannot divide by zero")
return a / b
tools = [ add, subtract, multiply, divide ]
llm= ChatOpenAI(model="gpt-4", temperature=0)
llm_with_tools = llm.bind_tools(tools, parallel_tool_calls=False)
## Let's create a simple node for defining the assistant behaviour.
from langgraph.graph import MessagesState
from langchain_core.messages import SystemMessage
sys_msg = SystemMessage(content= ' You are a helpful assistant tasked with performing arthimetic operations ')
def assistant_node(state: MessagesState):
return {'messages' : [llm_with_tools.invoke([sys_msg] + state["messages"])]}
## now let's build and compile the graph
from langgraph.graph import StateGraph, START
from langgraph.prebuilt import ToolNode
from langgraph.prebuilt import tools_condition
def agent():
builder = StateGraph(MessagesState)
builder.add_node("assistant", assistant_node)
builder.add_node("tools", ToolNode(tools, handle_tool_errors=True))
builder.add_edge(START, "assistant")
builder.add_conditional_edges(
"assistant",
tools_condition
)
# IMPORTANT! -> Now the 'tools' node points back to the 'assistant' node, creating a loop
builder.add_edge("tools", "assistant")
return builder.compile()