Skip to content

Commit 026306e

Browse files
Aayush KatariaAayush Kataria
authored andcommitted
resolving comments
1 parent 08bcd00 commit 026306e

46 files changed

Lines changed: 1051 additions & 749 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Docs/azure_testing.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -314,9 +314,9 @@ for i in range(10):
314314

315315
# Wait for the change-feed processor to catch up, then read derived memories.
316316
import time; time.sleep(15)
317-
print(memory.get_memories(user_id="user-1", thread_id="thread-1", memory_types=["summary"]))
317+
print(memory.get_thread_summary(user_id="user-1", thread_id="thread-1"))
318318
print(memory.get_memories(user_id="user-1", memory_types=["fact"]))
319-
print(memory.get_memories(user_id="user-1", memory_types=["user_summary"]))
319+
print(memory.get_user_summary(user_id="user-1"))
320320
```
321321

322322
### Change feed auto-processing
@@ -339,7 +339,7 @@ for i in range(3):
339339
# Wait a few seconds for the change feed to trigger, then check:
340340
import time
341341
time.sleep(10)
342-
results = memory.get_memories(user_id="user-1", thread_id=thread_id, memory_types=["summary"])
342+
results = memory.get_thread_summary(user_id="user-1", thread_id=thread_id)
343343
print(results) # Should contain an auto-generated summary
344344
```
345345

@@ -348,9 +348,9 @@ Check the Function App logs to confirm the `on_memory_change` trigger fired and
348348
### Verify stored results
349349

350350
```python
351-
print(memory.get_memories(user_id="user-1", memory_types=["summary"]))
351+
print(memory.get_thread_summary(user_id="user-1", thread_id="thread-1"))
352352
print(memory.get_memories(user_id="user-1", memory_types=["fact"]))
353-
print(memory.get_memories(user_id="user-1", memory_types=["user_summary"]))
353+
print(memory.get_user_summary(user_id="user-1"))
354354
```
355355

356356
---

Docs/local_testing.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,9 @@ No Azure resources are required for local in-memory operations.
115115

116116
```python
117117
import uuid
118-
from agent_memory_toolkit import AgentMemory
118+
from agent_memory_toolkit import CosmosMemoryClient
119119

120-
memory = AgentMemory(use_default_credential=False)
120+
memory = CosmosMemoryClient(use_default_credential=False)
121121

122122
THREAD_ID = str(uuid.uuid4())
123123

@@ -133,7 +133,7 @@ memory.delete_local(mem_id)
133133
print(f"Remaining: {len(memory.get_local())}")
134134
```
135135

136-
`AsyncAgentMemory` works the same way for local operations (local methods are synchronous).
136+
`AsyncCosmosMemoryClient` works the same way for local operations (local methods are synchronous).
137137

138138
---
139139

@@ -153,11 +153,11 @@ Then run a minimal smoke test:
153153
import os, uuid
154154
from dotenv import load_dotenv
155155
from azure.identity import DefaultAzureCredential
156-
from agent_memory_toolkit import AgentMemory
156+
from agent_memory_toolkit import CosmosMemoryClient
157157

158158
load_dotenv()
159159

