-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstreaming-interruption.py
217 lines (131 loc) · 6.11 KB
/
streaming-interruption.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# ===============================================
# Streaming - Human in loop
# ===============================================
# -----------------------------------------------
# Let's load our environment variables
# -----------------------------------------------
from dotenv import load_dotenv
load_dotenv()
# -----------------------------------------------
# LLM Model
# -----------------------------------------------
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
# -----------------------------------------------
# MessageState
# -----------------------------------------------
from langgraph.graph import MessagesState
class State(MessagesState):
summary : str
# -----------------------------------------------
# Call Model to get Summary
# -----------------------------------------------
from langchain_core.messages import SystemMessage
def call_model(state: State):
# Get summary if exist
summary = state.get("summary", "")
if summary:
system_message = f"Summary of earlier conversation: {summary} "
messages = [SystemMessage(content=system_message) + state["messages"]]
else:
messages = state["messages"]
response = llm.invoke(messages)
return {"messages" : response}
# -----------------------------------------------
# Summarize the conversation
# -----------------------------------------------
from langchain_core.messages import HumanMessage, RemoveMessage
def summarize_conversation(state: State):
summary = state.get("summary", "")
if summary:
summary_message = {
f"This is summary for previous conversation : {summary}\n\n"
"Extend the summary by taking into account the new message above"
}
else:
summary_message = "Create Summary for the above conversation"
# Add history to our prompt
message = state["messages"] + [HumanMessage(content=summary_message)]
response = llm.invoke(message)
# Keep only last 2 messages
delete_messages = [RemoveMessage(id=m.id) for m in state["messages"][:-2]]
return {"summary" : response.content, "messages" : delete_messages}
# -----------------------------------------------
# Generate Summary based on the conversation length
# -----------------------------------------------
from langgraph.graph import END
def should_continue(state : State):
"""
Returns the next node to exceute.
"""
messages = state["messages"]
# If there are more than 6 messages, we will summarize the conversation
if len(messages) > 6:
return "summarize_conversation"
return END
# -----------------------------------------------
# Adding Memory using checkpointer
# -----------------------------------------------
from langgraph.graph import StateGraph, START
from langgraph.checkpoint.memory import MemorySaver
from IPython.display import Image, display
# Graph
workflow = StateGraph(State)
# Nodes
workflow.add_node("conversation", call_model)
workflow.add_node(summarize_conversation)
# Edges
workflow.add_edge(START, "conversation")
workflow.add_conditional_edges("conversation", should_continue)
workflow.add_edge("summarize_conversation", END)
# Compile with checkpointer
memory = MemorySaver()
graph = workflow.compile(checkpointer=memory)
# Display graph
display(Image(graph.get_graph().draw_mermaid_png()))
# -----------------------------------------------
# Stream using Stream mode = "updates"
# -----------------------------------------------
config = {"configurable" : {"thread_id" : "1"}}
input_message = HumanMessage(content="Hi! I am Sushant")
for chunk in graph.stream({"messages" : [input_message]}, config, stream_mode="updates"):
print(chunk)
# Print just the state update
for chunk in graph.stream({"messages" : [input_message]}, config, stream_mode="updates"):
chunk["conversation"]["messages"].pretty_print()
# -----------------------------------------------
# Stream using Stream mode = "values"
# -----------------------------------------------
config = {"configurable" : {"thread_id" : "2"}}
input_message = HumanMessage(content="Hi! I am Sushant")
for event in graph.stream({"messages" : [input_message]}, config, stream_mode="values"):
for msg in event["messages"]:
msg.pretty_print()
print("---"*25)
# -----------------------------------------------
# Streaming tokens using Stream mode = "values"
# -----------------------------------------------
config = {"configurable" : {"thread_id" : "3"}}
input_message = HumanMessage(content="Tell me about the RL Agents")
async for event in graph.astream_events({"messages" : [input_message]}, config, version="v2"):
print(f"Node: {event['metadata'].get('langgraph_node', '')}. Type: {event['event']}. Name: {event['name']}")
# -----------------------------------------------
# Streaming tokens for a specific node
# -----------------------------------------------
node_to_stream = "conversation"
config = {"configurable" : {"thread_id" : "4"}}
input_message = HumanMessage(content="Tell me about the RL Agents")
async for event in graph.astream_events({"messages" : [input_message]}, config, version="v2"):
if event["event"] == "on_chat_model_stream" and event["metadata"].get('langgraph_node', '') == node_to_stream:
print(event["data"])
# -----------------------------------------------
# Streaming tokens for a node using 'chunk' key
# -----------------------------------------------
node_to_stream = "conversation"
config = {"configurable" : {"thread_id" : "5"}}
input_message = HumanMessage(content="Tell me about the RL Agents")
async for event in graph.astream_events({"messages" : [input_message]}, config, version="v2"):
if event["event"] == "on_chat_model_stream" and event["metadata"].get('langgraph_node', '') == node_to_stream:
data = event["data"]
print(data["chunk"].content, end="|")
# -----------------------------------------------