llm-smartmem 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.
Files changed (40) hide show
  1. llm_smartmem-0.1.0/.gitignore +64 -0
  2. llm_smartmem-0.1.0/LICENSE +21 -0
  3. llm_smartmem-0.1.0/PKG-INFO +365 -0
  4. llm_smartmem-0.1.0/README.md +317 -0
  5. llm_smartmem-0.1.0/examples/01_basic_usage.py +86 -0
  6. llm_smartmem-0.1.0/examples/02_callbacks.py +78 -0
  7. llm_smartmem-0.1.0/examples/03_multi_user.py +101 -0
  8. llm_smartmem-0.1.0/examples/04_with_gemini.py +116 -0
  9. llm_smartmem-0.1.0/examples/04_with_openai.py +105 -0
  10. llm_smartmem-0.1.0/examples/05_langchain_integration.py +110 -0
  11. llm_smartmem-0.1.0/examples/06_langgraph_integration.py +126 -0
  12. llm_smartmem-0.1.0/examples/07_postgres_storage.py +114 -0
  13. llm_smartmem-0.1.0/examples/08_mongodb_storage.py +131 -0
  14. llm_smartmem-0.1.0/examples/09_e2e_agent_test.py +234 -0
  15. llm_smartmem-0.1.0/examples/10_custom_storage.py +262 -0
  16. llm_smartmem-0.1.0/examples/README.md +103 -0
  17. llm_smartmem-0.1.0/llmem/__init__.py +8 -0
  18. llm_smartmem-0.1.0/llmem/health.py +98 -0
  19. llm_smartmem-0.1.0/llmem/integrations/__init__.py +14 -0
  20. llm_smartmem-0.1.0/llmem/integrations/langchain.py +260 -0
  21. llm_smartmem-0.1.0/llmem/integrations/langgraph.py +311 -0
  22. llm_smartmem-0.1.0/llmem/memory.py +435 -0
  23. llm_smartmem-0.1.0/llmem/storage/__init__.py +17 -0
  24. llm_smartmem-0.1.0/llmem/storage/base.py +104 -0
  25. llm_smartmem-0.1.0/llmem/storage/memory.py +94 -0
  26. llm_smartmem-0.1.0/llmem/storage/mongo.py +192 -0
  27. llm_smartmem-0.1.0/llmem/storage/postgres.py +283 -0
  28. llm_smartmem-0.1.0/llmem/types.py +110 -0
  29. llm_smartmem-0.1.0/llmem/utils/__init__.py +5 -0
  30. llm_smartmem-0.1.0/llmem/utils/tokens.py +107 -0
  31. llm_smartmem-0.1.0/pyproject.toml +86 -0
  32. llm_smartmem-0.1.0/tests/__init__.py +1 -0
  33. llm_smartmem-0.1.0/tests/conftest.py +18 -0
  34. llm_smartmem-0.1.0/tests/test_health.py +122 -0
  35. llm_smartmem-0.1.0/tests/test_integrations.py +179 -0
  36. llm_smartmem-0.1.0/tests/test_memory.py +307 -0
  37. llm_smartmem-0.1.0/tests/test_memory_async.py +212 -0
  38. llm_smartmem-0.1.0/tests/test_storage.py +216 -0
  39. llm_smartmem-0.1.0/tests/test_tokens.py +97 -0
  40. llm_smartmem-0.1.0/tests/test_types.py +166 -0
