ladybug-memory 0.1.0__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.
@@ -0,0 +1,13 @@
1
+ *.swp
2
+ *.swo
3
+ *~
4
+ *.swn
5
+ *.pyc
6
+ .coverage
7
+ .ropeproject
8
+ *.egg-info
9
+ .tox
10
+ build
11
+ .DS_Store
12
+ dist/
13
+ uv.lock
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Ladybug Memory, Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,36 @@
1
+ Metadata-Version: 2.4
2
+ Name: ladybug-memory
3
+ Version: 0.1.0
4
+ Summary: Agent memory interface with implementations based on mem0 and engram
5
+ License-File: LICENSE
6
+ Requires-Python: >=3.10
7
+ Requires-Dist: fastembed
8
+ Requires-Dist: real-ladybug>=0.14.0
9
+ Provides-Extra: dev
10
+ Requires-Dist: pytest>=7.0.0; extra == 'dev'
11
+ Requires-Dist: ruff>=0.1.0; extra == 'dev'
12
+ Description-Content-Type: text/markdown
13
+
14
+ # Memory
15
+
16
+ Agent memory interface with implementations based on mem0 and engram APIs.
17
+
18
+ ## Installation
19
+
20
+ ```bash
21
+ uv sync
22
+ ```
23
+
24
+ ## Usage
25
+
26
+ ```python
27
+ from memory import LadybugMemory
28
+
29
+ mem = LadybugMemory("memory.lbug")
30
+
31
+ mem.store("User prefers Python", memory_type="preference", importance=8)
32
+
33
+ results = mem.search("python")
34
+ for r in results:
35
+ print(r.entry.content)
36
+ ```
@@ -0,0 +1,23 @@
1
+ # Memory
2
+
3
+ Agent memory interface with implementations based on mem0 and engram APIs.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ uv sync
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```python
14
+ from memory import LadybugMemory
15
+
16
+ mem = LadybugMemory("memory.lbug")
17
+
18
+ mem.store("User prefers Python", memory_type="preference", importance=8)
19
+
20
+ results = mem.search("python")
21
+ for r in results:
22
+ print(r.entry.content)
23
+ ```
@@ -0,0 +1,38 @@
1
+ from memory import LadybugMemory
2
+
3
+
4
+ def main() -> None:
5
+ mem = LadybugMemory("memory.lbdb")
6
+
7
+ mem.store(
8
+ "User prefers Python for data analysis", memory_type="preference", importance=8
9
+ )
10
+ mem.store("User is allergic to nuts", memory_type="preference", importance=9)
11
+ mem.store("Fixed bug in login handler", memory_type="work", importance=7)
12
+ mem.store(
13
+ "The python is a large snake found in tropical regions",
14
+ memory_type="fact",
15
+ importance=5,
16
+ )
17
+
18
+ print("=== Keyword Search for 'python' ===")
19
+ results = mem.search("python")
20
+ for r in results:
21
+ print(f" - {r.entry.content} (score: {r.score})")
22
+
23
+ print("\n=== Semantic Search for 'snake' ===")
24
+ results = mem.semantic_search("snake", limit=3)
25
+ for r in results:
26
+ print(f" - {r.entry.content} (score: {r.score:.4f})")
27
+
28
+ print("\nRecall all memories:")
29
+ for entry in mem.recall():
30
+ print(
31
+ f" - [{entry.memory_type}] {entry.content} (importance: {entry.importance})"
32
+ )
33
+
34
+ print(f"\nTotal memories: {mem.count()}")
35
+
36
+
37
+ if __name__ == "__main__":
38
+ main()
Binary file
@@ -0,0 +1,29 @@
1
+ [project]
2
+ name = "ladybug-memory"
3
+ version = "0.1.0"
4
+ description = "Agent memory interface with implementations based on mem0 and engram"
5
+ readme = "README.md"
6
+ requires-python = ">=3.10"
7
+ dependencies = [
8
+ "real-ladybug>=0.14.0",
9
+ "fastembed",
10
+ ]
11
+
12
+ [project.optional-dependencies]
13
+ dev = [
14
+ "pytest>=7.0.0",
15
+ "ruff>=0.1.0",
16
+ ]
17
+
18
+ [build-system]
19
+ requires = ["hatchling"]
20
+ build-backend = "hatchling.build"
21
+
22
+ [tool.hatch.build.targets.wheel]
23
+ packages = ["src/memory"]
24
+
25
+ [tool.uv]
26
+ dev-dependencies = [
27
+ "pytest>=7.0.0",
28
+ "ruff>=0.1.0",
29
+ ]
@@ -0,0 +1,9 @@
1
+ from memory.interface import AgentMemory, MemoryEntry, MemorySearchResult
2
+ from memory.ladybug import LadybugMemory
3
+
4
+ __all__ = [
5
+ "AgentMemory",
6
+ "MemoryEntry",
7
+ "MemorySearchResult",
8
+ "LadybugMemory",
9
+ ]
@@ -0,0 +1,100 @@
1
+ from abc import ABC, abstractmethod
2
+ from dataclasses import dataclass
3
+ from datetime import datetime
4
+ from typing import Any
5
+
6
+
7
+ @dataclass
8
+ class MemoryEntry:
9
+ id: str
10
+ content: str
11
+ memory_type: str = "general"
12
+ importance: int = 5
13
+ metadata: dict[str, Any] | None = None
14
+ created_at: datetime | None = None
15
+ updated_at: datetime | None = None
16
+
17
+
18
+ @dataclass
19
+ class MemorySearchResult:
20
+ entry: MemoryEntry
21
+ score: float = 1.0
22
+
23
+
24
+ class AgentMemory(ABC):
25
+ @abstractmethod
26
+ def store(
27
+ self,
28
+ content: str,
29
+ memory_type: str = "general",
30
+ importance: int = 5,
31
+ metadata: dict[str, Any] | None = None,
32
+ ) -> MemoryEntry:
33
+ pass
34
+
35
+ @abstractmethod
36
+ def search(
37
+ self,
38
+ query: str,
39
+ limit: int = 5,
40
+ memory_type: str | None = None,
41
+ ) -> list[MemorySearchResult]:
42
+ pass
43
+
44
+ @abstractmethod
45
+ def semantic_search(
46
+ self,
47
+ query: str,
48
+ limit: int = 5,
49
+ memory_type: str | None = None,
50
+ ) -> list[MemorySearchResult]:
51
+ pass
52
+
53
+ @abstractmethod
54
+ def recall(
55
+ self,
56
+ limit: int = 10,
57
+ min_importance: int = 0,
58
+ memory_type: str | None = None,
59
+ ) -> list[MemoryEntry]:
60
+ pass
61
+
62
+ @abstractmethod
63
+ def get(self, memory_id: str) -> MemoryEntry | None:
64
+ pass
65
+
66
+ @abstractmethod
67
+ def update(
68
+ self,
69
+ memory_id: str,
70
+ content: str | None = None,
71
+ importance: int | None = None,
72
+ metadata: dict[str, Any] | None = None,
73
+ ) -> MemoryEntry | None:
74
+ pass
75
+
76
+ @abstractmethod
77
+ def delete(self, memory_id: str) -> bool:
78
+ pass
79
+
80
+ @abstractmethod
81
+ def link(
82
+ self,
83
+ source_id: str,
84
+ target_id: str,
85
+ relation: str = "related",
86
+ ) -> bool:
87
+ pass
88
+
89
+ @abstractmethod
90
+ def get_related(
91
+ self,
92
+ memory_id: str,
93
+ relation: str | None = None,
94
+ max_depth: int = 1,
95
+ ) -> list[tuple[MemoryEntry, str]]:
96
+ pass
97
+
98
+ @abstractmethod
99
+ def count(self) -> int:
100
+ pass
@@ -0,0 +1,405 @@
1
+ import os
2
+ import uuid
3
+ from datetime import datetime
4
+ from typing import Any, cast
5
+
6
+ import real_ladybug as lb
7
+ from fastembed import TextEmbedding
8
+
9
+ from memory.interface import (
10
+ AgentMemory,
11
+ MemoryEntry,
12
+ MemorySearchResult,
13
+ )
14
+
15
+
16
+ def _get_result(result: lb.QueryResult | list[lb.QueryResult]) -> lb.QueryResult:
17
+ if isinstance(result, list):
18
+ return result[0]
19
+ return result
20
+
21
+
22
+ class LadybugMemory(AgentMemory):
23
+ def __init__(self, db_path: str):
24
+ self.db = lb.Database(db_path)
25
+ self.conn = lb.Connection(self.db)
26
+ self._init_schema()
27
+ self._init_fts_index()
28
+ self._init_vector_search()
29
+
30
+ def _init_schema(self) -> None:
31
+ self.conn.execute("INSTALL JSON; LOAD EXTENSION JSON;")
32
+ self.conn.execute("INSTALL FTS; LOAD EXTENSION FTS;")
33
+ self.conn.execute("INSTALL vector; LOAD EXTENSION vector;")
34
+ self.conn.execute(
35
+ """
36
+ CREATE NODE TABLE IF NOT EXISTS Memory(
37
+ id STRING PRIMARY KEY,
38
+ content STRING,
39
+ memory_type STRING,
40
+ importance INT64,
41
+ metadata JSON,
42
+ embedding FLOAT[384],
43
+ created_at TIMESTAMP,
44
+ updated_at TIMESTAMP
45
+ )
46
+ """
47
+ )
48
+ self.conn.execute(
49
+ """
50
+ CREATE REL TABLE IF NOT EXISTS MemoryLink(
51
+ FROM Memory TO Memory,
52
+ relation STRING
53
+ )
54
+ """
55
+ )
56
+
57
+ def _init_fts_index(self) -> None:
58
+ try:
59
+ self.conn.execute(
60
+ """
61
+ CALL CREATE_FTS_INDEX(
62
+ 'Memory',
63
+ 'memory_content_fts',
64
+ ['content'],
65
+ stemmer := 'porter'
66
+ )
67
+ """
68
+ )
69
+ except Exception:
70
+ pass
71
+
72
+ def _init_embedding_model(self) -> None:
73
+ self._embedding_model = TextEmbedding("BAAI/bge-small-en-v1.5")
74
+
75
+ def _get_embedding(self, text: str) -> list[float]:
76
+ if not hasattr(self, "_embedding_model"):
77
+ self._init_embedding_model()
78
+ embeddings = list(self._embedding_model.embed([text]))
79
+ return embeddings[0]
80
+
81
+ def _init_vector_search(self) -> None:
82
+ try:
83
+ self.conn.execute(
84
+ """
85
+ CALL CREATE_VECTOR_INDEX(
86
+ 'Memory',
87
+ 'memory_content_index',
88
+ 'embedding',
89
+ metric := 'cosine'
90
+ )
91
+ """
92
+ )
93
+ except Exception:
94
+ pass
95
+
96
+ def _row_to_entry(self, row: list | dict) -> MemoryEntry:
97
+ if isinstance(row, dict):
98
+ return MemoryEntry(
99
+ id=cast(str, row.get("id")),
100
+ content=cast(str, row.get("content")),
101
+ memory_type=cast(str, row.get("memory_type")),
102
+ importance=cast(int, row.get("importance")),
103
+ metadata=row.get("metadata"),
104
+ created_at=cast(datetime, row.get("created_at")),
105
+ updated_at=cast(datetime, row.get("updated_at")),
106
+ )
107
+ return MemoryEntry(
108
+ id=str(row[0]),
109
+ content=str(row[1]),
110
+ memory_type=str(row[2]),
111
+ importance=int(row[3]),
112
+ metadata=row[4],
113
+ created_at=row[5] if isinstance(row[5], datetime) else None,
114
+ updated_at=row[6] if isinstance(row[6], datetime) else None,
115
+ )
116
+
117
+ def store(
118
+ self,
119
+ content: str,
120
+ memory_type: str = "general",
121
+ importance: int = 5,
122
+ metadata: dict[str, Any] | None = None,
123
+ ) -> MemoryEntry:
124
+ memory_id = str(uuid.uuid4())
125
+ now = datetime.now()
126
+ metadata_json = f"CAST('{str(metadata)}' AS JSON)" if metadata else "NULL"
127
+ embedding = self._get_embedding(content)
128
+ embedding_str = "[" + ",".join(str(x) for x in embedding) + "]"
129
+
130
+ self.conn.execute(
131
+ f"""
132
+ CREATE (m:Memory {{
133
+ id: '{memory_id}',
134
+ content: '{content.replace("'", "''")}',
135
+ memory_type: '{memory_type}',
136
+ importance: {importance},
137
+ metadata: {metadata_json},
138
+ embedding: {embedding_str},
139
+ created_at: timestamp('{now.strftime("%Y-%m-%d %H:%M:%S")}'),
140
+ updated_at: timestamp('{now.strftime("%Y-%m-%d %H:%M:%S")}')
141
+ }})
142
+ RETURN m.id
143
+ """
144
+ )
145
+
146
+ return MemoryEntry(
147
+ id=memory_id,
148
+ content=content,
149
+ memory_type=memory_type,
150
+ importance=importance,
151
+ metadata=metadata,
152
+ created_at=now,
153
+ updated_at=now,
154
+ )
155
+
156
+ def search(
157
+ self,
158
+ query: str,
159
+ limit: int = 5,
160
+ memory_type: str | None = None,
161
+ ) -> list[MemorySearchResult]:
162
+ if memory_type:
163
+ cypher = f"""
164
+ CALL QUERY_FTS_INDEX('Memory', 'memory_content_fts', '{query.replace("'", "''")}', top := {limit})
165
+ WITH node AS m, score
166
+ WHERE m.memory_type = '{memory_type}'
167
+ RETURN m.id, m.content, m.memory_type, m.importance, m.metadata, m.created_at, m.updated_at, score
168
+ ORDER BY score DESC
169
+ """
170
+ else:
171
+ cypher = f"""
172
+ CALL QUERY_FTS_INDEX('Memory', 'memory_content_fts', '{query.replace("'", "''")}', top := {limit})
173
+ WITH node AS m, score
174
+ RETURN m.id, m.content, m.memory_type, m.importance, m.metadata, m.created_at, m.updated_at, score
175
+ ORDER BY score DESC
176
+ """
177
+
178
+ raw_result = self.conn.execute(cypher)
179
+ result = _get_result(raw_result)
180
+ search_results = []
181
+
182
+ while result.has_next():
183
+ row = result.get_next()
184
+ entry = self._row_to_entry(row)
185
+ score = float(row[7]) if len(row) > 7 else 1.0
186
+ search_results.append(MemorySearchResult(entry=entry, score=score))
187
+
188
+ return search_results
189
+
190
+ def semantic_search(
191
+ self,
192
+ query: str,
193
+ limit: int = 5,
194
+ memory_type: str | None = None,
195
+ ) -> list[MemorySearchResult]:
196
+ query_embedding = self._get_embedding(query)
197
+ embedding_str = "[" + ",".join(str(x) for x in query_embedding) + "]"
198
+
199
+ if memory_type:
200
+ cypher = f"""
201
+ CALL QUERY_VECTOR_INDEX(
202
+ 'Memory',
203
+ 'memory_content_index',
204
+ {embedding_str},
205
+ {limit}
206
+ )
207
+ WITH node AS m, distance
208
+ WHERE m.memory_type = '{memory_type}'
209
+ RETURN m.id, m.content, m.memory_type, m.importance, m.metadata, m.created_at, m.updated_at, distance
210
+ ORDER BY distance
211
+ """
212
+ else:
213
+ cypher = f"""
214
+ CALL QUERY_VECTOR_INDEX(
215
+ 'Memory',
216
+ 'memory_content_index',
217
+ {embedding_str},
218
+ {limit}
219
+ )
220
+ WITH node AS m, distance
221
+ RETURN m.id, m.content, m.memory_type, m.importance, m.metadata, m.created_at, m.updated_at, distance
222
+ ORDER BY distance
223
+ """
224
+
225
+ raw_result = self.conn.execute(cypher)
226
+ result = _get_result(raw_result)
227
+ search_results = []
228
+
229
+ while result.has_next():
230
+ row = result.get_next()
231
+ entry = self._row_to_entry(row)
232
+ distance = float(row[7]) if len(row) > 7 else 0.0
233
+ score = 1.0 / (1.0 + distance)
234
+ search_results.append(MemorySearchResult(entry=entry, score=score))
235
+
236
+ return search_results
237
+
238
+ def recall(
239
+ self,
240
+ limit: int = 10,
241
+ min_importance: int = 0,
242
+ memory_type: str | None = None,
243
+ ) -> list[MemoryEntry]:
244
+ if memory_type:
245
+ cypher = f"""
246
+ MATCH (m:Memory)
247
+ WHERE m.memory_type = '{memory_type}'
248
+ AND m.importance >= {min_importance}
249
+ RETURN m.id, m.content, m.memory_type, m.importance, m.metadata, m.created_at, m.updated_at
250
+ ORDER BY m.importance DESC, m.created_at DESC
251
+ LIMIT {limit}
252
+ """
253
+ else:
254
+ cypher = f"""
255
+ MATCH (m:Memory)
256
+ WHERE m.importance >= {min_importance}
257
+ RETURN m.id, m.content, m.memory_type, m.importance, m.metadata, m.created_at, m.updated_at
258
+ ORDER BY m.importance DESC, m.created_at DESC
259
+ LIMIT {limit}
260
+ """
261
+
262
+ raw_result = self.conn.execute(cypher)
263
+ result = _get_result(raw_result)
264
+ entries = []
265
+
266
+ while result.has_next():
267
+ row = result.get_next()
268
+ entries.append(self._row_to_entry(row))
269
+
270
+ return entries
271
+
272
+ def get(self, memory_id: str) -> MemoryEntry | None:
273
+ cypher = f"""
274
+ MATCH (m:Memory)
275
+ WHERE m.id = '{memory_id}'
276
+ RETURN m.id, m.content, m.memory_type, m.importance, m.metadata, m.created_at, m.updated_at
277
+ """
278
+
279
+ raw_result = self.conn.execute(cypher)
280
+ result = _get_result(raw_result)
281
+
282
+ if result.has_next():
283
+ row = result.get_next()
284
+ return self._row_to_entry(row)
285
+
286
+ return None
287
+
288
+ def update(
289
+ self,
290
+ memory_id: str,
291
+ content: str | None = None,
292
+ importance: int | None = None,
293
+ metadata: dict[str, Any] | None = None,
294
+ ) -> MemoryEntry | None:
295
+ updates = []
296
+
297
+ if content is not None:
298
+ updates.append(f"m.content = '{content.replace("'", "''")}'")
299
+ if importance is not None:
300
+ updates.append(f"m.importance = {importance}")
301
+ if metadata is not None:
302
+ updates.append(f"m.metadata = CAST('{str(metadata)}' AS JSON)")
303
+
304
+ if not updates:
305
+ return self.get(memory_id)
306
+
307
+ now = datetime.now()
308
+ updates.append(
309
+ f"m.updated_at = timestamp('{now.strftime('%Y-%m-%d %H:%M:%S')}')"
310
+ )
311
+
312
+ cypher = f"""
313
+ MATCH (m:Memory)
314
+ WHERE m.id = '{memory_id}'
315
+ SET {", ".join(updates)}
316
+ RETURN m.id, m.content, m.memory_type, m.importance, m.metadata, m.created_at, m.updated_at
317
+ """
318
+
319
+ raw_result = self.conn.execute(cypher)
320
+ result = _get_result(raw_result)
321
+
322
+ if result.has_next():
323
+ row = result.get_next()
324
+ return self._row_to_entry(row)
325
+
326
+ return None
327
+
328
+ def delete(self, memory_id: str) -> bool:
329
+ cypher = f"""
330
+ MATCH (m:Memory)
331
+ WHERE m.id = '{memory_id}'
332
+ DELETE m
333
+ """
334
+
335
+ self.conn.execute(cypher)
336
+ return True
337
+
338
+ def link(
339
+ self,
340
+ source_id: str,
341
+ target_id: str,
342
+ relation: str = "related",
343
+ ) -> bool:
344
+ cypher = f"""
345
+ MATCH (a:Memory), (b:Memory)
346
+ WHERE a.id = '{source_id}' AND b.id = '{target_id}'
347
+ CREATE (a)-[r:MemoryLink {{relation: '{relation}'}}]->(b)
348
+ """
349
+
350
+ self.conn.execute(cypher)
351
+ return True
352
+
353
+ def get_related(
354
+ self,
355
+ memory_id: str,
356
+ relation: str | None = None,
357
+ max_depth: int = 1,
358
+ ) -> list[tuple[MemoryEntry, str]]:
359
+ if relation:
360
+ cypher = f"""
361
+ MATCH (m:Memory)-[r:MemoryLink {{relation: '{relation}'}}]->(related:Memory)
362
+ WHERE m.id = '{memory_id}'
363
+ RETURN related.id, related.content, related.memory_type, related.importance, related.metadata, related.created_at, related.updated_at, r.relation
364
+ """
365
+ else:
366
+ cypher = f"""
367
+ MATCH (m:Memory)-[r:MemoryLink]->(related:Memory)
368
+ WHERE m.id = '{memory_id}'
369
+ RETURN related.id, related.content, related.memory_type, related.importance, related.metadata, related.created_at, related.updated_at, r.relation
370
+ """
371
+
372
+ raw_result = self.conn.execute(cypher)
373
+ result = _get_result(raw_result)
374
+ related = []
375
+
376
+ while result.has_next():
377
+ row = result.get_next()
378
+ entry = MemoryEntry(
379
+ id=str(row[0]),
380
+ content=str(row[1]),
381
+ memory_type=str(row[2]),
382
+ importance=int(row[3]),
383
+ metadata=row[4],
384
+ created_at=row[5] if isinstance(row[5], datetime) else None,
385
+ updated_at=row[6] if isinstance(row[6], datetime) else None,
386
+ )
387
+ relation_type = str(row[7])
388
+ related.append((entry, relation_type))
389
+
390
+ return related
391
+
392
+ def count(self) -> int:
393
+ cypher = """
394
+ MATCH (m:Memory)
395
+ RETURN count(m)
396
+ """
397
+
398
+ raw_result = self.conn.execute(cypher)
399
+ result = _get_result(raw_result)
400
+
401
+ if result.has_next():
402
+ row = result.get_next()
403
+ return int(row[0])
404
+
405
+ return 0