mirror of
https://github.com/langchain-ai/memory-agent-functional-api.git
synced 2026-07-01 20:34:44 -04:00
44 lines
1.4 KiB
Python
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}"
|