generated from langchain-ai/data-enrichment
-
Notifications
You must be signed in to change notification settings - Fork 15
/
graph.py
89 lines (73 loc) · 3.62 KB
/
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
"""Example chatbot that incorporates user memories."""
from dataclasses import dataclass
from datetime import datetime, timezone
from langchain_core.runnables import RunnableConfig
from langgraph.graph import StateGraph
from langgraph.graph.message import Messages, add_messages
from langgraph.store.base import BaseStore
from langgraph_sdk import get_client
from typing_extensions import Annotated
from chatbot.configuration import ChatConfigurable
from chatbot.utils import format_memories, init_model
@dataclass
class ChatState:
"""The state of the chatbot."""
messages: Annotated[list[Messages], add_messages]
async def bot(
state: ChatState, config: RunnableConfig, store: BaseStore
) -> dict[str, list[Messages]]:
"""Prompt the bot to resopnd to the user, incorporating memories (if provided)."""
configurable = ChatConfigurable.from_runnable_config(config)
namespace = (configurable.user_id,)
# This lists ALL user memories in the provided namespace (up to the `limit`)
# you can also filter by content.
query = "\n".join(str(message.content) for message in state.messages)
items = await store.asearch(namespace, query=query, limit=10)
model = init_model(configurable.model)
prompt = configurable.system_prompt.format(
user_info=format_memories(items),
time=datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M:%S"),
)
m = await model.ainvoke(
[{"role": "system", "content": prompt}, *state.messages],
)
return {"messages": [m]}
async def schedule_memories(state: ChatState, config: RunnableConfig) -> None:
"""Prompt the bot to respond to the user, incorporating memories (if provided)."""
configurable = ChatConfigurable.from_runnable_config(config)
memory_client = get_client()
await memory_client.runs.create(
# We enqueue the memory formation process on the same thread.
# This means that IF this thread doesn't receive more messages before `after_seconds`,
# it will read from the shared state and extract memories for us.
# If a new request comes in for this thread before the scheduled run is executed,
# that run will be canceled, and a **new** one will be scheduled once
# this node is executed again.
thread_id=config["configurable"]["thread_id"],
# This memory-formation run will be enqueued and run later
# If a new run comes in before it is scheduled, it will be cancelled,
# then when this node is executed again, a *new* run will be scheduled
multitask_strategy="enqueue",
# This lets us "debounce" repeated requests to the memory graph
# if the user is actively engaging in a conversation. This saves us $$ and
# can help reduce the occurrence of duplicate memories.
after_seconds=configurable.delay_seconds,
# Specify the graph and/or graph configuration to handle the memory processing
assistant_id=configurable.mem_assistant_id,
# the memory service is running in the same deployment & thread, meaning
# it shares state with this chat bot. No content needs to be sent
input={"messages": []},
config={
"configurable": {
# Ensure the memory service knows where to save the extracted memories
"user_id": configurable.user_id,
"memory_types": configurable.memory_types,
},
},
)
builder = StateGraph(ChatState, config_schema=ChatConfigurable)
builder.add_node(bot)
builder.add_node(schedule_memories)
builder.add_edge("__start__", "bot")
builder.add_edge("bot", "schedule_memories")
graph = builder.compile()