memorydb-langchain 0.2.1__tar.gz
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.
- memorydb_langchain-0.2.1/PKG-INFO +54 -0
- memorydb_langchain-0.2.1/README.md +34 -0
- memorydb_langchain-0.2.1/memorydb_langchain/__init__.py +4 -0
- memorydb_langchain-0.2.1/memorydb_langchain/chat_history.py +69 -0
- memorydb_langchain-0.2.1/memorydb_langchain.egg-info/PKG-INFO +54 -0
- memorydb_langchain-0.2.1/memorydb_langchain.egg-info/SOURCES.txt +9 -0
- memorydb_langchain-0.2.1/memorydb_langchain.egg-info/dependency_links.txt +1 -0
- memorydb_langchain-0.2.1/memorydb_langchain.egg-info/requires.txt +2 -0
- memorydb_langchain-0.2.1/memorydb_langchain.egg-info/top_level.txt +1 -0
- memorydb_langchain-0.2.1/pyproject.toml +22 -0
- memorydb_langchain-0.2.1/setup.cfg +4 -0
|
@@ -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,34 @@
|
|
|
1
|
+
# memorydb-langchain
|
|
2
|
+
|
|
3
|
+
LangChain chat message history backed by
|
|
4
|
+
[MemoryDB](https://github.com/humaniods/MemoryDB), so agent conversations survive process
|
|
5
|
+
restarts and stay searchable alongside the facts you ingest from them.
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pip install memorydb-langchain
|
|
9
|
+
export MEMORYDB_URL=memorydb://TOKEN@127.0.0.1:8080
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
```python
|
|
13
|
+
from memorydb import MemoryDB
|
|
14
|
+
from memorydb_langchain import MemoryDBChatMessageHistory
|
|
15
|
+
from langchain_core.messages import HumanMessage, AIMessage
|
|
16
|
+
|
|
17
|
+
history = MemoryDBChatMessageHistory(session_id="user-42", client=MemoryDB())
|
|
18
|
+
history.add_message(HumanMessage("What is the capital of Rajasthan?"))
|
|
19
|
+
history.add_message(AIMessage("Jaipur."))
|
|
20
|
+
history.messages # [HumanMessage, AIMessage]
|
|
21
|
+
history.clear()
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
Every append writes a new immutable revision of one record rather than rewriting a table, so the
|
|
25
|
+
server keeps the full trail:
|
|
26
|
+
|
|
27
|
+
```python
|
|
28
|
+
db = MemoryDB()
|
|
29
|
+
db.objects.history(pk=history.record_id)["results"] # versions 1..n, a free audit log
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
Use one principal (token) per deployed product so sessions of different products land in
|
|
33
|
+
different workspaces, and give each end-user their own `session_id`. Search the whole history
|
|
34
|
+
with `db.objects.search("Jaipur")`, including hybrid BM25 + vector ranking.
|
|
@@ -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,9 @@
|
|
|
1
|
+
README.md
|
|
2
|
+
pyproject.toml
|
|
3
|
+
memorydb_langchain/__init__.py
|
|
4
|
+
memorydb_langchain/chat_history.py
|
|
5
|
+
memorydb_langchain.egg-info/PKG-INFO
|
|
6
|
+
memorydb_langchain.egg-info/SOURCES.txt
|
|
7
|
+
memorydb_langchain.egg-info/dependency_links.txt
|
|
8
|
+
memorydb_langchain.egg-info/requires.txt
|
|
9
|
+
memorydb_langchain.egg-info/top_level.txt
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
memorydb_langchain
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=77"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
[project]
|
|
5
|
+
name = "memorydb-langchain"
|
|
6
|
+
version = "0.2.1"
|
|
7
|
+
description = "LangChain chat-message history backed by the MemoryDB agent-memory engine"
|
|
8
|
+
readme = "README.md"
|
|
9
|
+
requires-python = ">=3.10"
|
|
10
|
+
license = "MIT"
|
|
11
|
+
authors = [{ name = "humaniods" }]
|
|
12
|
+
keywords = ["langchain", "agent-memory", "chat-history", "llm", "database"]
|
|
13
|
+
classifiers = ["Development Status :: 4 - Beta", "Intended Audience :: Developers",
|
|
14
|
+
"Operating System :: POSIX", "Programming Language :: Python :: 3",
|
|
15
|
+
"Topic :: Scientific/Engineering :: Artificial Intelligence"]
|
|
16
|
+
dependencies = ["superb-memory", "langchain-core>=0.2"]
|
|
17
|
+
[tool.setuptools]
|
|
18
|
+
packages = ["memorydb_langchain"]
|
|
19
|
+
[project.urls]
|
|
20
|
+
Homepage = "https://github.com/humaniods/MemoryDB"
|
|
21
|
+
Repository = "https://github.com/humaniods/MemoryDB"
|
|
22
|
+
Issues = "https://github.com/humaniods/MemoryDB/issues"
|