@@ -0,0 +1,64 @@
1
+ # Environment and secrets
2
+ .env
3
+ .env.*
4
+ *.env
5
+
6
+ # Planning/Discussion files (contain patent ideas - DO NOT COMMIT)
7
+ IMPLEMENTATION_PLAN.md
8
+ RESEARCH_FULL.md
9
+ SCALABILITY_ANALYSIS.md
10
+ DESIGN.md
11
+ *_NOTES.md
12
+ *_PLAN.md
13
+ *_RESEARCH.md
14
+
15
+ # Python
16
+ __pycache__/
17
+ *.py[cod]
18
+ *$py.class
19
+ *.so
20
+ .Python
21
+ build/
22
+ develop-eggs/
23
+ dist/
24
+ downloads/
25
+ eggs/
26
+ .eggs/
27
+ lib/
28
+ lib64/
29
+ parts/
30
+ sdist/
31
+ var/
32
+ wheels/
33
+ *.egg-info/
34
+ .installed.cfg
35
+ *.egg
36
+ MANIFEST
37
+
38
+ # Virtual environments
39
+ venv/
40
+ env/
41
+ .venv/
42
+ ENV/
43
+
44
+ # IDE
45
+ .vscode/
46
+ .idea/
47
+ *.swp
48
+ *.swo
49
+ *~
50
+
51
+ # OS
52
+ .DS_Store
53
+ Thumbs.db
54
+
55
+ # Testing
56
+ .coverage
57
+ .pytest_cache/
58
+ htmlcov/
59
+ .tox/
60
+ coverage.xml
61
+
62
+ # Logs
63
+ *.log
64
+ logs/
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 LLMem Contributors
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,365 @@
1
+ Metadata-Version: 2.4
2
+ Name: llm-smartmem
3
+ Version: 0.1.0
4
+ Summary: Smart memory management for LLM conversations - topic-aware compression that just works
5
+ Project-URL: Homepage, https://github.com/sharanharsoor/llmem
6
+ Project-URL: Repository, https://github.com/sharanharsoor/llmem
7
+ Project-URL: Issues, https://github.com/sharanharsoor/llmem/issues
8
+ Author: Sharan
9
+ License: MIT
10
+ License-File: LICENSE
11
+ Keywords: ai,chatbot,compression,context,langchain,langgraph,llm,memory
12
+ Classifier: Development Status :: 4 - Beta
13
+ Classifier: Intended Audience :: Developers
14
+ Classifier: License :: OSI Approved :: MIT License
15
+ Classifier: Programming Language :: Python :: 3
16
+ Classifier: Programming Language :: Python :: 3.9
17
+ Classifier: Programming Language :: Python :: 3.10
18
+ Classifier: Programming Language :: Python :: 3.11
19
+ Classifier: Programming Language :: Python :: 3.12
20
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
21
+ Requires-Python: >=3.9
22
+ Requires-Dist: python-dotenv>=1.0.0
23
+ Requires-Dist: tiktoken>=0.5.0
24
+ Provides-Extra: all
25
+ Requires-Dist: asyncpg>=0.29.0; extra == 'all'
26
+ Requires-Dist: google-generativeai>=0.3.0; extra == 'all'
27
+ Requires-Dist: langchain-core>=0.1.0; extra == 'all'
28
+ Requires-Dist: langgraph>=0.0.50; extra == 'all'
29
+ Requires-Dist: motor>=3.0.0; extra == 'all'
30
+ Requires-Dist: openai>=1.0.0; extra == 'all'
31
+ Provides-Extra: dev
32
+ Requires-Dist: pytest-asyncio>=0.21.0; extra == 'dev'
33
+ Requires-Dist: pytest-cov>=4.0.0; extra == 'dev'
34
+ Requires-Dist: pytest>=7.0.0; extra == 'dev'
35
+ Provides-Extra: google
36
+ Requires-Dist: google-generativeai>=0.3.0; extra == 'google'
37
+ Provides-Extra: langchain
38
+ Requires-Dist: langchain-core>=0.1.0; extra == 'langchain'
39
+ Provides-Extra: langgraph
40
+ Requires-Dist: langgraph>=0.0.50; extra == 'langgraph'
41
+ Provides-Extra: mongo
42
+ Requires-Dist: motor>=3.0.0; extra == 'mongo'
43
+ Provides-Extra: openai
44
+ Requires-Dist: openai>=1.0.0; extra == 'openai'
45
+ Provides-Extra: postgres
46
+ Requires-Dist: asyncpg>=0.29.0; extra == 'postgres'
47
+ Description-Content-Type: text/markdown
48
+
49
+ # LLMem
50
+
51
+ Smart memory management for LLM conversations - topic-aware compression that just works.
52
+
53
+ ## Features
54
+
55
+ - **LLM-agnostic** - Works with OpenAI, Gemini, Anthropic, local models, or any LLM
56
+ - **Topic-aware compression** - Intelligently compresses based on conversation topics, not just token count
57
+ - **Storage-agnostic** - Works with PostgreSQL, MongoDB, or in-memory
58
+ - **LangChain/LangGraph compatible** - Works seamlessly with popular frameworks
59
+ - **Zero-config start** - Works out of the box with smart defaults
60
+ - **Multi-user safe** - Thread isolation for millions of users via `thread_id`
61
+ - **Fast** - Target <100ms for context retrieval
62
+
63
+ ## Installation
64
+
65
+ ```bash
66
+ pip install llmem
67
+ ```
68
+
69
+ With optional dependencies:
70
+
71
+ ```bash
72
+ pip install llmem[postgres] # PostgreSQL storage
73
+ pip install llmem[mongo] # MongoDB storage
74
+ pip install llmem[all] # Everything
75
+ ```
76
+
77
+ ## Quick Start
78
+
79
+ ```python
80
+ from llmem import Memory
81
+
82
+ # Create memory (zero config)
83
+ memory = Memory()
84
+
85
+ # Add conversation turns
86
+ memory.add("How do I setup my VR headset?", role="user")
87
+ memory.add("To setup your VR headset, first...", role="assistant")
88
+ memory.add("What games do you recommend?", role="user")
89
+ memory.add("I recommend these games...", role="assistant")
90
+
91
+ # Get optimized context for next LLM call
92
+ context = memory.get_context()
93
+
94
+ # Check health
95
+ health = memory.check_health()
96
+ print(f"Status: {health.status.value}, Tokens: {health.token_count}")
97
+ ```
98
+
99
+ ## With Persistent Storage
100
+
101
+ ### PostgreSQL
102
+
103
+ ```python
104
+ import asyncpg
105
+ from llmem import Memory
106
+ from llmem.storage.postgres import PostgresStorage
107
+
108
+ pool = await asyncpg.create_pool("postgresql://user:pass@localhost/db")
109
+ storage = PostgresStorage(pool=pool)
110
+ memory = Memory(storage=storage)
111
+
112
+ # Thread ID for multi-user isolation
113
+ memory.add("Hello", role="user", thread_id="user-123")
114
+ context = memory.get_context(thread_id="user-123")
115
+ ```
116
+
117
+ ### MongoDB
118
+
119
+ ```python
120
+ from motor.motor_asyncio import AsyncIOMotorClient
121
+ from llmem import Memory
122
+ from llmem.storage.mongo import MongoStorage
123
+
124
+ client = AsyncIOMotorClient("mongodb://localhost:27017")
125
+ storage = MongoStorage(db=client.mydb)
126
+ memory = Memory(storage=storage)
127
+ ```
128
+
129
+ ## With Any LLM
130
+
131
+ LLMem is **LLM-agnostic** - it manages conversation memory, you bring your own model:
132
+
133
+ ```python
134
+ from llmem import Memory
135
+
136
+ memory = Memory()
137
+
138
+ # Add user message
139
+ memory.add(user_input, role="user")
140
+
141
+ # Get optimized context
142
+ context = memory.get_context()
143
+
144
+ # Use with ANY LLM - OpenAI, Gemini, Anthropic, local models, etc.
145
+ response = your_llm.generate(context)
146
+
147
+ # Track response
148
+ memory.add(response, role="assistant")
149
+ ```
150
+
151
+ ### OpenAI Example
152
+
153
+ ```python
154
+ from openai import OpenAI
155
+ from llmem import Memory
156
+
157
+ client = OpenAI()
158
+ memory = Memory()
159
+
160
+ memory.add(user_input, role="user")
161
+ context = memory.get_context()
162
+
163
+ response = client.chat.completions.create(
164
+ model="your-model",
165
+ messages=context
166
+ )
167
+ memory.add(response.choices[0].message.content, role="assistant")
168
+ ```
169
+
170
+ ### Google Gemini Example
171
+
172
+ ```python
173
+ import google.generativeai as genai
174
+ from llmem import Memory
175
+
176
+ genai.configure(api_key="your-key")
177
+ model = genai.GenerativeModel("your-model")
178
+ memory = Memory()
179
+
180
+ memory.add(user_input, role="user")
181
+ context = memory.get_context()
182
+ response = model.generate_content(str(context))
183
+ memory.add(response.text, role="assistant")
184
+ ```
185
+
186
+ ### Anthropic Claude Example
187
+
188
+ ```python
189
+ from anthropic import Anthropic
190
+ from llmem import Memory
191
+
192
+ client = Anthropic()
193
+ memory = Memory()
194
+
195
+ memory.add(user_input, role="user")
196
+ context = memory.get_context()
197
+
198
+ response = client.messages.create(
199
+ model="your-model",
200
+ messages=context
201
+ )
202
+ memory.add(response.content[0].text, role="assistant")
203
+ ```
204
+
205
+ ### With LangChain (Any Provider)
206
+
207
+ ```python
208
+ from langchain_core.messages import HumanMessage, AIMessage
209
+ from llmem import Memory
210
+
211
+ # Use any LangChain-supported LLM
212
+ # from langchain_openai import ChatOpenAI
213
+ # from langchain_google_genai import ChatGoogleGenerativeAI
214
+ # from langchain_anthropic import ChatAnthropic
215
+
216
+ llm = YourLangChainLLM()
217
+ memory = Memory()
218
+
219
+ memory.add(user_input, role="user")
220
+ context = memory.get_context()
221
+
222
+ # Convert to LangChain messages
223
+ messages = [HumanMessage(content=m["content"]) if m["role"] == "user"
224
+ else AIMessage(content=m["content"]) for m in context]
225
+
226
+ response = llm.invoke(messages)
227
+ memory.add(response.content, role="assistant")
228
+ ```
229
+
230
+ ## Health Monitoring
231
+
232
+ ```python
233
+ health = memory.check_health()
234
+ print(f"Status: {health.status.value}") # healthy, warning, critical
235
+ print(f"Token usage: {health.token_usage:.1%}")
236
+ print(f"Recommendation: {health.recommendation.value}")
237
+
238
+ stats = memory.get_stats()
239
+ print(f"Total turns: {stats['total_turns']}")
240
+ print(f"Total tokens: {stats['total_tokens']}")
241
+ ```
242
+
243
+ ## Callbacks
244
+
245
+ ```python
246
+ memory = Memory(
247
+ on_compress=lambda info: print(f"Compressed: {info}"),
248
+ on_health_change=lambda health: print(f"Health: {health.status.value}")
249
+ )
250
+ ```
251
+
252
+ ## Examples
253
+
254
+ See the [examples/](examples/) folder for complete working demos:
255
+
256
+ | Example | Description |
257
+ |---------|-------------|
258
+ | `01_basic_usage.py` | Core functionality - add, get, health, stats |
259
+ | `02_callbacks.py` | Compression and health callbacks |
260
+ | `03_multi_user.py` | Thread isolation for multi-user apps |
261
+ | `04_with_openai.py` | Integration with OpenAI GPT |
262
+ | `04_with_gemini.py` | Integration with Google Gemini |
263
+ | `05_langchain_integration.py` | LangChain with any LLM provider |
264
+ | `06_langgraph_integration.py` | LangGraph agents |
265
+ | `07_postgres_storage.py` | PostgreSQL persistent storage |
266
+ | `08_mongodb_storage.py` | MongoDB persistent storage |
267
+ | `09_e2e_agent_test.py` | End-to-end test with all backends |
268
+ | `10_custom_storage.py` | Build your own storage backend |
269
+
270
+ ### Running Examples
271
+
272
+ ```bash
273
+ # Clone and setup
274
+ git clone https://github.com/sharanharsoor/llmem.git
275
+ cd llmem
276
+ python -m venv venv && source venv/bin/activate
277
+ pip install -e ".[dev]"
278
+
279
+ # Create .env file with your credentials
280
+ echo "GOOGLE_API_KEY=your-key" > .env
281
+ echo "DATABASE_URL=postgresql://user:pass@localhost/db" >> .env
282
+ echo "MONGODB_URL=mongodb://localhost:27017" >> .env
283
+
284
+ # Run examples
285
+ python examples/01_basic_usage.py
286
+ python examples/04_with_gemini.py
287
+ ```
288
+
289
+ ## API Reference
290
+
291
+ ### Memory Class
292
+
293
+ | Method | Description |
294
+ |--------|-------------|
295
+ | `add(content, role, thread_id=None)` | Add a conversation turn |
296
+ | `get_context(thread_id=None)` | Get optimized context |
297
+ | `get_context_for(query, thread_id=None)` | Get context relevant to query |
298
+ | `check_health(thread_id=None)` | Get context health metrics |
299
+ | `get_stats(thread_id=None)` | Get statistics |
300
+ | `compress(thread_id=None)` | Force compression |
301
+ | `clear(thread_id=None)` | Clear memory |
302
+
303
+ ### Storage Backends
304
+
305
+ | Backend | Description |
306
+ |---------|-------------|
307
+ | `InMemoryStorage` | Default, no persistence |
308
+ | `PostgresStorage` | PostgreSQL with asyncpg |
309
+ | `MongoStorage` | MongoDB with motor |
310
+ | Custom | Implement `StorageBackend` for any database |
311
+
312
+ ## Custom Storage Backend
313
+
314
+ LLMem supports **any database**. Implement the `StorageBackend` interface:
315
+
316
+ ```python
317
+ from llmem.storage.base import StorageBackend
318
+ from llmem.types import Turn, Topic
319
+
320
+ class MyCustomStorage(StorageBackend):
321
+ """Your custom storage (Redis, SQLite, DynamoDB, etc.)"""
322
+
323
+ async def save_turn(self, turn: Turn, thread_id: str) -> None:
324
+ # Save turn to your database
325
+ pass
326
+
327
+ async def get_turns(self, thread_id: str, limit=None, offset=0) -> list:
328
+ # Retrieve turns from your database
329
+ pass
330
+
331
+ async def get_turn_count(self, thread_id: str) -> int:
332
+ # Return count of turns
333
+ pass
334
+
335
+ async def update_turn(self, turn: Turn, thread_id: str) -> None:
336
+ # Update existing turn
337
+ pass
338
+
339
+ async def delete_turns(self, turn_ids: list, thread_id: str) -> None:
340
+ # Delete specific turns
341
+ pass
342
+
343
+ async def clear(self, thread_id: str) -> None:
344
+ # Clear all turns for thread
345
+ pass
346
+
347
+ # Use your custom storage
348
+ storage = MyCustomStorage()
349
+ memory = Memory(storage=storage)
350
+ ```
351
+
352
+ See `examples/10_custom_storage.py` for complete Redis and SQLite reference implementations.
353
+
354
+ ## Configuration
355
+
356
+ ```python
357
+ memory = Memory(
358
+ max_tokens=128000, # Max context tokens
359
+ compression_threshold=0.7, # Compress at 70% usage
360
+ )
361
+ ```
362
+
363
+ ## License
364
+
365
+ MIT