memlib-ai 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,642 @@
1
+ Metadata-Version: 2.4
2
+ Name: memlib-ai
3
+ Version: 0.1.0
4
+ Summary: Lightweight persistent memory infrastructure for AI agents
5
+ Requires-Python: >=3.13
6
+ Description-Content-Type: text/markdown
7
+ Requires-Dist: langchain-core
8
+ Requires-Dist: neo4j==6.3.0
9
+
10
+ # memlib
11
+
12
+ `memlib` is a lightweight persistent-memory layer for AI agents and conversational applications.
13
+
14
+ It is designed to plug into existing LangChain or LangGraph applications without controlling the agent architecture itself.
15
+
16
+ A typical integration may look like:
17
+
18
+ ```text
19
+ User message
20
+ ↓
21
+ memlib.get_context(...)
22
+ ↓
23
+ LangChain / LangGraph agent
24
+ ↓
25
+ Assistant response
26
+ ↓
27
+ memlib.add(...)
28
+ ````
29
+
30
+ Memlib handles persistent user memory while your application remains responsible for agent execution, tools, routing, and response generation.
31
+
32
+ ---
33
+
34
+ ## Installation
35
+
36
+ ```bash
37
+ pip install memlib
38
+ ```
39
+
40
+ or:
41
+
42
+ ```bash
43
+ uv add memlib
44
+ ```
45
+
46
+ ---
47
+
48
+ # Basic Usage
49
+
50
+ `Memory` is the main interface.
51
+
52
+ A memory instance is associated with:
53
+
54
+ * a `user_id`;
55
+ * a `chat_id`;
56
+ * an LLM adapter;
57
+ * a persistent store;
58
+ * a vector store;
59
+ * optionally, a graph store.
60
+
61
+ Example:
62
+
63
+ ```python
64
+ from langchain_chroma import Chroma
65
+ from langchain_groq import ChatGroq
66
+ from langchain_huggingface import HuggingFaceEmbeddings
67
+
68
+ from memlib.llm import LLMClient
69
+ from memlib.memory import Memory
70
+ from memlib.store import MemoryStore
71
+ from memlib.vector_store import MemoryVectorStore
72
+
73
+
74
+ chat_model = ChatGroq(
75
+ model="openai/gpt-oss-120b",
76
+ temperature=0,
77
+ )
78
+
79
+ llm = LLMClient(chat_model)
80
+
81
+ embeddings = HuggingFaceEmbeddings(
82
+ model_name="sentence-transformers/all-MiniLM-L6-v2"
83
+ )
84
+
85
+ chroma = Chroma(
86
+ collection_name="agent_memories",
87
+ embedding_function=embeddings,
88
+ persist_directory="./memory_vectors",
89
+ )
90
+
91
+ vector_store = MemoryVectorStore(chroma)
92
+
93
+ store = MemoryStore("memory.db")
94
+
95
+ memory = Memory(
96
+ llm=llm,
97
+ chat_model=chat_model,
98
+ user_id="user-123",
99
+ chat_id="chat-456",
100
+ store=store,
101
+ vector_store=vector_store,
102
+ )
103
+ ```
104
+
105
+ ---
106
+
107
+ # Using Memory in an Agent Loop
108
+
109
+ The normal integration requires two operations:
110
+
111
+ ```text
112
+ Before generating the response:
113
+ memory.get_context(...)
114
+
115
+ After generating the response:
116
+ memory.add(...)
117
+ ```
118
+
119
+ ## Retrieve memory before the agent runs
120
+
121
+ ```python
122
+ user_message = "Which football club do I support?"
123
+
124
+ context = memory.get_context(
125
+ user_message,
126
+ limit=5,
127
+ )
128
+ ```
129
+
130
+ `context` can then be inserted into your model or agent prompt.
131
+
132
+ For example:
133
+
134
+ ```python
135
+ response = llm.complete(
136
+ system_prompt=(
137
+ "You are a helpful assistant. "
138
+ "Use persistent user memory when it is relevant."
139
+ ),
140
+ user_prompt=(
141
+ f"Persistent memory:\n"
142
+ f"{context or 'No relevant memories found.'}\n\n"
143
+ f"User message:\n"
144
+ f"{user_message}"
145
+ ),
146
+ )
147
+ ```
148
+
149
+ After the response has been generated:
150
+
151
+ ```python
152
+ memory.add(
153
+ user_message=user_message,
154
+ assistant_response=response,
155
+ )
156
+ ```
157
+
158
+ Memlib can then use the completed turn to maintain persistent memory for future interactions.
159
+
160
+ ---
161
+
162
+ # Integration with LangChain
163
+
164
+ Memlib does not replace LangChain.
165
+
166
+ Instead, it can sit alongside your existing chain or agent.
167
+
168
+ For example:
169
+
170
+ ```python
171
+ user_message = "What technology am I currently learning?"
172
+
173
+ memory_context = memory.get_context(
174
+ user_message,
175
+ limit=5,
176
+ )
177
+
178
+ result = chain.invoke(
179
+ {
180
+ "input": user_message,
181
+ "memory_context": memory_context,
182
+ }
183
+ )
184
+
185
+ assistant_response = result["output"]
186
+
187
+ memory.add(
188
+ user_message=user_message,
189
+ assistant_response=assistant_response,
190
+ )
191
+ ```
192
+
193
+ Your prompt can expose the memory context through a normal prompt variable:
194
+
195
+ ```text
196
+ Persistent user context:
197
+ {memory_context}
198
+
199
+ Current user message:
200
+ {input}
201
+ ```
202
+
203
+ This keeps memory retrieval separate from the agent's main reasoning and tool execution.
204
+
205
+ ---
206
+
207
+ # Integration with LangGraph
208
+
209
+ Memlib can also be used as a memory layer around a LangGraph workflow.
210
+
211
+ A common flow is:
212
+
213
+ ```text
214
+ START
215
+ ↓
216
+ Retrieve persistent memory
217
+ ↓
218
+ Agent / tool nodes
219
+ ↓
220
+ Generate final response
221
+ ↓
222
+ Persist completed turn
223
+ ↓
224
+ END
225
+ ```
226
+
227
+ For example:
228
+
229
+ ```python
230
+ from typing import TypedDict
231
+
232
+
233
+ class AgentState(TypedDict):
234
+ user_message: str
235
+ memory_context: str
236
+ response: str
237
+ ```
238
+
239
+ A memory retrieval node can run before the agent:
240
+
241
+ ```python
242
+ def retrieve_memory(state: AgentState) -> dict:
243
+ context = memory.get_context(
244
+ state["user_message"],
245
+ limit=5,
246
+ )
247
+
248
+ return {
249
+ "memory_context": context,
250
+ }
251
+ ```
252
+
253
+ Your agent node then receives both the current request and persistent context:
254
+
255
+ ```python
256
+ def agent_node(state: AgentState) -> dict:
257
+ prompt = f"""
258
+ Persistent user memory:
259
+ {state["memory_context"] or "No relevant memories found."}
260
+
261
+ User:
262
+ {state["user_message"]}
263
+ """
264
+
265
+ response = chat_model.invoke(prompt)
266
+
267
+ return {
268
+ "response": response.content,
269
+ }
270
+ ```
271
+
272
+ After the final response is generated, persist the completed turn:
273
+
274
+ ```python
275
+ def save_memory(state: AgentState) -> dict:
276
+ memory.add(
277
+ user_message=state["user_message"],
278
+ assistant_response=state["response"],
279
+ )
280
+
281
+ return {}
282
+ ```
283
+
284
+ The graph can then be structured conceptually as:
285
+
286
+ ```text
287
+ START
288
+ ↓
289
+ retrieve_memory
290
+ ↓
291
+ agent
292
+ ↓
293
+ save_memory
294
+ ↓
295
+ END
296
+ ```
297
+
298
+ For larger LangGraph applications, the same pattern can wrap more complex flows:
299
+
300
+ ```text
301
+ START
302
+ ↓
303
+ retrieve_memory
304
+ ↓
305
+ planner
306
+ ↓
307
+ tools
308
+ ↓
309
+ agent
310
+ ↓
311
+ final_response
312
+ ↓
313
+ save_memory
314
+ ↓
315
+ END
316
+ ```
317
+
318
+ Memlib remains independent of the graph topology.
319
+
320
+ ---
321
+
322
+ # Provider-Adaptive LLM Usage
323
+
324
+ Memlib is designed to work with LangChain-compatible chat models.
325
+
326
+ The application selects the provider and model.
327
+
328
+ For example, with Groq:
329
+
330
+ ```python
331
+ from langchain_groq import ChatGroq
332
+
333
+ chat_model = ChatGroq(
334
+ model="openai/gpt-oss-120b",
335
+ )
336
+ ```
337
+
338
+ With OpenAI:
339
+
340
+ ```python
341
+ from langchain_openai import ChatOpenAI
342
+
343
+ chat_model = ChatOpenAI(
344
+ model="gpt-5-mini",
345
+ )
346
+ ```
347
+
348
+ With Gemini:
349
+
350
+ ```python
351
+ from langchain_google_genai import ChatGoogleGenerativeAI
352
+
353
+ chat_model = ChatGoogleGenerativeAI(
354
+ model="gemini-2.5-flash",
355
+ )
356
+ ```
357
+
358
+ Then create the memlib adapter:
359
+
360
+ ```python
361
+ from memlib.llm import LLMClient
362
+
363
+ llm = LLMClient(chat_model)
364
+ ```
365
+
366
+ and pass both interfaces to `Memory`:
367
+
368
+ ```python
369
+ memory = Memory(
370
+ llm=llm,
371
+ chat_model=chat_model,
372
+ user_id="user-123",
373
+ chat_id="chat-456",
374
+ store=store,
375
+ vector_store=vector_store,
376
+ )
377
+ ```
378
+
379
+ This allows the surrounding agent application to choose its own LangChain-compatible provider.
380
+
381
+ ---
382
+
383
+ # Different Models for Different Agent Responsibilities
384
+
385
+ Your main agent model does not need to be the same model used for every part of your application.
386
+
387
+ For example, your LangGraph agent may use one provider while memlib is configured with another:
388
+
389
+ ```python
390
+ agent_model = ChatOpenAI(
391
+ model="gpt-5-mini",
392
+ )
393
+
394
+ memory_model = ChatGroq(
395
+ model="openai/gpt-oss-120b",
396
+ )
397
+
398
+ memory_llm = LLMClient(memory_model)
399
+
400
+ memory = Memory(
401
+ llm=memory_llm,
402
+ chat_model=memory_model,
403
+ user_id="user-123",
404
+ chat_id="chat-456",
405
+ store=store,
406
+ vector_store=vector_store,
407
+ )
408
+ ```
409
+
410
+ Your LangGraph nodes can continue using:
411
+
412
+ ```python
413
+ agent_model
414
+ ```
415
+
416
+ while memlib uses:
417
+
418
+ ```python
419
+ memory_model
420
+ ```
421
+
422
+ for memory-related operations.
423
+
424
+ This keeps the memory layer decoupled from the application's main agent model.
425
+
426
+ ---
427
+
428
+ # Knowledge Graph Support
429
+
430
+ Knowledge-graph retrieval can be enabled by supplying a graph store.
431
+
432
+ Example with Neo4j:
433
+
434
+ ```python
435
+ from memlib.graph_store import Neo4jGraphStore
436
+
437
+
438
+ graph_store = Neo4jGraphStore(
439
+ uri="neo4j+s://...",
440
+ username="neo4j",
441
+ password="your-password",
442
+ database="your-database",
443
+ )
444
+ ```
445
+
446
+ Then:
447
+
448
+ ```python
449
+ memory = Memory(
450
+ llm=llm,
451
+ chat_model=chat_model,
452
+ user_id="user-123",
453
+ chat_id="chat-456",
454
+ store=store,
455
+ vector_store=vector_store,
456
+ graph_store=graph_store,
457
+ )
458
+ ```
459
+
460
+ You do not need to change the normal application flow.
461
+
462
+ The same call remains:
463
+
464
+ ```python
465
+ context = memory.get_context(user_message)
466
+ ```
467
+
468
+ Memlib can decide whether normal semantic memory retrieval is sufficient or whether additional relationship-based context should be included.
469
+
470
+ For example, previously stored information such as:
471
+
472
+ ```text
473
+ User follows football.
474
+ User's favourite club is Manchester United.
475
+ ```
476
+
477
+ can also contribute relationship context such as:
478
+
479
+ ```text
480
+ User --FOLLOWS--> Football
481
+ User --FAVOURITE_CLUB--> Manchester United
482
+ ```
483
+
484
+ From the application developer's perspective, the interface remains:
485
+
486
+ ```python
487
+ memory.get_context(...)
488
+ ```
489
+
490
+ ---
491
+
492
+ # Multiple Users
493
+
494
+ Use a different `user_id` for each application user.
495
+
496
+ ```python
497
+ memory = Memory(
498
+ llm=llm,
499
+ chat_model=chat_model,
500
+ user_id=current_user.id,
501
+ chat_id=current_chat.id,
502
+ store=store,
503
+ vector_store=vector_store,
504
+ )
505
+ ```
506
+
507
+ Persistent memories are associated with the user rather than with a single conversation.
508
+
509
+ ---
510
+
511
+ # Multiple Conversations
512
+
513
+ Keep the same `user_id` and change the `chat_id` for new conversations:
514
+
515
+ ```python
516
+ memory = Memory(
517
+ llm=llm,
518
+ chat_model=chat_model,
519
+ user_id="user-123",
520
+ chat_id="chat-789",
521
+ store=store,
522
+ vector_store=vector_store,
523
+ )
524
+ ```
525
+
526
+ This allows conversation-specific context to remain separate while long-term user memory can persist across sessions.
527
+
528
+ ---
529
+
530
+ # Searching Memories Directly
531
+
532
+ Use:
533
+
534
+ ```python
535
+ memories = memory.search(
536
+ "What does the user enjoy?",
537
+ limit=5,
538
+ )
539
+ ```
540
+
541
+ The result contains relevant memory objects.
542
+
543
+ ```python
544
+ for item in memories:
545
+ print(item.id)
546
+ print(item.content)
547
+ ```
548
+
549
+ For most chatbot or agent integrations, however, `get_context()` is usually more convenient because it returns prompt-ready context.
550
+
551
+ ---
552
+
553
+ # Deleting Memory
554
+
555
+ Delete a specific memory by ID:
556
+
557
+ ```python
558
+ memory.delete(memory_id)
559
+ ```
560
+
561
+ ---
562
+
563
+ # Clearing User Memory
564
+
565
+ Clear the long-term memories associated with the current `Memory` instance:
566
+
567
+ ```python
568
+ memory.clear()
569
+ ```
570
+
571
+ ---
572
+
573
+ # Main API
574
+
575
+ The primary application-facing methods are:
576
+
577
+ ```python
578
+ memory.get_context(
579
+ query,
580
+ limit=5,
581
+ )
582
+ ```
583
+
584
+ Retrieve prompt-ready persistent context.
585
+
586
+ ```python
587
+ memory.search(
588
+ query,
589
+ limit=5,
590
+ )
591
+ ```
592
+
593
+ Retrieve matching memory objects.
594
+
595
+ ```python
596
+ memory.add(
597
+ user_message,
598
+ assistant_response,
599
+ )
600
+ ```
601
+
602
+ Process a completed interaction and maintain persistent memory.
603
+
604
+ ```python
605
+ memory.delete(
606
+ memory_id,
607
+ )
608
+ ```
609
+
610
+ Delete an individual memory.
611
+
612
+ ```python
613
+ memory.clear()
614
+ ```
615
+
616
+ Clear long-term memory for the current user.
617
+
618
+ ---
619
+
620
+ # Recommended Agent Pattern
621
+
622
+ For most LangChain or LangGraph applications:
623
+
624
+ 1. Receive user message
625
+
626
+ 2. Retrieve memory
627
+ `memory.get_context(user_message)`
628
+
629
+ 3. Pass memory context into the agent state or prompt
630
+
631
+ 4. Run the normal agent / tool workflow
632
+
633
+ 5. Produce the final assistant response
634
+
635
+ 6. Persist the completed turn
636
+ `memory.add(user_message, assistant_response)`
637
+
638
+
639
+ This lets the agent architecture remain independent while memlib provides a persistent memory layer around it.
640
+
641
+ Source Code - https://github.com/SitanshuA091/memlib-ai
642
+