Skip to content

Commit

Permalink
Update docstring on scheduling
Browse files Browse the repository at this point in the history
  • Loading branch information
hinthornw committed Oct 4, 2024
1 parent 8b71f2d commit 7b1cba3
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions src/chatbot/graph.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Example chatbot that incorporates user memories."""

import uuid
from dataclasses import dataclass
from datetime import datetime, timezone

Expand Down Expand Up @@ -47,8 +48,26 @@ 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()
mem_thread_id = str(
uuid.uuid5(
uuid.NAMESPACE_URL,
configurable.mem_assistant_id + config["configurable"]["thread_id"],
)
)
await memory_client.threads.create(thread_id=mem_thread_id, if_exists="do_nothing")
await memory_client.runs.create(
thread_id=config["configurable"]["thread_id"],
# Generate a thread so we can run the memory service on a separate
# but consistent thread. This lets us cancel scheduled runs if
# a new message arrives to our chatbot before the memory service
# begins processing.
thread_id=mem_thread_id,
# Rollback & cancel any scheduled runs for the target thread
# that haven't completed
multitask_strategy="rollback",
# This lets us "debounce" repeated requests to the memory graph
# if the user is actively engaging in a conversation
after_seconds=configurable.delay_seconds,
# Specify the graph and/or graph configuration to handle the memory processing
assistant_id=configurable.mem_assistant_id,
input={
# the service dedupes messages by ID, so we can send the full convo each time
Expand All @@ -57,13 +76,10 @@ async def schedule_memories(state: ChatState, config: RunnableConfig) -> None:
},
config={
"configurable": {
# Ensure the memory service knows where to save the extracted memories
"user_id": configurable.user_id,
},
},
multitask_strategy="enqueue",
# This lets us "debounce" repeated requests to the memory graph
# if the user is actively engaging in a conversation
after_seconds=configurable.delay_seconds,
)


Expand Down

0 comments on commit 7b1cba3

Please sign in to comment.