-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchain_in_graph.py
144 lines (95 loc) · 4.24 KB
/
chain_in_graph.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
# ===============================================
# Chain in Graph
# ===============================================
# Let's build up to a simple chain that combines 4 concepts:
# Using chat messages as our graph state
# Using chat models in graph nodes
# Binding tools to our chat model
# Executing tool calls in graph nodes
# -----------------------------------------------
from dotenv import load_dotenv
load_dotenv()
# -----------------------------------------------
# Let's create a list of messages
# -----------------------------------------------
from pprint import pprint
from langchain_core.messages import AIMessage, HumanMessage
messages = [AIMessage(content=f"So you said you were researching LLM Agents?", name="Model")]
messages.append(HumanMessage(content=f"Yes, that's correct.", name="Sushant"))
messages.append(AIMessage(content=f"Great, what would you like to learn about.", name="Model"))
messages.append(HumanMessage(content=f"I want to learn about the 3 best Agentic framworks.", name="Sushant"))
for msg in messages:
msg.pretty_print()
# -----------------------------------------------
# Let's load a chat model and invoke above list of messages
# -----------------------------------------------
from langchain_openai import ChatOpenAI
model = ChatOpenAI(model="gpt-4o-mini")
result = model.invoke(messages)
pprint(result.content)
pprint(result.response_metadata)
# -----------------------------------------------
# Let's create and use a simple tool
# -----------------------------------------------
def multiply(a: int, b: int) -> int:
"""
Multiply a and b.
Args:
a: first int
b: second int
"""
return a * b
llm_with_tools = model.bind_tools([multiply])
tool_call = llm_with_tools.invoke([HumanMessage(content=f"What is 2 multiplied by 3", name="Sushant")])
pprint(tool_call)
pprint(tool_call.additional_kwargs['tool_calls'])
# -----------------------------------------------
# Let's create a graph state for the list of messages
# -----------------------------------------------
from typing import Annotated
from typing_extensions import TypedDict
from langchain_core.messages import AnyMessage
from langgraph.graph.message import add_messages
class MessagesState(TypedDict):
messages: Annotated[list[AnyMessage], add_messages]
initial_message = [AIMessage(content="Hello! How can I assist you?", name="Model"),
HumanMessage(content="I want to learn about LLM Agents", name="Sushant")
]
new_message = AIMessage(content="Sure, I can help with that. What specifically are you interested in?", name="Model")
add_messages(initial_message, new_message)
pprint(messages)
# -----------------------------------------------
# Let's use reducer functioncreate a graph state
# -----------------------------------------------
from langgraph.graph import MessagesState
class MessagesState(MessagesState):
pass
# -----------------------------------------------
# Let's use MessagesState with a graph
# -----------------------------------------------
from langgraph.graph import StateGraph, START, END
# Build a node
def tool_calling_llm(state: MessagesState):
return {"messages" : [llm_with_tools.invoke(state["messages"])]}
# Build graph
builder = StateGraph(MessagesState)
builder.add_node("tool_calling_llm", tool_calling_llm)
builder.add_edge(START, "tool_calling_llm")
builder.add_edge("tool_calling_llm", END)
# Compile graph
graph = builder.compile()
# View
from IPython.display import Image, display
display(Image(graph.get_graph().draw_mermaid_png()))
# -----------------------------------------------
# Let's invoke the graph with a simple message
# -----------------------------------------------
messages = graph.invoke({"messages": HumanMessage(content="Hello! How are you?")})
for msg in messages["messages"]:
msg.pretty_print()
# -----------------------------------------------
# Now, let's invoke the graph with multiplication question
# -----------------------------------------------
messages = graph.invoke({"messages": HumanMessage(content="What is 2 multiplied by 3")})
for msg in messages["messages"]:
msg.pretty_print()