feat(memory): expose expiration controls in client docs (#5874)
Co-authored-by: Soumil Rathi <soumilrathi@gmail.com>
This commit is contained in:
co-authored by
Soumil Rathi
parent
818c2981b7
commit
0fbbb2f525
+20
-5
@@ -182,14 +182,16 @@ class MemoryCreate(BaseModel):
|
||||
agent_id: Optional[str] = None
|
||||
run_id: Optional[str] = None
|
||||
metadata: Optional[Dict[str, Any]] = None
|
||||
expiration_date: Optional[str] = Field(None, description="Expiration date in YYYY-MM-DD format.")
|
||||
infer: Optional[bool] = Field(None, description="Whether to extract facts from messages. Defaults to True.")
|
||||
memory_type: Optional[str] = Field(None, description="Type of memory to store (e.g. 'core').")
|
||||
prompt: Optional[str] = Field(None, description="Custom prompt to use for fact extraction.")
|
||||
|
||||
|
||||
class MemoryUpdate(BaseModel):
|
||||
text: str = Field(..., description="New content to update the memory with.")
|
||||
text: Optional[str] = Field(None, description="New content to update the memory with.")
|
||||
metadata: Optional[Dict[str, Any]] = Field(None, description="Metadata to update.")
|
||||
expiration_date: Optional[str] = Field(None, description="Expiration date in YYYY-MM-DD format, or null to clear.")
|
||||
|
||||
|
||||
class SearchRequest(BaseModel):
|
||||
@@ -201,6 +203,7 @@ class SearchRequest(BaseModel):
|
||||
top_k: Optional[int] = Field(None, description="Maximum number of results to return.")
|
||||
threshold: Optional[float] = Field(None, description="Minimum similarity score for results.")
|
||||
explain: Optional[bool] = Field(None, description="Include score details for each search result.")
|
||||
show_expired: Optional[bool] = Field(None, description="Include expired memories.")
|
||||
|
||||
|
||||
class GenerateInstructionsRequest(BaseModel):
|
||||
@@ -379,7 +382,7 @@ def add_memory(memory_create: MemoryCreate, _auth=Depends(verify_auth)):
|
||||
|
||||
|
||||
ALL_MEMORIES_LIMIT = 1000
|
||||
_RESERVED_PAYLOAD_KEYS = {"data", "user_id", "agent_id", "run_id", "hash", "created_at", "updated_at"}
|
||||
_RESERVED_PAYLOAD_KEYS = {"data", "user_id", "agent_id", "run_id", "hash", "created_at", "updated_at", "expiration_date"}
|
||||
|
||||
|
||||
def _serialize_memory(row: Any) -> Dict[str, Any]:
|
||||
@@ -391,6 +394,7 @@ def _serialize_memory(row: Any) -> Dict[str, Any]:
|
||||
"agent_id": payload.get("agent_id"),
|
||||
"run_id": payload.get("run_id"),
|
||||
"hash": payload.get("hash"),
|
||||
"expiration_date": payload.get("expiration_date"),
|
||||
"metadata": {k: v for k, v in payload.items() if k not in _RESERVED_PAYLOAD_KEYS},
|
||||
"created_at": payload.get("created_at"),
|
||||
"updated_at": payload.get("updated_at"),
|
||||
@@ -410,6 +414,7 @@ def get_all_memories(
|
||||
run_id: Optional[str] = None,
|
||||
agent_id: Optional[str] = None,
|
||||
top_k: Optional[int] = Query(None, ge=0, le=ALL_MEMORIES_LIMIT),
|
||||
show_expired: bool = Query(False),
|
||||
_auth=Depends(verify_auth),
|
||||
):
|
||||
"""Retrieve stored memories. Lists all memories when no identifier is provided (admin only)."""
|
||||
@@ -418,6 +423,7 @@ def get_all_memories(
|
||||
auth_type = getattr(request.state, "auth_type", "none")
|
||||
if _auth is not None and _auth.role != "admin" and auth_type not in {"admin_api_key", "disabled"}:
|
||||
raise HTTPException(status_code=403, detail="Admin role required to list all memories.")
|
||||
# Admin all-memory listing is intentionally raw; scoped get_all below applies expiry visibility.
|
||||
return _list_all_memories(limit=top_k if top_k is not None else ALL_MEMORIES_LIMIT)
|
||||
filters = {
|
||||
k: v for k, v in {"user_id": user_id, "run_id": run_id, "agent_id": agent_id}.items() if v is not None
|
||||
@@ -425,6 +431,7 @@ def get_all_memories(
|
||||
params = {"filters": filters}
|
||||
if top_k is not None:
|
||||
params["top_k"] = top_k
|
||||
params["show_expired"] = show_expired
|
||||
return get_memory_instance().get_all(**params)
|
||||
except HTTPException:
|
||||
raise
|
||||
@@ -465,6 +472,8 @@ def search_memories(search_req: SearchRequest, _auth=Depends(verify_auth)):
|
||||
params["threshold"] = search_req.threshold
|
||||
if search_req.explain is not None:
|
||||
params["explain"] = search_req.explain
|
||||
if search_req.show_expired is not None:
|
||||
params["show_expired"] = search_req.show_expired
|
||||
return get_memory_instance().search(query=search_req.query, filters=filters, **params)
|
||||
except ValueError as e:
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
@@ -478,9 +487,15 @@ def search_memories(search_req: SearchRequest, _auth=Depends(verify_auth)):
|
||||
def update_memory(memory_id: str, updated_memory: MemoryUpdate, _auth=Depends(verify_auth)):
|
||||
"""Update an existing memory."""
|
||||
try:
|
||||
return get_memory_instance().update(
|
||||
memory_id=memory_id, data=updated_memory.text, metadata=updated_memory.metadata
|
||||
)
|
||||
fields_set = getattr(updated_memory, "model_fields_set", getattr(updated_memory, "__fields_set__", set()))
|
||||
params = {"memory_id": memory_id}
|
||||
if "text" in fields_set:
|
||||
params["data"] = updated_memory.text
|
||||
if "metadata" in fields_set:
|
||||
params["metadata"] = updated_memory.metadata
|
||||
if "expiration_date" in fields_set:
|
||||
params["expiration_date"] = updated_memory.expiration_date
|
||||
return get_memory_instance().update(**params)
|
||||
except (ValueError, Mem0ValidationError) as e:
|
||||
raise _client_error(e)
|
||||
except Exception:
|
||||
|
||||
Reference in New Issue
Block a user