-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_store.py
More file actions
84 lines (65 loc) · 2.87 KB
/
Copy pathexample_store.py
File metadata and controls
84 lines (65 loc) · 2.87 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
#!/usr/bin/env python3
"""Option C example — GoodMemStore managing multiple collections.
A single store owns one HTTP connection shared across all collections.
Usage:
GOODMEM_API_KEY=your-key \
GOODMEM_BASE_URL=https://localhost:8080 \
GOODMEM_VERIFY_SSL=false \
python example_store.py
"""
import asyncio
import os
from dataclasses import dataclass
from typing import Annotated
from semantic_kernel.data.vector import VectorStoreField, vectorstoremodel
from goodmem_semantic_kernel import GoodMemStore
@vectorstoremodel
@dataclass
class Note:
id: Annotated[str | None, VectorStoreField("key")] = None
content: Annotated[str, VectorStoreField("data", type="str")] = ""
source: Annotated[str | None, VectorStoreField("data")] = None
async def main() -> None:
for var in ("GOODMEM_API_KEY", "GOODMEM_BASE_URL", "GOODMEM_VERIFY_SSL"):
if not os.getenv(var):
raise SystemExit(f"Set {var} before running this script.")
async with GoodMemStore() as store:
# 1. List all spaces currently visible to this API key
names = await store.list_collection_names()
print(f"Existing spaces: {names or '(none)'}")
# 2. Get two collections from the same store (shared HTTP connection)
notes = store.get_collection(Note, collection_name="store-notes")
todos = store.get_collection(Note, collection_name="store-todos")
# 3. Fresh slate for both
for collection in (notes, todos):
await collection.ensure_collection_deleted()
await collection.ensure_collection_exists()
print("Both collections ready (fresh).")
# 4. Write into each
await notes.upsert([
Note(content="The Eiffel Tower is in Paris", source="facts"),
Note(content="Mount Fuji is in Japan", source="facts"),
])
await todos.upsert([
Note(content="Buy groceries", source="chat"),
Note(content="Call the dentist", source="chat"),
])
print("Upserted into both collections.")
# 5. Wait for embeddings
print("Waiting for embeddings...")
await asyncio.sleep(3)
# 6. Search each independently
print("\n--- notes search: 'famous landmarks in europe' ---")
results = await notes.search("famous landmarks in europe", top=3)
async for r in results.results:
print(f" [{r.score:.3f}] {r.record.content!r}")
print("\n--- todos search: 'health appointments' ---")
results = await todos.search("health appointments", top=3)
async for r in results.results:
print(f" [{r.score:.3f}] {r.record.content!r}")
# 7. Uncomment to clean up:
# for collection in (notes, todos):
# await collection.ensure_collection_deleted()
# print("\nCollections deleted.")
if __name__ == "__main__":
asyncio.run(main())