forked from AzureCosmosDB/AgentMemoryToolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadvanced_search_patterns.py
More file actions
221 lines (179 loc) · 7.84 KB
/
Copy pathadvanced_search_patterns.py
File metadata and controls
221 lines (179 loc) · 7.84 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
"""
Advanced Search Patterns — Agent Memory Toolkit
Demonstrates vector, hybrid, and filtered search patterns using Cosmos DB
with AI Foundry embeddings.
Required environment variables:
COSMOS_DB_ENDPOINT – Azure Cosmos DB endpoint URL
AI_FOUNDRY_ENDPOINT – Azure AI Foundry endpoint URL (for embeddings)
"""
import os
from dotenv import load_dotenv
load_dotenv()
import uuid
from agent_memory_toolkit import CosmosMemoryClient
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
def print_header(title: str) -> None:
"""Print a formatted section header."""
print(f"\n{'=' * 60}")
print(f" {title}")
print(f"{'=' * 60}")
def print_results(results: list) -> None:
"""Print search results in a readable format."""
if not results:
print(" (no results)")
return
for i, result in enumerate(results, 1):
content = result.get("content", "") if isinstance(result, dict) else str(result)
role = result.get("role", "n/a") if isinstance(result, dict) else "n/a"
mem_type = result.get("type", "n/a") if isinstance(result, dict) else "n/a"
salience = result.get("salience") if isinstance(result, dict) else None
suffix = f" [salience: {salience:.2f}]" if isinstance(salience, (int, float)) else ""
print(f" {i}. [{role}] ({mem_type}) {content[:100]}{suffix}")
# ---------------------------------------------------------------------------
# Seed data
# ---------------------------------------------------------------------------
def seed_memories(mem: CosmosMemoryClient, user_id: str, thread_id: str) -> None:
"""Populate the store with sample memories for searching."""
entries = [
{"role": "user", "content": "I love hiking in the Pacific Northwest", "memory_type": "turn"},
{
"role": "agent",
"content": "The PNW has amazing trails like the Wonderland Trail and the PCT!",
"memory_type": "turn",
},
{"role": "user", "content": "My favorite food is sushi, especially salmon nigiri", "memory_type": "fact"},
{"role": "user", "content": "I usually run 5 miles every morning before work", "memory_type": "turn"},
{
"role": "agent",
"content": "Running is a great way to stay fit! Do you prefer road or trail running?",
"memory_type": "turn",
},
{"role": "user", "content": "I work as a software engineer at a startup in Seattle", "memory_type": "fact"},
{"role": "user", "content": "My preferred programming language is Python", "memory_type": "fact"},
{
"role": "agent",
"content": "Python is very popular for AI/ML workloads. What frameworks do you use?",
"memory_type": "turn",
},
]
print("Seeding memories …")
for entry in entries:
mem.add_cosmos(
user_id=user_id,
role=entry["role"],
content=entry["content"],
memory_type=entry["memory_type"],
thread_id=thread_id,
)
print(f" ✓ {len(entries)} memories added (thread {thread_id[:8]}…)\n")
# ---------------------------------------------------------------------------
# Search patterns
# ---------------------------------------------------------------------------
def vector_search(mem: CosmosMemoryClient, user_id: str) -> None:
"""Pattern 1 — Pure vector (semantic similarity) search."""
print_header("1. Vector Search (semantic similarity)")
print(" Query: 'outdoor activities'")
print(" Finds semantically related memories even without exact keyword matches.\n")
results = mem.search_cosmos(
search_terms="outdoor activities",
user_id=user_id,
top_k=5,
)
print_results(results)
def hybrid_search(mem: CosmosMemoryClient, user_id: str) -> None:
"""Pattern 2 — Hybrid search (vector + full-text)."""
print_header("2. Hybrid Search (vector + full-text)")
print(" Query: 'hiking trails Pacific Northwest'")
print(" Combines embedding similarity with BM25 keyword matching.\n")
results = mem.search_cosmos(
search_terms="hiking trails Pacific Northwest",
user_id=user_id,
hybrid_search=True,
top_k=5,
)
print_results(results)
def filtered_by_role(mem: CosmosMemoryClient, user_id: str) -> None:
"""Pattern 3 — Filtered search: only user messages."""
print_header("3. Filtered Search — by role ('user')")
print(" Query: 'preferences'")
print(" Restricts results to a specific conversation role.\n")
results = mem.search_cosmos(
search_terms="preferences",
user_id=user_id,
role="user",
top_k=3,
)
print_results(results)
def filtered_by_memory_type(mem: CosmosMemoryClient, user_id: str) -> None:
"""Pattern 4 — Filtered search: only 'fact' memories."""
print_header("4. Filtered Search — by memory_type ('fact')")
print(" Query: 'food preferences'")
print(" Narrows results to a specific memory category.\n")
results = mem.search_cosmos(
search_terms="food preferences",
user_id=user_id,
memory_type="fact",
top_k=3,
)
print_results(results)
def filtered_by_thread(mem: CosmosMemoryClient, user_id: str, thread_id: str) -> None:
"""Pattern 5 — Filtered search: scoped to a single thread."""
print_header("5. Filtered Search — by thread_id")
print(f" Query: 'activities' | thread: {thread_id[:8]}…")
print(" Limits results to a specific conversation thread.\n")
results = mem.search_cosmos(
search_terms="activities",
user_id=user_id,
thread_id=thread_id,
top_k=3,
)
print_results(results)
def top_k_tuning(mem: CosmosMemoryClient, user_id: str) -> None:
"""Pattern 6 — top-k tuning comparison."""
print_header("6. Top-K Tuning Comparison")
print(" Query: 'hobbies and interests'")
print(" Demonstrates how top_k affects the breadth of results.\n")
for k in (1, 3, 5):
results = mem.search_cosmos(
search_terms="hobbies and interests",
user_id=user_id,
top_k=k,
)
print(f" --- top_k={k} → {len(results)} result(s) ---")
print_results(results)
# ---------------------------------------------------------------------------
# Main
# ---------------------------------------------------------------------------
def main() -> None:
cosmos_endpoint = os.environ.get("COSMOS_DB_ENDPOINT")
ai_foundry_endpoint = os.environ.get("AI_FOUNDRY_ENDPOINT")
if not cosmos_endpoint or not ai_foundry_endpoint:
raise SystemExit(
"Error: Set COSMOS_DB_ENDPOINT and AI_FOUNDRY_ENDPOINT environment variables."
)
mem = CosmosMemoryClient(
cosmos_endpoint=cosmos_endpoint,
cosmos_database=os.environ.get("COSMOS_DB_DATABASE", "ai_memory"),
cosmos_container=os.environ.get("COSMOS_DB_CONTAINER", "memories"),
cosmos_key=os.environ.get("COSMOS_DB_KEY"),
ai_foundry_endpoint=ai_foundry_endpoint,
ai_foundry_api_key=os.environ.get("AI_FOUNDRY_API_KEY"),
embedding_deployment_name=os.environ.get("AI_FOUNDRY_EMBEDDING_DEPLOYMENT_NAME", "text-embedding-3-large"),
chat_deployment_name=os.environ.get("AI_FOUNDRY_CHAT_DEPLOYMENT_NAME", "gpt-4o-mini"),
)
user_id = "search-demo-user"
thread_id = str(uuid.uuid4())
seed_memories(mem, user_id, thread_id)
vector_search(mem, user_id)
hybrid_search(mem, user_id)
filtered_by_role(mem, user_id)
filtered_by_memory_type(mem, user_id)
filtered_by_thread(mem, user_id, thread_id)
top_k_tuning(mem, user_id)
print(f"\n{'=' * 60}")
print(" All search patterns complete.")
print(f"{'=' * 60}\n")
if __name__ == "__main__":
main()