-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathlanggraph_store.py
More file actions
69 lines (53 loc) · 1.93 KB
/
Copy pathlanggraph_store.py
File metadata and controls
69 lines (53 loc) · 1.93 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
"""Minimal LangGraph store example for OceanBaseStore."""
from __future__ import annotations
from langchain_core.embeddings import Embeddings
from langchain_oceanbase import OceanBaseStore
class DemoEmbeddings(Embeddings):
def embed_documents(self, texts: list[str]) -> list[list[float]]:
return [self._embed(text) for text in texts]
def embed_query(self, text: str) -> list[float]:
return self._embed(text)
async def aembed_documents(self, texts: list[str]) -> list[list[float]]:
return self.embed_documents(texts)
async def aembed_query(self, text: str) -> list[float]:
return self.embed_query(text)
def _embed(self, text: str) -> list[float]:
lowered = text.lower()
return [
1.0 if "python" in lowered else 0.0,
1.0 if "database" in lowered else 0.0,
float((len(lowered) % 13) + 1),
]
def main() -> None:
store = OceanBaseStore(
connection_args={
"host": "127.0.0.1",
"port": "2881",
"user": "root@test",
"password": "",
"db_name": "test",
},
index={"dims": 3, "embed": DemoEmbeddings(), "fields": ["memory"]},
ttl_config={"refresh_on_read": True, "default_ttl": 60},
)
store.setup()
namespace = ("memories", "user-123")
store.put(
namespace,
"favorite-language",
{"memory": "The user prefers Python for data tooling."},
)
store.put(
namespace,
"database-note",
{"memory": "OceanBase is the preferred database backend."},
ttl=30,
)
exact = store.get(namespace, "favorite-language")
semantic = store.search(namespace, query="python tooling", limit=2)
namespaces = store.list_namespaces(prefix=("memories",))
print("Exact item:", exact)
print("Semantic results:", semantic)
print("Namespaces:", namespaces)
if __name__ == "__main__":
main()