160-
memory = AgentMemory(
160+
memory = CosmosMemoryClient(
161161
cosmos_endpoint=os.getenv("COSMOS_DB_ENDPOINT"),
162162
cosmos_database=os.getenv("COSMOS_DB_DATABASE"),
163163
cosmos_container=os.getenv("COSMOS_DB_MEMORIES_CONTAINER"),
@@ -196,11 +196,11 @@ for r in results:
196196
import os, uuid
197197
from dotenv import load_dotenv
198198
from azure.identity.aio import DefaultAzureCredential as AsyncDefaultAzureCredential
199-
from agent_memory_toolkit.aio import AsyncAgentMemory
199+
from agent_memory_toolkit.aio import AsyncCosmosMemoryClient
200200

201201
load_dotenv()
202202

203-
memory = AsyncAgentMemory(
203+
memory = AsyncCosmosMemoryClient(
204204
cosmos_endpoint=os.getenv("COSMOS_DB_ENDPOINT"),
205205
cosmos_database=os.getenv("COSMOS_DB_DATABASE"),
206206
cosmos_container=os.getenv("COSMOS_DB_MEMORIES_CONTAINER"),

Docs/public_api.md

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@
2828
- `delete_local(memory_id) -> None` — remove a local buffered memory.
2929
- `add_cosmos(user_id, role, content, memory_type='turn', metadata=None, thread_id=None, tags=None, ttl=None, salience=None, embedding=None, embed=None) -> str` — upsert one memory to Cosmos and return its id.
3030
- `push_to_cosmos(batch_size=25) -> None` — flush local buffered memories to Cosmos.
31-
- `get_memories(memory_id=None, user_id=None, thread_id=None, role=None, memory_types=None, recent_k=None, tags_all=None, tags_any=None, exclude_tags=None, include_superseded=False, min_salience=None, min_confidence=None, created_after=None, created_before=None) -> list[dict]` — retrieve memories with filters.
32-
- `update_cosmos(memory_id, content=None, role=None, memory_type=None, metadata=None) -> None` — update a Cosmos memory.
33-
- `delete_cosmos(memory_id, thread_id, user_id) -> None` — delete a Cosmos memory.
34-
- `get_thread(thread_id, user_id=None, memory_types=None, recent_k=None, tags_all=None, tags_any=None, exclude_tags=None, include_superseded=False, created_after=None, created_before=None) -> list[dict]` — retrieve a thread oldest-first.
31+
- `get_memories(memory_id=None, user_id=None, thread_id=None, role=None, memory_types=None, recent_k=None, tags_all=None, tags_any=None, exclude_tags=None, include_superseded=False, min_salience=None, min_confidence=None, created_after=None, created_before=None) -> list[dict]` — retrieve memories from the MEMORIES container. `memory_types` defaults to `["fact", "episodic", "procedural"]` and must be a subset of those three.
32+
- `update_cosmos(memory_id, *, user_id, thread_id, memory_type, content=None, role=None, metadata=None) -> None` — point-update a memory in the container that holds `memory_type`. The `type` field itself is never mutated.
33+
- `delete_cosmos(memory_id, *, user_id, thread_id, memory_type) -> None` — delete a memory from the container that holds `memory_type`.
34+
- `get_thread(thread_id, user_id=None, recent_k=None, tags_all=None, tags_any=None, exclude_tags=None, include_superseded=False, created_after=None, created_before=None) -> list[dict]` — retrieve turns from the TURNS container oldest-first.
35+
- `get_thread_summary(user_id, thread_id, recent_k=None) -> list[dict]` — retrieve thread summary documents from the SUMMARIES container for a single `(user_id, thread_id)` partition.
3536
- `get_user_summary(user_id) -> Optional[dict]` — retrieve the active user-summary document.
3637

3738
### Retrieval
@@ -56,8 +57,8 @@
5657

5758
### Tagging
5859

59-
- `add_tags(memory_id, user_id, thread_id, tags) -> None` — add tags to a memory.
60-
- `remove_tags(memory_id, user_id, thread_id, tags) -> None` — remove tags from a memory.
60+
- `add_tags(memory_id, user_id, thread_id, memory_type, tags) -> None` — add tags to a memory. `memory_type` must be one of `fact`, `episodic`, `procedural`.
61+
- `remove_tags(memory_id, user_id, thread_id, memory_type, tags) -> None` — remove tags from a memory. `memory_type` must be one of `fact`, `episodic`, `procedural`.
6162
- `list_tags(user_id, *, thread_id=None, prefix=None, include_sys=False) -> list[str]` — list sorted, deduped tags for a user; omits `sys:*` by default.
6263

6364
## AsyncCosmosMemoryClient
@@ -80,10 +81,11 @@ Local-buffer methods remain synchronous in-memory operations; Cosmos, retrieval,
8081
- `delete_local(memory_id) -> None` — remove a local buffered memory.
8182
- `async add_cosmos(user_id, role, content, memory_type='turn', metadata=None, thread_id=None, tags=None, ttl=None, salience=None, embedding=None, embed=None) -> str` — upsert one memory to Cosmos and return its id.
8283
- `async push_to_cosmos(batch_size=25) -> None` — flush local buffered memories to Cosmos.
83-
- `async get_memories(memory_id=None, user_id=None, thread_id=None, role=None, memory_types=None, recent_k=None, tags_all=None, tags_any=None, exclude_tags=None, include_superseded=False, min_salience=None, min_confidence=None, created_after=None, created_before=None) -> list[dict]` — retrieve memories with filters.
84-
- `async update_cosmos(memory_id, content=None, role=None, memory_type=None, metadata=None) -> None` — update a Cosmos memory.
85-
- `async delete_cosmos(memory_id, thread_id, user_id) -> None` — delete a Cosmos memory.
86-
- `async get_thread(thread_id, user_id=None, memory_types=None, recent_k=None, tags_all=None, tags_any=None, exclude_tags=None, include_superseded=False, created_after=None, created_before=None) -> list[dict]` — retrieve a thread oldest-first.
84+
- `async get_memories(memory_id=None, user_id=None, thread_id=None, role=None, memory_types=None, recent_k=None, tags_all=None, tags_any=None, exclude_tags=None, include_superseded=False, min_salience=None, min_confidence=None, created_after=None, created_before=None) -> list[dict]` — retrieve memories from the MEMORIES container. `memory_types` defaults to `["fact", "episodic", "procedural"]` and must be a subset of those three.
85+
- `async update_cosmos(memory_id, *, user_id, thread_id, memory_type, content=None, role=None, metadata=None) -> None` — point-update a memory in the container that holds `memory_type`. The `type` field itself is never mutated.
86+
- `async delete_cosmos(memory_id, *, user_id, thread_id, memory_type) -> None` — delete a memory from the container that holds `memory_type`.
87+
- `async get_thread(thread_id, user_id=None, recent_k=None, tags_all=None, tags_any=None, exclude_tags=None, include_superseded=False, created_after=None, created_before=None) -> list[dict]` — retrieve turns from the TURNS container oldest-first.
88+
- `async get_thread_summary(user_id, thread_id, recent_k=None) -> list[dict]` — retrieve thread summary documents from the SUMMARIES container for a single `(user_id, thread_id)` partition.
8789
- `async get_user_summary(user_id) -> Optional[dict]` — retrieve the active user-summary document.
8890

8991
### Retrieval
@@ -108,8 +110,8 @@ Local-buffer methods remain synchronous in-memory operations; Cosmos, retrieval,
108110

109111
### Tagging
110112

111-
- `async add_tags(memory_id, user_id, thread_id, tags) -> None` — add tags to a memory.
112-
- `async remove_tags(memory_id, user_id, thread_id, tags) -> None` — remove tags from a memory.
113+
- `async add_tags(memory_id, user_id, thread_id, memory_type, tags) -> None` — add tags to a memory. `memory_type` must be one of `fact`, `episodic`, `procedural`.
114+
- `async remove_tags(memory_id, user_id, thread_id, memory_type, tags) -> None` — remove tags from a memory. `memory_type` must be one of `fact`, `episodic`, `procedural`.
113115
- `async list_tags(user_id, *, thread_id=None, prefix=None, include_sys=False) -> list[str]` — list sorted, deduped tags for a user; omits `sys:*` by default.
114116

115117
## Topology validation

Samples/Advanced/advanced_memory_lifecycle.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ def main() -> None:
9292

9393
_header(5, "Archive: delete raw turns, keep derived memories")
9494
deleted = 0
95-
for m in mem.get_memories(user_id=user_id, thread_id=thread_id, memory_types=["turn"]):
96-
mem.delete_cosmos(memory_id=m["id"], thread_id=thread_id, user_id=user_id)
95+
for m in mem.get_thread(thread_id=thread_id, user_id=user_id):
96+
mem.delete_cosmos(memory_id=m["id"], user_id=user_id, thread_id=thread_id, memory_type="turn")
9797
deleted += 1
9898
print(f" deleted {deleted} raw turn(s)")
9999

Samples/Advanced/advanced_search_patterns.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@
1010
"""
1111

1212
import os
13+
import uuid
1314

1415
from dotenv import load_dotenv
15-
load_dotenv()
16-
import uuid
1716

1817
from agent_memory_toolkit import CosmosMemoryClient
1918

19+
load_dotenv()
20+
2021
# ---------------------------------------------------------------------------
2122
# Helpers
2223
# ---------------------------------------------------------------------------

Samples/Notebooks/Demo.ipynb

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,14 @@
5050
},
5151
"outputs": [],
5252
"source": [
53-
"import os, json\n",
54-
"from dotenv import load_dotenv\n",
55-
"from azure.identity import DefaultAzureCredential\n",
53+
"import json\n",
54+
"import os\n",
5655
"\n",
5756
"# Add repository root to path so we can import the package easily\n",
5857
"import sys\n",
58+
"\n",
59+
"from dotenv import load_dotenv\n",
60+
"\n",
5961
"sys.path.insert(0, os.path.abspath(\"../..\"))\n",
6062
"from agent_memory_toolkit import CosmosMemoryClient\n",
6163
"\n",
@@ -143,6 +145,7 @@
143145
"outputs": [],
144146
"source": [
145147
"import uuid\n",
148+
"\n",
146149
"THREAD_ID = str(uuid.uuid4())\n",
147150
"# Use a unique user_id per demo run so we get a clean extraction without\n",
148151
"# inheriting facts from prior runs that would dedup new content away.\n",
@@ -499,8 +502,8 @@
499502
"# Verify the memories were added directly to Cosmos DB (not in local memory)\n",
500503
"print(f\"Local memory count (should be unchanged): {len(memory.local_memory)}\\n\")\n",
501504
"\n",
502-
"cosmos_results = memory.get_memories(user_id=\"user-002\", thread_id=new_thread_id)\n",
503-
"print(f\"Memories in Cosmos DB for new thread: {len(cosmos_results)}\")\n",
505+
"cosmos_results = memory.get_thread(thread_id=new_thread_id, user_id=\"user-002\")\n",
506+
"print(f\"Turns in Cosmos DB for new thread: {len(cosmos_results)}\")\n",
504507
"for r in cosmos_results:\n",
505508
" print(f\" [{r['thread_id'][:8]}...] [{r['id'][:8]}...] role={r['role']:<6} {r['content'][:60]}\")"
506509
]
@@ -510,9 +513,9 @@
510513
"id": "40d603f1",
511514
"metadata": {},
512515
"source": [
513-
"### 3c. Retrieve memories from Cosmos DB with `get_memories`\n",
516+
"### 3c. Retrieve memories from Cosmos DB\n",
514517
"\n",
515-
"Supports the same filters as `get_local`: `memory_id`, `user_id`, `role`, `memory_type`."
518+
"`get_memories(...)` returns derived memories (`fact`, `episodic`, `procedural`) from the memories container. For other containers use `get_thread(thread_id=...)` for turns, `get_thread_summary(user_id, thread_id)` for thread summaries, and `get_user_summary(user_id)` for the cross-thread profile."
516519
]
517520
},
518521
{
@@ -533,16 +536,17 @@
533536
},
534537
"outputs": [],
535538
"source": [
536-
"# Get all memories for user-001\n",
539+
"# Derived memories (fact/episodic/procedural) for user-001. Empty until section 5 runs extract_memories.\n",
537540
"results = memory.get_memories(user_id=USER_ID)\n",
538-
"print(f\"Memories for user-001: {len(results)}\\n\")\n",
541+
"print(f\"Derived memories for {USER_ID}: {len(results)}\\n\")\n",
539542
"for r in results:\n",
540-
" print(f\" [{r['thread_id'][:8]}...] [{r['id'][:8]}...] role={r['role']:<6} type={r['type']:<8} {r['content'][:50]}\")\n",
543+
" print(f\" [{r['thread_id'][:8]}...] [{r['id'][:8]}...] type={r['type']:<10} {r['content'][:50]}\")\n",
541544
"\n",
542-
"# Get only agent memories\n",
543-
"agent_results = memory.get_memories(role=\"agent\")\n",
544-
"print(f\"\\nAgent memories: {len(agent_results)}\")\n",
545-
"for r in agent_results:\n",
545+
"# Turns live in a separate container — fetch via get_thread and filter in-process.\n",
546+
"turns = memory.get_thread(thread_id=THREAD_ID, user_id=USER_ID)\n",
547+
"agent_turns = [t for t in turns if t.get(\"role\") == \"agent\"]\n",
548+
"print(f\"\\nAgent turns in seed thread: {len(agent_turns)}\")\n",
549+
"for r in agent_turns:\n",
546550
" print(f\" [{r['thread_id'][:8]}...] [{r['id'][:8]}...] role={r['role']:<6} type={r['type']:<8} {r['content'][:50]}\")"
547551
]
548552
},
@@ -574,18 +578,21 @@
574578
},
575579
"outputs": [],
576580
"source": [
577-
"# Update the user's budget message to add a hotel budget constraint\n",
578-
"user_msgs = memory.get_memories(user_id=USER_ID, role=\"user\")\n",
579-
"target = [m for m in user_msgs if \"Pike Place\" in m[\"content\"]][0]\n",
581+
"# Update the user's budget turn (in the seed thread) to add a hotel budget constraint\n",
582+
"turns = memory.get_thread(thread_id=THREAD_ID, user_id=USER_ID)\n",
583+
"target = [m for m in turns if m.get(\"role\") == \"user\" and \"Pike Place\" in m[\"content\"]][0]\n",
580584
"print(f\"Before: {target['content']}\\n\")\n",
581585
"\n",
582586
"memory.update_cosmos(\n",
583587
" memory_id=target[\"id\"],\n",
588+
" user_id=target[\"user_id\"],\n",
589+
" thread_id=target[\"thread_id\"],\n",
590+
" memory_type=target[\"type\"],\n",
584591
" content=\"Something near Pike Place Market would be great. Keep flights under $300 round trip and hotels under $200/night.\",\n",
585592
" metadata={\"edited\": True, \"reason\": \"user clarified hotel budget\"},\n",
586593
")\n",
587594
"\n",
588-
"updated = memory.get_memories(memory_id=target[\"id\"])[0]\n",
595+
"updated = [m for m in memory.get_thread(thread_id=THREAD_ID, user_id=USER_ID) if m[\"id\"] == target[\"id\"]][0]\n",
589596
"print(f\"After: {updated['content']}\")"
590597
]
591598
},
@@ -607,20 +614,21 @@
607614
},
608615
"outputs": [],
609616
"source": [
610-
"# Delete the tool memory from Cosmos\n",
611-
"tool_mems = memory.get_memories(user_id=\"user-002\", role=\"tool\")\n",
617+
"# Delete the tool turn from the new thread in Cosmos\n",
618+
"tool_mems = [t for t in memory.get_thread(thread_id=new_thread_id, user_id=\"user-002\") if t.get(\"role\") == \"tool\"]\n",
612619
"print(tool_mems[0])\n",
613620
"if tool_mems:\n",
614621
" memory.delete_cosmos(\n",
615622
" tool_mems[0][\"id\"],\n",
616-
" thread_id=tool_mems[0][\"thread_id\"],\n",
617623
" user_id=tool_mems[0][\"user_id\"],\n",
624+
" thread_id=tool_mems[0][\"thread_id\"],\n",
625+
" memory_type=tool_mems[0][\"type\"],\n",
618626
" )\n",
619627
" print(f\"Deleted tool memory {tool_mems[0]['id'][:8]}...\")\n",
620628
"\n",
621-
"# Verify\n",
622-
"remaining = memory.get_memories()\n",
623-
"print(f\"\\nRemaining memories in Cosmos DB: {len(remaining)}\")\n",
629+
"# Verify the remaining turns in the new thread\n",
630+
"remaining = memory.get_thread(thread_id=new_thread_id, user_id=\"user-002\")\n",
631+
"print(f\"\\nRemaining turns in new thread: {len(remaining)}\")\n",
624632
"for r in remaining:\n",
625633
" print(f\" [{r['thread_id'][:8]}...] [{r['id'][:8]}...] role={r['role']:<6} type={r['type']:<8} {r['content'][:50]}\")"
626634
]
@@ -662,14 +670,14 @@
662670
"print(f\"Using thread_id: {thread_id}\\n\")\n",
663671
"\n",
664672
"# Get all documents in the thread\n",
665-
"thread_all = memory.get_thread(thread_id=thread_id)\n",
673+
"thread_all = memory.get_thread(thread_id=thread_id, user_id=USER_ID)\n",
666674
"print(f\"All memories in thread: {len(thread_all)}\")\n",
667675
"for m in thread_all:\n",
668676
" print(f\" [{m['thread_id'][:8]}...] [{m['id'][:8]}...] role={m.get('role','?'):6} type={m['type']:8} {m['content'][:60]}\")\n",
669677
"\n",
670678
"# Most recent 2\n",
671-
"recent = memory.get_thread(thread_id=thread_id, recent_k=2)\n",
672-
"print(f\"\\nMost recent 2 memories:\")\n",
679+
"recent = memory.get_thread(thread_id=thread_id, user_id=USER_ID, recent_k=2)\n",
680+
"print(\"\\nMost recent 2 memories:\")\n",
673681
"for m in recent:\n",
674682
" print(f\" [{m['thread_id'][:8]}...] [{m['id'][:8]}...] role={m.get('role','?'):6} type={m['type']:8} {m['content'][:60]}\")\n",
675683
"\n",

0 commit comments

Comments
 (0)