-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_3_agent.py
More file actions
56 lines (38 loc) · 1.43 KB
/
Copy path_3_agent.py
File metadata and controls
56 lines (38 loc) · 1.43 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
from langgraph.graph import StateGraph, START, END
from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage
from langgraph.graph import MessagesState
from langgraph.prebuilt import ToolNode, tools_condition
from dotenv import load_dotenv
load_dotenv()
def multiply(a,b):
"""This function multiplies 2 numbers"""
return a * b
def add(a,b):
"""This function adds 2 numbers"""
return a + b
def divide(a,b):
"""This function divide first number by the second number"""
return a / b
# Define the LLM model with tools bind to it
tools = [multiply, add, divide]
llm = ChatOpenAI(model="gpt-3.5-turbo")
llm_with_tools = llm.bind_tools(tools, parallel_tool_calls = False)
# Define a Node function - to invoke llm with tools model
def llm_with_tools_calling(state: MessagesState):
return {"messages" : llm_with_tools.invoke(state["messages"])}
# Define the graph
builder = StateGraph(MessagesState)
builder.add_node("assistant", llm_with_tools_calling)
builder.add_node("tools", ToolNode(tools))
builder.add_edge(START, "assistant")
builder.add_conditional_edges("assistant", tools_condition)
builder.add_edge("tools","assistant")
lgraph = builder.compile()
# Define a system prompt
# Define a user prompt
prompt = "Multiply 2 and 4. Add 4 to it. Divide it by 4"
response = lgraph.invoke({"messages" : prompt})
print(f"Response : {response}")
for m in response['messages']:
m.pretty_print()