memorydb-langchain 0.2.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,4 @@
1
+ """LangChain integration for MemoryDB."""
2
+ from memorydb_langchain.chat_history import MemoryDBChatMessageHistory
3
+
4
+ __all__ = ["MemoryDBChatMessageHistory"]
@@ -0,0 +1,69 @@
1
+ """LangChain integration for MemoryDB: chat message history backed by the memory engine.
2
+
3
+ Each session lives in one `chat` record; LangChain-style appends become new immutable
4
+ revisions in the WAL, so `db.messages.get(pk).history()` is a free audit trail.
5
+
6
+ Install: pip install ./sdk && pip install langchain-core
7
+ """
8
+ import json
9
+
10
+ from langchain_core.chat_history import BaseChatMessageHistory
11
+ from langchain_core.messages import BaseMessage, messages_from_dict, message_to_dict
12
+
13
+ from memorydb import MemoryDB, MemoryDBError
14
+
15
+
16
+ class MemoryDBChatMessageHistory(BaseChatMessageHistory):
17
+ """Conversational memory stored as a versioned MemoryDB chat record."""
18
+
19
+ def __init__(self, session_id: str, *, client: MemoryDB | None = None,
20
+ url: str = "http://127.0.0.1:8080", token: str | None = None,
21
+ source: str = "langchain", max_retries: int = 3):
22
+ self.client = client or MemoryDB(url, token=token)
23
+ self.session_id = session_id
24
+ self.source = source
25
+ self.max_retries = max_retries
26
+
27
+ def _find(self):
28
+ listed = self.client.request("GET", f"memories/?kind=chat&source={self.source}&limit=500")
29
+ for hit in listed["results"]:
30
+ if hit["record"].get("metadata", {}).get("session_id") == self.session_id:
31
+ return hit["record"]
32
+ return None
33
+
34
+ @property
35
+ def messages(self):
36
+ record = self._find()
37
+ if record is None:
38
+ return []
39
+ return messages_from_dict(json.loads(record["content"]))
40
+
41
+ def add_messages(self, messages):
42
+ payload_new = [message_to_dict(m) for m in messages]
43
+ for attempt in range(self.max_retries):
44
+ record = self._find()
45
+ try:
46
+ if record is None:
47
+ # Full-record PUT carries the version we read; a concurrent writer bumps it first,
48
+ # so a conflict means re-read and retry rather than lose the other append.
49
+ self.client.request("POST", "memories/", {
50
+ "kind": "chat", "content": json.dumps(payload_new),
51
+ "source": self.source, "layer": "raw",
52
+ "metadata": {"session_id": self.session_id, "method": "langchain chat history"}})
53
+ else:
54
+ updated = dict(record)
55
+ updated["content"] = json.dumps(json.loads(record["content"]) + payload_new)
56
+ self.client.request("PUT", "memories/" + record["id"] + "/", updated)
57
+ return
58
+ except MemoryDBError:
59
+ if attempt == self.max_retries - 1:
60
+ raise
61
+
62
+ def add_message(self, message: BaseMessage):
63
+ self.add_messages([message])
64
+
65
+ def clear(self):
66
+ record = self._find()
67
+ if record is not None:
68
+ self.client.request("DELETE", "memories/" + record["id"] + "/",
69
+ headers={"If-Match": str(record["version"])})
@@ -0,0 +1,54 @@
1
+ Metadata-Version: 2.4
2
+ Name: memorydb-langchain
3
+ Version: 0.2.1
4
+ Summary: LangChain chat-message history backed by the MemoryDB agent-memory engine
5
+ Author: humaniods
6
+ License-Expression: MIT
7
+ Project-URL: Homepage, https://github.com/humaniods/MemoryDB
8
+ Project-URL: Repository, https://github.com/humaniods/MemoryDB
9
+ Project-URL: Issues, https://github.com/humaniods/MemoryDB/issues
10
+ Keywords: langchain,agent-memory,chat-history,llm,database
11
+ Classifier: Development Status :: 4 - Beta
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: Operating System :: POSIX
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
16
+ Requires-Python: >=3.10
17
+ Description-Content-Type: text/markdown
18
+ Requires-Dist: superb-memory
19
+ Requires-Dist: langchain-core>=0.2
20
+
21
+ # memorydb-langchain
22
+
23
+ LangChain chat message history backed by
24
+ [MemoryDB](https://github.com/humaniods/MemoryDB), so agent conversations survive process
25
+ restarts and stay searchable alongside the facts you ingest from them.
26
+
27
+ ```bash
28
+ pip install memorydb-langchain
29
+ export MEMORYDB_URL=memorydb://TOKEN@127.0.0.1:8080
30
+ ```
31
+
32
+ ```python
33
+ from memorydb import MemoryDB
34
+ from memorydb_langchain import MemoryDBChatMessageHistory
35
+ from langchain_core.messages import HumanMessage, AIMessage
36
+
37
+ history = MemoryDBChatMessageHistory(session_id="user-42", client=MemoryDB())
38
+ history.add_message(HumanMessage("What is the capital of Rajasthan?"))
39
+ history.add_message(AIMessage("Jaipur."))
40
+ history.messages # [HumanMessage, AIMessage]
41
+ history.clear()
42
+ ```
43
+
44
+ Every append writes a new immutable revision of one record rather than rewriting a table, so the
45
+ server keeps the full trail:
46
+
47
+ ```python
48
+ db = MemoryDB()
49
+ db.objects.history(pk=history.record_id)["results"] # versions 1..n, a free audit log
50
+ ```
51
+
52
+ Use one principal (token) per deployed product so sessions of different products land in
53
+ different workspaces, and give each end-user their own `session_id`. Search the whole history
54
+ with `db.objects.search("Jaipur")`, including hybrid BM25 + vector ranking.
@@ -0,0 +1,6 @@
1
+ memorydb_langchain/__init__.py,sha256=5dZLH8sPHnFE4X4ujGx_mK5q4WoPjmA8lMZ4ZutkrAs,155
2
+ memorydb_langchain/chat_history.py,sha256=gnA3YjUL1z_6Ba80IBpL6ns_TxL4METtWWcbK0cEJr4,2984
3
+ memorydb_langchain-0.2.1.dist-info/METADATA,sha256=g__gulqh4Hb-LD3BKKYzs9lmC-NgDsc1uSizZIYDDVk,2060
4
+ memorydb_langchain-0.2.1.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
5
+ memorydb_langchain-0.2.1.dist-info/top_level.txt,sha256=heoqqw4PcZpOa9tN7HsXrn0HZsMPbQW71bYbm8IFlOs,19
6
+ memorydb_langchain-0.2.1.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (84.2.1)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1 @@
1
+ memorydb_langchain