forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlanggraph.py
More file actions
492 lines (404 loc) · 14.5 KB
/
Copy pathlanggraph.py
File metadata and controls
492 lines (404 loc) · 14.5 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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
"""
LangChain specific utilities for CopilotKit
"""
import uuid
import json
import warnings
import asyncio
from typing import List, Optional, Any, Union, Dict, Callable, cast
from typing_extensions import TypedDict
from langgraph.graph import MessagesState
from langchain_core.messages import (
HumanMessage,
SystemMessage,
BaseMessage,
AIMessage,
ToolMessage
)
from langchain_core.runnables import RunnableConfig
from langchain_core.callbacks.manager import adispatch_custom_event
from langgraph.types import interrupt
from .types import Message, IntermediateStateConfig
from .logging import get_logger
logger = get_logger(__name__)
class CopilotKitProperties(TypedDict):
"""CopilotKit state"""
actions: List[Any]
class CopilotKitState(MessagesState):
"""CopilotKit state"""
copilotkit: CopilotKitProperties
def copilotkit_messages_to_langchain(
use_function_call: bool = False
) -> Callable[[List[Message]], List[BaseMessage]]:
"""
Convert CopilotKit messages to LangChain messages
"""
def _copilotkit_messages_to_langchain(messages: List[Message]) -> List[BaseMessage]:
result = []
processed_action_executions = set()
for message in cast(Any, messages):
if message["type"] == "TextMessage":
if message["role"] == "user":
result.append(HumanMessage(content=message["content"], id=message["id"]))
elif message["role"] == "system":
result.append(SystemMessage(content=message["content"], id=message["id"]))
elif message["role"] == "assistant":
result.append(AIMessage(content=message["content"], id=message["id"]))
elif message["type"] == "ActionExecutionMessage":
if use_function_call:
result.append(AIMessage(
id=message["id"],
content="",
additional_kwargs={
'function_call':{
'name': message["name"],
'arguments': json.dumps(message["arguments"]),
}
}
))
else:
# convert multiple tool calls to a single message
message_id = message.get("parentMessageId")
if message_id is None:
message_id = message["id"]
if message_id in processed_action_executions:
continue
processed_action_executions.add(message_id)
all_tool_calls = []
# Find all tool calls for this message
for msg in messages:
if msg.get("parentMessageId", None) == message_id or msg["id"] == message_id:
all_tool_calls.append(msg)
tool_calls = [{
"name": t["name"],
"args": t["arguments"],
"id": t["id"],
} for t in all_tool_calls]
result.append(
AIMessage(
id=message_id,
content="",
tool_calls=tool_calls
)
)
elif message["type"] == "ResultMessage":
result.append(ToolMessage(
id=message["id"],
content=message["result"],
name=message["actionName"],
tool_call_id=message["actionExecutionId"]
))
return result
return _copilotkit_messages_to_langchain
def langchain_messages_to_copilotkit(
messages: List[BaseMessage]
) -> List[Message]:
"""
Convert LangChain messages to CopilotKit messages
"""
result = []
tool_call_names = {}
for message in messages:
if isinstance(message, AIMessage):
for tool_call in message.tool_calls or []:
tool_call_names[tool_call["id"]] = tool_call["name"]
for message in messages:
content = None
if hasattr(message, "content"):
content = message.content
# Check if content is a list and use the first element
if isinstance(content, list):
content = content[0]
# Anthropic models return a dict with a "text" key
if isinstance(content, dict):
content = content.get("text", "")
if isinstance(message, HumanMessage):
result.append({
"role": "user",
"content": content,
"id": message.id,
})
elif isinstance(message, SystemMessage):
result.append({
"role": "system",
"content": content,
"id": message.id,
})
elif isinstance(message, AIMessage):
if message.tool_calls:
for tool_call in message.tool_calls:
result.append({
"id": tool_call["id"],
"name": tool_call["name"],
"arguments": tool_call["args"],
"parentMessageId": message.id,
})
else:
result.append({
"role": "assistant",
"content": content,
"id": message.id,
"parentMessageId": message.id,
})
elif isinstance(message, ToolMessage):
result.append({
"actionExecutionId": message.tool_call_id,
"actionName": tool_call_names.get(message.tool_call_id, message.name or ""),
"result": content,
"id": message.id,
})
# Create a dictionary to map message ids to their corresponding messages
results_dict = {msg["actionExecutionId"]: msg for msg in result if "actionExecutionId" in msg}
# since we are splitting multiple tool calls into multiple messages,
# we need to reorder the corresponding result messages to be after the tool call
reordered_result = []
for msg in result:
# add all messages that are not tool call results
if not "actionExecutionId" in msg:
reordered_result.append(msg)
# if the message is a tool call, also add the corresponding result message
# immediately after the tool call
if "arguments" in msg:
msg_id = msg["id"]
if msg_id in results_dict:
reordered_result.append(results_dict[msg_id])
else:
logger.warning("Tool call result message not found for id: %s", msg_id)
return reordered_result
def copilotkit_customize_config(
base_config: Optional[RunnableConfig] = None,
*,
emit_messages: Optional[bool] = None,
emit_tool_calls: Optional[Union[bool, str, List[str]]] = None,
emit_intermediate_state: Optional[List[IntermediateStateConfig]] = None,
emit_all: Optional[bool] = None, # deprecated
) -> RunnableConfig:
"""
Customize the LangGraph configuration for use in CopilotKit.
To install the CopilotKit SDK, run:
```bash
pip install copilotkit
```
### Examples
Disable emitting messages and tool calls:
```python
from copilotkit.langgraph import copilotkit_customize_config
config = copilotkit_customize_config(
config,
emit_messages=False,
emit_tool_calls=False
)
```
To emit a tool call as streaming LangGraph state, pass the destination key in state,
the tool name and optionally the tool argument. (If you don't pass the argument name,
all arguments are emitted under the state key.)
```python
from copilotkit.langgraph import copilotkit_customize_config
config = copilotkit_customize_config(
config,
emit_intermediate_state=[
{
"state_key": "steps",
"tool": "SearchTool",
"tool_argument": "steps"
},
]
)
```
Parameters
----------
base_config : Optional[RunnableConfig]
The LangChain/LangGraph configuration to customize. Pass None to make a new configuration.
emit_messages : Optional[bool]
Configure how messages are emitted. By default, all messages are emitted. Pass False to
disable emitting messages.
emit_tool_calls : Optional[Union[bool, str, List[str]]]
Configure how tool calls are emitted. By default, all tool calls are emitted. Pass False to
disable emitting tool calls. Pass a string or list of strings to emit only specific tool calls.
emit_intermediate_state : Optional[List[IntermediateStateConfig]]
Lets you emit tool calls as streaming LangGraph state.
Returns
-------
RunnableConfig
The customized LangGraph configuration.
"""
if emit_all is not None:
warnings.warn(
"The `emit_all` parameter is deprecated and will be removed in a future version. "
"CopilotKit will now emit all messages and tool calls by default.",
DeprecationWarning,
stacklevel=2
)
metadata = base_config.get("metadata", {}) if base_config else {}
if emit_all is True:
metadata["copilotkit:emit-tool-calls"] = True
metadata["copilotkit:emit-messages"] = True
else:
if emit_tool_calls is not None:
metadata["copilotkit:emit-tool-calls"] = emit_tool_calls
if emit_messages is not None:
metadata["copilotkit:emit-messages"] = emit_messages
if emit_intermediate_state:
metadata["copilotkit:emit-intermediate-state"] = emit_intermediate_state
base_config = base_config or {}
return {
**base_config,
"metadata": metadata
}
async def copilotkit_exit(config: RunnableConfig):
"""
Exits the current agent after the run completes. Calling copilotkit_exit() will
not immediately stop the agent. Instead, it signals to CopilotKit to stop the agent after
the run completes.
### Examples
```python
from copilotkit.langgraph import copilotkit_exit
def my_node(state: Any):
await copilotkit_exit(config)
return state
```
Parameters
----------
config : RunnableConfig
The LangGraph configuration.
Returns
-------
Awaitable[bool]
Always return True.
"""
await adispatch_custom_event(
"copilotkit_exit",
{},
config=config,
)
await asyncio.sleep(0.02)
return True
async def copilotkit_emit_state(config: RunnableConfig, state: Any):
"""
Emits intermediate state to CopilotKit. Useful if you have a longer running node and you want to
update the user with the current state of the node.
### Examples
```python
from copilotkit.langgraph import copilotkit_emit_state
for i in range(10):
await some_long_running_operation(i)
await copilotkit_emit_state(config, {"progress": i})
```
Parameters
----------
config : RunnableConfig
The LangGraph configuration.
state : Any
The state to emit (Must be JSON serializable).
Returns
-------
Awaitable[bool]
Always return True.
"""
await adispatch_custom_event(
"copilotkit_manually_emit_intermediate_state",
state,
config=config,
)
await asyncio.sleep(0.02)
return True
async def copilotkit_emit_message(config: RunnableConfig, message: str):
"""
Manually emits a message to CopilotKit. Useful in longer running nodes to update the user.
Important: You still need to return the messages from the node.
### Examples
```python
from copilotkit.langgraph import copilotkit_emit_message
message = "Step 1 of 10 complete"
await copilotkit_emit_message(config, message)
# Return the message from the node
return {
"messages": [AIMessage(content=message)]
}
```
Parameters
----------
config : RunnableConfig
The LangGraph configuration.
message : str
The message to emit.
Returns
-------
Awaitable[bool]
Always return True.
"""
await adispatch_custom_event(
"copilotkit_manually_emit_message",
{
"message": message,
"message_id": str(uuid.uuid4()),
"role": "assistant"
},
config=config,
)
await asyncio.sleep(0.02)
return True
async def copilotkit_emit_tool_call(config: RunnableConfig, *, name: str, args: Dict[str, Any]):
"""
Manually emits a tool call to CopilotKit.
```python
from copilotkit.langgraph import copilotkit_emit_tool_call
await copilotkit_emit_tool_call(config, name="SearchTool", args={"steps": 10})
```
Parameters
----------
config : RunnableConfig
The LangGraph configuration.
name : str
The name of the tool to emit.
args : Dict[str, Any]
The arguments to emit.
Returns
-------
Awaitable[bool]
Always return True.
"""
await adispatch_custom_event(
"copilotkit_manually_emit_tool_call",
{
"name": name,
"args": args,
"id": str(uuid.uuid4())
},
config=config,
)
await asyncio.sleep(0.02)
return True
def copilotkit_interrupt(
message: Optional[str] = None,
action: Optional[str] = None,
args: Optional[Dict[str, Any]] = None
):
if message is None and action is None:
raise ValueError('Either message or action (and optional arguments) must be provided')
interrupt_message = None
interrupt_values = None
answer = None
if message is not None:
interrupt_values = message
interrupt_message = AIMessage(content=message, id=str(uuid.uuid4()))
else:
tool_id = str(uuid.uuid4())
interrupt_message = AIMessage(
content="",
tool_calls=[{
"id": tool_id,
"name": action,
"args": args or {}
}]
)
interrupt_values = {
"action": action,
"args": args or {}
}
response = interrupt({
"__copilotkit_interrupt_value__": interrupt_values,
"__copilotkit_messages__": [interrupt_message]
})
answer = response.content
return answer, [interrupt_message, response]