Files
memory-agent-functional-api/src/memory_agent/tools.py
T
William Fu-Hinthorn 3581dce4c6 Use indexing
2024-12-02 18:01:33 -08:00

44 lines
1.4 KiB
Python

"""Define he agent's tools."""
import uuid
from typing import Annotated, Optional
from langchain_core.runnables import RunnableConfig
from langchain_core.tools import InjectedToolArg
from langgraph.store.base import BaseStore
from memory_agent.configuration import Configuration
async def upsert_memory(
content: str,
context: str,
*,
memory_id: Optional[uuid.UUID] = None,
# Hide these arguments from the model.
config: Annotated[RunnableConfig, InjectedToolArg],
store: Annotated[BaseStore, InjectedToolArg],
):
"""Upsert a memory in the database.
If a memory conflicts with an existing one, then just UPDATE the
existing one by passing in memory_id - don't create two memories
that are the same. If the user corrects a memory, UPDATE it.
Args:
content: The main content of the memory. For example:
"User expressed interest in learning about French."
context: Additional context for the memory. For example:
"This was mentioned while discussing career options in Europe."
memory_id: ONLY PROVIDE IF UPDATING AN EXISTING MEMORY.
The memory to overwrite.
"""
mem_id = memory_id or uuid.uuid4()
user_id = Configuration.from_runnable_config(config).user_id
await store.aput(
("memories", user_id),
key=str(mem_id),
value={"content": content, "context": context},
)
return f"Stored memory {mem_id}"