-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory.py
More file actions
136 lines (112 loc) · 4.48 KB
/
Copy pathmemory.py
File metadata and controls
136 lines (112 loc) · 4.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env python3
"""
Dakera Python SDK — Memory & Session Operations.
Run:
python examples/memory.py
"""
import contextlib
import os
import sys
from dakera import BatchRecallRequest, DakeraClient
def main() -> None:
client = DakeraClient(
os.environ.get("DAKERA_API_URL", "http://localhost:3000"),
api_key=os.environ.get("DAKERA_API_KEY", "dk-mykey"),
)
agent_id = "agent-demo"
# -------------------------------------------------------------------------
# Store memories
# -------------------------------------------------------------------------
print("--- Storing Memories ---")
mem1 = client.store_memory(
agent_id,
content="The user prefers concise responses with code examples.",
memory_type="semantic",
importance=0.9,
metadata={"source": "user-feedback"},
)
mem1_id = mem1.get("id")
print(f"Stored memory: {mem1_id}")
mem2 = client.store_memory(
agent_id,
content="User is building a Python microservice with FastAPI.",
memory_type="episodic",
importance=0.7,
)
mem2_id = mem2.get("id")
print(f"Stored memory: {mem2_id}")
# -------------------------------------------------------------------------
# Recall memories (semantic search)
# -------------------------------------------------------------------------
print("\n--- Recalling Memories ---")
recalled = client.recall(agent_id, "What does the user prefer?", top_k=5)
for m in recalled.memories:
score = f"{m.score:.2f}" if m.score is not None else "n/a"
print(f" [{score}] {m.memory_type} — {m.content}")
# -------------------------------------------------------------------------
# Search memories by type
# -------------------------------------------------------------------------
print("\n--- Search Memories (type=semantic) ---")
searched = client.search_memories(
agent_id,
query="user preferences",
memory_type="semantic",
top_k=3,
)
for m in searched:
score = f"{m.get('score', 0.0):.2f}"
print(f" [{score}] {m.get('content', '')}")
# -------------------------------------------------------------------------
# Batch recall (filter-based, no embedding)
# -------------------------------------------------------------------------
print("\n--- Batch Recall ---")
batch_resp = client.batch_recall(
BatchRecallRequest(agent_id=agent_id, min_importance=0.8)
)
print(f"Batch recall found {batch_resp.filtered} memories")
# -------------------------------------------------------------------------
# Session management
# -------------------------------------------------------------------------
print("\n--- Session Management ---")
session = client.start_session(agent_id, metadata={"task": "code-review"})
session_id = session.get("id")
print(f"Started session: {session_id}")
# Store a session-scoped memory
client.store_memory(
agent_id,
content="Reviewing PR #42: refactor authentication middleware.",
session_id=session_id,
)
print("Stored session-scoped memory")
# End the session
try:
end_resp = client.end_session(session_id)
mem_count = end_resp.get("memory_count", "n/a")
print(f"Ended session (memories: {mem_count})")
except Exception as e:
print(f"endSession not fully supported on this server version: {e}")
# -------------------------------------------------------------------------
# Agent stats
# -------------------------------------------------------------------------
print("\n--- Agent Stats ---")
try:
stats = client.agent_stats(agent_id)
print(f"Agent: {stats.get('agent_id', agent_id)}")
print(f" Total memories: {stats.get('total_memories', 'n/a')}")
print(f" Total sessions: {stats.get('total_sessions', 'n/a')}")
except Exception as e:
print(f"Agent stats not fully supported: {e}")
# -------------------------------------------------------------------------
# Cleanup
# -------------------------------------------------------------------------
for mid in [mem1_id, mem2_id]:
if mid:
with contextlib.suppress(Exception):
client.forget(agent_id, mid)
print("\nCleaned up memories")
if __name__ == "__main__":
try:
main()
except Exception as e:
print(f"FATAL: {e}", file=sys.stderr)
sys.exit(1)