Compare commits

..

118 Commits

Author SHA1 Message Date
Mragank Shekhar a140829395 chore: update user facing timestamp for a memory (#4066) 2026-02-18 03:27:46 +05:30
Zlo7 a6810819ca OpenClaw plugin: fix auto-recall injection and auto-capture message drop (#4065) 2026-02-17 13:26:45 -08:00
Saket Aryan a02205e519 chore: remove legacy v0.x docs and version dropdown (#4060) 2026-02-16 13:25:05 -08:00
Saket Aryan 69a832dc58 chore: add update project options (#3947) 2026-02-03 10:55:43 +05:30
Deshraj Yadav 70baa46cb1 fix: add OpenClaw to docs navigation (#3965) 2026-02-02 10:17:19 -08:00
Deshraj Yadav 3d3e875d21 Feature: Add OpenClaw plugin and documentation (#3964) 2026-02-02 10:04:08 -08:00
Saket Aryan dba7f0458a (version-bump): update the project version to v1.0.2 (#3902) 2026-01-13 13:01:38 +05:30
Saket Aryan 27e5db5831 (fix): mongodb distribution name, azure ai search, and workflow trigger (#3900) 2026-01-13 12:52:49 +05:30
Saket Aryan 2c90eedfff chore: do a disk cleanup in gh actions to fix memo build (#3899) 2026-01-13 11:42:54 +05:30
Noah Stapp a1db0f6362 Add DriverInfo metadata to MongoDB vector store (#3648) 2026-01-12 21:07:42 -08:00
Saket Aryan 90a7b1afa0 feat(ts-sdk): add support for keyword arguments in add and search methods (#3895) 2026-01-10 21:19:55 +05:30
Saket Aryan 69a552d8a8 fix(docs): Improve light mode support for introduction page and organize thumbnails (#3880) 2026-01-03 21:21:24 +05:30
Saket Aryan 417ebffadd (ts-sdk-update): Update for TypeScript SDK v2.2. (#3865) 2025-12-29 15:01:18 +05:30
Saket Aryan 1dc07d3550 (docs): update to use the v2 URL Patterns in delete user route (#3864) 2025-12-29 14:51:42 +05:30
Saket Aryan 65e22e34d9 chore: remove unnecessary dependencies from Vercel AI SDK to reduce package size (#3856) 2025-12-26 22:24:40 +05:30
Parth Sharma e08f44c5f2 [docs] link to fix api key redirect (#3843) 2025-12-18 00:20:29 +05:30
Parth Sharma 16d989bbcd [docs] Series of docs for mem0-mcp (#3831) 2025-12-15 22:03:23 +05:30
Swarnaprakash Udayakumar 0f8654bd40 Add Strands agent (with AWS ElastiCache and Neptune) example mention in Joint blog post by Mem0 and AWS (#3824) 2025-12-13 14:00:29 +05:30
Parth Sharma 654089fcfc [docs] Filters fix in docs (#3815) 2025-12-11 18:38:08 +05:30
Parth Sharma 222c6ceea1 (docs-fix): fix broken redirect in python and node quickstart (#3826) 2025-12-11 15:50:20 +05:30
Parth Sharma 84bd6e3b97 fix(docs): Correct API authentication header from Bearer to Token (#3820) 2025-12-11 00:32:59 +05:30
Parth Sharma 5676bebd5f docs: migration guide v1 (#3822) 2025-12-10 23:45:19 +05:30
Parth Sharma f14132db44 [docs] Gemini-3 demo with mem0-mcp (#3810) 2025-12-09 23:43:49 +05:30
Parth Sharma 903c3635cc [docs] Add memory and v2 docs fixup (#3792) 2025-11-27 23:41:51 +05:30
Parth Sharma cc2894aaec [docs] Docs redirect to platform (#3769) 2025-11-22 10:17:58 +05:30
Deshraj Yadav 97cbff77ef Add events API docs and spec updates (#3752) 2025-11-14 20:53:31 -08:00
Parth Sharma e29220efda [docs] new redirect for entity doc (#3750) 2025-11-14 08:51:25 -08:00
Prateek Chhikara 3b84a234e1 Updates to python sdk (#3749) 2025-11-13 14:22:39 -08:00
Parth Sharma 2bca30ebe6 [docs] Minor Docs fixes ( enhancements , restructure ) (#3748) 2025-11-13 11:58:36 -08:00
Parth Sharma 3297ec1a46 [doc] Partition Memories by Entity , features doc and cookbook (#3735) 2025-11-13 10:26:41 -08:00
Parth Sharma 61e2a40d55 [docs] python quickstart fix (#3742) 2025-11-13 10:18:55 -08:00
Parth Sharma 9f921e27cb [docs] add callouts and comparision to clear the problem of when to use Infer=True/False (#3738) 2025-11-10 14:23:49 -08:00
Parth Sharma 568e97d013 [docs] graph memory docs fix (#3728) 2025-11-07 09:41:35 -08:00
Parth Sharma ac5660e26d [docs] LLM.txt + Context menu to make our Docs LLM friendly (#3726) 2025-11-07 09:38:12 -08:00
Parth Sharma 76abd5117d [docs] API References and search Doc fix (#3712) 2025-11-04 13:53:37 -08:00
Prateek Chhikara 978babd3db Docs Update (#3706) 2025-11-03 11:03:33 -08:00
Parth Sharma 2b0a457198 [docs] Custom categories Documentation fix (#3702) 2025-11-03 10:11:04 -08:00
Parth Sharma 6a7277070f [docs] Add Redirects to the new docs to fix broken links (#3701) 2025-11-01 06:19:47 -07:00
Parth Sharma 4c53930e47 [docs] complete redirects (#3700) 2025-10-31 20:26:30 -07:00
Parth Sharma 84687fc3d2 [fix] list' object has no attribute 'id' - Id fault with chroma pinecone and other providers (#3693) 2025-10-31 16:46:10 +05:30
Parth Sharma 3ca939e210 [docs] redirect with fixed ci fails - langchain (#3699) 2025-10-31 16:45:15 +05:30
Parth Sharma 5f5e64b44b [docs] tab icon cleanup (#3679) 2025-10-28 09:42:19 +01:00
Parth Sharma 5cb6b31690 [docs] Cookbook name cleanup (#3678) 2025-10-28 09:20:23 +01:00
Parth Sharma ee8955d08b [docs] OSS Features , Overview , Brushup (#3676) 2025-10-27 12:31:40 -07:00
Parth Sharma 80c9139c5b [docs] zoom effect fix (#3670) 2025-10-27 08:56:32 +05:30
Parth Sharma 7be32641c7 [docs] Essential cookbook added and overall revamp to the structure of cookbooks (#3668) 2025-10-27 00:19:29 +05:30
Parth Sharma 2c18355dd2 [docs] platform core concept revamp and overview update (#3664) 2025-10-26 15:15:13 +05:30
Parth Sharma 61faf71064 [docs] Template moulding in docs/platform and index improvement (#3663) 2025-10-25 14:05:18 -07:00
Parth Sharma ac9598a67f [docs] Added Templates and Contribution Guidelines (#3662) 2025-10-25 12:58:40 -07:00
Parth Sharma f98a17c716 [docs] Welcome page thumbnail and reranker fix (#3660) 2025-10-25 12:20:52 -07:00
Vedant Thakkar 639d26e1ac feat(api): add vector store configuration endpoints (#3583) 2025-10-23 18:36:25 +05:30
Parth Sharma f7d7c53001 Added improved docs index and overview pages and quickstart (#3603) 2025-10-22 10:30:45 -07:00
Frederik Berg ec1a60bf8d Add delete_memories MCP tool for targeted deletion (#3616) 2025-10-22 10:08:28 -07:00
Frederik Berg 77c71a134a Fix REST API infer parameter ignored (#3607) 2025-10-22 10:08:15 -07:00
Frederik Berg 2692e49d50 Fix: Add missing filter methods to AsyncMemory (#3624)
Co-authored-by: Claude <noreply@anthropic.com>
2025-10-22 16:08:04 +05:30
Ronak Bhalgami eb2f8a3738 fix: Prevent Mock object issues in graph memory tests (#3627)
Co-authored-by: parshvadaftari <daftariparshva@gmail.com>
2025-10-22 03:53:12 +05:30
Prateek Chhikara 4a30745592 Add redirect to new apis doc page (#3639) 2025-10-21 13:55:14 -07:00
Rahul Sharma 3b1a4c2e68 Fix condition check for memories_result type in AsyncMemory class (#3621) 2025-10-22 01:55:28 +05:30
Mrinank Bhowmick 5227b0a062 Fix embedder config schema to support embeddingDims and url parameters (#3633) 2025-10-21 09:30:44 -07:00
Prateek Chhikara 8031f0bf8f Changes to docs (#3637) 2025-10-20 16:13:34 -07:00
Prateek Chhikara dd3e5363dd Update docs (#3636) 2025-10-20 16:12:11 -07:00
Frederik Berg d5a130b785 Fix memory deletion not removing from vector store (#3610) 2025-10-18 14:17:03 -07:00
Frederik Berg 8ede1df10a Fix list_memories endpoint Pydantic validation error (#3608) 2025-10-18 14:16:49 -07:00
Parshva Daftari 8ba18bf8bc [fix] docs for search memories (#3622) 2025-10-18 13:27:15 -07:00
Ronak Bhalgami de224dd26d feat: Add configurable embedding similarity threshold for graph store node matching (#3593) 2025-10-18 13:03:30 -07:00
Faizan Habib 9ef644b95e Add Apache Cassandra vector store support (#3578) 2025-10-17 23:48:49 +05:30
Aashis kumar 7afbaae7a3 Fix condition check for memories_result type in Memory class (#3596) 2025-10-17 02:17:18 +05:30
Tarun Jain 1090784302 [feat add]FastEmbed embedding for local embeddings (#3552) 2025-10-16 22:52:22 +05:30
Parshva Daftari 394203d1b5 Mem0 1.0.0 (#3545) 2025-10-16 15:50:20 +05:30
Kabir Kohli 8f5151c344 asycn mode default change (#3585) 2025-10-16 04:58:53 +05:30
Parshva Daftari 41cfb3ab1a [Update] Default LLM (#3587) 2025-10-15 11:19:52 -07:00
G Karthik Koundinya a40314c971 feat: Add Azure AI Search vector store support for TypeScript SDK (#3549) 2025-10-15 11:19:10 -07:00
Vedant Thakkar ea22e8d9cd feat: Allow custom model and params with huggingface_base_url (#3574) 2025-10-14 17:33:18 +05:30
Alex Kondratev ce8a285003 Validate embedding_dims in kuzu, fix #3556 (#3558) 2025-10-11 03:55:03 +05:30
Parshva Daftari 4559623501 [fix] milvus db bug and added tests (#3566) 2025-10-11 02:21:35 +05:30
Deshraj Yadav 37c86aa3c0 Update main script and remove stale code (#3561) 2025-10-09 15:30:20 -07:00
Mrinank Bhowmick 335a7d7862 Fix TypeScript build error (#3535) 2025-10-09 11:09:31 -07:00
Mrinank Bhowmick 64571002f5 fixed hardcoded embeddingDims (#3537) 2025-10-09 11:09:20 -07:00
Vishaal LS 9000576173 fix: handle non-serializable objects in config deepcopy (#3464) (#3544) 2025-10-09 19:35:08 +05:30
Josh Hayes 922471f43b fix: Databricks Vector Store (#3546) 2025-10-09 19:19:07 +05:30
Saket Aryan b93ce5548b (docs): add v2 filter documentations (#3469) 2025-10-07 14:12:58 +05:30
Alex Kondratev ee0202764b Tool call support for LangchainLLM (#3542) 2025-10-06 00:03:44 +05:30
Saket Aryan 8ba032e029 (fix): added version=v2 as default param in ai sdk add calls (#3540) 2025-10-05 02:16:02 +05:30
yashikabadaya fbf3bd640c support dependency openai 2.x (#3533) 2025-10-03 21:50:49 +05:30
dog-last 51ce6f1347 Bug fix of thinking llm in vllm (#3510) 2025-10-03 19:13:08 +05:30
Parshva Daftari 346d89d244 Added azure mysql for mem0 (#3531) 2025-10-02 21:41:03 +05:30
Vishaal LS 1104b52d99 docs: add detailed explanation for output_format v1.1 parameter (#3517) 2025-10-01 14:19:34 -07:00
Parshva Daftari e19b748ad0 Refactor docs and fix get memories playground (#3527) 2025-10-01 10:40:27 -07:00
Matan Cohen 517a266d74 Fix bug in weaviate search method (#3521) 2025-10-01 14:24:03 +05:30
Frederik Berg cbf56477be Fix: Serialize response to JSON in add_memories MCP tool (#3523) 2025-09-30 15:28:50 -07:00
Vishaal LS 58cc44ff38 fix: handle missing 'data' key in memory payload during search operations (#3524) 2025-09-30 15:10:43 -07:00
Vishaal LS 445286a138 fix: update license information in README and pyproject.toml (#3522) 2025-09-30 13:58:08 -07:00
Deshraj Yadav d68ed11d58 Update Docs (#3520) 2025-09-30 08:41:36 -07:00
Parshva Daftari 135883935f refactor: v2 search and update examples (#3508) 2025-09-26 22:55:53 +05:30
Parshva Daftari ed5a1e9fc6 Update version to 0.1.118 (#3505) 2025-09-26 02:03:37 +05:30
Karthikeya Kollu dc883b0f9e [test] Add comprehensive test suite for SQLiteManager (#3494) 2025-09-25 22:30:03 +05:30
Saket Aryan 5616844b9c docs-fix: Quickstart cURL example fixed (#3503) 2025-09-25 20:44:39 +05:30
Saket Aryan 6e1d02c137 feat(ai-sdk): added file support for multimodal capabilities with memory context (#3500) 2025-09-25 10:32:06 +05:30
Parshva Daftari a199ee4ff8 Refactored example title for aws (#3492) 2025-09-22 18:28:28 +05:30
Parshva Daftari 88ae952483 [DOCS] Changing 1.0 to 1.0.0 (#3486) 2025-09-20 20:07:23 +05:30
Abdullah Irfan 9df392b26a Fixed s3 vectors memory initialization issue from configuration (#3481) 2025-09-20 12:43:18 +05:30
Parshva Daftari ead210ffe4 Added weaviate db test (#3483) 2025-09-18 14:40:44 -07:00
Andrew Carbonetto a015e2ff4a Add Neptune-DB graph store with vector store (#3443)
Signed-off-by: Andrew Carbonetto <andrew.carbonetto@improving.com>
Co-authored-by: Siddhartha Sahu <dev@sdht.in>
2025-09-19 02:54:33 +05:30
Brinlee Kidd d4e98dba38 feat: implement structured exception classes with error codes and sug… (#3279) 2025-09-19 02:31:35 +05:30
Parshva Daftari ac72eb5ecc Aspen theme for 1.x (#3473) 2025-09-18 21:03:17 +05:30
Andy Kwok 6b5582f474 Feat: Mem0 vector store backend integration for Neptune Analytics (#3453)
Signed-off-by: Andy Kwok <andy.kwok@improving.com>
2025-09-17 19:26:03 +05:30
Parshva Daftari d38e3f1962 Fix json parsing with new memories (#3456) 2025-09-12 17:29:34 +05:30
◢ 徇 ◤ a0685f3e8c fix: correct typo in knowledge graph extraction guidelines (#3449) 2025-09-12 15:07:37 +05:30
Parshva Daftari d48b1832c7 Fixes ollama and updates openai dependency (#3452) 2025-09-12 01:39:39 +05:30
Saket Aryan 21d69307dc docs: Update Search V2/Get All V2 Filters (#3450) 2025-09-11 19:10:19 +05:30
Andrew Carbonetto 9e5810dfb7 Fix bedrock anthropic models to use system field (#3438)
Signed-off-by: Andrew Carbonetto <andrew.carbonetto@improving.com>
2025-09-11 02:55:45 +05:30
Swarnaprakash Udayakumar e3f0277cb9 feat(vector-store): Add Valkey vector store support (#3272) 2025-09-10 04:01:53 +05:30
Prateek Chhikara e64488b598 updates to the category docs (#3437) 2025-09-09 11:47:09 -07:00
Parshva Daftari f5e0fb9e4b Added support for chromadb cloud (#3436) 2025-09-09 22:22:09 +05:30
Ranjith kumar 77b4b6a2b9 fix: 🐛 replace hardcoded llm provider with provider from config (#3423) 2025-09-05 22:38:04 +05:30
Josh Hayes 9477184582 databricks bug fixes (#3416) 2025-09-05 16:22:09 +05:30
Gabe Goodhart b27879bfd4 fix: Use ConfigDict instead of class-based Config (#3409)
Signed-off-by: Gabe Goodhart <ghart@us.ibm.com>
2025-09-04 20:18:25 +05:30
Saket Aryan f0e8c3f760 feat: Add metadata param to TS-SDK in client.update (#3415) 2025-09-04 03:22:12 +05:30
392 changed files with 36420 additions and 10873 deletions
+11
View File
@@ -7,6 +7,8 @@ on:
- 'mem0/**'
- 'tests/**'
- 'embedchain/**'
- '.github/workflows/**'
- 'pyproject.toml'
pull_request:
paths:
- 'mem0/**'
@@ -28,6 +30,8 @@ jobs:
mem0:
- 'mem0/**'
- 'tests/**'
- '.github/workflows/**'
- 'pyproject.toml'
embedchain:
- 'embedchain/**'
@@ -44,6 +48,13 @@ jobs:
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Clean up disk space
run: |
df -h
sudo rm -rf /usr/share/dotnet /usr/local/lib/android /opt/ghc /opt/hostedtoolcache/CodeQL
sudo docker image prune --all --force
sudo docker builder prune -a
df -h
- name: Install Hatch
run: pip install hatch
- name: Load cached venv
+3 -3
View File
@@ -103,7 +103,7 @@ memory = Memory()
# With custom configuration
config = MemoryConfig(
vector_store={"provider": "qdrant", "config": {"host": "localhost"}},
llm={"provider": "openai", "config": {"model": "gpt-4o-mini"}},
llm={"provider": "openai", "config": {"model": "gpt-4.1-nano-2025-04-14"}},
embedder={"provider": "openai", "config": {"model": "text-embedding-3-small"}}
)
memory = Memory(config)
@@ -339,7 +339,7 @@ config = MemoryConfig(
llm={
"provider": "openai",
"config": {
"model": "gpt-4o-mini",
"model": "gpt-4.1-nano-2025-04-14",
"temperature": 0.1,
"max_tokens": 1000
}
@@ -527,7 +527,7 @@ const memory = new Memory({
},
llm: {
provider: 'openai',
config: { model: 'gpt-4o-mini' }
config: { model: 'gpt-4.1-nano' }
}
});
+221
View File
@@ -0,0 +1,221 @@
# Migration Guide: Upgrading to mem0 1.0.0
## TL;DR
**What changed?** We simplified the API by removing confusing version parameters. Now everything returns a consistent format: `{"results": [...]}`.
**What you need to do:**
1. Upgrade: `pip install mem0ai==1.0.0`
2. Remove `version` and `output_format` parameters from your code
3. Update response handling to use `result["results"]` instead of treating responses as lists
**Time needed:** ~5-10 minutes for most projects
---
## Quick Migration Guide
### 1. Install the Update
```bash
pip install mem0ai==1.0.0
```
### 2. Update Your Code
**If you're using the Memory API:**
```python
# Before
memory = Memory(config=MemoryConfig(version="v1.1"))
result = memory.add("I like pizza")
# After
memory = Memory() # That's it - version is automatic now
result = memory.add("I like pizza")
```
**If you're using the Client API:**
```python
# Before
client.add(messages, output_format="v1.1")
client.search(query, version="v2", output_format="v1.1")
# After
client.add(messages) # Just remove those extra parameters
client.search(query)
```
### 3. Update How You Handle Responses
All responses now use the same format: a dictionary with `"results"` key.
```python
# Before - you might have done this
result = memory.add("I like pizza")
for item in result: # Treating it as a list
print(item)
# After - do this instead
result = memory.add("I like pizza")
for item in result["results"]: # Access the results key
print(item)
# Graph relations (if you use them)
if "relations" in result:
for relation in result["relations"]:
print(relation)
```
---
## Enhanced Message Handling
The platform client (MemoryClient) now supports the same flexible message formats as the OSS version:
```python
from mem0 import MemoryClient
client = MemoryClient(api_key="your-key")
# All three formats now work:
# 1. Single string (automatically converted to user message)
client.add("I like pizza", user_id="alice")
# 2. Single message dictionary
client.add({"role": "user", "content": "I like pizza"}, user_id="alice")
# 3. List of messages (conversation)
client.add([
{"role": "user", "content": "I like pizza"},
{"role": "assistant", "content": "I'll remember that!"}
], user_id="alice")
```
### Async Mode Configuration
The `async_mode` parameter now defaults to `True` but can be configured:
```python
# Default behavior (async_mode=True)
client.add(messages, user_id="alice")
# Explicitly set async mode
client.add(messages, user_id="alice", async_mode=True)
# Disable async mode if needed
client.add(messages, user_id="alice", async_mode=False)
```
**Note:** `async_mode=True` provides better performance for most use cases. Only set it to `False` if you have specific synchronous processing requirements.
---
## That's It!
For most users, that's all you need to know. The changes are:
- ✅ No more `version` or `output_format` parameters
- ✅ Consistent `{"results": [...]}` response format
- ✅ Cleaner, simpler API
---
## Common Issues
**Getting `KeyError: 'results'`?**
Your code is still treating the response as a list. Update it:
```python
# Change this:
for memory in response:
# To this:
for memory in response["results"]:
```
**Getting `TypeError: unexpected keyword argument`?**
You're still passing old parameters. Remove them:
```python
# Change this:
client.add(messages, output_format="v1.1")
# To this:
client.add(messages)
```
**Seeing deprecation warnings?**
Remove any explicit `version="v1.0"` from your config:
```python
# Change this:
memory = Memory(config=MemoryConfig(version="v1.0"))
# To this:
memory = Memory()
```
---
## What's New in 1.0.0
- **Better vector stores:** Fixed OpenSearch and improved reliability across all stores
- **Cleaner API:** One way to do things, no more confusing options
- **Enhanced GCP support:** Better Vertex AI configuration options
- **Flexible message input:** Platform client now accepts strings, dicts, and lists (aligned with OSS)
- **Configurable async_mode:** Now defaults to `True` but users can override if needed
---
## Need Help?
- Check [GitHub Issues](https://github.com/mem0ai/mem0/issues)
- Read the [documentation](https://docs.mem0.ai/)
- Open a new issue if you're stuck
---
## Advanced: Configuration Changes
**If you configured vector stores with version:**
```python
# Before
config = MemoryConfig(
version="v1.1",
vector_store=VectorStoreConfig(...)
)
# After
config = MemoryConfig(
vector_store=VectorStoreConfig(...)
)
```
---
## Testing Your Migration
Quick sanity check:
```python
from mem0 import Memory
memory = Memory()
# Add should return a dict with "results"
result = memory.add("I like pizza", user_id="test")
assert "results" in result
# Search should return a dict with "results"
search = memory.search("food", user_id="test")
assert "results" in search
# Get all should return a dict with "results"
all_memories = memory.get_all(user_id="test")
assert "results" in all_memories
print("✅ Migration successful!")
```
+1 -1
View File
@@ -13,7 +13,7 @@ install:
install_all:
pip install ruff==0.6.9 groq together boto3 litellm ollama chromadb weaviate weaviate-client sentence_transformers vertexai \
google-generativeai elasticsearch opensearch-py vecs "pinecone<7.0.0" pinecone-text faiss-cpu langchain-community \
upstash-vector azure-search-documents langchain-memgraph langchain-neo4j langchain-aws rank-bm25 pymochow pymongo psycopg kuzu databricks-sdk
upstash-vector azure-search-documents langchain-memgraph langchain-neo4j langchain-aws rank-bm25 pymochow pymongo psycopg kuzu databricks-sdk valkey
# Format code with ruff
format:
+5 -3
View File
@@ -47,6 +47,8 @@
<strong>⚡ +26% Accuracy vs. OpenAI Memory • 🚀 91% Faster • 💰 90% Fewer Tokens</strong>
</p>
> **🎉 mem0ai v1.0.0 is now available!** This major release includes API modernization, improved vector store support, and enhanced GCP integration. [See migration guide →](MIGRATION_GUIDE_v1.0.md)
## 🔥 Research Highlights
- **+26% Accuracy** over OpenAI Memory on the LOCOMO benchmark
- **91% Faster Responses** than full-context, ensuring low-latency at scale
@@ -95,7 +97,7 @@ npm install mem0ai
### Basic Usage
Mem0 requires an LLM to function, with `gpt-4o-mini` from OpenAI as the default. However, it supports a variety of LLMs; for details, refer to our [Supported LLMs documentation](https://docs.mem0.ai/components/llms/overview).
Mem0 requires an LLM to function, with `gpt-4.1-nano-2025-04-14 from OpenAI as the default. However, it supports a variety of LLMs; for details, refer to our [Supported LLMs documentation](https://docs.mem0.ai/components/llms/overview).
First step is to instantiate the memory:
@@ -114,7 +116,7 @@ def chat_with_memories(message: str, user_id: str = "default_user") -> str:
# Generate Assistant response
system_prompt = f"You are a helpful AI. Answer the question based on query and memories.\nUser Memories:\n{memories_str}"
messages = [{"role": "system", "content": system_prompt}, {"role": "user", "content": message}]
response = openai_client.chat.completions.create(model="gpt-4o-mini", messages=messages)
response = openai_client.chat.completions.create(model="gpt-4.1-nano-2025-04-14", messages=messages)
assistant_response = response.choices[0].message.content
# Create new memories from the conversation
@@ -166,4 +168,4 @@ We now have a paper you can cite:
## ⚖️ License
Apache 2.0 — see the [LICENSE](LICENSE) file for details.
Apache 2.0 — see the [LICENSE](https://github.com/mem0ai/mem0/blob/main/LICENSE) file for details.
+1 -1
View File
@@ -1,3 +1,3 @@
<Note type="info">
📢 Announcing our research paper: Mem0 achieves <strong>26%</strong> higher accuracy than OpenAI Memory, <strong>91%</strong> lower latency, and <strong>90%</strong> token savings! [Read the paper](https://mem0.ai/research) to learn how we're revolutionizing AI agent memory.
<strong>🎉 Mem0 1.0.0 is here!</strong> Enhanced filtering, reranking, and smarter memory management.
</Note>
+88 -171
View File
@@ -1,191 +1,108 @@
---
title: Overview
icon: "info"
title: "Overview"
icon: "terminal"
iconType: "solid"
description: "REST APIs for memory management, search, and entity operations"
---
Mem0 provides a powerful set of APIs that allow you to integrate advanced memory management capabilities into your applications. Our APIs are designed to be intuitive, efficient, and scalable, enabling you to create, retrieve, update, and delete memories across various entities such as users, agents, apps, and runs.
## Mem0 REST API
## Key Features
Mem0 provides a comprehensive REST API for integrating advanced memory capabilities into your applications. Create, search, update, and manage memories across users, agents, and custom entities with simple HTTP requests.
- **Memory Management**: Add, retrieve, update, and delete memories with ease.
- **Entity-based Operations**: Perform operations on memories associated with specific users, agents, apps, or runs.
- **Advanced Search**: Utilize our search API to find relevant memories based on various criteria.
- **History Tracking**: Access the history of memory interactions for comprehensive analysis.
- **User Management**: Manage user entities and their associated memories.
<Info>
**Quick start:** Get your API key from the [Mem0 Dashboard](https://app.mem0.ai/dashboard/api-keys) and make your first memory operation in minutes.
</Info>
## API Structure
---
Our API is organized into several main categories:
## Quick Start Guide
1. **Memory APIs**: Core operations for managing individual memories and collections.
2. **Entities APIs**: Manage different entity types (users, agents, etc.) and their associated memories.
3. **Search API**: Advanced search functionality to retrieve relevant memories.
4. **History API**: Track and retrieve the history of memory interactions.
Get started with Mem0 API in three simple steps:
1. **[Add Memories](/api-reference/memory/add-memories)** - Store information and context from user conversations
2. **[Search Memories](/api-reference/memory/v2-search-memories)** - Retrieve relevant memories using semantic search
3. **[Get Memories](/api-reference/memory/v2-get-memories)** - Fetch all memories for a specific entity
---
## Core Operations
<CardGroup cols={2}>
<Card title="Add Memories" icon="plus" href="/api-reference/memory/add-memories">
Store new memories from conversations and interactions
</Card>
<Card title="Search Memories" icon="magnifying-glass" href="/api-reference/memory/v2-search-memories">
Find relevant memories using semantic search with filters
</Card>
<Card title="Update Memory" icon="pen" href="/api-reference/memory/update-memory">
Modify existing memory content and metadata
</Card>
<Card title="Delete Memory" icon="trash" href="/api-reference/memory/delete-memory">
Remove specific memories or batch delete operations
</Card>
</CardGroup>
---
## API Categories
Explore the full API organized by functionality:
<CardGroup cols={2}>
<Card title="Memory APIs" icon="microchip" href="/api-reference/memory/add-memories">
Core and advanced operations: CRUD, search, batch updates, history, and exports
</Card>
<Card title="Events APIs" icon="clock" href="/api-reference/events/get-events">
Track and monitor the status of asynchronous memory operations
</Card>
<Card title="Entities APIs" icon="users" href="/api-reference/entities/get-users">
Manage users, agents, and their associated memory data
</Card>
<Card title="Organizations & Projects" icon="building" href="/api-reference/organizations-projects">
Multi-tenant support, access control, and team collaboration
</Card>
<Card title="Webhooks" icon="webhook" href="/api-reference/webhook/create-webhook">
Real-time notifications for memory events and updates
</Card>
</CardGroup>
<Note>
**Building multi-tenant apps?** Learn about [Organizations & Projects](/api-reference/organizations-projects) for team isolation and access control.
</Note>
---
## Authentication
All API requests require authentication using HTTP Basic Auth. Ensure you include your API key in the Authorization header of each request.
All API requests require authentication using Token-based authentication. Include your API key in the Authorization header:
## Organizations and projects (optional)
Organizations and projects provide the following capabilities:
- **Multi-org/project Support**: Specify organization and project when initializing the Mem0 client to attribute API usage appropriately
- **Member Management**: Control access to data through organization and project membership
- **Access Control**: Only members can access memories and data within their organization/project scope
- **Team Isolation**: Maintain data separation between different teams and projects for secure collaboration
Example with the mem0 Python package:
<Tabs>
<Tab title="Python">
```python
from mem0 import MemoryClient
client = MemoryClient(org_id='YOUR_ORG_ID', project_id='YOUR_PROJECT_ID')
```bash
Authorization: Token <your-api-key>
```
</Tab>
Get your API key from the [Mem0 Dashboard](https://app.mem0.ai/dashboard/api-keys).
<Tab title="Node.js">
<Warning>
**Keep your API key secure.** Never expose it in client-side code or public repositories. Use environment variables and server-side requests only.
</Warning>
```javascript
import { MemoryClient } from "mem0ai";
const client = new MemoryClient({organizationId: "YOUR_ORG_ID", projectId: "YOUR_PROJECT_ID"});
```
---
</Tab>
</Tabs>
## Next Steps
### Project Management Methods
<CardGroup cols={2}>
<Card title="Add Your First Memory" icon="rocket" href="/api-reference/memory/add-memories">
Start storing memories via the REST API
</Card>
The Mem0 client provides comprehensive project management capabilities through the `client.project` interface:
#### Get Project Details
Retrieve information about the current project:
```python
# Get all project details
project_info = client.project.get()
# Get specific fields only
project_info = client.project.get(fields=["name", "description", "custom_categories"])
```
#### Create a New Project
Create a new project within your organization:
```python
# Create a project with name and description
new_project = client.project.create(
name="My New Project",
description="A project for managing customer support memories"
)
```
#### Update Project Settings
Modify project configuration including custom instructions, categories, and graph settings:
```python
# Update project with custom categories
client.project.update(
custom_categories=[
{"customer_preferences": "Customer likes, dislikes, and preferences"},
{"support_history": "Previous support interactions and resolutions"}
]
)
# Update project with custom instructions
client.project.update(
custom_instructions="..."
)
# Enable graph memory for the project
client.project.update(enable_graph=True)
# Update multiple settings at once
client.project.update(
custom_instructions="...",
custom_categories=[
{"personal_info": "User personal information and preferences"},
{"work_context": "Professional context and work-related information"}
],
enable_graph=True
)
```
#### Delete Project
<Note>
This action will remove all memories, messages, and other related data in the project. This operation is irreversible.
</Note>
Remove a project and all its associated data:
```python
# Delete the current project (irreversible)
result = client.project.delete()
```
#### Member Management
Manage project members and their access levels:
```python
# Get all project members
members = client.project.get_members()
# Add a new member as a reader
client.project.add_member(
email="colleague@company.com",
role="READER" # or "OWNER"
)
# Update a member's role
client.project.update_member(
email="colleague@company.com",
role="OWNER"
)
# Remove a member from the project
client.project.remove_member(email="colleague@company.com")
```
#### Member Roles
- **READER**: Can view and search memories, but cannot modify project settings or manage members
- **OWNER**: Full access including project modification, member management, and all reader permissions
#### Async Support
All project methods are also available in async mode:
```python
from mem0 import AsyncMemoryClient
async def manage_project():
client = AsyncMemoryClient(org_id='YOUR_ORG_ID', project_id='YOUR_PROJECT_ID')
# All methods support async/await
project_info = await client.project.get()
await client.project.update(enable_graph=True)
members = await client.project.get_members()
# To call the async function properly
import asyncio
asyncio.run(manage_project())
```
## Getting Started
To begin using the Mem0 API, you'll need to:
1. Sign up for a [Mem0 account](https://app.mem0.ai) and obtain your API key.
2. Familiarize yourself with the API endpoints and their functionalities.
3. Make your first API call to add or retrieve a memory.
Explore the detailed documentation for each API endpoint to learn more about request/response formats, parameters, and example usage.
<Card title="Search with Filters" icon="filter" href="/api-reference/memory/v2-search-memories">
Learn advanced search and filtering techniques
</Card>
</CardGroup>
+1 -1
View File
@@ -1,4 +1,4 @@
---
title: 'Delete User'
openapi: delete /v1/entities/{entity_type}/{entity_id}/
openapi: delete /v2/entities/{entity_type}/{entity_id}/
---
+6
View File
@@ -0,0 +1,6 @@
---
title: 'Get Event'
openapi: get /v1/event/{event_id}/
---
Retrieve details about a specific event by passing its `event_id`. This endpoint is particularly helpful for tracking the status, payload, and completion details of asynchronous memory operations.
+13
View File
@@ -0,0 +1,13 @@
---
title: 'Get Events'
openapi: get /v1/events/
---
List recent events for your organization and project.
## Use Cases
- **Dashboards**: Summarize adds/searches over time by paging through events.
- **Alerting**: Poll for `FAILED` events and trigger follow-up workflows.
- **Audit**: Store the returned payload/metadata for compliance logs.
+94 -1
View File
@@ -1,4 +1,97 @@
---
title: 'Add Memories'
openapi: post /v1/memories/
---
---
Add new facts, messages, or metadata to a user’s memory store. The Add Memories endpoint accepts either raw text or conversational turns and commits them asynchronously so the memory is ready for later search, retrieval, and graph queries.
## Endpoint
- **Method**: `POST`
- **URL**: `/v1/memories/`
- **Content-Type**: `application/json`
Memories are processed asynchronously by default. The response contains queued events you can track while the platform finalizes enrichment.
## Required headers
| Header | Required | Description |
| --- | --- | --- |
| `Authorization: Token <MEM0_API_KEY>` | Yes | API key scoped to your workspace. |
| `Accept: application/json` | Yes | Ensures a JSON response. |
## Request body
Provide at least one message or direct memory string. Most callers supply `messages` so Mem0 can infer structured memories as part of ingestion.
<CodeGroup>
```json Basic request
{
"user_id": "alice",
"messages": [
{ "role": "user", "content": "I moved to Austin last month." }
],
"metadata": {
"source": "onboarding_form"
}
}
```
</CodeGroup>
### Common fields
| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `user_id` | string | No* | Associates the memory with a user. Provide when you want the memory scoped to a specific identity. |
| `messages` | array | No* | Conversation turns for Mem0 to infer memories from. Each object should include `role` and `content`. |
| `metadata` | object | Optional | Custom key/value metadata (e.g., `{"topic": "preferences"}`). |
| `infer` | boolean (default `true`) | Optional | Set to `false` to skip inference and store the provided text as-is. |
| `async_mode` | boolean (default `true`) | Optional | Controls asynchronous processing. Most clients leave this enabled. |
| `output_format` | string (default `v1.1`) | Optional | Response format. `v1.1` wraps results in a `results` array. |
> \* Provide at least one `messages` entry to describe what you are storing. For scoped memories, include `user_id`. You can also attach `agent_id`, `app_id`, `run_id`, `project_id`, or `org_id` to refine ownership.
## Response
Successful requests return an array of events queued for processing. Each event includes the generated memory text and an identifier you can persist for auditing.
<CodeGroup>
```json 200 response
[
{
"id": "mem_01JF8ZS4Y0R0SPM13R5R6H32CJ",
"event": "ADD",
"data": {
"memory": "The user moved to Austin in 2025."
}
}
]
```
```json 400 response
{
"error": "400 Bad Request",
"details": {
"message": "Invalid input data. Please refer to the memory creation documentation at https://docs.mem0.ai/platform/quickstart#4-1-create-memories for correct formatting and required fields."
}
}
```
</CodeGroup>
## Graph relationships
Add Memories can enrich the knowledge graph on write. Set `enable_graph: true` to create entity nodes and relationships for the stored memory. Use this when you want downstream `get_all` or search calls to traverse connected entities.
<CodeGroup>
```json Graph-aware request
{
"user_id": "alice",
"messages": [
{ "role": "user", "content": "I met with Dr. Lee at General Hospital." }
],
"enable_graph": true
}
```
</CodeGroup>
The response follows the same format, and related entities become available in [Graph Memory](/platform/features/graph-memory) queries.
@@ -3,4 +3,4 @@ title: 'Create Memory Export'
openapi: post /v1/exports/
---
Submit a job to create a structured export of memories using a customizable Pydantic schema. This process may take some time to complete, especially if you’re exporting a large number of memories. You can tailor the export by applying various filters (e.g., user_id, agent_id, run_id, or session_id) and by modifying the Pydantic schema to ensure the final data matches your exact needs.
Submit a job to create a structured export of memories using a customizable Pydantic schema. This process may take some time to complete, especially if you're exporting a large number of memories. You can tailor the export by applying various filters (e.g., `user_id`, `agent_id`, `run_id`, or `session_id`) and by modifying the Pydantic schema to ensure the final data matches your exact needs.
@@ -0,0 +1,99 @@
---
title: "Get Memories"
openapi: post /v2/memories/
---
The v2 get memories API is powerful and flexible, allowing for more precise memory listing without the need for a search query. It supports complex logical operations (AND, OR, NOT) and comparison operators for advanced filtering capabilities. The comparison operators include:
- `in`: Matches any of the values specified
- `gte`: Greater than or equal to
- `lte`: Less than or equal to
- `gt`: Greater than
- `lt`: Less than
- `ne`: Not equal to
- `icontains`: Case-insensitive containment check
- `*`: Wildcard character that matches everything
<CodeGroup>
```python Code
memories = client.get_all(
filters={
"AND": [
{
"user_id": "alex"
},
{
"created_at": {"gte": "2024-07-01", "lte": "2024-07-31"}
}
]
}
)
```
```python Output
{
"results": [
{
"id": "f4cbdb08-7062-4f3e-8eb2-9f5c80dfe64c",
"memory": "Alex is planning a trip to San Francisco from July 1st to July 10th",
"created_at": "2024-07-01T12:00:00Z",
"updated_at": "2024-07-01T12:00:00Z"
},
{
"id": "a2b8c3d4-5e6f-7g8h-9i0j-1k2l3m4n5o6p",
"memory": "Alex prefers vegetarian restaurants",
"created_at": "2024-07-05T15:30:00Z",
"updated_at": "2024-07-05T15:30:00Z"
}
],
"total": 2
}
```
</CodeGroup>
## Graph Memory
To retrieve graph memory relationships between entities, pass `output_format="v1.1"` in your request. This will return memories with entity and relationship information from the knowledge graph.
<CodeGroup>
```python Code
memories = client.get_all(
filters={
"user_id": "alex"
},
output_format="v1.1"
)
```
```python Output
{
"results": [
{
"id": "f4cbdb08-7062-4f3e-8eb2-9f5c80dfe64c",
"memory": "Alex is planning a trip to San Francisco",
"entities": [
{
"id": "entity-1",
"name": "Alex",
"type": "person"
},
{
"id": "entity-2",
"name": "San Francisco",
"type": "location"
}
],
"relations": [
{
"source": "entity-1",
"target": "entity-2",
"relationship": "traveling_to"
}
]
}
]
}
```
</CodeGroup>
@@ -0,0 +1,104 @@
---
title: 'Search Memories'
openapi: post /v2/memories/search/
---
The v2 search API is powerful and flexible, allowing for more precise memory retrieval. It supports complex logical operations (AND, OR, NOT) and comparison operators for advanced filtering capabilities. The comparison operators include:
- `in`: Matches any of the values specified
- `gte`: Greater than or equal to
- `lte`: Less than or equal to
- `gt`: Greater than
- `lt`: Less than
- `ne`: Not equal to
- `icontains`: Case-insensitive containment check
- `*`: Wildcard character that matches everything
<CodeGroup>
```python Platform API Example
related_memories = client.search(
query="What are Alice's hobbies?",
filters={
"OR": [
{
"user_id": "alice"
},
{
"agent_id": {"in": ["travel-agent", "sports-agent"]}
}
]
},
)
```
```json Output
{
"memories": [
{
"id": "ea925981-272f-40dd-b576-be64e4871429",
"memory": "Likes to play cricket and plays cricket on weekends.",
"metadata": {
"category": "hobbies"
},
"score": 0.32116443111457704,
"created_at": "2024-07-26T10:29:36.630547-07:00",
"updated_at": null,
"user_id": "alice",
"agent_id": "sports-agent"
}
],
}
```
</CodeGroup>
<CodeGroup>
```python Wildcard Example
# Using wildcard to match all run_ids for a specific user
all_memories = client.search(
query="What are Alice's hobbies?",
filters={
"AND": [
{
"user_id": "alice"
},
{
"run_id": "*"
}
]
},
)
```
</CodeGroup>
<CodeGroup>
```python Categories Filter Examples
# Example 1: Using 'contains' for partial matching
finance_memories = client.search(
query="What are my financial goals?",
filters={
"AND": [
{ "user_id": "alice" },
{
"categories": {
"contains": "finance"
}
}
]
},
)
# Example 2: Using 'in' for exact matching
personal_memories = client.search(
query="What personal information do you have?",
filters={
"AND": [
{ "user_id": "alice" },
{
"categories": {
"in": ["personal_information"]
}
}
]
},
)
```
</CodeGroup>
@@ -1,4 +0,0 @@
---
title: 'Get Memories (v1 - Deprecated)'
openapi: get /v1/memories/
---
@@ -1,4 +0,0 @@
---
title: 'Search Memories (v1 - Deprecated)'
openapi: post /v1/memories/search/
---
@@ -1,65 +0,0 @@
---
title: 'Get Memories (v2)'
openapi: post /v2/memories/
---
The v2 get memories API is powerful and flexible, allowing for more precise memory listing without the need for a search query. It supports complex logical operations (AND, OR, NOT) and comparison operators for advanced filtering capabilities. The comparison operators include:
- `in`: Matches any of the values specified
- `gte`: Greater than or equal to
- `lte`: Less than or equal to
- `gt`: Greater than
- `lt`: Less than
- `ne`: Not equal to
- `icontains`: Case-insensitive containment check
- `*`: Wildcard character that matches everything
<CodeGroup>
```python Code
memories = m.get_all(
filters={
"AND": [
{
"user_id": "alex"
},
{
"created_at": {"gte": "2024-07-01", "lte": "2024-07-31"}
}
]
},
version="v2"
)
```
```json Output
[
{
"id":"f38b689d-6b24-45b7-bced-17fbb4d8bac7",
"memory":"Name: Alex. Vegetarian. Allergic to nuts.",
"user_id":"alex",
"hash":"62bc074f56d1f909f1b4c2b639f56f6a",
"metadata":null,
"created_at":"2024-07-25T23:57:00.108347-07:00",
"updated_at":"2024-07-25T23:57:00.108367-07:00"
}
]
```
</CodeGroup>
<CodeGroup>
```python Wildcard Example
# Using wildcard to get all memories for a specific user across all run_ids
memories = m.get_all(
filters={
"AND": [
{
"user_id": "alex"
},
{
"run_id": "*"
}
]
},
version="v2"
)
```
</CodeGroup>
@@ -1,72 +0,0 @@
---
title: 'Search Memories (v2)'
openapi: post /v2/memories/search/
---
The v2 search API is powerful and flexible, allowing for more precise memory retrieval. It supports complex logical operations (AND, OR, NOT) and comparison operators for advanced filtering capabilities. The comparison operators include:
- `in`: Matches any of the values specified
- `gte`: Greater than or equal to
- `lte`: Less than or equal to
- `gt`: Greater than
- `lt`: Less than
- `ne`: Not equal to
- `icontains`: Case-insensitive containment check
- `*`: Wildcard character that matches everything
<CodeGroup>
```python Code
related_memories = m.search(
query="What are Alice's hobbies?",
version="v2",
filters={
"OR": [
{
"user_id": "alice"
},
{
"agent_id": {"in": ["travel-agent", "sports-agent"]}
}
]
},
)
```
```json Output
{
"memories": [
{
"id": "ea925981-272f-40dd-b576-be64e4871429",
"memory": "Likes to play cricket and plays cricket on weekends.",
"metadata": {
"category": "hobbies"
},
"score": 0.32116443111457704,
"created_at": "2024-07-26T10:29:36.630547-07:00",
"updated_at": null,
"user_id": "alice",
"agent_id": "sports-agent"
}
],
}
```
</CodeGroup>
<CodeGroup>
```python Wildcard Example
# Using wildcard to match all run_ids for a specific user
all_memories = m.search(
query="What are Alice's hobbies?",
version="v2",
filters={
"AND": [
{
"user_id": "alice"
},
{
"run_id": "*"
}
]
},
)
```
</CodeGroup>
@@ -0,0 +1,197 @@
---
title: Organizations & Projects
icon: "building"
description: "Manage multi-tenant applications with organization and project APIs"
---
## Overview
Organizations and projects provide multi-tenant support, access control, and team collaboration capabilities for Mem0 Platform. Use these APIs to build applications that support multiple teams, customers, or isolated environments.
<Info>
Organizations and projects are **optional** features. You can use Mem0 without them for single-user or simple multi-user applications.
</Info>
## Key Capabilities
- **Multi-org/project Support**: Specify organization and project when initializing the Mem0 client to attribute API usage appropriately
- **Member Management**: Control access to data through organization and project membership
- **Access Control**: Only members can access memories and data within their organization/project scope
- **Team Isolation**: Maintain data separation between different teams and projects for secure collaboration
---
## Using Organizations & Projects
### Initialize with Org/Project Context
Example with the mem0 Python package:
<Tabs>
<Tab title="Python">
```python
from mem0 import MemoryClient
client = MemoryClient(org_id='YOUR_ORG_ID', project_id='YOUR_PROJECT_ID')
```
</Tab>
<Tab title="Node.js">
```javascript
import { MemoryClient } from "mem0ai";
const client = new MemoryClient({
organizationId: "YOUR_ORG_ID",
projectId: "YOUR_PROJECT_ID"
});
```
</Tab>
</Tabs>
---
## Project Management
The Mem0 client provides comprehensive project management through the `client.project` interface:
### Get Project Details
Retrieve information about the current project:
```python
# Get all project details
project_info = client.project.get()
# Get specific fields only
project_info = client.project.get(fields=["name", "description", "custom_categories"])
```
### Create a New Project
Create a new project within your organization:
```python
# Create a project with name and description
new_project = client.project.create(
name="My New Project",
description="A project for managing customer support memories"
)
```
### Update Project Settings
Modify project configuration including custom instructions, categories, and graph settings:
```python
# Update project with custom categories
client.project.update(
custom_categories=[
{"customer_preferences": "Customer likes, dislikes, and preferences"},
{"support_history": "Previous support interactions and resolutions"}
]
)
# Update project with custom instructions
client.project.update(
custom_instructions="..."
)
# Enable graph memory for the project
client.project.update(enable_graph=True)
# Update multiple settings at once
client.project.update(
custom_instructions="...",
custom_categories=[
{"personal_info": "User personal information and preferences"},
{"work_context": "Professional context and work-related information"}
],
enable_graph=True
)
```
### Delete Project
<Warning>
This action will remove all memories, messages, and other related data in the project. **This operation is irreversible.**
</Warning>
Remove a project and all its associated data:
```python
# Delete the current project (irreversible)
result = client.project.delete()
```
---
## Member Management
Manage project members and their access levels:
```python
# Get all project members
members = client.project.get_members()
# Add a new member as a reader
client.project.add_member(
email="colleague@company.com",
role="READER" # or "OWNER"
)
# Update a member's role
client.project.update_member(
email="colleague@company.com",
role="OWNER"
)
# Remove a member from the project
client.project.remove_member(email="colleague@company.com")
```
### Member Roles
| Role | Permissions |
|------|-------------|
| **READER** | Can view and search memories, but cannot modify project settings or manage members |
| **OWNER** | Full access including project modification, member management, and all reader permissions |
---
## Async Support
All project methods are available in async mode:
```python
from mem0 import AsyncMemoryClient
async def manage_project():
client = AsyncMemoryClient(org_id='YOUR_ORG_ID', project_id='YOUR_PROJECT_ID')
# All methods support async/await
project_info = await client.project.get()
await client.project.update(enable_graph=True)
members = await client.project.get_members()
# To call the async function properly
import asyncio
asyncio.run(manage_project())
```
---
## API Reference
For complete API specifications and additional endpoints, see:
<CardGroup cols={2}>
<Card title="Organizations APIs" icon="building" href="/api-reference/organization/create-org">
Create, get, and manage organizations
</Card>
<Card title="Project APIs" icon="folder" href="/api-reference/project/create-project">
Full project CRUD and member management endpoints
</Card>
</CardGroup>
@@ -3,7 +3,3 @@ title: 'Create Webhook'
openapi: post /api/v1/webhooks/projects/{project_id}/
---
## Create Webhook
Create a webhook by providing the project ID and the webhook details.
@@ -2,7 +2,3 @@
title: 'Delete Webhook'
openapi: delete /api/v1/webhooks/{webhook_id}/
---
## Delete Webhook
Delete a webhook by providing the webhook ID.
@@ -3,7 +3,3 @@ title: 'Get Webhook'
openapi: get /api/v1/webhooks/projects/{project_id}/
---
## Get Webhook
Get a webhook by providing the project ID.
@@ -3,7 +3,3 @@ title: 'Update Webhook'
openapi: put /api/v1/webhooks/{webhook_id}/
---
## Update Webhook
Update a webhook by providing the webhook ID and the fields to update.
+174 -3
View File
@@ -7,6 +7,119 @@ mode: "wide"
<Tabs>
<Tab title="Python">
<Update label="2026-02-17" description="v1.0.4">
**New Features & Updates:**
- **Memory Update:**
- Added `timestamp` parameter to `update()` — accepts Unix epoch (int/float) or ISO 8601 string
</Update>
<Update label="2026-01-29" description="v1.0.3">
**New Features & Updates:**
- **Project Settings:**
- Added inclusion prompt, exclusion prompt, memory depth, and usecase setting
</Update>
<Update label="2026-01-13" description="v1.0.2">
**New Features & Updates:**
- **Vector Stores:**
- Added DriverInfo metadata to MongoDB vector store
</Update>
<Update label="2025-11-14" description="v1.0.1">
**New Features & Updates:**
- **Vector Stores:**
- Added Apache Cassandra vector store support
- **Embeddings:**
- Added FastEmbed embedding support for local embeddings
- **Graph Store:**
- Added configurable embedding similarity threshold for graph store node matching
**Bug Fixes:**
- **Core:**
- Fixed condition check for memories_result type in Memory class
- Fixed list_memories endpoint Pydantic validation error
- Fixed memory deletion not removing from vector store
</Update>
<Update label="2025-10-16" description="v1.0.0">
**New Features & Updates:**
- **Vector Stores:**
- Added Azure MySQL support
- Added Azure AI Search Vector Store support
- **LLMs:**
- Added Tool Call support for LangchainLLM
- Enabled custom model and parameters for Hugging Face with huggingface_base_url
- Updated default LLM configuration
- **Rerankers:**
- Added reranker support: Cohere, ZeroEntropy, Hugging Face, Sentence Transformers, and LLMs
- **Core:**
- Added metadata filtering for OSS
- Added Assistant memory retrieval
- Enabled async mode as default
**Improvements:**
- **Prompts:**
- Improved prompt for better memory retrieval
- **Dependencies:**
- Updated dependency compatibility with OpenAI 2.x
- **Validation:**
- Validated embedding_dims for Kuzu integration
**Bug Fixes:**
- **Vector Stores:**
- Fixed Databricks Vector Store integration
- Fixed Milvus DB bug and added test coverage
- Fixed Weaviate search method
- **LLMs:**
- Fixed bug with thinking LLM in vLLM
</Update>
<Update label="2025-09-25" description="v0.1.118">
**New Features & Updates:**
- **Vector Stores:**
- Added Valkey vector store support
- Added support for ChromaDB Cloud
- Added Mem0 vector store backend integration for Neptune Analytics
- **Graph Store:**
- Added Neptune-DB graph store with vector store
- **Core:**
- Implemented structured exception classes with error codes and suggested actions
**Improvements:**
- **Dependencies:**
- Updated OpenAI dependency and improved Ollama compatibility
- **Testing:**
- Added Weaviate DB test
- Added comprehensive test suite for SQLiteManager
- **Documentation:**
- Updated category docs
- Updated Search V2 / Get All V2 filters documentation
- Refactored AWS example title
- Fixed Quickstart cURL example
**Bug Fixes:**
- **Vector Stores:**
- Databricks bug fixes
- Fixed S3 Vectors memory initialization issue from configuration
- **Core:**
- Fixed JSON parsing with new memories
- Replaced hardcoded LLM provider with provider from configuration
- **LLMs:**
- Fixed Bedrock Anthropic models to use system field
</Update>
<Update label="2025-09-03" description="v0.1.117">
**New Features & Updates:**
@@ -611,6 +724,49 @@ mode: "wide"
<Tab title="TypeScript">
<Update label="2026-02-17" description="v2.2.3">
**New Features & Updates:**
- **Memory Update:**
- Added `timestamp` parameter to `update()` — accepts Unix epoch or ISO 8601 string
</Update>
<Update label="2026-01-29" description="v2.2.2">
**New Features & Updates:**
- **Project Settings:**
- Added inclusion prompt, exclusion prompt, memory depth, and usecase setting
</Update>
<Update label="2025-12-30" description="v2.2.1">
**Improvements:**
- **Client:** Added support for keyword arguments in `add` and `search` methods, allowing additional properties beyond defined options for experimental features
</Update>
<Update label="2025-12-29" description="v2.2.0">
**New Features:**
- **Vector Stores:** Added Azure AI Search vector store support
**Improvements:**
- **Config:** Fixed embedder config schema to support `embeddingDims` and `url` parameters
- **Graph Memory:** Replaced hardcoded LLM provider with provider from configuration
**Bug Fixes:**
- **Embedders:** Fixed hardcoded `embeddingDims` values in embedders (OpenAI, Ollama, Google, Azure)
- **Build:** Fixed TypeScript build errors
</Update>
<Update label="2025-09-04" description="v2.1.38">
**New Features:**
- **Client:** Added `metadata` param to `update` method.
</Update>
<Update label="2025-08-04" description="v2.1.37">
**New Features:**
- **OSS:** Added `RedisCloud` search module check
@@ -632,17 +788,17 @@ mode: "wide"
</Update>
<Update label="2025-06-24" description="v2.1.33">
**Improvement :**
**Improvement:**
- **Client:** Added `immutable` param to `add` method.
</Update>
<Update label="2025-06-20" description="v2.1.32">
**Improvement :**
**Improvement:**
- **Client:** Made `api_version` V2 as default.
</Update>
<Update label="2025-06-17" description="v2.1.31">
**Improvement :**
**Improvement:**
- **Client:** Added param `filter_memories`.
</Update>
@@ -1082,6 +1238,21 @@ mode: "wide"
<Tab title="Vercel AI SDK">
<Update label="2025-12-26" description="v2.0.5">
**Bug Fix:**
- **Vercel AI SDK:** Removed unnecessary dependencies to make the package lighter.
</Update>
<Update label="2025-09-25" description="v2.0.4">
**Bug Fix:**
- **Vercel AI SDK:** Fixed version parameter in the AI SDK to use V2 for addition.
</Update>
<Update label="2025-09-25" description="v2.0.3">
**New Features:**
- **Vercel AI SDK:** Added file support for multimodal capabilities with memory context
</Update>
<Update label="2025-09-03" description="v2.0.2">
**Bug Fix:**
- **Vercel AI SDK:** Fixed streaming response in the AI SDK.
-2
View File
@@ -1,7 +1,5 @@
---
title: Configurations
icon: "gear"
iconType: "solid"
---
@@ -41,7 +41,7 @@ config = {
m = Memory.from_config(config)
messages = [
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
{"role": "assistant", "content": "How about thriller movies? They can be quite engaging."},
{"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."},
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
]
@@ -23,7 +23,7 @@ config = {
"embedder": {
"provider": "azure_openai",
"config": {
"model": "text-embedding-3-large"
"model": "text-embedding-3-large",
"azure_kwargs": {
"api_version": "",
"azure_deployment": "",
@@ -40,7 +40,7 @@ config = {
m = Memory.from_config(config)
messages = [
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
{"role": "assistant", "content": "How about thriller movies? They can be quite engaging."},
{"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."},
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
]
@@ -68,7 +68,7 @@ const memory = new Memory(config);
const messages = [
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
{"role": "assistant", "content": "How about thriller movies? They can be quite engaging."},
{"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."},
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
]
@@ -117,9 +117,20 @@ Refer to [Azure Identity troubleshooting tips](https://github.com/Azure/azure-sd
### Config
Here are the parameters available for configuring Azure OpenAI embedder:
<Tabs>
<Tab title="Python">
| Parameter | Description | Default Value |
| --- | --- | --- |
| `model` | The name of the embedding model to use | `text-embedding-3-small` |
| `embedding_dims` | Dimensions of the embedding model | `1536` |
| `azure_kwargs` | The Azure OpenAI configs | `config_keys` |
</Tab>
<Tab title="TypeScript">
| Parameter | Description | Default Value |
| ----------------- | --------------------------------------------- | -------------------------- |
| `model` | The name of the embedding model to use | `text-embedding-3-small` |
| `embeddingDims` | Dimensions of the embedding model | `1536` |
| `apiKey` | Azure OpenAI API key | `None` |
| `modelProperties` | Object containing endpoint and other settings | `{ endpoint: "",...rest }`|
</Tab>
</Tabs>
+24 -14
View File
@@ -26,7 +26,7 @@ config = {
m = Memory.from_config(config)
messages = [
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
{"role": "assistant", "content": "How about thriller movies? They can be quite engaging."},
{"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."},
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
]
@@ -38,19 +38,19 @@ import { Memory } from 'mem0ai/oss';
const config = {
embedder: {
provider: 'google',
config: {
apiKey: process.env.GOOGLE_API_KEY || '',
model: 'text-embedding-004',
// The output dimensionality is fixed at 768 for Google AI embeddings
provider: "google",
config: {
apiKey: process.env["GOOGLE_API_KEY"],
model: "gemini-embedding-001",
embeddingDims: 1536,
},
},
},
};
const memory = new Memory(config);
const messages = [
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
{"role": "assistant", "content": "How about thriller movies? They can be quite engaging."},
{"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."},
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
]
@@ -61,9 +61,19 @@ await memory.add(messages, { userId: "john" });
### Config
Here are the parameters available for configuring Gemini embedder:
| Parameter | Description | Default Value |
| --- | --- | --- |
| `model` | The name of the embedding model to use | `models/text-embedding-004` |
| `embedding_dims` | Dimensions of the embedding model (output_dimensionality will be considered as embedding_dims, so please set embedding_dims accordingly) | `768` |
| `api_key` | The Google API key | `None` |
<Tabs>
<Tab title="Python">
| Parameter | Description | Default Value |
| ---------------- | ------------------------------------ | ----------------------- |
| `model` | The name of the embedding model to use| `models/text-embedding-004` |
| `embedding_dims` | Dimensions of the embedding model | `1536` |
| `api_key` | The Google API key | `None` |
</Tab>
<Tab title="TypeScript">
| Parameter | Description | Default Value |
| ----------------- | --------------------------------------------- | -------------------------- |
| `model` | The name of the embedding model to use | `gemini-embedding-001` |
| `embeddingDims` | Dimensions of the embedding model | `1536` |
| `apiKey` | Google API key | `None` |
</Tab>
</Tabs>
@@ -24,7 +24,7 @@ config = {
m = Memory.from_config(config)
messages = [
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
{"role": "assistant", "content": "How about thriller movies? They can be quite engaging."},
{"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."},
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
]
@@ -36,7 +36,7 @@ config = {
m = Memory.from_config(config)
messages = [
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
{"role": "assistant", "content": "How about thriller movies? They can be quite engaging."},
{"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."},
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
]
@@ -66,7 +66,7 @@ const config = {
const memory = new Memory(config);
const messages = [
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
{"role": "assistant", "content": "How about thriller movies? They can be quite engaging."},
{"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."},
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
]
@@ -20,7 +20,7 @@ config = {
m = Memory.from_config(config)
messages = [
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
{"role": "assistant", "content": "How about thriller movies? They can be quite engaging."},
{"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."},
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
]
@@ -29,10 +29,10 @@ m.add(messages, user_id="john")
### Config
Here are the parameters available for configuring Ollama embedder:
Here are the parameters available for configuring LM Studio embedder:
| Parameter | Description | Default Value |
| --- | --- | --- |
| `model` | The name of the OpenAI model to use | `nomic-embed-text-v1.5-GGUF/nomic-embed-text-v1.5.f16.gguf` |
| `model` | The name of the LM Studio model to use | `nomic-embed-text-v1.5-GGUF/nomic-embed-text-v1.5.f16.gguf` |
| `embedding_dims` | Dimensions of the embedding model | `1536` |
| `lmstudio_base_url` | Base URL for LM Studio connection | `http://localhost:1234/v1` |
+3 -2
View File
@@ -21,7 +21,7 @@ config = {
m = Memory.from_config(config)
messages = [
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
{"role": "assistant", "content": "How about thriller movies? They can be quite engaging."},
{"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."},
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
]
@@ -44,7 +44,7 @@ const config = {
const memory = new Memory(config);
const messages = [
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
{"role": "assistant", "content": "How about thriller movies? They can be quite engaging."},
{"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."},
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
]
@@ -69,5 +69,6 @@ Here are the parameters available for configuring Ollama embedder:
| --- | --- | --- |
| `model` | The name of the Ollama model to use | `nomic-embed-text:latest` |
| `url` | Base URL for Ollama server | `http://localhost:11434` |
| `embeddingDims` | Dimensions of the embedding model | 768
</Tab>
</Tabs>
+1 -1
View File
@@ -25,7 +25,7 @@ config = {
m = Memory.from_config(config)
messages = [
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
{"role": "assistant", "content": "How about thriller movies? They can be quite engaging."},
{"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."},
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
]
@@ -27,7 +27,7 @@ config = {
m = Memory.from_config(config)
messages = [
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
{"role": "assistant", "content": "How about thriller movies? They can be quite engaging."},
{"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."},
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
]
@@ -27,7 +27,7 @@ config = {
m = Memory.from_config(config)
messages = [
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
{"role": "assistant", "content": "How about thriller movies? They can be quite engaging."},
{"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."},
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
]
+2 -4
View File
@@ -1,7 +1,5 @@
---
title: Overview
icon: "info"
iconType: "solid"
---
Mem0 offers support for various embedding models, allowing users to choose the one that best suits their needs.
@@ -29,6 +27,6 @@ See the list of supported embedders below.
## Usage
To utilize a embedder, you must provide a configuration to customize its usage. If no configuration is supplied, a default configuration will be applied, and `OpenAI` will be used as the embedder.
To utilize an embedding model, you must provide a configuration to customize its usage. If no configuration is supplied, a default configuration will be applied, and `OpenAI` will be used as the embedding model.
For a comprehensive list of available parameters for embedder configuration, please refer to [Config](./config).
For a comprehensive list of available parameters for embedding model configuration, please refer to [Config](./config).
-2
View File
@@ -1,7 +1,5 @@
---
title: Configurations
icon: "gear"
iconType: "solid"
---
## How to define configurations?
+2 -2
View File
@@ -29,7 +29,7 @@ config = {
m = Memory.from_config(config)
messages = [
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
{"role": "assistant", "content": "How about thriller movies? They can be quite engaging."},
{"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."},
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
]
@@ -54,7 +54,7 @@ const config = {
const memory = new Memory(config);
const messages = [
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
{"role": "assistant", "content": "How about thriller movies? They can be quite engaging."},
{"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."},
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
]
+1 -1
View File
@@ -31,7 +31,7 @@ config = {
m = Memory.from_config(config)
messages = [
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
{"role": "assistant", "content": "How about thriller movies? They can be quite engaging."},
{"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."},
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
]
+2 -2
View File
@@ -48,7 +48,7 @@ config = {
m = Memory.from_config(config)
messages = [
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
{"role": "assistant", "content": "How about thriller movies? They can be quite engaging."},
{"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."},
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
]
@@ -77,7 +77,7 @@ const config = {
const memory = new Memory(config);
const messages = [
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
{"role": "assistant", "content": "How about thriller movies? They can be quite engaging."},
{"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."},
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
]
+1 -1
View File
@@ -28,7 +28,7 @@ config = {
m = Memory.from_config(config)
messages = [
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
{"role": "assistant", "content": "How about thriller movies? They can be quite engaging."},
{"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."},
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
]
+2 -2
View File
@@ -30,7 +30,7 @@ config = {
m = Memory.from_config(config)
messages = [
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
{"role": "assistant", "content": "How about thriller movies? They can be quite engaging."},
{"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."},
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
]
@@ -55,7 +55,7 @@ const config = {
const memory = new Memory(config);
const messages = [
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
{"role": "assistant", "content": "How about thriller movies? They can be quite engaging."},
{"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."},
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
]
+3 -3
View File
@@ -20,7 +20,7 @@ os.environ["OPENAI_API_KEY"] = "your-api-key"
# Initialize a LangChain model directly
openai_model = ChatOpenAI(
model="gpt-4o",
model="gpt-4.1-nano-2025-04-14",
temperature=0.2,
max_tokens=2000
)
@@ -38,7 +38,7 @@ config = {
m = Memory.from_config(config)
messages = [
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
{"role": "assistant", "content": "How about thriller movies? They can be quite engaging."},
{"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."},
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
]
@@ -69,7 +69,7 @@ const config = {
const memory = new Memory(config);
const messages = [
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
{"role": "assistant", "content": "How about thriller movies? They can be quite engaging."},
{"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."},
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
]
+2 -2
View File
@@ -12,7 +12,7 @@ config = {
"llm": {
"provider": "litellm",
"config": {
"model": "gpt-4o-mini",
"model": "gpt-4.1-nano-2025-04-14",
"temperature": 0.2,
"max_tokens": 2000,
}
@@ -22,7 +22,7 @@ config = {
m = Memory.from_config(config)
messages = [
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
{"role": "assistant", "content": "How about thriller movies? They can be quite engaging."},
{"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."},
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
]
+2 -2
View File
@@ -29,7 +29,7 @@ config = {
m = Memory.from_config(config)
messages = [
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
{"role": "assistant", "content": "How about thriller movies? They can be quite engaging."},
{"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."},
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
]
@@ -57,7 +57,7 @@ config = {
m = Memory.from_config(config)
messages = [
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
{"role": "assistant", "content": "How about thriller movies? They can be quite engaging."},
{"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."},
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
]
+2 -2
View File
@@ -28,7 +28,7 @@ config = {
m = Memory.from_config(config)
messages = [
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
{"role": "assistant", "content": "How about thriller movies? They can be quite engaging."},
{"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."},
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
]
@@ -53,7 +53,7 @@ const config = {
const memory = new Memory(config);
const messages = [
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
{"role": "assistant", "content": "How about thriller movies? They can be quite engaging."},
{"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."},
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
]
+7 -3
View File
@@ -1,4 +1,8 @@
You can use LLMs from Ollama to run Mem0 locally. These [models](https://ollama.com/search?c=tools) support tool support.
---
title: Ollama
---
You can use LLMs from Ollama to run Mem0 locally. These [models](https://ollama.com/search?c=tools) support tool calling.
## Usage
@@ -23,7 +27,7 @@ config = {
m = Memory.from_config(config)
messages = [
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
{"role": "assistant", "content": "How about thriller movies? They can be quite engaging."},
{"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."},
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
]
@@ -47,7 +51,7 @@ const config = {
const memory = new Memory(config);
const messages = [
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
{"role": "assistant", "content": "How about thriller movies? They can be quite engaging."},
{"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."},
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
]
+4 -4
View File
@@ -19,7 +19,7 @@ config = {
"llm": {
"provider": "openai",
"config": {
"model": "gpt-4o",
"model": "gpt-4.1-nano-2025-04-14",
"temperature": 0.2,
"max_tokens": 2000,
}
@@ -40,7 +40,7 @@ config = {
m = Memory.from_config(config)
messages = [
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
{"role": "assistant", "content": "How about thriller movies? They can be quite engaging."},
{"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."},
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
]
@@ -65,7 +65,7 @@ const config = {
const memory = new Memory(config);
const messages = [
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
{"role": "assistant", "content": "How about thriller movies? They can be quite engaging."},
{"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."},
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
]
@@ -85,7 +85,7 @@ config = {
"llm": {
"provider": "openai_structured",
"config": {
"model": "gpt-4o-2024-08-06",
"model": "gpt-4.1-nano-2025-04-14",
"temperature": 0.0,
}
}
+1 -1
View File
@@ -28,7 +28,7 @@ config = {
m = Memory.from_config(config)
messages = [
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
{"role": "assistant", "content": "How about thriller movies? They can be quite engaging."},
{"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."},
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
]
+7 -3
View File
@@ -1,4 +1,8 @@
To use TogetherAI LLM models, you have to set the `TOGETHER_API_KEY` environment variable. You can obtain the TogetherAI API key from their [Account settings page](https://api.together.xyz/settings/api-keys).
---
title: Together
---
To use Together LLM models, you have to set the `TOGETHER_API_KEY` environment variable. You can obtain the Together API key from their [Account settings page](https://api.together.xyz/settings/api-keys).
## Usage
@@ -23,7 +27,7 @@ config = {
m = Memory.from_config(config)
messages = [
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
{"role": "assistant", "content": "How about thriller movies? They can be quite engaging."},
{"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."},
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
]
@@ -32,4 +36,4 @@ m.add(messages, user_id="alice", metadata={"category": "movies"})
## Config
All available parameters for the `togetherai` config are present in [Master List of All Params in Config](../config).
All available parameters for the `together` config are present in [Master List of All Params in Config](../config).
+1 -1
View File
@@ -29,7 +29,7 @@ config = {
m = Memory.from_config(config)
messages = [
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
{"role": "assistant", "content": "How about thriller movies? They can be quite engaging."},
{"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."},
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
]
+2 -4
View File
@@ -1,7 +1,5 @@
---
title: Overview
icon: "info"
iconType: "solid"
---
Mem0 includes built-in support for various popular large language models. Memory can utilize the LLM provided by the user, ensuring efficient use for specific needs.
@@ -28,8 +26,8 @@ See the list of supported LLMs below.
<Card title="Together" href="/components/llms/models/together" />
<Card title="Groq" href="/components/llms/models/groq" />
<Card title="Litellm" href="/components/llms/models/litellm" />
<Card title="Mistral AI" href="/components/llms/models/mistral_ai" />
<Card title="Google AI" href="/components/llms/models/google_ai" />
<Card title="Mistral AI" href="/components/llms/models/mistral_AI" />
<Card title="Google AI" href="/components/llms/models/google_AI" />
<Card title="AWS bedrock" href="/components/llms/models/aws_bedrock" />
<Card title="DeepSeek" href="/components/llms/models/deepseek" />
<Card title="xAI" href="/components/llms/models/xAI" />
+105
View File
@@ -0,0 +1,105 @@
---
title: Config
description: "Configuration options for rerankers in Mem0"
---
## Common Configuration Parameters
All rerankers share these common configuration parameters:
| Parameter | Description | Type | Default |
| ---------- | --------------------------------------------------- | ----- | -------- |
| `provider` | Reranker provider name | `str` | Required |
| `top_k` | Maximum number of results to return after reranking | `int` | `None` |
| `api_key` | API key for the reranker service | `str` | `None` |
## Provider-Specific Configuration
### Zero Entropy
| Parameter | Description | Type | Default |
| --------- | -------------------------------------------- | ----- | ------------ |
| `model` | Model to use: `zerank-1` or `zerank-1-small` | `str` | `"zerank-1"` |
| `api_key` | Zero Entropy API key | `str` | `None` |
### Cohere
| Parameter | Description | Type | Default |
| -------------------- | -------------------------------------------- | ------ | ----------------------- |
| `model` | Cohere rerank model | `str` | `"rerank-english-v3.0"` |
| `api_key` | Cohere API key | `str` | `None` |
| `return_documents` | Whether to return document texts in response | `bool` | `False` |
| `max_chunks_per_doc` | Maximum chunks per document | `int` | `None` |
### Sentence Transformer
| Parameter | Description | Type | Default |
| ------------------- | -------------------------------------------- | ------ | ---------------------------------------- |
| `model` | HuggingFace cross-encoder model name | `str` | `"cross-encoder/ms-marco-MiniLM-L-6-v2"` |
| `device` | Device to run model on (`cpu`, `cuda`, etc.) | `str` | `None` |
| `batch_size` | Batch size for processing | `int` | `32` |
| `show_progress_bar` | Show progress during processing | `bool` | `False` |
### Hugging Face
| Parameter | Description | Type | Default |
| --------- | -------------------------------------------- | ----- | --------------------------- |
| `model` | HuggingFace reranker model name | `str` | `"BAAI/bge-reranker-large"` |
| `api_key` | HuggingFace API token | `str` | `None` |
| `device` | Device to run model on (`cpu`, `cuda`, etc.) | `str` | `None` |
### LLM-based
| Parameter | Description | Type | Default |
| ---------------- | ------------------------------------------ | ------- | ---------------------- |
| `model` | LLM model to use for scoring | `str` | `"gpt-4o-mini"` |
| `provider` | LLM provider (`openai`, `anthropic`, etc.) | `str` | `"openai"` |
| `api_key` | API key for LLM provider | `str` | `None` |
| `temperature` | Temperature for LLM generation | `float` | `0.0` |
| `max_tokens` | Maximum tokens for LLM response | `int` | `100` |
| `scoring_prompt` | Custom prompt template for scoring | `str` | Default scoring prompt |
### LLM Reranker
| Parameter | Description | Type | Default |
| -------------- | --------------------------- | ------ | -------- |
| `llm.provider` | LLM provider for reranking | `str` | Required |
| `llm.config` | LLM configuration object | `dict` | Required |
| `top_n` | Number of results to return | `int` | `None` |
## Environment Variables
You can set API keys using environment variables:
- `ZERO_ENTROPY_API_KEY` - Zero Entropy API key
- `COHERE_API_KEY` - Cohere API key
- `HUGGINGFACE_API_KEY` - HuggingFace API token
- `OPENAI_API_KEY` - OpenAI API key (for LLM-based reranker)
- `ANTHROPIC_API_KEY` - Anthropic API key (for LLM-based reranker)
## Basic Configuration Example
```python Python
config = {
"vector_store": {
"provider": "chroma",
"config": {
"collection_name": "my_memories",
"path": "./chroma_db"
}
},
"llm": {
"provider": "openai",
"config": {
"model": "gpt-4.1-nano-2025-04-14"
}
},
"reranker": {
"provider": "zero_entropy",
"config": {
"model": "zerank-1",
"top_k": 5
}
}
}
```
@@ -0,0 +1,220 @@
---
title: Custom Prompts
---
When using LLM rerankers, you can customize the prompts used for ranking to better suit your specific use case and domain.
## Default Prompt
The default LLM reranker prompt is designed to be general-purpose:
```
Given a query and a list of memory entries, rank the memory entries based on their relevance to the query.
Rate each memory on a scale of 1-10 where 10 is most relevant.
Query: {query}
Memory entries:
{memories}
Provide your ranking as a JSON array with scores for each memory.
```
## Custom Prompt Configuration
You can provide a custom prompt template when configuring the LLM reranker:
```python
from mem0 import Memory
custom_prompt = """
You are an expert at ranking memories for a personal AI assistant.
Given a user query and a list of memory entries, rank each memory based on:
1. Direct relevance to the query
2. Temporal relevance (recent memories may be more important)
3. Emotional significance
4. Actionability
Query: {query}
User Context: {user_context}
Memory entries:
{memories}
Rate each memory from 1-10 and provide reasoning.
Return as JSON: {{"rankings": [{{"index": 0, "score": 8, "reason": "..."}}]}}
"""
config = {
"reranker": {
"provider": "llm_reranker",
"config": {
"llm": {
"provider": "openai",
"config": {
"model": "gpt-4.1-nano-2025-04-14",
"api_key": "your-openai-key"
}
},
"custom_prompt": custom_prompt,
"top_n": 5
}
}
}
memory = Memory.from_config(config)
```
## Prompt Variables
Your custom prompt can use the following variables:
| Variable | Description |
| ---------------- | ------------------------------------- |
| `{query}` | The search query |
| `{memories}` | The list of memory entries to rank |
| `{user_id}` | The user ID (if available) |
| `{user_context}` | Additional user context (if provided) |
## Domain-Specific Examples
### Customer Support
```python
customer_support_prompt = """
You are ranking customer support conversation memories.
Prioritize memories that:
- Relate to the current customer issue
- Show previous resolution patterns
- Indicate customer preferences or constraints
Query: {query}
Customer Context: Previous interactions with this customer
Memories:
{memories}
Rank each memory 1-10 based on support relevance.
"""
```
### Educational Content
```python
educational_prompt = """
Rank these learning memories for a student query.
Consider:
- Prerequisite knowledge requirements
- Learning progression and difficulty
- Relevance to current learning objectives
Student Query: {query}
Learning Context: {user_context}
Available memories:
{memories}
Score each memory for educational value (1-10).
"""
```
### Personal Assistant
```python
personal_assistant_prompt = """
Rank personal memories for relevance to the user's query.
Consider:
- Recent vs. historical importance
- Personal preferences and habits
- Contextual relationships between memories
Query: {query}
Personal context: {user_context}
Memories to rank:
{memories}
Provide relevance scores (1-10) with brief explanations.
"""
```
## Advanced Prompt Techniques
### Multi-Criteria Ranking
```python
multi_criteria_prompt = """
Evaluate memories using multiple criteria:
1. RELEVANCE (40%): How directly related to the query
2. RECENCY (20%): How recent the memory is
3. IMPORTANCE (25%): Personal or business significance
4. ACTIONABILITY (15%): How useful for next steps
Query: {query}
Context: {user_context}
Memories:
{memories}
For each memory, provide:
- Overall score (1-10)
- Breakdown by criteria
- Final ranking recommendation
Format: JSON with detailed scoring
"""
```
### Contextual Ranking
```python
contextual_prompt = """
Consider the following context when ranking memories:
- Current user situation: {user_context}
- Time of day: {current_time}
- Recent activities: {recent_activities}
Query: {query}
Rank these memories considering both direct relevance and contextual appropriateness:
{memories}
Provide contextually-aware relevance scores (1-10).
"""
```
## Best Practices
1. **Be Specific**: Clearly define what makes a memory relevant for your use case
2. **Use Examples**: Include examples in your prompt for better model understanding
3. **Structure Output**: Specify the exact JSON format you want returned
4. **Test Iteratively**: Refine your prompt based on actual ranking performance
5. **Consider Token Limits**: Keep prompts concise while being comprehensive
## Prompt Testing
You can test different prompts by comparing ranking results:
```python
# Test multiple prompt variations
prompts = [
default_prompt,
custom_prompt_v1,
custom_prompt_v2
]
for i, prompt in enumerate(prompts):
config["reranker"]["config"]["custom_prompt"] = prompt
memory = Memory.from_config(config)
results = memory.search("test query", user_id="test_user")
print(f"Prompt {i+1} results: {results}")
```
## Common Issues
- **Too Long**: Keep prompts under token limits for your chosen LLM
- **Too Vague**: Be specific about ranking criteria
- **Inconsistent Format**: Ensure JSON output format is clearly specified
- **Missing Context**: Include relevant variables for your use case
+145
View File
@@ -0,0 +1,145 @@
---
title: Cohere
description: "Reranking with Cohere"
---
Cohere provides enterprise-grade reranking models with excellent multilingual support and production-ready performance.
## Models
Cohere offers several reranking models:
- **`rerank-english-v3.0`**: Latest English reranker with best performance
- **`rerank-multilingual-v3.0`**: Multilingual support for global applications
- **`rerank-english-v2.0`**: Previous generation English reranker
## Installation
```bash
pip install cohere
```
## Configuration
```python Python
from mem0 import Memory
config = {
"vector_store": {
"provider": "chroma",
"config": {
"collection_name": "my_memories",
"path": "./chroma_db"
}
},
"llm": {
"provider": "openai",
"config": {
"model": "gpt-4.1-nano-2025-04-14"
}
},
"reranker": {
"provider": "cohere",
"config": {
"model": "rerank-english-v3.0",
"api_key": "your-cohere-api-key", # or set COHERE_API_KEY
"top_k": 5,
"return_documents": False,
"max_chunks_per_doc": None
}
}
}
memory = Memory.from_config(config)
```
## Environment Variables
Set your API key as an environment variable:
```bash
export COHERE_API_KEY="your-api-key"
```
## Usage Example
```python Python
import os
from mem0 import Memory
# Set API key
os.environ["COHERE_API_KEY"] = "your-api-key"
# Initialize memory with Cohere reranker
config = {
"vector_store": {"provider": "chroma"},
"llm": {"provider": "openai", "config": {"model": "gpt-4o-mini"}},
"rerank": {
"provider": "cohere",
"config": {
"model": "rerank-english-v3.0",
"top_k": 3
}
}
}
memory = Memory.from_config(config)
# Add memories
messages = [
{"role": "user", "content": "I work as a data scientist at Microsoft"},
{"role": "user", "content": "I specialize in machine learning and NLP"},
{"role": "user", "content": "I enjoy playing tennis on weekends"}
]
memory.add(messages, user_id="bob")
# Search with reranking
results = memory.search("What is the user's profession?", user_id="bob")
for result in results['results']:
print(f"Memory: {result['memory']}")
print(f"Vector Score: {result['score']:.3f}")
print(f"Rerank Score: {result['rerank_score']:.3f}")
print()
```
## Multilingual Support
For multilingual applications, use the multilingual model:
```python Python
config = {
"rerank": {
"provider": "cohere",
"config": {
"model": "rerank-multilingual-v3.0",
"top_k": 5
}
}
}
```
## Configuration Parameters
| Parameter | Description | Type | Default |
| -------------------- | -------------------------------- | ------ | ----------------------- |
| `model` | Cohere rerank model to use | `str` | `"rerank-english-v3.0"` |
| `api_key` | Cohere API key | `str` | `None` |
| `top_k` | Maximum documents to return | `int` | `None` |
| `return_documents` | Whether to return document texts | `bool` | `False` |
| `max_chunks_per_doc` | Maximum chunks per document | `int` | `None` |
## Features
- **High Quality**: Enterprise-grade relevance scoring
- **Multilingual**: Support for 100+ languages
- **Scalable**: Production-ready with high throughput
- **Reliable**: SLA-backed service with 99.9% uptime
## Best Practices
1. **Model Selection**: Use `rerank-english-v3.0` for English, `rerank-multilingual-v3.0` for other languages
2. **Batch Processing**: Process multiple queries efficiently
3. **Error Handling**: Implement retry logic for production systems
4. **Monitoring**: Track reranking performance and costs
@@ -0,0 +1,350 @@
---
title: Hugging Face Reranker
description: 'Access thousands of reranking models from Hugging Face Hub'
---
## Overview
The Hugging Face reranker provider gives you access to thousands of reranking models available on the Hugging Face Hub. This includes popular models like BAAI's BGE rerankers and other state-of-the-art cross-encoder models.
## Configuration
### Basic Setup
```python
from mem0 import Memory
config = {
"reranker": {
"provider": "huggingface",
"config": {
"model": "BAAI/bge-reranker-base",
"device": "cpu"
}
}
}
m = Memory.from_config(config)
```
### Configuration Parameters
| Parameter | Type | Default | Description |
|-----------|------|---------|-------------|
| `model` | str | Required | Hugging Face model identifier |
| `device` | str | "cpu" | Device to run model on ("cpu", "cuda", "mps") |
| `batch_size` | int | 32 | Batch size for processing |
| `max_length` | int | 512 | Maximum input sequence length |
| `trust_remote_code` | bool | False | Allow remote code execution |
### Advanced Configuration
```python
config = {
"reranker": {
"provider": "huggingface",
"config": {
"model": "BAAI/bge-reranker-large",
"device": "cuda",
"batch_size": 16,
"max_length": 512,
"trust_remote_code": False,
"model_kwargs": {
"torch_dtype": "float16"
}
}
}
}
```
## Popular Models
### BGE Rerankers (Recommended)
```python
# Base model - good balance of speed and quality
config = {
"reranker": {
"provider": "huggingface",
"config": {
"model": "BAAI/bge-reranker-base",
"device": "cuda"
}
}
}
# Large model - better quality, slower
config = {
"reranker": {
"provider": "huggingface",
"config": {
"model": "BAAI/bge-reranker-large",
"device": "cuda"
}
}
}
# v2 models - latest improvements
config = {
"reranker": {
"provider": "huggingface",
"config": {
"model": "BAAI/bge-reranker-v2-m3",
"device": "cuda"
}
}
}
```
### Multilingual Models
```python
# Multilingual BGE reranker
config = {
"reranker": {
"provider": "huggingface",
"config": {
"model": "BAAI/bge-reranker-v2-multilingual",
"device": "cuda"
}
}
}
```
### Domain-Specific Models
```python
# For code search
config = {
"reranker": {
"provider": "huggingface",
"config": {
"model": "microsoft/codebert-base",
"device": "cuda"
}
}
}
# For biomedical content
config = {
"reranker": {
"provider": "huggingface",
"config": {
"model": "dmis-lab/biobert-base-cased-v1.1",
"device": "cuda"
}
}
}
```
## Usage Examples
### Basic Usage
```python
from mem0 import Memory
m = Memory.from_config(config)
# Add some memories
m.add("I love hiking in the mountains", user_id="alice")
m.add("Pizza is my favorite food", user_id="alice")
m.add("I enjoy reading science fiction books", user_id="alice")
# Search with reranking
results = m.search(
"What outdoor activities do I enjoy?",
user_id="alice",
rerank=True
)
for result in results["results"]:
print(f"Memory: {result['memory']}")
print(f"Score: {result['score']:.3f}")
```
### Batch Processing
```python
# Process multiple queries efficiently
queries = [
"What are my hobbies?",
"What food do I like?",
"What books interest me?"
]
results = []
for query in queries:
result = m.search(query, user_id="alice", rerank=True)
results.append(result)
```
## Performance Optimization
### GPU Acceleration
```python
# Use GPU for better performance
config = {
"reranker": {
"provider": "huggingface",
"config": {
"model": "BAAI/bge-reranker-base",
"device": "cuda",
"batch_size": 64, # Increase batch size for GPU
}
}
}
```
### Memory Optimization
```python
# For limited memory environments
config = {
"reranker": {
"provider": "huggingface",
"config": {
"model": "BAAI/bge-reranker-base",
"device": "cpu",
"batch_size": 8, # Smaller batch size
"max_length": 256, # Shorter sequences
"model_kwargs": {
"torch_dtype": "float16" # Half precision
}
}
}
}
```
## Model Comparison
| Model | Size | Quality | Speed | Memory | Best For |
|-------|------|---------|-------|---------|----------|
| bge-reranker-base | 278M | Good | Fast | Low | General use |
| bge-reranker-large | 560M | Better | Medium | Medium | High quality needs |
| bge-reranker-v2-m3 | 568M | Best | Medium | Medium | Latest improvements |
| bge-reranker-v2-multilingual | 568M | Good | Medium | Medium | Multiple languages |
## Error Handling
```python
try:
results = m.search(
"test query",
user_id="alice",
rerank=True
)
except Exception as e:
print(f"Reranking failed: {e}")
# Fall back to vector search only
results = m.search(
"test query",
user_id="alice",
rerank=False
)
```
## Custom Models
### Using Private Models
```python
# Use a private model from Hugging Face
config = {
"reranker": {
"provider": "huggingface",
"config": {
"model": "your-org/custom-reranker",
"device": "cuda",
"use_auth_token": "your-hf-token"
}
}
}
```
### Local Model Path
```python
# Use a locally downloaded model
config = {
"reranker": {
"provider": "huggingface",
"config": {
"model": "/path/to/local/model",
"device": "cuda"
}
}
}
```
## Best Practices
1. **Choose the Right Model**: Balance quality vs speed based on your needs
2. **Use GPU**: Significantly faster than CPU for larger models
3. **Optimize Batch Size**: Tune based on your hardware capabilities
4. **Monitor Memory**: Watch GPU/CPU memory usage with large models
5. **Cache Models**: Download once and reuse to avoid repeated downloads
## Troubleshooting
### Common Issues
**Out of Memory Error**
```python
# Reduce batch size and sequence length
config = {
"reranker": {
"provider": "huggingface",
"config": {
"model": "BAAI/bge-reranker-base",
"batch_size": 4,
"max_length": 256
}
}
}
```
**Model Download Issues**
```python
# Set cache directory
import os
os.environ["TRANSFORMERS_CACHE"] = "/path/to/cache"
# Or use offline mode
config = {
"reranker": {
"provider": "huggingface",
"config": {
"model": "BAAI/bge-reranker-base",
"local_files_only": True
}
}
}
```
**CUDA Not Available**
```python
import torch
config = {
"reranker": {
"provider": "huggingface",
"config": {
"model": "BAAI/bge-reranker-base",
"device": "cuda" if torch.cuda.is_available() else "cpu"
}
}
}
```
## Next Steps
<CardGroup cols={2}>
<Card title="Reranker Overview" icon="sort" href="/components/rerankers/overview">
Learn about reranking concepts
</Card>
<Card title="Configuration Guide" icon="gear" href="/components/rerankers/config">
Detailed configuration options
</Card>
</CardGroup>
+226
View File
@@ -0,0 +1,226 @@
---
title: LLM as Reranker
description: 'Flexible reranking using LLMs'
---
<Warning>
**This page has been superseded.** Please see [LLM Reranker](/components/rerankers/models/llm_reranker) for the complete and up-to-date documentation on using LLMs for reranking.
</Warning>
LLM-based reranker provides maximum flexibility by using any Large Language Model to score document relevance. This approach allows for custom prompts and domain-specific scoring logic.
## Supported LLM Providers
Any LLM provider supported by Mem0 can be used for reranking:
- **OpenAI**: GPT-4, GPT-3.5-turbo, etc.
- **Anthropic**: Claude models
- **Together**: Open-source models
- **Groq**: Fast inference
- **Ollama**: Local models
- And more...
## Configuration
```python Python
from mem0 import Memory
config = {
"vector_store": {
"provider": "chroma",
"config": {
"collection_name": "my_memories",
"path": "./chroma_db"
}
},
"llm": {
"provider": "openai",
"config": {
"model": "gpt-4o-mini"
}
},
"reranker": {
"provider": "llm",
"config": {
"model": "gpt-4o-mini",
"provider": "openai",
"api_key": "your-openai-api-key", # or set OPENAI_API_KEY
"top_k": 5,
"temperature": 0.0
}
}
}
memory = Memory.from_config(config)
```
## Custom Scoring Prompt
You can provide a custom prompt for relevance scoring:
```python Python
custom_prompt = """You are a relevance scoring assistant. Rate how well this document answers the query.
Query: "{query}"
Document: "{document}"
Score from 0.0 to 1.0 where:
- 1.0: Perfect match, directly answers the query
- 0.8-0.9: Highly relevant, good match
- 0.6-0.7: Moderately relevant, partial match
- 0.4-0.5: Slightly relevant, limited useful information
- 0.0-0.3: Not relevant or no useful information
Provide only a single numerical score between 0.0 and 1.0."""
config["reranker"]["config"]["scoring_prompt"] = custom_prompt
```
## Usage Example
```python Python
import os
from mem0 import Memory
# Set API key
os.environ["OPENAI_API_KEY"] = "your-api-key"
# Initialize memory with LLM reranker
config = {
"vector_store": {"provider": "chroma"},
"llm": {"provider": "openai", "config": {"model": "gpt-4o-mini"}},
"reranker": {
"provider": "llm",
"config": {
"model": "gpt-4o-mini",
"provider": "openai",
"temperature": 0.0
}
}
}
memory = Memory.from_config(config)
# Add memories
messages = [
{"role": "user", "content": "I'm learning Python programming"},
{"role": "user", "content": "I find object-oriented programming challenging"},
{"role": "user", "content": "I love hiking in national parks"}
]
memory.add(messages, user_id="david")
# Search with LLM reranking
results = memory.search("What programming topics is the user studying?", user_id="david")
for result in results['results']:
print(f"Memory: {result['memory']}")
print(f"Vector Score: {result['score']:.3f}")
print(f"Rerank Score: {result['rerank_score']:.3f}")
print()
```
```text Output
Memory: I'm learning Python programming
Vector Score: 0.856
Rerank Score: 0.920
Memory: I find object-oriented programming challenging
Vector Score: 0.782
Rerank Score: 0.850
```
## Domain-Specific Scoring
Create specialized scoring for your domain:
```python Python
medical_prompt = """You are a medical relevance expert. Score how relevant this medical record is to the clinical query.
Clinical Query: "{query}"
Medical Record: "{document}"
Consider:
- Clinical relevance and accuracy
- Patient safety implications
- Diagnostic value
- Treatment relevance
Score from 0.0 to 1.0. Provide only the numerical score."""
config = {
"reranker": {
"provider": "llm",
"config": {
"model": "gpt-4o-mini",
"provider": "openai",
"scoring_prompt": medical_prompt,
"temperature": 0.0
}
}
}
```
## Multiple LLM Providers
Use different LLM providers for reranking:
```python Python
# Using Anthropic Claude
anthropic_config = {
"reranker": {
"provider": "llm",
"config": {
"model": "claude-3-haiku-20240307",
"provider": "anthropic",
"temperature": 0.0
}
}
}
# Using local Ollama model
ollama_config = {
"reranker": {
"provider": "llm",
"config": {
"model": "llama2:7b",
"provider": "ollama",
"temperature": 0.0
}
}
}
```
## Configuration Parameters
| Parameter | Description | Type | Default |
|-----------|-------------|------|---------|
| `model` | LLM model to use for scoring | `str` | `"gpt-4o-mini"` |
| `provider` | LLM provider name | `str` | `"openai"` |
| `api_key` | API key for the LLM provider | `str` | `None` |
| `top_k` | Maximum documents to return | `int` | `None` |
| `temperature` | Temperature for LLM generation | `float` | `0.0` |
| `max_tokens` | Maximum tokens for LLM response | `int` | `100` |
| `scoring_prompt` | Custom prompt template | `str` | Default prompt |
## Advantages
- **Maximum Flexibility**: Custom prompts for any use case
- **Domain Expertise**: Leverage LLM knowledge for specialized domains
- **Interpretability**: Understand scoring through prompt engineering
- **Multi-criteria**: Score based on multiple relevance factors
## Considerations
- **Latency**: Higher latency than specialized rerankers
- **Cost**: LLM API costs per reranking operation
- **Consistency**: May have slight variations in scoring
- **Prompt Engineering**: Requires careful prompt design
## Best Practices
1. **Temperature**: Use 0.0 for consistent scoring
2. **Prompt Design**: Be specific about scoring criteria
3. **Token Efficiency**: Keep prompts concise to reduce costs
4. **Caching**: Cache results for repeated queries when possible
5. **Fallback**: Handle API errors gracefully
@@ -0,0 +1,489 @@
---
title: LLM Reranker
description: 'Use any language model as a reranker with custom prompts'
---
## Overview
The LLM reranker allows you to use any supported language model as a reranker. This approach uses prompts to instruct the LLM to score and rank memories based on their relevance to the query. While slower than specialized rerankers, it offers maximum flexibility and can be fine-tuned with custom prompts.
## Configuration
### Basic Setup
```python
from mem0 import Memory
config = {
"reranker": {
"provider": "llm_reranker",
"config": {
"llm": {
"provider": "openai",
"config": {
"model": "gpt-4",
"api_key": "your-openai-api-key"
}
}
}
}
}
m = Memory.from_config(config)
```
### Configuration Parameters
| Parameter | Type | Default | Description |
|-----------|------|---------|-------------|
| `llm` | dict | Required | LLM configuration object |
| `top_k` | int | 10 | Number of results to rerank |
| `temperature` | float | 0.0 | LLM temperature for consistency |
| `custom_prompt` | str | None | Custom reranking prompt |
| `score_range` | tuple | (0, 10) | Score range for relevance |
### Advanced Configuration
```python
config = {
"reranker": {
"provider": "llm_reranker",
"config": {
"llm": {
"provider": "anthropic",
"config": {
"model": "claude-3-sonnet-20240229",
"api_key": "your-anthropic-api-key"
}
},
"top_k": 15,
"temperature": 0.0,
"score_range": (1, 5),
"custom_prompt": """
Rate the relevance of each memory to the query on a scale of 1-5.
Consider semantic similarity, context, and practical utility.
Only provide the numeric score.
"""
}
}
}
```
## Supported LLM Providers
### OpenAI
```python
config = {
"reranker": {
"provider": "llm_reranker",
"config": {
"llm": {
"provider": "openai",
"config": {
"model": "gpt-4",
"api_key": "your-openai-api-key",
"temperature": 0.0
}
}
}
}
}
```
### Anthropic
```python
config = {
"reranker": {
"provider": "llm_reranker",
"config": {
"llm": {
"provider": "anthropic",
"config": {
"model": "claude-3-sonnet-20240229",
"api_key": "your-anthropic-api-key"
}
}
}
}
}
```
### Ollama (Local)
```python
config = {
"reranker": {
"provider": "llm_reranker",
"config": {
"llm": {
"provider": "ollama",
"config": {
"model": "llama2",
"ollama_base_url": "http://localhost:11434"
}
}
}
}
}
```
### Azure OpenAI
```python
config = {
"reranker": {
"provider": "llm_reranker",
"config": {
"llm": {
"provider": "azure_openai",
"config": {
"model": "gpt-4",
"api_key": "your-azure-api-key",
"azure_endpoint": "https://your-resource.openai.azure.com/",
"azure_deployment": "gpt-4-deployment"
}
}
}
}
}
```
## Custom Prompts
### Default Prompt Behavior
The default prompt asks the LLM to score relevance on a 0-10 scale:
```
Given a query and a memory, rate how relevant the memory is to answering the query.
Score from 0 (completely irrelevant) to 10 (perfectly relevant).
Only provide the numeric score.
Query: {query}
Memory: {memory}
Score:
```
### Custom Prompt Examples
#### Domain-Specific Scoring
```python
custom_prompt = """
You are a medical information specialist. Rate how relevant each memory is for answering the medical query.
Consider clinical accuracy, specificity, and practical applicability.
Rate from 1-10 where:
- 1-3: Irrelevant or potentially harmful
- 4-6: Somewhat relevant but incomplete
- 7-8: Relevant and helpful
- 9-10: Highly relevant and clinically useful
Query: {query}
Memory: {memory}
Score:
"""
config = {
"reranker": {
"provider": "llm_reranker",
"config": {
"llm": {
"provider": "openai",
"config": {
"model": "gpt-4",
"api_key": "your-api-key"
}
},
"custom_prompt": custom_prompt
}
}
}
```
#### Contextual Relevance
```python
contextual_prompt = """
Rate how well this memory answers the specific question asked.
Consider:
- Direct relevance to the question
- Completeness of information
- Recency and accuracy
- Practical usefulness
Rate 1-5:
1 = Not relevant
2 = Slightly relevant
3 = Moderately relevant
4 = Very relevant
5 = Perfectly answers the question
Query: {query}
Memory: {memory}
Score:
"""
```
#### Conversational Context
```python
conversation_prompt = """
You are helping evaluate which memories are most useful for a conversational AI assistant.
Rate how helpful this memory would be for generating a relevant response.
Consider:
- Direct relevance to user's intent
- Emotional appropriateness
- Factual accuracy
- Conversation flow
Rate 0-10:
Query: {query}
Memory: {memory}
Score:
"""
```
## Usage Examples
### Basic Usage
```python
from mem0 import Memory
m = Memory.from_config(config)
# Add memories
m.add("I'm allergic to peanuts", user_id="alice")
m.add("I love Italian food", user_id="alice")
m.add("I'm vegetarian", user_id="alice")
# Search with LLM reranking
results = m.search(
"What foods should I avoid?",
user_id="alice",
rerank=True
)
for result in results["results"]:
print(f"Memory: {result['memory']}")
print(f"LLM Score: {result['score']:.2f}")
```
### Batch Processing with Error Handling
```python
def safe_llm_rerank_search(query, user_id, max_retries=3):
for attempt in range(max_retries):
try:
return m.search(query, user_id=user_id, rerank=True)
except Exception as e:
print(f"Attempt {attempt + 1} failed: {e}")
if attempt == max_retries - 1:
# Fall back to vector search
return m.search(query, user_id=user_id, rerank=False)
# Use the safe function
results = safe_llm_rerank_search("What are my preferences?", "alice")
```
## Performance Considerations
### Speed vs Quality Trade-offs
| Model Type | Speed | Quality | Cost | Best For |
|------------|-------|---------|------|----------|
| GPT-3.5 Turbo | Fast | Good | Low | High-volume applications |
| GPT-4 | Medium | Excellent | Medium | Quality-critical applications |
| Claude 3 Sonnet | Medium | Excellent | Medium | Balanced performance |
| Ollama Local | Variable | Good | Free | Privacy-sensitive applications |
### Optimization Strategies
```python
# Fast configuration for high-volume use
fast_config = {
"reranker": {
"provider": "llm_reranker",
"config": {
"llm": {
"provider": "openai",
"config": {
"model": "gpt-3.5-turbo",
"api_key": "your-api-key"
}
},
"top_k": 5, # Limit candidates
"temperature": 0.0
}
}
}
# High-quality configuration
quality_config = {
"reranker": {
"provider": "llm_reranker",
"config": {
"llm": {
"provider": "openai",
"config": {
"model": "gpt-4",
"api_key": "your-api-key"
}
},
"top_k": 15,
"temperature": 0.0
}
}
}
```
## Advanced Use Cases
### Multi-Step Reasoning
```python
reasoning_prompt = """
Evaluate this memory's relevance using multi-step reasoning:
1. What is the main intent of the query?
2. What key information does the memory contain?
3. How directly does the memory address the query?
4. What additional context might be needed?
Based on this analysis, rate relevance 1-10:
Query: {query}
Memory: {memory}
Analysis:
Step 1 (Intent):
Step 2 (Information):
Step 3 (Directness):
Step 4 (Context):
Final Score:
"""
```
### Comparative Ranking
```python
comparative_prompt = """
You will see a query and multiple memories. Rank them in order of relevance.
Consider which memories best answer the question and would be most helpful.
Query: {query}
Memories to rank:
{memories}
Provide scores 1-10 for each memory, considering their relative usefulness.
"""
```
### Emotional Intelligence
```python
emotional_prompt = """
Consider both factual relevance and emotional appropriateness.
Rate how suitable this memory is for responding to the user's query.
Factors to consider:
- Factual accuracy and relevance
- Emotional tone and sensitivity
- User's likely emotional state
- Appropriateness of response
Query: {query}
Memory: {memory}
Emotional Context: {context}
Score (1-10):
"""
```
## Error Handling and Fallbacks
```python
class RobustLLMReranker:
def __init__(self, primary_config, fallback_config=None):
self.primary = Memory.from_config(primary_config)
self.fallback = Memory.from_config(fallback_config) if fallback_config else None
def search(self, query, user_id, max_retries=2):
# Try primary LLM reranker
for attempt in range(max_retries):
try:
return self.primary.search(query, user_id=user_id, rerank=True)
except Exception as e:
print(f"Primary reranker attempt {attempt + 1} failed: {e}")
# Try fallback reranker
if self.fallback:
try:
return self.fallback.search(query, user_id=user_id, rerank=True)
except Exception as e:
print(f"Fallback reranker failed: {e}")
# Final fallback: vector search only
return self.primary.search(query, user_id=user_id, rerank=False)
# Usage
primary_config = {
"reranker": {
"provider": "llm_reranker",
"config": {"llm": {"provider": "openai", "config": {"model": "gpt-4"}}}
}
}
fallback_config = {
"reranker": {
"provider": "llm_reranker",
"config": {"llm": {"provider": "openai", "config": {"model": "gpt-3.5-turbo"}}}
}
}
reranker = RobustLLMReranker(primary_config, fallback_config)
results = reranker.search("What are my preferences?", "alice")
```
## Best Practices
1. **Use Specific Prompts**: Tailor prompts to your domain and use case
2. **Set Temperature to 0**: Ensure consistent scoring across runs
3. **Limit Top-K**: Don't rerank too many candidates to control costs
4. **Implement Fallbacks**: Always have a backup plan for API failures
5. **Monitor Costs**: Track API usage, especially with expensive models
6. **Cache Results**: Consider caching reranking results for repeated queries
7. **Test Prompts**: Experiment with different prompts to find what works best
## Troubleshooting
### Common Issues
**Inconsistent Scores**
- Set temperature to 0.0
- Use more specific prompts
- Consider using multiple calls and averaging
**API Rate Limits**
- Implement exponential backoff
- Use cheaper models for high-volume scenarios
- Add retry logic with delays
**Poor Ranking Quality**
- Refine your custom prompt
- Try different LLM models
- Add examples to your prompt
## Next Steps
<CardGroup cols={2}>
<Card title="Custom Prompts Guide" icon="pencil" href="/components/rerankers/custom-prompts">
Learn to craft effective reranking prompts
</Card>
<Card title="Performance Optimization" icon="bolt" href="/components/rerankers/optimization">
Optimize LLM reranker performance
</Card>
</CardGroup>
@@ -0,0 +1,159 @@
---
title: Sentence Transformer
description: 'Local reranking with HuggingFace cross-encoder models'
---
Sentence Transformer reranker provides local reranking using HuggingFace cross-encoder models, perfect for privacy-focused deployments where you want to keep data on-premises.
## Models
Any HuggingFace cross-encoder model can be used. Popular choices include:
- **`cross-encoder/ms-marco-MiniLM-L-6-v2`**: Default, good balance of speed and accuracy
- **`cross-encoder/ms-marco-TinyBERT-L-2-v2`**: Fastest, smaller model size
- **`cross-encoder/ms-marco-electra-base`**: Higher accuracy, larger model
- **`cross-encoder/stsb-distilroberta-base`**: Good for semantic similarity tasks
## Installation
```bash
pip install sentence-transformers
```
## Configuration
```python Python
from mem0 import Memory
config = {
"vector_store": {
"provider": "chroma",
"config": {
"collection_name": "my_memories",
"path": "./chroma_db"
}
},
"llm": {
"provider": "openai",
"config": {
"model": "gpt-4o-mini"
}
},
"rerank": {
"provider": "sentence_transformer",
"config": {
"model": "cross-encoder/ms-marco-MiniLM-L-6-v2",
"device": "cpu", # or "cuda" for GPU
"batch_size": 32,
"show_progress_bar": False,
"top_k": 5
}
}
}
memory = Memory.from_config(config)
```
## GPU Acceleration
For better performance, use GPU acceleration:
```python Python
config = {
"rerank": {
"provider": "sentence_transformer",
"config": {
"model": "cross-encoder/ms-marco-MiniLM-L-6-v2",
"device": "cuda", # Use GPU
"batch_size": 64 # high batch size for high memory GPUs
}
}
}
```
## Usage Example
```python Python
from mem0 import Memory
# Initialize memory with local reranker
config = {
"vector_store": {"provider": "chroma"},
"llm": {"provider": "openai", "config": {"model": "gpt-4o-mini"}},
"rerank": {
"provider": "sentence_transformer",
"config": {
"model": "cross-encoder/ms-marco-MiniLM-L-6-v2",
"device": "cpu"
}
}
}
memory = Memory.from_config(config)
# Add memories
messages = [
{"role": "user", "content": "I love reading science fiction novels"},
{"role": "user", "content": "My favorite author is Isaac Asimov"},
{"role": "user", "content": "I also enjoy watching sci-fi movies"}
]
memory.add(messages, user_id="charlie")
# Search with local reranking
results = memory.search("What books does the user like?", user_id="charlie")
for result in results['results']:
print(f"Memory: {result['memory']}")
print(f"Vector Score: {result['score']:.3f}")
print(f"Rerank Score: {result['rerank_score']:.3f}")
print()
```
## Custom Models
You can use any HuggingFace cross-encoder model:
```python Python
# Using a different model
config = {
"rerank": {
"provider": "sentence_transformer",
"config": {
"model": "cross-encoder/stsb-distilroberta-base",
"device": "cpu"
}
}
}
```
## Configuration Parameters
| Parameter | Description | Type | Default |
|-----------|-------------|------|---------|
| `model` | HuggingFace cross-encoder model name | `str` | `"cross-encoder/ms-marco-MiniLM-L-6-v2"` |
| `device` | Device to run model on (`cpu`, `cuda`, etc.) | `str` | `None` |
| `batch_size` | Batch size for processing documents | `int` | `32` |
| `show_progress_bar` | Show progress bar during processing | `bool` | `False` |
| `top_k` | Maximum documents to return | `int` | `None` |
## Advantages
- **Privacy**: Complete local processing, no external API calls
- **Cost**: No per-token charges after initial model download
- **Customization**: Use any HuggingFace cross-encoder model
- **Offline**: Works without internet connection after model download
## Performance Considerations
- **First Run**: Model download may take time initially
- **Memory Usage**: Models require GPU/CPU memory
- **Batch Size**: Optimize batch size based on available memory
- **Device**: GPU acceleration significantly improves speed
## Best Practices
1. **Model Selection**: Choose model based on accuracy vs speed requirements
2. **Device Management**: Use GPU when available for better performance
3. **Batch Processing**: Process multiple documents together for efficiency
4. **Memory Monitoring**: Monitor system memory usage with larger models
@@ -0,0 +1,117 @@
---
title: Zero Entropy
description: 'Neural reranking with Zero Entropy'
---
[Zero Entropy](https://www.zeroentropy.dev) provides neural reranking models that significantly improve search relevance with fast performance.
## Models
Zero Entropy offers two reranking models:
- **`zerank-1`**: Flagship state-of-the-art reranker (non-commercial license)
- **`zerank-1-small`**: Open-source model (Apache 2.0 license)
## Installation
```bash
pip install zeroentropy
```
## Configuration
```python Python
from mem0 import Memory
config = {
"vector_store": {
"provider": "chroma",
"config": {
"collection_name": "my_memories",
"path": "./chroma_db"
}
},
"llm": {
"provider": "openai",
"config": {
"model": "gpt-4o-mini"
}
},
"rerank": {
"provider": "zero_entropy",
"config": {
"model": "zerank-1", # or "zerank-1-small"
"api_key": "your-zero-entropy-api-key", # or set ZERO_ENTROPY_API_KEY
"top_k": 5
}
}
}
memory = Memory.from_config(config)
```
## Environment Variables
Set your API key as an environment variable:
```bash
export ZERO_ENTROPY_API_KEY="your-api-key"
```
## Usage Example
```python Python
import os
from mem0 import Memory
# Set API key
os.environ["ZERO_ENTROPY_API_KEY"] = "your-api-key"
# Initialize memory with Zero Entropy reranker
config = {
"vector_store": {"provider": "chroma"},
"llm": {"provider": "openai", "config": {"model": "gpt-4o-mini"}},
"rerank": {"provider": "zero_entropy", "config": {"model": "zerank-1"}}
}
memory = Memory.from_config(config)
# Add memories
messages = [
{"role": "user", "content": "I love Italian pasta, especially carbonara"},
{"role": "user", "content": "Japanese sushi is also amazing"},
{"role": "user", "content": "I enjoy cooking Mediterranean dishes"}
]
memory.add(messages, user_id="alice")
# Search with reranking
results = memory.search("What Italian food does the user like?", user_id="alice")
for result in results['results']:
print(f"Memory: {result['memory']}")
print(f"Vector Score: {result['score']:.3f}")
print(f"Rerank Score: {result['rerank_score']:.3f}")
print()
```
## Configuration Parameters
| Parameter | Description | Type | Default |
|-----------|-------------|------|---------|
| `model` | Model to use: `"zerank-1"` or `"zerank-1-small"` | `str` | `"zerank-1"` |
| `api_key` | Zero Entropy API key | `str` | `None` |
| `top_k` | Maximum documents to return after reranking | `int` | `None` |
## Performance
- **Fast**: Optimized neural architecture for low latency
- **Accurate**: State-of-the-art relevance scoring
- **Cost-effective**: ~$0.025/1M tokens processed
## Best Practices
1. **Model Selection**: Use `zerank-1` for best quality, `zerank-1-small` for faster processing
2. **Batch Size**: Process multiple queries together when possible
3. **Top-k Limiting**: Set reasonable `top_k` values (5-20) for best performance
4. **API Key Management**: Use environment variables for secure key storage
+310
View File
@@ -0,0 +1,310 @@
---
title: Performance Optimization
---
Optimizing reranker performance is crucial for maintaining fast search response times while improving result quality. This guide covers best practices for different reranker types.
## General Optimization Principles
### Candidate Set Size
The number of candidates sent to the reranker significantly impacts performance:
```python
# Optimal candidate sizes for different rerankers
config_map = {
"cohere": {"initial_candidates": 100, "top_n": 10},
"sentence_transformer": {"initial_candidates": 50, "top_n": 10},
"huggingface": {"initial_candidates": 30, "top_n": 5},
"llm_reranker": {"initial_candidates": 20, "top_n": 5}
}
```
### Batching Strategy
Process multiple queries efficiently:
```python
# Configure for batch processing
config = {
"reranker": {
"provider": "sentence_transformer",
"config": {
"model": "cross-encoder/ms-marco-MiniLM-L-6-v2",
"batch_size": 16, # Process multiple candidates at once
"top_n": 10
}
}
}
```
## Provider-Specific Optimizations
### Cohere Optimization
```python
# Optimized Cohere configuration
config = {
"reranker": {
"provider": "cohere",
"config": {
"model": "rerank-english-v3.0",
"top_n": 10,
"max_chunks_per_doc": 10, # Limit chunk processing
"return_documents": False # Reduce response size
}
}
}
```
**Best Practices:**
- Use v3.0 models for better speed/accuracy balance
- Limit candidates to 100 or fewer
- Cache API responses when possible
- Monitor API rate limits
### Sentence Transformer Optimization
```python
# Performance-optimized configuration
config = {
"reranker": {
"provider": "sentence_transformer",
"config": {
"model": "cross-encoder/ms-marco-MiniLM-L-6-v2",
"device": "cuda", # Use GPU when available
"batch_size": 32,
"top_n": 10,
"max_length": 512 # Limit input length
}
}
}
```
**Device Optimization:**
```python
import torch
# Auto-detect best device
device = "cuda" if torch.cuda.is_available() else "mps" if torch.backends.mps.is_available() else "cpu"
config = {
"reranker": {
"provider": "sentence_transformer",
"config": {
"device": device,
"model": "cross-encoder/ms-marco-MiniLM-L-6-v2"
}
}
}
```
### Hugging Face Optimization
```python
# Optimized for Hugging Face models
config = {
"reranker": {
"provider": "huggingface",
"config": {
"model": "BAAI/bge-reranker-base",
"use_fp16": True, # Half precision for speed
"max_length": 512,
"batch_size": 8,
"top_n": 10
}
}
}
```
### LLM Reranker Optimization
```python
# Optimized LLM reranker configuration
config = {
"reranker": {
"provider": "llm_reranker",
"config": {
"llm": {
"provider": "openai",
"config": {
"model": "gpt-3.5-turbo", # Faster than gpt-4
"temperature": 0, # Deterministic results
"max_tokens": 500 # Limit response length
}
},
"batch_ranking": True, # Rank multiple at once
"top_n": 5, # Fewer results for faster processing
"timeout": 10 # Request timeout
}
}
}
```
## Performance Monitoring
### Latency Tracking
```python
import time
from mem0 import Memory
def measure_reranker_performance(config, queries, user_id):
memory = Memory.from_config(config)
latencies = []
for query in queries:
start_time = time.time()
results = memory.search(query, user_id=user_id)
latency = time.time() - start_time
latencies.append(latency)
return {
"avg_latency": sum(latencies) / len(latencies),
"max_latency": max(latencies),
"min_latency": min(latencies)
}
```
### Memory Usage Monitoring
```python
import psutil
import os
def monitor_memory_usage():
process = psutil.Process(os.getpid())
return {
"memory_mb": process.memory_info().rss / 1024 / 1024,
"memory_percent": process.memory_percent()
}
```
## Caching Strategies
### Result Caching
```python
from functools import lru_cache
import hashlib
class CachedReranker:
def __init__(self, config):
self.memory = Memory.from_config(config)
self.cache_size = 1000
@lru_cache(maxsize=1000)
def search_cached(self, query_hash, user_id):
return self.memory.search(query, user_id=user_id)
def search(self, query, user_id):
query_hash = hashlib.md5(f"{query}_{user_id}".encode()).hexdigest()
return self.search_cached(query_hash, user_id)
```
### Model Caching
```python
# Pre-load models to avoid initialization overhead
config = {
"reranker": {
"provider": "sentence_transformer",
"config": {
"model": "cross-encoder/ms-marco-MiniLM-L-6-v2",
"cache_folder": "/path/to/model/cache",
"device": "cuda"
}
}
}
```
## Parallel Processing
### Async Configuration
```python
import asyncio
from mem0 import Memory
async def parallel_search(config, queries, user_id):
memory = Memory.from_config(config)
# Process multiple queries concurrently
tasks = [
memory.search_async(query, user_id=user_id)
for query in queries
]
results = await asyncio.gather(*tasks)
return results
```
## Hardware Optimization
### GPU Configuration
```python
# Optimize for GPU usage
import torch
if torch.cuda.is_available():
torch.cuda.set_per_process_memory_fraction(0.8) # Reserve GPU memory
config = {
"reranker": {
"provider": "sentence_transformer",
"config": {
"device": "cuda",
"model": "cross-encoder/ms-marco-electra-base",
"batch_size": 64, # Larger batch for GPU
"fp16": True # Half precision
}
}
}
```
### CPU Optimization
```python
import torch
# Optimize CPU threading
torch.set_num_threads(4) # Adjust based on your CPU
config = {
"reranker": {
"provider": "sentence_transformer",
"config": {
"device": "cpu",
"model": "cross-encoder/ms-marco-MiniLM-L-6-v2",
"num_workers": 4 # Parallel processing
}
}
}
```
## Benchmarking Different Configurations
```python
def benchmark_rerankers():
configs = [
{"provider": "cohere", "model": "rerank-english-v3.0"},
{"provider": "sentence_transformer", "model": "cross-encoder/ms-marco-MiniLM-L-6-v2"},
{"provider": "huggingface", "model": "BAAI/bge-reranker-base"}
]
test_queries = ["sample query 1", "sample query 2", "sample query 3"]
results = {}
for config in configs:
provider = config["provider"]
performance = measure_reranker_performance(
{"reranker": {"provider": provider, "config": config}},
test_queries,
"test_user"
)
results[provider] = performance
return results
```
## Production Best Practices
1. **Model Selection**: Choose the right balance of speed vs. accuracy
2. **Resource Allocation**: Monitor CPU/GPU usage and memory consumption
3. **Error Handling**: Implement fallbacks for reranker failures
4. **Load Balancing**: Distribute reranking load across multiple instances
5. **Monitoring**: Track latency, throughput, and error rates
6. **Caching**: Cache frequent queries and model predictions
7. **Batch Processing**: Group similar queries for efficient processing
+78
View File
@@ -0,0 +1,78 @@
---
title: Overview
description: 'Pick the right reranker path to boost Mem0 search relevance.'
---
Mem0 rerankers rescore vector search hits so your agents surface the most relevant memories. Use this hub to decide when reranking helps, configure a provider, and fine-tune performance.
<Info>
Reranking trades extra latency for better precision. Start once you have baseline search working and measure before/after relevance.
</Info>
<CardGroup cols={3}>
<Card
title="Understand Reranking"
description="See how reranker-enhanced search changes your retrieval flow."
icon="search"
href="/open-source/features/reranker-search"
/>
<Card
title="Configure Providers"
description="Add reranker blocks to your memory configuration."
icon="settings"
href="/components/rerankers/config"
/>
<Card
title="Optimize Performance"
description="Balance relevance, latency, and cost with tuning tactics."
icon="speedometer"
href="/components/rerankers/optimization"
/>
<Card
title="Custom Prompts"
description="Shape LLM-based reranking with tailored instructions."
icon="code"
href="/components/rerankers/custom-prompts"
/>
<Card
title="Zero Entropy Guide"
description="Adopt the managed neural reranker for production workloads."
icon="sparkles"
href="/components/rerankers/models/zero_entropy"
/>
<Card
title="Sentence Transformers"
description="Keep reranking on-device with cross-encoder models."
icon="cpu"
href="/components/rerankers/models/sentence_transformer"
/>
</CardGroup>
## Picking the Right Reranker
- **API-first** when you need top quality and can absorb request costs (Cohere, Zero Entropy).
- **Self-hosted** for privacy-sensitive deployments that must stay on your hardware (Sentence Transformer, Hugging Face).
- **LLM-driven** when you need bespoke scoring logic or complex prompts.
- **Hybrid** by enabling reranking only on premium journeys to control spend.
## Implementation Checklist
1. Confirm baseline search KPIs so you can measure uplift.
2. Select a provider and add the `reranker` block to your config.
3. Test latency impact with production-like query batches.
4. Decide whether to enable reranking globally or per-search via the `rerank` flag.
<CardGroup cols={2}>
<Card
title="Set Up Reranking"
description="Walk through the configuration fields and defaults."
icon="settings"
href="/components/rerankers/config"
/>
<Card
title="Example: Reranker Search"
description="Follow the feature guide to see reranking in action."
icon="rocket"
href="/open-source/features/reranker-search"
/>
</CardGroup>
+1 -3
View File
@@ -1,14 +1,12 @@
---
title: Configurations
icon: "gear"
iconType: "solid"
---
## How to define configurations?
The `config` is defined as an object with two main keys:
- `vector_store`: Specifies the vector database provider and its configuration
- `provider`: The name of the vector database (e.g., "chroma", "pgvector", "qdrant", "milvus", "upstash_vector", "azure_ai_search", "vertex_ai_vector_search")
- `provider`: The name of the vector database (e.g., "chroma", "pgvector", "qdrant", "milvus", "upstash_vector", "azure_ai_search", "vertex_ai_vector_search", "valkey")
- `config`: A nested dictionary containing provider-specific settings
+6 -6
View File
@@ -27,7 +27,7 @@ config = {
m = Memory.from_config(config)
messages = [
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
{"role": "assistant", "content": "How about thriller movies? They can be quite engaging."},
{"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."},
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
]
@@ -107,13 +107,13 @@ To enable Role-Based Access Control (RBAC) for Azure AI Search, follow these ste
- Click **Add** > **Add role assignment**.
6. **Choose Role:**
- Mem0 requires the **Search Index Data Contributor** and **Search Service Contributor** role.
7. **Choose Member**
- To assign to a User, Group, Service Principle or Managed Identity:
7. **Choose Member**
- To assign to a User, Group, Service Principal or Managed Identity:
- For production it is recommended to use a service principal or managed identity.
- For a service principal: select **User, group, or service principal** and search for the service principal.
- For a managed identity: select **Managed identity** and choose the managed identity.
- For development, you can assign the role to a user account.
- For development: select ***User, group, or service principal** and pick a Azure Entra ID account (the same used with `az login`).
- For development: select **User, group, or service principal** and pick an Azure Entra ID account (the same used with `az login`).
8. **Complete the Assignment:**
- Click **Review + Assign**.
@@ -133,7 +133,7 @@ config = {
}
```
### Environment Variables to set to use Azure Identity Credential:
### Environment Variables to Use Azure Identity Credential
* For an Environment Credential, you will need to setup a Service Principal and set the following environment variables:
- `AZURE_TENANT_ID`: Your Azure Active Directory tenant ID.
- `AZURE_CLIENT_ID`: The client ID of your service principal or managed identity.
@@ -142,7 +142,7 @@ config = {
- `AZURE_CLIENT_ID`: The client ID of the user-assigned managed identity.
* For a System-Assigned Managed Identity, no additional environment variables are needed.
### Developer logins to use for a Azure Identity Credential:
### Developer Logins for Azure Identity Credential
* For an Azure CLI Credential, you need to have the Azure CLI installed and logged in with `az login`.
* For an Azure PowerShell Credential, you need to have the Azure PowerShell module installed and logged in with `Connect-AzAccount`.
* For an Azure Developer CLI Credential, you need to have the Azure Developer CLI installed and logged in with `azd auth login`.
@@ -0,0 +1,128 @@
---
title: Azure MySQL
---
[Azure Database for MySQL](https://azure.microsoft.com/products/mysql) is a fully managed relational database service that provides enterprise-grade reliability and security. It supports JSON-based vector storage for semantic search capabilities in AI applications.
### Usage
```python
import os
from mem0 import Memory
os.environ["OPENAI_API_KEY"] = "sk-xx"
config = {
"vector_store": {
"provider": "azure_mysql",
"config": {
"host": "your-server.mysql.database.azure.com",
"port": 3306,
"user": "your_username",
"password": "your_password",
"database": "mem0_db",
"collection_name": "memories",
}
}
}
m = Memory.from_config(config)
messages = [
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
{"role": "assistant", "content": "How about thriller movies? They can be quite engaging."},
{"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."},
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
]
m.add(messages, user_id="alice", metadata={"category": "movies"})
```
#### Using Azure Managed Identity
For production deployments, use Azure Managed Identity instead of passwords:
```python
config = {
"vector_store": {
"provider": "azure_mysql",
"config": {
"host": "your-server.mysql.database.azure.com",
"user": "your_username",
"database": "mem0_db",
"collection_name": "memories",
"use_azure_credential": True, # Uses DefaultAzureCredential
"ssl_disabled": False
}
}
}
```
<Note>
When `use_azure_credential` is enabled, the password is obtained via Azure DefaultAzureCredential (supports Managed Identity, Azure CLI, etc.)
</Note>
### Config
Here are the parameters available for configuring Azure MySQL:
| Parameter | Description | Default Value |
| --- | --- | --- |
| `host` | MySQL server hostname | Required |
| `port` | MySQL server port | `3306` |
| `user` | Database user | Required |
| `password` | Database password (optional with Azure credential) | `None` |
| `database` | Database name | Required |
| `collection_name` | Table name for storing vectors | `"mem0"` |
| `embedding_model_dims` | Dimensions of embedding vectors | `1536` |
| `use_azure_credential` | Use Azure DefaultAzureCredential | `False` |
| `ssl_ca` | Path to SSL CA certificate | `None` |
| `ssl_disabled` | Disable SSL (not recommended) | `False` |
| `minconn` | Minimum connections in pool | `1` |
| `maxconn` | Maximum connections in pool | `5` |
### Setup
#### Create MySQL Flexible Server using Azure CLI:
```bash
# Create resource group
az group create --name mem0-rg --location eastus
# Create MySQL Flexible Server
az mysql flexible-server create \
--resource-group mem0-rg \
--name mem0-mysql-server \
--location eastus \
--admin-user myadmin \
--admin-password <YourPassword> \
--version 8.0.21
# Create database
az mysql flexible-server db create \
--resource-group mem0-rg \
--server-name mem0-mysql-server \
--database-name mem0_db
# Configure firewall
az mysql flexible-server firewall-rule create \
--resource-group mem0-rg \
--name mem0-mysql-server \
--rule-name AllowMyIP \
--start-ip-address <YourIP> \
--end-ip-address <YourIP>
```
#### Enable Azure AD Authentication:
1. In Azure Portal, navigate to your MySQL Flexible Server
2. Go to **Security** > **Authentication** and enable Azure AD
3. Add your application's managed identity as a MySQL user:
```sql
CREATE AADUSER 'your-app-identity' IDENTIFIED BY 'your-client-id';
GRANT ALL PRIVILEGES ON mem0_db.* TO 'your-app-identity'@'%';
FLUSH PRIVILEGES;
```
<Tip>
For production, use [Managed Identity](https://learn.microsoft.com/azure/active-directory/managed-identities-azure-resources/) to eliminate password management.
</Tip>
+1 -1
View File
@@ -37,7 +37,7 @@ m.add(messages, user_id="alice", metadata={"category": "movies"})
### Config
Here are the available parameters for the `mochow` config:
Here are the parameters available for configuring Baidu VectorDB:
| Parameter | Description | Default Value |
| --- | --- | --- |
+181
View File
@@ -0,0 +1,181 @@
---
title: Apache Cassandra
---
[Apache Cassandra](https://cassandra.apache.org/) is a highly scalable, distributed NoSQL database designed for handling large amounts of data across many commodity servers with no single point of failure. It supports vector storage for semantic search capabilities in AI applications and can scale to massive datasets with linear performance improvements.
### Usage
```python
import os
from mem0 import Memory
os.environ["OPENAI_API_KEY"] = "sk-xx"
config = {
"vector_store": {
"provider": "cassandra",
"config": {
"contact_points": ["127.0.0.1"],
"port": 9042,
"username": "cassandra",
"password": "cassandra",
"keyspace": "mem0",
"collection_name": "memories",
}
}
}
m = Memory.from_config(config)
messages = [
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
{"role": "assistant", "content": "How about thriller movies? They can be quite engaging."},
{"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."},
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
]
m.add(messages, user_id="alice", metadata={"category": "movies"})
```
#### Using DataStax Astra DB
For managed Cassandra with DataStax Astra DB:
```python
config = {
"vector_store": {
"provider": "cassandra",
"config": {
"contact_points": ["dummy"], # Not used with secure connect bundle
"username": "token",
"password": "AstraCS:...", # Your Astra DB application token
"keyspace": "mem0",
"collection_name": "memories",
"secure_connect_bundle": "/path/to/secure-connect-bundle.zip"
}
}
}
```
<Note>
When using DataStax Astra DB, provide the secure connect bundle path. The contact_points parameter is ignored when a secure connect bundle is provided.
</Note>
### Config
Here are the parameters available for configuring Apache Cassandra:
| Parameter | Description | Default Value |
| --- | --- | --- |
| `contact_points` | List of contact point IP addresses | Required |
| `port` | Cassandra port | `9042` |
| `username` | Database username | `None` |
| `password` | Database password | `None` |
| `keyspace` | Keyspace name | `"mem0"` |
| `collection_name` | Table name for storing vectors | `"memories"` |
| `embedding_model_dims` | Dimensions of embedding vectors | `1536` |
| `secure_connect_bundle` | Path to Astra DB secure connect bundle | `None` |
| `protocol_version` | CQL protocol version | `4` |
| `load_balancing_policy` | Custom load balancing policy | `None` |
### Setup
#### Option 1: Local Cassandra Setup using Docker:
```bash
# Pull and run Cassandra container
docker run --name mem0-cassandra \
-p 9042:9042 \
-e CASSANDRA_CLUSTER_NAME="Mem0Cluster" \
-d cassandra:latest
# Wait for Cassandra to start (may take 1-2 minutes)
docker exec -it mem0-cassandra cqlsh
# Create keyspace
CREATE KEYSPACE IF NOT EXISTS mem0
WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1};
```
#### Option 2: DataStax Astra DB (Managed Cloud):
1. Sign up at [DataStax Astra](https://astra.datastax.com/)
2. Create a new database
3. Download the secure connect bundle
4. Generate an application token
<Tip>
For production deployments, use DataStax Astra DB for fully managed Cassandra with automatic scaling, backups, and security.
</Tip>
#### Option 3: Install Cassandra Locally:
**Ubuntu/Debian:**
```bash
# Add Apache Cassandra repository
echo "deb https://downloads.apache.org/cassandra/debian 40x main" | sudo tee -a /etc/apt/sources.list.d/cassandra.sources.list
curl https://downloads.apache.org/cassandra/KEYS | sudo apt-key add -
# Install Cassandra
sudo apt-get update
sudo apt-get install cassandra
# Start Cassandra
sudo systemctl start cassandra
# Verify installation
nodetool status
```
**macOS:**
```bash
# Using Homebrew
brew install cassandra
# Start Cassandra
brew services start cassandra
# Connect to CQL shell
cqlsh
```
### Python Client Installation
Install the required Python package:
```bash
pip install cassandra-driver
```
### Performance Considerations
- **Replication Factor**: For production, use replication factor of at least 3
- **Consistency Level**: Balance between consistency and performance (QUORUM recommended)
- **Partitioning**: Cassandra automatically distributes data across nodes
- **Scaling**: Add nodes to linearly increase capacity and performance
### Advanced Configuration
```python
from cassandra.policies import DCAwareRoundRobinPolicy
config = {
"vector_store": {
"provider": "cassandra",
"config": {
"contact_points": ["node1.example.com", "node2.example.com", "node3.example.com"],
"port": 9042,
"username": "mem0_user",
"password": "secure_password",
"keyspace": "mem0_prod",
"collection_name": "memories",
"protocol_version": 4,
"load_balancing_policy": DCAwareRoundRobinPolicy(local_dc='DC1')
}
}
}
```
<Warning>
For production use, configure appropriate replication strategies and consistency levels based on your availability and consistency requirements.
</Warning>
+10 -3
View File
@@ -1,7 +1,9 @@
[Chroma](https://www.trychroma.com/) is an AI-native open-source vector database that simplifies building LLM apps by providing tools for storing, embedding, and searching embeddings with a focus on simplicity and speed.
[Chroma](https://www.trychroma.com/) is an AI-native open-source vector database that simplifies building LLM apps by providing tools for storing, embedding, and searching embeddings with a focus on simplicity and speed. It supports both local deployment and cloud hosting through ChromaDB Cloud.
### Usage
#### Local Installation
```python
import os
from mem0 import Memory
@@ -14,6 +16,9 @@ config = {
"config": {
"collection_name": "test",
"path": "db",
# Optional: ChromaDB Cloud configuration
# "api_key": "your-chroma-cloud-api-key",
# "tenant": "your-chroma-cloud-tenant-id",
}
}
}
@@ -21,7 +26,7 @@ config = {
m = Memory.from_config(config)
messages = [
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
{"role": "assistant", "content": "How about thriller movies? They can be quite engaging."},
{"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."},
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
]
@@ -38,4 +43,6 @@ Here are the parameters available for configuring Chroma:
| `client` | Custom client for Chroma | `None` |
| `path` | Path for the Chroma database | `db` |
| `host` | The host where the Chroma server is running | `None` |
| `port` | The port where the Chroma server is running | `None` |
| `port` | The port where the Chroma server is running | `None` |
| `api_key` | ChromaDB Cloud API key (for cloud usage) | `None` |
| `tenant` | ChromaDB Cloud tenant ID (for cloud usage) | `None` |
@@ -31,7 +31,7 @@ config = {
m = Memory.from_config(config)
messages = [
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
{"role": "assistant", "content": "How about thriller movies? They can be quite engaging."},
{"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."},
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
]
@@ -40,7 +40,7 @@ m.add(messages, user_id="alice", metadata={"category": "movies"})
### Config
Let's see the available parameters for the `elasticsearch` config:
Here are the parameters available for configuring Elasticsearch:
| Parameter | Description | Default Value |
| ---------------------- | -------------------------------------------------- | ------------- |
+1 -1
View File
@@ -22,7 +22,7 @@ config = {
m = Memory.from_config(config)
messages = [
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
{"role": "assistant", "content": "How about thriller movies? They can be quite engaging."},
{"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."},
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
]
+3 -3
View File
@@ -38,7 +38,7 @@ config = {
m = Memory.from_config(config)
messages = [
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
{"role": "assistant", "content": "How about thriller movies? They can be quite engaging."},
{"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."},
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
]
@@ -64,12 +64,12 @@ const memory = new Memory(config);
const messages = [
{ role: "user", content: "I'm planning to watch a movie tonight. Any recommendations?" },
{ role: "assistant", content: "How about a thriller movies? They can be quite engaging." },
{ role: "assistant", content: "How about thriller movies? They can be quite engaging." },
{ role: "user", content: "I'm not a big fan of thriller movies but I love sci-fi movies." },
{ role: "assistant", content: "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future." }
]
memory.add(messages, user_id="alice", metadata={"category": "movies"})
await memory.add(messages, { userId: "alice", metadata: { category: "movies" } });
```
</CodeGroup>
+4 -4
View File
@@ -1,4 +1,4 @@
[Milvus](https://milvus.io/) Milvus is an open-source vector database that suits AI applications of every size from running a demo chatbot in Jupyter notebook to building web-scale search that serves billions of users.
[Milvus](https://milvus.io/) is an open-source vector database that suits AI applications of every size, from running a demo chatbot in a Jupyter notebook to building web-scale search that serves billions of users.
### Usage
@@ -11,7 +11,7 @@ config = {
"provider": "milvus",
"config": {
"collection_name": "test",
"embedding_model_dims": "123",
"embedding_model_dims": 1536,
"url": "127.0.0.1",
"token": "8e4b8ca8cf2c67",
"db_name": "my_database",
@@ -22,7 +22,7 @@ config = {
m = Memory.from_config(config)
messages = [
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
{"role": "assistant", "content": "How about thriller movies? They can be quite engaging."},
{"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."},
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
]
@@ -31,7 +31,7 @@ m.add(messages, user_id="alice", metadata={"category": "movies"})
### Config
Here's the parameters available for configuring Milvus Database:
Here are the parameters available for configuring Milvus:
| Parameter | Description | Default Value |
| --- | --- | --- |
+3 -3
View File
@@ -24,7 +24,7 @@ config = {
m = Memory.from_config(config)
messages = [
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
{"role": "assistant", "content": "How about thriller movies? They can be quite engaging."},
{"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."},
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
]
@@ -40,6 +40,6 @@ Here are the parameters available for configuring MongoDB:
| db_name | Name of the MongoDB database | `"mem0_db"` |
| collection_name | Name of the MongoDB collection | `"mem0_collection"` |
| embedding_model_dims | Dimensions of the embedding vectors | `1536` |
| mongo_uri | The mongo URI connection string | mongodb://username:password@localhost:27017 |
| mongo_uri | The MongoDB URI connection string | `mongodb://username:password@localhost:27017` |
> **Note**: If Mongo_uri is not provided it will default to mongodb://username:password@localhost:27017.
> **Note**: If `mongo_uri` is not provided, it will default to `mongodb://username:password@localhost:27017`.
@@ -0,0 +1,42 @@
# Neptune Analytics Vector Store
[Neptune Analytics](https://docs.aws.amazon.com/neptune-analytics/latest/userguide/what-is-neptune-analytics.html/) is a memory-optimized graph database engine for analytics. With Neptune Analytics, you can get insights and find trends by processing large amounts of graph data in seconds, including vector search.
## Installation
```bash
pip install mem0ai[vector_stores]
```
## Usage
```python
config = {
"vector_store": {
"provider": "neptune",
"config": {
"collection_name": "mem0",
"endpoint": f"neptune-graph://my-graph-identifier",
},
},
}
m = Memory.from_config(config)
messages = [
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
{"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."},
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
]
m.add(messages, user_id="alice", metadata={"category": "movies"})
```
## Parameters
Let's see the available parameters for the `neptune` config:
| Parameter | Description | Default Value |
| --- | --- | --- |
| `collection_name` | The name of the collection to store the vectors | `mem0` |
| `endpoint` | Connection URL for the Neptune Analytics service | `neptune-graph://my-graph-identifier` |
+5 -5
View File
@@ -58,7 +58,7 @@ config = {
m = Memory.from_config(config)
messages = [
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
{"role": "assistant", "content": "How about thriller movies? They can be quite engaging."},
{"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."},
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
]
@@ -74,8 +74,8 @@ results = m.search("What kind of movies does Alice like?", user_id="alice")
### Features
- Fast and Efficient Vector Search
- Can be deployed on-premises, in containers, or on cloud platforms like AWS OpenSearch Service.
- Multiple Authentication and Security Methods (Basic Authentication, API Keys, LDAP, SAML, and OpenID Connect)
- Can be deployed on-premises, in containers, or on cloud platforms like AWS OpenSearch Service
- Multiple authentication and security methods (Basic Authentication, API Keys, LDAP, SAML, and OpenID Connect)
- Automatic index creation with optimized mappings for vector search
- Memory Optimization through Disk-Based Vector Search and Quantization
- Real-Time Analytics and Observability
- Memory optimization through disk-based vector search and quantization
- Real-time analytics and observability
+4 -4
View File
@@ -1,4 +1,4 @@
[pgvector](https://github.com/pgvector/pgvector) is open-source vector similarity search for Postgres. After connecting with postgres run `CREATE EXTENSION IF NOT EXISTS vector;` to create the vector extension.
[pgvector](https://github.com/pgvector/pgvector) is an open-source vector similarity search extension for Postgres. After connecting to Postgres, run `CREATE EXTENSION IF NOT EXISTS vector;` to create the vector extension.
### Usage
@@ -24,7 +24,7 @@ config = {
m = Memory.from_config(config)
messages = [
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
{"role": "assistant", "content": "How about thriller movies? They can be quite engaging."},
{"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."},
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
]
@@ -54,7 +54,7 @@ const config = {
const memory = new Memory(config);
const messages = [
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
{"role": "assistant", "content": "How about thriller movies? They can be quite engaging."},
{"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."},
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
]
@@ -64,7 +64,7 @@ await memory.add(messages, { userId: "alice", metadata: { category: "movies" } }
### Config
Here's the parameters available for configuring pgvector:
Here are the parameters available for configuring pgvector:
| Parameter | Description | Default Value |
| --- | --- | --- |
+1 -1
View File
@@ -33,7 +33,7 @@ config = {
m = Memory.from_config(config)
messages = [
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
{"role": "assistant", "content": "How about thriller movies? They can be quite engaging."},
{"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."},
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
]
+2 -2
View File
@@ -23,7 +23,7 @@ config = {
m = Memory.from_config(config)
messages = [
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
{"role": "assistant", "content": "How about thriller movies? They can be quite engaging."},
{"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."},
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
]
@@ -48,7 +48,7 @@ const config = {
const memory = new Memory(config);
const messages = [
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
{"role": "assistant", "content": "How about thriller movies? They can be quite engaging."},
{"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."},
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
]
+2 -2
View File
@@ -34,7 +34,7 @@ config = {
m = Memory.from_config(config)
messages = [
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
{"role": "assistant", "content": "How about thriller movies? They can be quite engaging."},
{"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."},
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
]
@@ -60,7 +60,7 @@ const config = {
const memory = new Memory(config);
const messages = [
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
{"role": "assistant", "content": "How about thriller movies? They can be quite engaging."},
{"role": "user", "content": "I’m not a big fan of thriller movies but I love sci-fi movies."},
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
]
+18 -18
View File
@@ -28,7 +28,7 @@ config = {
"provider": "s3_vectors",
"config": {
"vector_bucket_name": "my-mem0-vector-bucket",
"index_name": "my-memories-index",
"collection_name": "my-memories-index",
"embedding_model_dims": 1536,
"distance_metric": "cosine",
"region_name": "us-east-1"
@@ -48,15 +48,15 @@ m.add(messages, user_id="alice", metadata={"category": "movies"})
### Config
Here are the available parameters for the `s3_vectors` config:
Here are the parameters available for configuring Amazon S3 Vectors:
| Parameter | Description | Default Value |
| ---------------------- | -------------------------------------------------------------------- | ------------- |
| `vector_bucket_name` | The name of the S3 Vector bucket to use. It will be created if it doesn't exist. | Required |
| `index_name` | The name of the vector index within the bucket. | `mem0` |
| `embedding_model_dims` | Dimensions of the embedding model. Must match your embedder. | `1536` |
| `distance_metric` | Distance metric for similarity search. Options: `cosine`, `euclidean`. | `cosine` |
| `region_name` | The AWS region where the bucket and index reside. | `None` (uses default from AWS config) |
| Parameter | Description | Default Value |
| ---------------------- | -------------------------------------------------------------------------------- | ------------------------------------- |
| `vector_bucket_name` | The name of the S3 Vector bucket to use. It will be created if it doesn't exist. | Required |
| `collection_name` | The name of the vector index within the bucket. | `mem0` |
| `embedding_model_dims` | Dimensions of the embedding model. Must match your embedder. | `1536` |
| `distance_metric` | Distance metric for similarity search. Options: `cosine`, `euclidean`. | `cosine` |
| `region_name` | The AWS region where the bucket and index reside. | `None` (uses default from AWS config) |
### IAM Permissions
@@ -64,15 +64,15 @@ Your AWS identity (user or role) needs permissions to perform actions on S3 Vect
```json
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "s3vectors:*",
"Resource": "*"
}
]
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "s3vectors:*",
"Resource": "*"
}
]
}
```
For production, it is recommended to scope down the resource ARN to your specific buckets and indexes.
For production, it is recommended to scope down the resource ARN to your specific buckets and indexes.
+3 -3
View File
@@ -26,7 +26,7 @@ config = {
m = Memory.from_config(config)
messages = [
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
{"role": "assistant", "content": "How about thriller movies? They can be quite engaging."},
{"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."},
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
]
@@ -53,7 +53,7 @@ const memory = new Memory(config);
const messages = [
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
{"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
{"role": "assistant", "content": "How about thriller movies? They can be quite engaging."},
{"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."},
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
]
@@ -109,7 +109,7 @@ end;
$$;
```
Goto [Supabase](https://supabase.com/dashboard/projects) and run the above SQL migrations inside the SQL Editor.
Go to [Supabase](https://supabase.com/dashboard/projects) and run the above SQL migrations in the SQL Editor.
### Config
+49
View File
@@ -0,0 +1,49 @@
# Valkey Vector Store
[Valkey](https://valkey.io/) is an open source (BSD) high-performance key/value datastore that supports a variety of workloads and rich datastructures including vector search.
## Installation
```bash
pip install mem0ai[vector_stores]
```
## Usage
```python
config = {
"vector_store": {
"provider": "valkey",
"config": {
"collection_name": "test",
"valkey_url": "valkey://localhost:6379",
"embedding_model_dims": 1536,
"index_type": "flat"
}
}
}
m = Memory.from_config(config)
messages = [
{"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
{"role": "assistant", "content": "How about thriller movies? They can be quite engaging."},
{"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."},
{"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
]
m.add(messages, user_id="alice", metadata={"category": "movies"})
```
## Parameters
Here are the parameters available for configuring Valkey:
| Parameter | Description | Default Value |
| --- | --- | --- |
| `collection_name` | The name of the collection to store the vectors | `mem0` |
| `valkey_url` | Connection URL for the Valkey server | `valkey://localhost:6379` |
| `embedding_model_dims` | Dimensions of the embedding model | `1536` |
| `index_type` | Vector index algorithm (`hnsw` or `flat`) | `hnsw` |
| `hnsw_m` | Number of bi-directional links for HNSW | `16` |
| `hnsw_ef_construction` | Size of dynamic candidate list for HNSW | `200` |
| `hnsw_ef_runtime` | Size of dynamic candidate list for search | `10` |
| `distance_metric` | Distance metric for vector similarity | `cosine` |
+1 -1
View File
@@ -31,7 +31,7 @@ await memory.add(messages, { userId: "bob", metadata: { interest: "books" } });
### Config
Let's see the available parameters for the `vectorize` config:
Here are the parameters available for configuring Vectorize:
<Tabs>
<Tab title="TypeScript">
+1 -1
View File
@@ -37,7 +37,7 @@ m.add(messages, user_id="alice", metadata={"category": "movies"})
### Config
Let's see the available parameters for the `weaviate` config:
Here are the parameters available for configuring Weaviate:
| Parameter | Description | Default Value |
| --- | --- | --- |
+6 -9
View File
@@ -1,7 +1,5 @@
---
title: Overview
icon: "info"
iconType: "solid"
---
Mem0 includes built-in support for various popular databases. Memory can utilize the database provided by the user, ensuring efficient use for specific needs.
@@ -11,19 +9,20 @@ Mem0 includes built-in support for various popular databases. Memory can utilize
See the list of supported vector databases below.
<Note>
The following vector databases are supported in the Python implementation. The TypeScript implementation currently only supports Qdrant, Redis,Vectorize and in-memory vector database.
The following vector databases are supported in the Python implementation. The TypeScript implementation currently only supports Qdrant, Redis, Valkey, Vectorize and in-memory vector database.
</Note>
<CardGroup cols={3}>
<Card title="Qdrant" href="/components/vectordbs/dbs/qdrant"></Card>
<Card title="Chroma" href="/components/vectordbs/dbs/chroma"></Card>
<Card title="Pgvector" href="/components/vectordbs/dbs/pgvector"></Card>
<Card title="PGVector" href="/components/vectordbs/dbs/pgvector"></Card>
<Card title="Upstash Vector" href="/components/vectordbs/dbs/upstash-vector"></Card>
<Card title="Milvus" href="/components/vectordbs/dbs/milvus"></Card>
<Card title="Pinecone" href="/components/vectordbs/dbs/pinecone"></Card>
<Card title="MongoDB" href="/components/vectordbs/dbs/mongodb"></Card>
<Card title="Azure" href="/components/vectordbs/dbs/azure"></Card>
<Card title="Redis" href="/components/vectordbs/dbs/redis"></Card>
<Card title="Valkey" href="/components/vectordbs/dbs/valkey"></Card>
<Card title="Elasticsearch" href="/components/vectordbs/dbs/elasticsearch"></Card>
<Card title="OpenSearch" href="/components/vectordbs/dbs/opensearch"></Card>
<Card title="Supabase" href="/components/vectordbs/dbs/supabase"></Card>
@@ -43,12 +42,10 @@ For a comprehensive list of available parameters for vector database configurati
## Common issues
### Using model with different dimensions
### Using Model with Different Dimensions
If you are using customized model, which is having different dimensions other than 1536
for example 768, you may encounter below error:
If you are using a customized model with different dimensions other than 1536 (for example, 768), you may encounter the following error:
`ValueError: shapes (0,1536) and (768,) not aligned: 1536 (dim 1) != 768 (dim 0)`
you could add `"embedding_model_dims": 768,` to the config of the vector_store to overcome this issue.
You can add `"embedding_model_dims": 768,` to the config of the vector_store to resolve this issue.
+14 -14
View File
@@ -19,25 +19,25 @@ To contribute, follow these steps:
4. **Code Quality Checks**:
- Run **linting** to catch style issues
- Ensure **all tests pass**
5. **Submit a Pull Request** 🚀
5. **Submit a Pull Request**
For detailed guidance on pull requests, refer to [GitHub's documentation](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/creating-a-pull-request).
---
## 📦 Dependency Management
## Dependency Management
We use `hatch` as our package manager. Install it by following the [official instructions](https://hatch.pypa.io/latest/install/).
⚠️ **Do NOT use `pip` or `conda` for dependency management.** Instead, follow these steps in order:
**Do NOT use `pip` or `conda` for dependency management.** Instead, follow these steps in order:
```bash
# 1. Install base dependencies
make install
# 2. Activate virtual environment (this will install deps.)
hatch shell (for default env)
hatch -e dev_py_3_11 shell (for dev_py_3_11) (differences are mentioned in pyproject.toml)
# 2. Activate virtual environment (this will install dependencies)
hatch shell # For default environment
hatch -e dev_py_3_11 shell # For dev_py_3_11 (differences are mentioned in pyproject.toml)
# 3. Install all optional dependencies
make install_all
@@ -45,9 +45,9 @@ make install_all
---
## 🛠️ Development Standards
## Development Standards
### ✅ Pre-commit Hooks
### Pre-commit Hooks
Ensure `pre-commit` is installed before contributing:
@@ -55,7 +55,7 @@ Ensure `pre-commit` is installed before contributing:
pre-commit install
```
### 🔍 Linting with `ruff`
### Linting with `ruff`
Run the linter and fix any reported issues before submitting your PR:
@@ -63,7 +63,7 @@ Run the linter and fix any reported issues before submitting your PR:
make lint
```
### 🎨 Code Formatting
### Code Formatting
To maintain a consistent code style, format your code:
@@ -71,7 +71,7 @@ To maintain a consistent code style, format your code:
make format
```
### 🧪 Testing with `pytest`
### Testing with `pytest`
Run tests to verify functionality before submitting your PR:
@@ -79,14 +79,14 @@ Run tests to verify functionality before submitting your PR:
make test
```
💡 **Note:** Some dependencies have been removed from the main dependencies to reduce package size. Run `make install_all` to install necessary dependencies before running tests.
**Note:** Some dependencies have been removed from the main dependencies to reduce package size. Run `make install_all` to install necessary dependencies before running tests.
---
## 🚀 Release Process
## Release Process
Currently, releases are handled manually. We aim for frequent releases, typically when new features or bug fixes are introduced.
---
Thank you for contributing to Mem0! 🎉
Thank you for contributing to Mem0!
+4 -4
View File
@@ -5,13 +5,13 @@ icon: "book"
# Documentation Contributions
## 📌 Prerequisites
## Prerequisites
Before getting started, ensure you have **Node.js (version 23.6.0 or higher)** installed on your system.
---
## 🚀 Setting Up Mintlify
## Setting Up Mintlify
### Step 1: Install Mintlify
@@ -41,7 +41,7 @@ The documentation website will be available at: [http://localhost:3000](http://l
---
## 🔧 Custom Ports
## Custom Ports
By default, Mintlify runs on **port 3000**. To use a different port, add the `--port` flag:
@@ -51,5 +51,5 @@ mintlify dev --port 3333
---
By following these steps, you can efficiently contribute to **Mem0's documentation**. Happy documenting! ✍️
By following these steps, you can efficiently contribute to Mem0's documentation.
@@ -1,7 +1,9 @@
---
title: Personalized AI Tutor
description: "Keep student progress and preferences persistent across tutoring sessions."
---
You can create a personalized AI Tutor using Mem0. This guide will walk you through the necessary steps and provide the complete code to get you started.
## Overview
@@ -9,6 +11,7 @@ You can create a personalized AI Tutor using Mem0. This guide will walk you thro
The Personalized AI Tutor leverages Mem0 to retain information across interactions, enabling a tailored learning experience. By integrating with OpenAI's GPT-4 model, the tutor can provide detailed and context-aware responses to user queries.
## Setup
Before you begin, ensure you have the required dependencies installed. You can install the necessary packages using pip:
```bash
@@ -57,7 +60,7 @@ class PersonalAITutor:
"""
# Start a streaming response request to the AI
response = self.client.responses.create(
model="gpt-4o",
model="gpt-4.1-nano-2025-04-14",
instructions="You are a personal AI Tutor.",
input=question,
stream=True
@@ -100,12 +103,23 @@ for m in memories['results']:
print(m['memory'])
```
### Key Points
## Key Points
- **Initialization**: The PersonalAITutor class is initialized with the necessary memory configuration and OpenAI client setup.
- **Asking Questions**: The ask method sends a question to the AI and stores the relevant information in memory.
- **Retrieving Memories**: The get_memories method fetches all stored memories associated with a user.
- **Initialization**: The PersonalAITutor class is initialized with the necessary memory configuration and OpenAI client setup
- **Asking Questions**: The ask method sends a question to the AI and stores the relevant information in memory
- **Retrieving Memories**: The get_memories method fetches all stored memories associated with a user
### Conclusion
## Conclusion
As the conversation progresses, Mem0's memory automatically updates based on the interactions, providing a continuously improving personalized learning experience. This setup ensures that the AI Tutor can offer contextually relevant and accurate responses, enhancing the overall educational process.
---
<CardGroup cols={2}>
<Card title="Build a Mem0 Companion" icon="users" href="/cookbooks/essentials/building-ai-companion">
Learn the foundations of memory-powered companions with production-ready patterns.
</Card>
<Card title="Travel Assistant with Mem0" icon="plane" href="/cookbooks/companions/travel-assistant">
Build a travel companion that remembers preferences and past conversations.
</Card>
</CardGroup>
@@ -1,20 +1,20 @@
---
title: Mem0 with Ollama
title: Self-Hosted AI Companion
description: "Run Mem0 end-to-end on your machine using Ollama-powered LLMs and embedders."
---
## Running Mem0 Locally with Ollama
Mem0 can be utilized entirely locally by leveraging Ollama for both the embedding model and the language model (LLM). This guide will walk you through the necessary steps and provide the complete code to get you started.
### Overview
## Overview
By using Ollama, you can run Mem0 locally, which allows for greater control over your data and models. This setup uses Ollama for both the embedding model and the language model, providing a fully local solution.
### Setup
## Setup
Before you begin, ensure you have Mem0 and Ollama installed and properly configured on your local machine.
### Full Code Example
## Full Code Example
Below is the complete code to set up and use Mem0 locally with Ollama:
@@ -60,13 +60,24 @@ m.add("I'm visiting Paris", user_id="john")
memories = m.get_all(user_id="john")
```
### Key Points
## Key Points
- **Configuration**: The setup involves configuring the vector store, language model, and embedding model to use local resources.
- **Vector Store**: Qdrant is used as the vector store, running on localhost.
- **Language Model**: Ollama is used as the LLM provider, with the "llama3.1:latest" model.
- **Embedding Model**: Ollama is also used for embeddings, with the "nomic-embed-text:latest" model.
- **Configuration**: The setup involves configuring the vector store, language model, and embedding model to use local resources
- **Vector Store**: Qdrant is used as the vector store, running on localhost
- **Language Model**: Ollama is used as the LLM provider, with the `llama3.1:latest` model
- **Embedding Model**: Ollama is also used for embeddings, with the `nomic-embed-text:latest` model
### Conclusion
## Conclusion
This local setup of Mem0 using Ollama provides a fully self-contained solution for memory management and AI interactions. It allows for greater control over your data and models while still leveraging the powerful capabilities of Mem0.
This local setup of Mem0 using Ollama provides a fully self-contained solution for memory management and AI interactions. It allows for greater control over your data and models while still leveraging the powerful capabilities of Mem0.
---
<CardGroup cols={2}>
<Card title="Configure Open Source" icon="gear" href="/open-source/configuration">
Explore advanced configuration options for vector stores, LLMs, and embedders.
</Card>
<Card title="Build a Mem0 Companion" icon="users" href="/cookbooks/essentials/building-ai-companion">
Learn core companion patterns that work with any LLM provider.
</Card>
</CardGroup>
@@ -1,8 +1,10 @@
---
title: AI Companion in Node.js
title: Build a Node.js Companion
description: "Build a JavaScript fitness coach that remembers user goals run after run."
---
You can create a personalised AI Companion using Mem0. This guide will walk you through the necessary steps and provide the complete code to get you started.
You can create a personalized AI Companion using Mem0. This guide will walk you through the necessary steps and provide the complete code to get you started.
## Overview
@@ -45,7 +47,7 @@ ${memoriesStr}`;
];
const response = await openaiClient.chat.completions.create({
model: "gpt-4o-mini",
model: "gpt-4.1-nano-2025-04-14",
messages: messages
});
@@ -124,3 +126,14 @@ export OPENAI_API_KEY=your_api_key
This implementation demonstrates how to create an AI Companion that maintains context across conversations using Mem0's memory capabilities. The system automatically stores and retrieves relevant information, creating a more personalized and context-aware interaction experience.
As users interact with the system, Mem0's memory system continuously learns and adapts, making future responses more relevant and personalized. This setup is ideal for creating long-term learning AI assistants that can maintain context and provide increasingly personalized responses over time.
---
<CardGroup cols={2}>
<Card title="Partition Memories by Entity" icon="layers" href="/cookbooks/essentials/entity-partitioning-playbook">
Separate user, agent, and session context to keep your companion consistent.
</Card>
<Card title="Quickstart Demo with Mem0" icon="rocket" href="/cookbooks/companions/quickstart-demo">
Run the full showcase app to see memory-powered companions in action.
</Card>
</CardGroup>
@@ -1,7 +1,9 @@
---
title: Mem0 Demo
title: Interactive Memory Demo
description: "Spin up the showcase companion app to see Mem0 memories in action."
---
You can create a personalized AI Companion using Mem0. This guide will walk you through the necessary steps and provide the complete setup instructions to get you started.
<video
@@ -53,9 +55,9 @@ Before you begin, follow these steps to set up the demo application:
## Enhancing the Next.js Application
Once the demo is running, you can customize and enhance the Next.js application by modifying the components in the `mem0-demo` folder. Consider:
- Adding new memory features to improve contextual retention.
- Customizing the UI to better suit your application needs.
- Integrating additional APIs or third-party services to extend functionality.
- Adding new memory features to improve contextual retention
- Customizing the UI to better suit your application needs
- Integrating additional APIs or third-party services to extend functionality
## Full Code
@@ -66,3 +68,13 @@ You can find the complete source code for this demo on GitHub:
This setup demonstrates how to build an AI Companion that maintains memory across interactions using Mem0. The system continuously adapts to user interactions, making future responses more relevant and personalized. Experiment with the application and enhance it further to suit your use case!
---
<CardGroup cols={2}>
<Card title="Build a Mem0 Companion" icon="users" href="/cookbooks/essentials/building-ai-companion">
Deep dive into production patterns for fitness coaches, tutors, and assistants.
</Card>
<Card title="Node.js Companion with Mem0" icon="code" href="/cookbooks/companions/nodejs-companion">
Implement a command-line companion using the Node.js SDK.
</Card>
</CardGroup>
@@ -1,5 +1,6 @@
---
title: Personal AI Travel Assistant
title: Smart Travel Assistant
description: "Plan itineraries that remember traveler preferences across trips."
---
@@ -35,7 +36,7 @@ config = {
"llm": {
"provider": "openai",
"config": {
"model": "gpt-4o",
"model": "gpt-4.1-nano-2025-04-14",
"temperature": 0.1,
"max_tokens": 2000,
}
@@ -76,7 +77,7 @@ class PersonalTravelAssistant:
# Generate response using Responses API
response = self.client.responses.create(
model="gpt-4o",
model="gpt-4.1-nano-2025-04-14",
input=prompt
)
@@ -140,9 +141,9 @@ class PersonalTravelAssistant:
prompt = f"User input: {question}\n Previous memories: {previous_memories}"
self.messages.append({"role": "user", "content": prompt})
# Generate response using GPT-4o
# Generate response using gpt-4.1-nano
response = self.client.chat.completions.create(
model="gpt-4o",
model="gpt-4.1-nano-2025-04-14"2025-04-14",
messages=self.messages
)
answer = response.choices[0].message.content
@@ -199,4 +200,15 @@ if __name__ == "__main__":
## Conclusion
This Personalized AI Travel Assistant leverages Mem0's memory capabilities to provide context-aware responses. As you interact with it, the assistant learns and improves, offering increasingly personalized travel advice and information.
This Personalized AI Travel Assistant leverages Mem0's memory capabilities to provide context-aware responses. As you interact with it, the assistant learns and improves, offering increasingly personalized travel advice and information.
---
<CardGroup cols={2}>
<Card title="Tag and Organize Memories" icon="tag" href="/cookbooks/essentials/tagging-and-organizing-memories">
Use categories to organize travel preferences, destinations, and user context.
</Card>
<Card title="AI Tutor with Mem0" icon="graduation-cap" href="/cookbooks/companions/ai-tutor">
Build an educational companion that remembers learning progress and preferences.
</Card>
</CardGroup>
@@ -1,9 +1,8 @@
---
title: 'Mem0 with OpenAI Agents SDK for Voice'
description: 'Integrate memory capabilities into your voice agents using Mem0 and OpenAI Agents SDK'
title: Voice-First AI Companion
description: "Pair the OpenAI Agents SDK with Mem0 to build a voice assistant that remembers."
---
# Building Voice Agents with Memory using Mem0 and OpenAI Agents SDK
This guide demonstrates how to combine OpenAI's Agents SDK for voice applications with Mem0's memory capabilities to create a voice assistant that remembers user preferences and past interactions.
@@ -130,7 +129,7 @@ async def search_memories(
user_id=USER_ID,
limit=5,
threshold=0.7, # Higher threshold for more relevant results
output_format="v1.1"
)
# Format and return the results
@@ -162,7 +161,7 @@ def create_memory_voice_agent():
Use the search_memories tool when you need context from past conversations or user asks you to recall something.
""",
),
model="gpt-4o",
model="gpt-4.1-nano-2025-04-14",
tools=[save_memories, search_memories],
)
@@ -171,7 +170,7 @@ def create_memory_voice_agent():
This function:
- Creates an OpenAI Agent with specific instructions
- Configures it to use gpt-4o (you can use other models)
- Configures it to use gpt-4.1-nano (you can use other models)
- Registers the memory-related tools with the agent
- Uses `prompt_with_handoff_instructions` to include standard voice agent behaviors
@@ -346,7 +345,7 @@ async def search_memories(
user_id=USER_ID,
limit=5,
threshold=0.7, # Higher threshold for more relevant results
output_format="v1.1"
)
# Format and return the results
@@ -369,7 +368,7 @@ def create_memory_voice_agent():
Use the search_memories tool when you need context from past conversations or user asks you to recall something.
""",
),
model="gpt-4o",
model="gpt-4.1-nano-2025-04-14",
tools=[save_memories, search_memories],
)
@@ -533,6 +532,17 @@ async def save_memories(
"""Store a user memory in memory."""
# This will be visible in your console
logger.debug(f"Saving memory: {memory} for user {USER_ID}")
# Rest of your function...
```
```
---
<CardGroup cols={2}>
<Card title="Multimodal Support" icon="image" href="/platform/features/multimodal-support">
Learn how to add vision and audio memory alongside voice interactions.
</Card>
<Card title="Build a Mem0 Companion" icon="users" href="/cookbooks/essentials/building-ai-companion">
Master the core patterns for building memory-powered companions.
</Card>
</CardGroup>
@@ -1,8 +1,10 @@
---
title: YouTube Assistant Extension
title: Research Assistant for YouTube
description: "Layer personalized context over any video using the Mem0 YouTube assistant."
---
Enhance your YouTube experience with Mem0's **YouTube Assistant**, a Chrome extension that brings AI-powered chat directly to your YouTube videos. Get instant, personalized answers about video content while leveraging your own knowledge and memories - all without leaving the page.
Enhance your YouTube experience with Mem0's YouTube Assistant, a Chrome extension that brings AI-powered chat directly to your YouTube videos. Get instant, personalized answers about video content while leveraging your own knowledge and memories, all without leaving the page.
## Features
@@ -29,12 +31,12 @@ This extension is not available on the Chrome Web Store yet. You can install it
### Manual Installation (Developer Mode)
1. **Download the Extension**: Clone or download the extension files from the [Mem0 GitHub repository](https://github.com/mem0ai/mem0/tree/main/examples).
2. **Build**: Run `npm install` followed by `npm run build` to install the dependencies and build the extension.
3. **Access Chrome Extensions**: Open Google Chrome and navigate to `chrome://extensions`.
4. **Enable Developer Mode**: Toggle the "Developer mode" switch in the top right corner.
5. **Load Unpacked Extension**: Click "Load unpacked" and select the directory containing the extension files.
6. **Confirm Installation**: The Mem0 YouTube Assistant Extension should now appear in your Chrome toolbar.
1. **Download the Extension**: Clone or download the extension files from the [Mem0 GitHub repository](https://github.com/mem0ai/mem0/tree/main/examples)
2. **Build**: Run `npm install` followed by `npm run build` to install the dependencies and build the extension
3. **Access Chrome Extensions**: Open Google Chrome and navigate to `chrome://extensions`
4. **Enable Developer Mode**: Toggle the "Developer mode" switch in the top right corner
5. **Load Unpacked Extension**: Click "Load unpacked" and select the directory containing the extension files
6. **Confirm Installation**: The Mem0 YouTube Assistant Extension should now appear in your Chrome toolbar
## Setup
@@ -50,7 +52,17 @@ This extension is not available on the Chrome Web Store yet. You can install it
- "How does this relate to what I already know?"
- "What are some practical applications of this topic related to my work?"
## Privacy and Data Security
Your API keys are stored locally in your browser. Your messages are sent to the Mem0 API for extracting and retrieving memories. Mem0 is committed to ensuring your data's privacy and security.
---
<CardGroup cols={2}>
<Card title="Tag and Organize Memories" icon="tag" href="/cookbooks/essentials/tagging-and-organizing-memories">
Categorize video insights to build a searchable research knowledge base.
</Card>
<Card title="Deep Research with Mem0" icon="magnifying-glass" href="/cookbooks/operations/deep-research">
Combine memory with search tools to conduct comprehensive research projects.
</Card>
</CardGroup>
@@ -0,0 +1,530 @@
---
title: Build a Companion with Mem0
description: "Spin up a fitness coach that remembers goals, adapts tone, and keeps sessions personal."
---
Essentially, creating a companion out of LLMs is as simple as a loop. But these loops work great for one type of character without personalization and fall short as soon as you restart the chat.
Problem: LLMs are stateless. GPT doesn't remember conversations. You could stuff everything inside the context window, but that becomes slow, expensive, and breaks at scale.
The solution: Mem0. It extracts and stores what matters from conversations, then retrieves it when needed. Your companion remembers user preferences, past events, and history.
In this cookbook we'll build a **fitness companion** that:
- Remembers user goals across sessions
- Recalls past workouts and progress
- Adapts its personality based on user preferences
- Handles both short-term context (today's chat) and long-term memory (months of history)
By the end, you'll have a working fitness companion and know how to handle common production challenges.
---
## The Basic Loop with Memory
Max wants to train for a marathon. He starts chatting with Ray, an AI running coach.
```python
from openai import OpenAI
from mem0 import MemoryClient
openai_client = OpenAI(api_key="your-openai-key")
mem0_client = MemoryClient(api_key="your-mem0-key")
def chat(user_input, user_id):
# Retrieve relevant memories
memories = mem0_client.search(user_input, user_id=user_id, limit=5)
context = "\\n".join(m["memory"] for m in memories["results"])
# Call LLM with memory context
response = openai_client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": f"You're Ray, a running coach. Memories:\\n{context}"},
{"role": "user", "content": user_input}
]
).choices[0].message.content
# Store the exchange
mem0_client.add([
{"role": "user", "content": user_input},
{"role": "assistant", "content": response}
], user_id=user_id)
return response
```
**Session 1:**
```python
chat("I want to run a marathon in under 4 hours", user_id="max")
# Output: "That's a solid goal. What's your current weekly mileage?"
# Stored in Mem0: "Max wants to run sub-4 marathon"
```
**Session 2 (next day, app restarted):**
```python
chat("What should I focus on today?", user_id="max")
# Output: "Based on your sub-4 marathon goal, let's work on building your aerobic base..."
```
<Info>
Ray remembers Max's goal across sessions. The app restarted, but the memory persisted. This is the core pattern: retrieve memories, pass them as context, store new exchanges.
</Info>
Ray remembers. Restart the app, and the goal persists. From here on, we'll focus on just the Mem0 API calls.
---
## Organizing Memory by Type
### Separating Temporary from Permanent
Max mentions his knee hurts. That's different from his marathon goal - one is temporary, the other is long-term.
**Categories vs Metadata:**
- **Categories**: AI-assigned by Mem0 based on content (you can't force them)
- **Metadata**: Manually set by you for forced tagging
Define custom categories at the project level. Mem0 will automatically tag memories with relevant categories based on content:
```python
mem0_client.project.update(custom_categories=[
{"goals": "Race targets and training objectives"},
{"constraints": "Injuries, limitations, recovery needs"},
{"preferences": "Training style, surfaces, schedules"}
])
```
<Note>
**Categories vs Metadata:** Categories are AI-assigned by Mem0 based on content semantics. You define the palette, Mem0 picks which ones apply. If you need guaranteed tagging, use `metadata` instead.
</Note>
Now when you add memories, Mem0 automatically assigns the appropriate categories:
```python
# Add goal - Mem0 automatically tags it as "goals"
mem0_client.add(
[{"role": "user", "content": "Sub-4 marathon is my A-race"}],
user_id="max"
)
# Add constraint - Mem0 automatically tags it as "constraints"
mem0_client.add(
[{"role": "user", "content": "My right knee flares up on downhills"}],
user_id="max"
)
```
Mem0 reads the content and intelligently picks which categories apply. You define the palette, it handles the tagging.
**Important:** You cannot force specific categories. Mem0's platform decides which categories are relevant based on content. If you need to force-tag something, use `metadata` instead:
```python
# Force tag using metadata (not categories)
mem0_client.add(
[{"role": "user", "content": "Some workout note"}],
user_id="max",
metadata={"workout_type": "speed", "forced_tag": "custom_label"}
)
```
### Filtering by Category
Retrieve just constraints for workout planning:
```python
constraints = mem0_client.search(
query="injury concerns",
filters={
"AND": [
{"user_id": "max"},
{"categories": {"in": ["constraints"]}}
]
},
threshold=0.0 # optional: widen recall for short phrases
)
print([m["memory"] for m in constraints["results"]])
# Output: ["Max's right knee flares up on downhills"]
```
Ray can plan workouts that avoid aggravating Max's knee, without pulling in race goals or other unrelated memories.
---
## Filtering What Gets Stored
### The Problem
Run the basic loop for a week and check what's stored:
```python
memories = mem0_client.get_all(filters={"AND": [{"user_id": "max"}]})
print([m["memory"] for m in memories["results"]])
# Output: ["Max wants to run marathon under 4 hours", "hey", "lol ok", "cool thanks", "gtg bye"]
```
<Warning>
Without filters, Mem0 stores everything—greetings, filler, and casual chat. This pollutes retrieval: instead of pulling "marathon goal," you get "lol ok." Set custom instructions to keep memory clean.
</Warning>
Noise. Greetings and filler clutter the memory.
### Custom Instructions
Tell Mem0 what matters:
```python
mem0_client.project.update(custom_instructions="""
Extract from running coach conversations:
- Training goals and race targets
- Physical constraints or injuries
- Training preferences (time of day, surfaces, weather)
- Progress milestones
Exclude:
- Greetings and filler
- Casual chatter
- Hypotheticals unless planning related
""")
```
Now chat again:
```python
chat("hey how's it going", user_id="max")
chat("I prefer trail running over roads", user_id="max")
memories = mem0_client.get_all(filters={"AND": [{"user_id": "max"}]})
print([m["memory"] for m in memories["results"]])
# Output: ["Max wants to run marathon under 4 hours", "Max prefers trail running over roads"]
```
<Info>
**Expected output:** Only 2 memories stored—the marathon goal and trail preference. The greeting "hey how's it going" was filtered out automatically. Custom instructions are working.
</Info>
Only meaningful facts. Filler gets dropped automatically.
---
---
## Agent Memory for Personality
### Why Agents Need Memory Too
Max prefers direct feedback, not motivational fluff. Ray needs to remember how to communicate - that's agent memory, separate from user memory.
Store agent personality:
```python
mem0_client.add(
[{"role": "system", "content": "Max wants direct, data-driven feedback. Skip motivational language."}],
agent_id="ray_coach"
)
```
Retrieve agent style alongside user memories:
```python
# Get coach personality
agent_memories = mem0_client.search("coaching style", agent_id="ray_coach")
# Output: ["Max wants direct, data-driven feedback. Skip motivational language."]
# Store conversations with agent_id
mem0_client.add([
{"role": "user", "content": "How'd my run look today?"},
{"role": "assistant", "content": "Pace was 8:15/mile. Heart rate 152, zone 2."}
], user_id="max", agent_id="ray_coach")
```
<Info>
**Expected behavior:** Ray's responses are now data-driven and direct. The agent memory stored the coaching style preference, so future responses adapt automatically without Max having to repeat his preference.
</Info>
No "Great job!" or "Keep it up!" - just data. Ray adapts to Max's preference.
---
## Managing Short-Term Context
### When to Store in Mem0
Don't send every single message to Mem0. Keep recent context in memory, let Mem0 handle the important long-term facts.
```python
# Store only meaningful exchanges in Mem0
mem0_client.add([
{"role": "user", "content": "I want to run a marathon"},
{"role": "assistant", "content": "Let's build a training plan"}
], user_id="max")
# Skip storing filler
# "hey" → don't store
# "cool thanks" → don't store
# Or rely on custom_instructions to filter automatically
```
Last 10 messages in your app's buffer. Important facts in Mem0. Faster, cheaper, still works.
---
## Time-Bound Memories
### Auto-Expiring Facts
Max tweaks his ankle. It'll heal in two weeks - the memory should expire too.
```python
from datetime import datetime, timedelta
expiration = (datetime.now() + timedelta(days=14)).strftime("%Y-%m-%d")
mem0_client.add(
[{"role": "user", "content": "Rolled my left ankle, needs rest"}],
user_id="max",
expiration_date=expiration
)
```
In 14 days, this memory disappears automatically. Ray stops asking about the ankle.
---
## Putting It All Together
Here's the Mem0 setup combining everything:
```python
from mem0 import MemoryClient
from datetime import datetime, timedelta
mem0_client = MemoryClient(api_key="your-mem0-key")
# Configure memory filtering and categories
mem0_client.project.update(
custom_instructions="""
Extract: goals, constraints, preferences, progress
Exclude: greetings, filler, casual chat
""",
custom_categories=[
{"name": "goals", "description": "Training targets"},
{"name": "constraints", "description": "Injuries and limitations"},
{"name": "preferences", "description": "Training style"}
]
)
```
**Week 1 - Store goals and preferences:**
```python
mem0_client.add([
{"role": "user", "content": "I want to run a sub-4 marathon"},
{"role": "assistant", "content": "Got it. Let's build a training plan."}
], user_id="max", agent_id="ray", categories=["goals"])
mem0_client.add([
{"role": "user", "content": "I prefer trail running over roads"}
], user_id="max", categories=["preferences"])
```
**Week 3 - Temporary injury with expiration:**
```python
expiration = (datetime.now() + timedelta(days=14)).strftime("%Y-%m-%d")
mem0_client.add(
[{"role": "user", "content": "Rolled ankle, need light workouts"}],
user_id="max",
categories=["constraints"],
expiration_date=expiration
)
```
**Retrieve for context:**
```python
memories = mem0_client.search("training plan", user_id="max", limit=5)
# Gets: marathon goal, trail preference, ankle injury (if still valid)
```
Ray remembers goals, preferences, and personality. Handles temporary injuries. Works across sessions.
---
## Common Production Patterns
### Episodic Stories with run_id
Training for Boston is different from training for New York. Separate the memory threads:
```python
mem0_client.add(messages, user_id="max", run_id="boston-2025")
mem0_client.add(messages, user_id="max", run_id="nyc-2025")
# Retrieve only Boston memories
boston_memories = mem0_client.search(
"training plan",
user_id="max",
run_id="boston-2025"
)
```
Each race gets its own episodic boundary. No cross-contamination.
### Importing Historical Data
Max has 6 months of training logs to backfill:
```python
old_logs = [
[{"role": "user", "content": "Completed 20-mile long run"}],
[{"role": "user", "content": "Hit 8:00 pace on tempo run"}],
]
for log in old_logs:
mem0_client.add(log, user_id="max")
```
### Handling Contradictions
Max changes his goal from sub-4 to sub-3:45:
```python
# Find the old memory
memories = mem0_client.get_all(filters={"AND": [{"user_id": "max"}]})
goal_memory = [m for m in memories["results"] if "sub-4" in m["memory"]][0]
# Update it
mem0_client.update(goal_memory["id"], "Max wants to run sub-3:45 marathon")
```
Update instead of creating duplicates.
### Multiple Agents
Max works with Ray for running and Jordan for strength training:
```python
chat("easy run today", user_id="max", agent_id="ray")
chat("leg day workout", user_id="max", agent_id="jordan")
```
Each coach maintains separate personality memory while sharing user context.
### Filtering by Date
Prioritize recent training over old data:
```python
recent = mem0_client.search(
"training progress",
user_id="max",
filters={"created_at": {"gte": "2025-10-01"}}
)
```
### Metadata Tagging
Tag workouts by type:
```python
mem0_client.add(
[{"role": "user", "content": "10x400m intervals"}],
user_id="max",
metadata={"workout_type": "speed", "intensity": "high"}
)
# Later, find all speed workouts
speed_sessions = mem0_client.search(
"speed work",
user_id="max",
filters={"metadata": {"workout_type": "speed"}}
)
```
### Pruning Old Memories
Delete irrelevant memories:
```python
mem0_client.delete(memory_id="mem_xyz")
# Or clear an entire run_id
mem0_client.delete_all(user_id="max", run_id="old-training-cycle")
```
---
## What You Built
A companion that:
- **Persists across sessions** - Mem0 storage
- **Filters noise** - custom instructions
- **Organizes by type** - categories
- **Adapts personality** - **`agent_id`**
- **Stays fast** - short-term buffer
- **Handles temporal facts** - expiration
- **Scales to production** - batching, metadata, pruning
This pattern works for any companion: fitness coaches, tutors, roleplay characters, therapy bots, creative writing partners.
---
<Tip>
Start with 2-3 categories max (e.g., goals, constraints, preferences). More categories dilute tagging accuracy. You can always add more later after seeing what Mem0 extracts.
</Tip>
---
## Production Checklist
Before launching:
- Set custom instructions for your domain
- Define 2-3 categories (goals, constraints, preferences)
- Add expiration strategy for time-bound facts
- Implement error handling for API calls
- Monitor memory quality in Mem0 dashboard
- Clear test data from production project
---
<CardGroup cols={2}>
<Card title="Partition Memories by Entity" icon="layers" href="/cookbooks/essentials/entity-partitioning-playbook">
Keep companions from leaking context by combining user, agent, and session scopes.
</Card>
<Card title="Tag Support Memories" icon="tag" href="/cookbooks/essentials/tagging-and-organizing-memories">
Organize customer context to keep assistants responsive at scale.
</Card>
</CardGroup>
@@ -0,0 +1,361 @@
---
title: Choose Vector vs Graph Memory
description: "Blend vector search with graph relationships to answer multi-hop questions."
---
Most AI agents use vector stores for RAG operations - they work great for semantic search and retrieving relevant context. But there's a gap when queries require understanding connections between entities.
Mem0 brings graph memory into the picture to fill this gap. In this cookbook, we'll create a company knowledge base with Mem0, using both vector and graph stores. You'll learn when each one helps along the way.
---
## Vector and Graph Stores
When you add a memory to Mem0, it goes into a **vector store** by default. Vector stores are excellent at semantic search - finding memories that match the meaning of your query.
**Graph stores** work differently. They extract **entities** (people, projects, teams) and **relationships between them** (works_with, reports_to, member_of). This lets you answer questions that need connecting information across multiple memories.
We will go through examples in this cookbook while building a company's knowledge base along the way.
---
## Starting Simple
Since we're building a company knowledge base, let's add some employee information:
```python
from mem0 import MemoryClient
client = MemoryClient(api_key="your-api-key")
# Add employee info
client.add("Emma is a software engineer in Seattle", user_id="company_kb")
client.add("David is a product manager in Austin", user_id="company_kb")
```
Now let's search for Emma's role:
```python
results = client.search("What does Emma do?", filters={"user_id": "company_kb"})
print(results['results'][0]['memory'])
```
**Output:**
```
Emma is a software engineer in Seattle
```
<Info>
**Expected output:** Vector search returned Emma's role instantly. When queries ask for facts directly stored in one memory, vector semantic search is perfect—fast and accurate.
</Info>
This works perfectly. Vector search found the memory that semantically matches "What does Emma do?" and returned Emma's role.
---
## Adding Team Structure
Let's add some information about how the team works together:
```python
client.add("Emma works with David on the mobile app redesign", user_id="company_kb")
client.add("David reports to Rachel, who manages the design team", user_id="company_kb")
```
Now we have two pieces of information stored:
1. Emma works with David
2. David reports to Rachel
Let's try asking something that needs both pieces:
```python
results = client.search(
"Who is Emma's teammate's manager?",
filters={"user_id": "company_kb"}
)
for r in results['results']:
print(r['memory'])
```
**Output:**
```
Emma works with David on the mobile app redesign
David reports to Rachel, who manages the design team
```
Vector search returned both memories, but it didn't connect them. You'd need to manually figure out:
- Emma's teammate is David (from memory 1)
- David's manager is Rachel (from memory 2)
- So the answer is Rachel
<Warning>
Vector search can't traverse relationships. It returns relevant memories, but you must connect the dots manually. For "Who is Emma's teammate's manager?", vector search gives you the pieces—not the answer. This breaks down as queries get more complex (3+ hops).
</Warning>
---
## Enter Graph Memory
Let's add the same information with graph memory enabled:
```python
client.add(
"Emma works with David on the mobile app redesign",
user_id="company_kb",
enable_graph=True
)
client.add(
"David reports to Rachel, who manages the design team",
user_id="company_kb",
enable_graph=True
)
```
When you set `enable_graph=True`, Mem0 extracts entities and relationships:
- `emma --[works_with]--> david`
- `david --[reports_to]--> rachel`
- `rachel --[manages]--> design_team`
Now the same query works differently:
```python
results = client.search(
"Who is Emma's teammate's manager?",
filters={"user_id": "company_kb"},
enable_graph=True
)
print(results['results'][0]['memory'])
print("\\nRelationships found:")
for rel in results.get('relations', []):
print(f" {rel['source']}, {rel['target']} ({rel['relationship']})")
```
**Output:**
```
David reports to Rachel, who manages the design team
Relationships found:
emma, david (works_with)
david, rachel (reports_to)
```
<Info>
**Expected behavior:** Graph memory returns the direct answer—"David reports to Rachel"—plus the relationship chain that got there. No manual connecting needed. The graph traversed: Emma → works_with → David → reports_to → Rachel.
</Info>
Graph memory traversed the relationships automatically: Emma works with David, David reports to Rachel, so Rachel is the answer.
---
## How It Connects
Here's what the graph looks like behind the scenes:
```mermaid
graph LR
Emma[Emma] -->|works_with| David[David]
David -->|reports_to| Rachel[Rachel]
Rachel -->|manages| DesignTeam[Design Team]
David -->|works_on| MobileApp[Mobile App]
Emma -->|works_on| MobileApp
```
Graph memory lets you discover relations and memories which are tricky to do with direct vector stores.
Vector search would need the exact words in your query to match. Graph memory follows the connections.
---
## When to Use Each
Use **vector store** (default) when:
- Searching documents by semantic similarity
- Looking up facts that don't need relationships
- Building FAQs or knowledge bases where each item stands alone
Use **graph memory** when:
- Tracking organizational hierarchies (who reports to whom)
- Understanding project teams (who collaborates with whom)
- Building CRMs (which contacts connect to which companies)
- Product recommendations (what items are bought together)
For our company knowledge base, we'll use both:
- Vector for individual facts: "Emma specializes in React"
- Graph for relationships: "Emma works with David"
---
## Putting It Together
Let's build a small company knowledge base with both approaches:
```python
# Facts about individuals - vector store is fine
client.add("Emma specializes in React and TypeScript", user_id="company_kb")
client.add("David has 5 years of product management experience", user_id="company_kb")
# Relationships - use graph memory
client.add(
"Emma and David work together on the mobile app",
user_id="company_kb",
enable_graph=True
)
client.add(
"David reports to Rachel",
user_id="company_kb",
enable_graph=True
)
client.add(
"Rachel runs weekly team syncs every Tuesday",
user_id="company_kb",
enable_graph=True
)
```
Now we can ask different types of questions:
```python
# Direct fact - vector search
results = client.search("What are Emma's skills?", filters={"user_id": "company_kb"})
print(results['results'][0]['memory'])
```
**Output:**
```
Emma specializes in React and TypeScript
```
```python
# Multi-hop relationship - graph search
results = client.search(
"What meetings does Emma's project manager's boss run?",
filters={"user_id": "company_kb"},
enable_graph=True
)
print(results['results'][0]['memory'])
```
**Output:**
```
Rachel runs weekly team syncs every Tuesday
```
Graph memory connected: Emma works with David, David reports to Rachel, Rachel runs team syncs.
<Tip>
Enable graph memory when your queries need multi-hop traversal: org charts (who reports to whom), project teams (who collaborates), CRMs (which contacts connect to companies). For single-fact lookups, stick with vector search—it's faster and cheaper.
</Tip>
---
## The Tradeoff
Graph memory adds processing time and cost. When you call `client.add()` with `enable_graph=True`, Mem0 makes extra LLM calls to extract entities and relationships.
<Note>
**Cost consideration:** Graph memory extraction adds ~2-3 extra LLM calls per `add()` operation to identify entities and relationships. Use it selectively—enable graph for organizational structure and long-term relationships, skip it for temporary notes and simple facts.
</Note>
Use graph memory when the relationship traversal adds real value. For most use cases, vector search is sufficient and faster.
```python
# Long-term organizational structure - worth using graph
client.add(
"Emma mentors two junior engineers on the frontend team",
user_id="company_kb",
enable_graph=True
)
# Temporary notes - skip graph, not worth the cost
client.add(
"Emma is out sick today",
user_id="company_kb",
run_id="daily_notes"
)
```
---
## Enabling Graph Memory
You can enable graph memory in two ways:
**Per-call** (recommended to start):
```python
client.add("Emma works with David", user_id="company_kb", enable_graph=True)
client.search("team structure", filters={"user_id": "company_kb"}, enable_graph=True)
```
**Project-wide** (if most of your data has relationships):
```python
client.project.update(enable_graph=True)
# Now every add uses graph automatically
client.add("Emma mentors Jordan", user_id="company_kb")
```
---
## What You Built
A hybrid company knowledge base that combines both architectures:
- **Vector search** - Fast semantic lookups for individual facts (Emma's skills, David's experience)
- **Graph memory** - Multi-hop relationship traversal (Emma's teammate's manager, project hierarchies)
- **Selective enablement** - Graph only for long-term organizational structure, vector for everything else
- **Cost optimization** - Skip graph extraction for temporary notes and simple facts
This pattern scales from 10-person startups to enterprise org charts with thousands of employees.
---
## Summary
Vector stores handle most memory operations efficiently—semantic search works great for finding relevant information. Add graph memory when your queries need to understand how entities connect across multiple hops.
The key is knowing which tool fits your query pattern: direct questions work with vectors, multi-hop relationship queries need graphs.
<CardGroup cols={2}>
<Card title="Partition Memories by Entity" icon="layers" href="/cookbooks/essentials/entity-partitioning-playbook">
Scope memories across users, agents, apps, and sessions to balance personalization and reuse.
</Card>
<Card title="Export Everything Safely" icon="download" href="/cookbooks/essentials/exporting-memories">
Learn how to migrate or audit stored memories with structured exports.
</Card>
</CardGroup>
@@ -0,0 +1,523 @@
---
title: Control Memory Ingestion
description: "Filter speculation, enforce formats, and gate low-confidence data before it persists."
---
AI assistants plugged with memory systems face a problem - they often store everything. Not every conversation needs to be remembered, and not every detail should go to the memory store. Without proper controls, memory systems accumulate unreliable data.
Mem0 lets you control your memory ingestion pipeline. In this cookbook, we'll demonstrate these controls using a medical assistant example - showing how to filter unwanted data, enforce data formats, and implement confidence-based storage.
---
## Overview
Without controls, everything gets stored - speculation, low-confidence data, and information that shouldn't persist. This uncontrolled ingestion leads to cluttered memory and retrieval failures.
Mem0 provides **three tools to control** what gets stored:
1. **Custom instructions** define what to remember and what to ignore.
2. **Confidence thresholds** ensure only verified facts persist.
3. **Memory updates** let you change information without creating duplicates.
In this tutorial, we will:
- Filter speculative statements with custom instructions
- Configure confidence thresholds for fact verification
- Update stored information without duplication
- Build a complete ingestion pipeline
---
## Setup
```python
from mem0 import MemoryClient
client = MemoryClient(api_key="your-api-key")
```
<Note>
Replace `your-api-key` with your actual Mem0 API key from the [dashboard](https://app.mem0.ai). Without proper API authentication, memory operations will fail.
</Note>
---
## The Problem
Uncontrolled ingestion stores everything, including speculation:
```python
# Patient mentions speculation
messages = [{"role": "user", "content": "I think I might be allergic to penicillin"}]
client.add(messages, user_id="patient_123")
# Check what got stored
results = client.search("patient allergies", filters={"user_id": "patient_123"})
print(results['results'][0]['memory'])
```
**Output:**
```
Patient is allergic to penicillin
```
<Warning>
Without custom instructions, AI assistants treat speculation as confirmed facts. "I think I might be allergic" becomes "Patient is allergic"—a dangerous transformation in sensitive domains like healthcare, legal, or financial services.
</Warning>
The speculation became a confirmed fact. Let's add controls.
---
## Custom Instructions
Custom instructions tell Mem0 what to store and what to ignore.
```python
instructions = """
Only store CONFIRMED medical facts.
Store:
- Confirmed diagnoses from doctors
- Known allergies with documented reactions
- Current medications being taken
Ignore:
- Speculation (words like "might", "maybe", "I think")
- Unverified symptoms
- Casual mentions without confirmation
"""
client.project.update(custom_instructions=instructions)
# Same speculative statement
messages = [{"role": "user", "content": "I think I might be allergic to penicillin"}]
client.add(messages, user_id="patient_123")
# Check what got stored
results = client.get_all(filters={"user_id": "patient_123"})
print(f"Memories stored: {len(results['results'])}")
```
**Output:**
```
Memories stored: 0
```
<Info>
**Expected output:** Zero memories stored. The speculative statement "I think I might be allergic" was filtered out before reaching storage. Custom instructions are actively blocking unreliable data.
</Info>
The speculation was filtered out.
---
## Designing Custom Instructions
When designing instructions, consider the trade-off between precision and recall:
**Too restrictive:** You'll miss important information (false negatives)
```python
# Too strict - filters out useful context
"""
Only store information if explicitly stated by a doctor with full name,
date, time, and medical license number.
"""
```
**Too permissive:** You'll store unreliable data (false positives)
```python
# Too loose - stores speculation as fact
"""
Store any health-related information mentioned.
"""
```
**Balanced approach:**
```python
# Clear categories with examples
"""
Store CONFIRMED facts:
- Diagnoses: "Dr. Smith diagnosed hypertension on March 15th"
- Allergies: "Patient had hives reaction to penicillin"
- Medications: "Taking Lisinopril 10mg daily"
Ignore SPECULATION:
- "I think I might have..."
- "Maybe it's..."
- "Could be related to..."
"""
```
<Tip>
Start with strict instructions (only store confirmed facts), then relax them based on your use case. It's easier to allow more data than to clean up polluted memory. Test with sample conversations before deploying to production.
</Tip>
Start with clear categories and iterate based on retrieval quality.
---
## Confidence Thresholds
Mem0 assigns confidence scores to extracted memories. Use these to filter low-quality data.
### Setting Thresholds
Setting the right confidence threshold depends on your application:
- **High-stakes domains** (medical, legal): Require 0.8+ confidence
- **General assistants**: 0.6+ confidence is often sufficient
- **Exploratory systems**: Lower thresholds (0.4+) capture more data
Test your pipeline with multiple input examples and threshold combinations to find what works for your use case.
```python
# Configure stricter instructions
client.project.update(
custom_instructions="""
Only extract memories with HIGH confidence.
Require specific details (dates, dosages, doctor names) for medical facts.
Skip vague or uncertain statements.
"""
)
# Test with uncertain statement
messages = [{"role": "user", "content": "The doctor mentioned something about my blood pressure"}]
result1 = client.add(messages, user_id="patient_123")
# Test with confirmed fact
messages = [{"role": "user", "content": "Dr. Smith diagnosed me with hypertension on March 15th"}]
result2 = client.add(messages, user_id="patient_123")
print("Vague statement stored:", len(result1['results']) > 0)
print("Confirmed fact stored:", len(result2['results']) > 0)
```
**Output:**
```
Vague statement stored: False
Confirmed fact stored: True
```
<Info icon="check">
**Expected behavior:** Low-confidence extractions are now filtered out automatically. Only verified facts with specific details (names, dates, dosages) persist in memory. The confidence threshold is working.
</Info>
The vague statement was filtered for low confidence. The confirmed fact with specific details was stored.
---
## Filtering Sensitive Information
Custom instructions can prevent storing personal identifiers:
```python
client.project.update(
custom_instructions="""
Medical memory rules:
STORE:
- Confirmed diagnoses
- Verified allergies
- Current medications
NEVER STORE:
- Social Security Numbers
- Insurance policy numbers
- Credit card information
- Full addresses
- Phone numbers
Replace identifiers with generic references if mentioned.
"""
)
# Test with PII
messages = [
{"role": "user", "content": "My SSN is 123-45-6789 and I'm allergic to penicillin"}
]
client.add(messages, user_id="patient_123")
# Check what was stored
results = client.get_all(filters={"user_id": "patient_123"})
for result in results['results']:
print(result['memory'])
```
**Output:**
```
Patient is allergic to penicillin
```
The SSN was filtered out, but the allergy was stored.
---
## Updating Memories
When information changes, update existing memories instead of creating duplicates.
```python
# Initial allergy stored
result = client.add(
[{"role": "user", "content": "Patient confirmed allergy to penicillin with documented hives reaction"}],
user_id="patient_123"
)
memory_id = result['results'][0]['id']
print(f"Stored memory: {memory_id}")
# Later, patient gets retested - allergy was false positive
client.update(
memory_id=memory_id,
text="Patient tested negative for penicillin allergy on April 2nd, 2025. Previous allergy was false positive.",
metadata={"verified": True, "updated_date": "2025-04-02"}
)
# Retrieve the updated memory
updated = client.get(memory_id)
print(f"\\nUpdated memory: {updated['memory']}")
print(f"Metadata: {updated['metadata']}")
```
**Output:**
```
Stored memory: mem_abc123
Updated memory: Patient tested negative for penicillin allergy on April 2nd, 2025. Previous allergy was false positive.
Metadata: {'verified': True, 'updated_date': '2025-04-02'}
```
### Benefits of Updating
**Preserves history:**
- `created_at` shows when the memory was first stored
- `updated_at` shows when it was modified
- Audit trail for compliance
**Avoids conflicts:**
- No duplicate or contradicting memories
- Single source of truth for each fact
<Warning>
That “no duplicates” promise comes from the inference pipeline. Keep `infer=True` when you rely on automatic updates. Raw imports (`infer=False`) skip conflict checks, so mixing the two modes for the same fact will create duplicates.
</Warning>
**Maintains relationships:**
- If using graph memory, connections to other entities persist
### Pick the right inference mode
| Mode | What it does | Best for | Watch out for |
| --- | --- | --- | --- |
| `infer=True` *(default)* | Runs the LLM pipeline so Mem0 extracts structured facts and resolves conflicts automatically. | Daily conversations, preference tracking, anything you want deduped. | Slightly slower because inference runs on every write. |
| `infer=False` | Stores your payload exactly as-is—no inference, no dedupe. | Bulk imports, compliance snapshots, curated facts you already trust. | Later `infer=True` calls for the same fact will create duplicates you must clean manually. |
<Tip>
Stay consistent per data source. If you need both behaviors, keep them in separate scopes (e.g., different `app_id` or `run_id`) so you always know which memories are inferred vs direct imports.
</Tip>
---
## Update vs Delete
When should you update vs delete?
### Update when:
- Information changes but remains relevant
- You need audit history
- The memory has relationships to other data
```python
# Medication dosage changed
client.update(
memory_id=med_id,
text="Taking Lisinopril 20mg daily (increased from 10mg on March 1st)"
)
```
### Delete when:
- Information was completely wrong
- Memory is no longer relevant
- Duplicate entry
```python
# Duplicate entry
client.delete(memory_id)
```
---
## Putting It Together
Here's a complete ingestion pipeline with all controls:
```python
from mem0 import MemoryClient
import os
# Initialize client
client = MemoryClient(api_key=os.getenv("MEM0_API_KEY"))
# Configure custom instructions
client.project.update(
custom_instructions="""
Medical memory assistant rules:
STORE:
- Confirmed diagnoses (with doctor name and date)
- Verified allergies (with reaction details)
- Current medications (with dosage)
IGNORE:
- Speculation (might, maybe, possibly)
- Unverified symptoms
- Personal identifiers (SSN, insurance numbers)
CONFIDENCE:
Require high confidence. Reject vague or uncertain statements.
Require specific details: names, dates, dosages.
"""
)
# Helper function for safe ingestion
def add_medical_memory(content, user_id, metadata=None):
"""Add memory with automatic filtering."""
result = client.add(
[{"role": "user", "content": content}],
user_id=user_id,
metadata=metadata or {}
)
if result['results']:
print(f"✓ Stored: {result['results'][0]['memory']}")
else:
print(f"✗ Filtered: {content}")
return result
# Test cases
print("Testing ingestion pipeline:\\n")
test_cases = [
"I think I might be allergic to penicillin",
"Dr. Johnson confirmed penicillin allergy on Jan 15th with hives reaction",
"Patient SSN is 123-45-6789",
"Currently taking Lisinopril 10mg daily for hypertension",
"Feeling tired lately",
"Dr. Martinez diagnosed Type 2 diabetes on February 3rd, 2025"
]
for content in test_cases:
add_medical_memory(content, user_id="patient_123")
print()
```
**Output:**
```
Testing ingestion pipeline:
✗ Filtered: I think I might be allergic to penicillin
✓ Stored: Patient has confirmed penicillin allergy diagnosed by Dr. Johnson on January 15th with hives reaction
✗ Filtered: Patient SSN is 123-45-6789
✓ Stored: Patient is currently taking Lisinopril 10mg daily for hypertension
✗ Filtered: Feeling tired lately
✓ Stored: Patient diagnosed with Type 2 diabetes by Dr. Martinez on February 3rd, 2025
```
---
## Per-Call Instructions
You can override project-level instructions for specific conversations:
First define custom instructions
```python
custom_instructions="""Emergency intake mode:Store ALL symptoms and observations immediately.
Flag for later review and verification."""
```
```python
# Emergency intake - store everything temporarily
emergency_messages = [
{"role": "user", "content": "Patient arrived with chest pain and shortness of breath"}
]
client.add(
emergency_messages,
user_id="patient_456",
custom_instructions=custom_instructions,
metadata={"type": "emergency", "review_required": True}
)
```
This is useful for:
- Different conversation types (emergency vs routine)
- Channel-specific rules (phone vs in-person)
- Temporary data collection that needs review
---
## What You Built
You now have a medical assistant with production-grade memory controls:
- **Custom instructions** - Filter speculation and enforce confirmed facts only
- **Confidence thresholds** - Gate extractions below 0.7 confidence score
- **Memory updates** - Modify stored information without creating duplicates
- **Per-call instructions** - Apply temporary rules for specific conversations
- **PII filtering** - Block sensitive data (SSNs, insurance numbers) automatically
These controls prevent retrieval failures and ensure your AI assistant works with reliable, verified information.
---
## Summary
Start with conservative filters (only store confirmed facts) and iterate based on your application's needs. Combine custom instructions with confidence thresholds for the most reliable memory ingestion pipeline.
<CardGroup cols={2}>
<Card title="Expire Short-Term Data" icon="timer" href="/cookbooks/essentials/memory-expiration-short-and-long-term">
Automatically clean up session context before it clutters retrieval.
</Card>
<Card title="Choose Your Memory Architecture" icon="sitemap" href="/cookbooks/essentials/choosing-memory-architecture-vector-vs-graph">
Learn when to layer graph memory alongside vectors for multi-hop queries.
</Card>
</CardGroup>
@@ -0,0 +1,336 @@
---
title: Partition Memories by Entity
description: Keep memories separate by tagging each write and query with user, agent, app, and session identifiers.
---
Nora runs a travel service. When she stored all memories in one bucket, a recruiter's nut allergy accidentally appeared in a traveler's dinner reservation. Let's fix this by properly separating memories for different users, agents, and applications.
<Info icon="clock">
**Time to complete:** ~15 minutes · **Languages:** Python
</Info>
## Setup
```python
from mem0 import MemoryClient
client = MemoryClient(api_key="m0-...")
```
Grab an API key from the <Link href="https://app.mem0.ai/">Mem0 dashboard</Link> to get started.
## Store and Retrieve Scoped Memories
Let's start by storing Cam's travel preferences and retrieving them:
```python
cam_messages = [
{"role": "user", "content": "I'm Cam. Keep in mind I avoid shellfish and prefer boutique hotels."},
{"role": "assistant", "content": "Noted! I'll use those preferences in future itineraries."}
]
result = client.add(
cam_messages,
user_id="traveler_cam",
agent_id="travel_planner",
run_id="tokyo-2025-weekend",
app_id="concierge_app"
)
```
The memory is now stored. Let's retrieve those memories with the same identifiers:
```python
user_scope = {
"AND": [
{"user_id": "traveler_cam"},
{"app_id": "concierge_app"},
{"run_id": "tokyo-2025-weekend"}
]
}
user_memories = client.search("Any dietary restrictions?", filters=user_scope)
print(user_memories)
agent_scope = {
"AND": [
{"agent_id": "travel_planner"},
{"app_id": "concierge_app"}
]
}
agent_memories = client.search("Any dietary restrictions?", filters=agent_scope)
print(agent_memories)
```
**Output:**
```
# User scope returns user's memory
{'results': [{'memory': 'avoids shellfish and prefers boutique hotels', ...}]}
# Agent scope returns agent's own memory
{'results': [{'memory': 'Cam prefers boutique hotels and avoids shellfish', ...}]}
```
<Tip icon="compass">
Memories can be written with several identifiers, but each search resolves one entity boundary at a time. Run separate queries for user and agent scopes—just like above—rather than combining both in a single filter.
</Tip>
## When Memories Leak
When Nora adds a chef agent, Cam's travel preferences leak into food recommendations:
```python
chef_filters = {"AND": [{"user_id": "traveler_cam"}]}
collision = client.search("What should I cook?", filters=chef_filters)
print(collision)
```
**Output:**
```
['avoids shellfish and prefers boutique hotels', 'prefers Kyoto kaiseki dining experiences']
```
The travel preferences appear because we only filtered by `user_id`. The chef agent shouldn't see hotel preferences.
## Fix the Leak with Proper Filters
First, let's add a memory specifically for the chef agent:
```python
chef_memory = [
{"role": "user", "content": "I'd like to try some authentic Kyoto cuisine."},
{"role": "assistant", "content": "I'll remember that you prefer Kyoto kaiseki dining experiences."}
]
client.add(
chef_memory,
user_id="traveler_cam",
agent_id="chef_recommender",
run_id="menu-planning-2025-04",
app_id="concierge_app"
)
```
Now search within the chef's scope:
```python
safe_filters = {
"AND": [
{"agent_id": "chef_recommender"},
{"app_id": "concierge_app"},
{"run_id": "menu-planning-2025-04"}
]
}
chef_memories = client.search("Any food alerts?", filters=safe_filters)
print(chef_memories)
```
**Output:**
```
{'results': [{'memory': 'prefers Kyoto kaiseki dining experiences', ...}]}
```
Now the chef agent only sees its own food preferences. The hotel preferences stay with the travel agent.
## Separate Apps with app_id
Nora white-labels her travel service for a sports brand. Use `app_id` to keep enterprise data separate:
```python
enterprise_filters = {
"AND": [
{"app_id": "sports_brand_portal"}
],
"OR": [
{"user_id": "*"},
{"agent_id": "*"}
]
}
page = client.get_all(filters=enterprise_filters, page=1, page_size=10)
print([row["user_id"] for row in page["results"]])
```
**Output:**
```
['athlete_jane', 'coach_mike', 'team_admin']
```
<Info>
Wildcards (`"*"` ) only match non-null values. Make sure you write memories with explicit `app_id` values.
</Info>
<Tip icon="sparkles">
Need a deeper tour of AND vs OR, nested filters, or wildcard tricks? Check the <Link href="/platform/features/v2-memory-filters">Memory Filters v2 guide</Link> for full examples you can copy into this flow.
</Tip>
When the sports brand offboards, delete all their data:
```python
client.delete_all(app_id="sports_brand_portal")
```
**Output:**
```
{'message': 'Memories deleted successfully!'}
```
## Production Patterns
```python
# Nightly audits - check all data for an app
def audit_app(app_id: str):
filters = {
"AND": [{"app_id": app_id}],
"OR": [{"user_id": "*"}, {"agent_id": "*"}]
}
return client.get_all(filters=filters, page=1, page_size=50)
# Session cleanup - delete temporary conversations
def close_ticket(ticket_id: str, user_id: str):
client.delete_all(user_id=user_id, run_id=ticket_id)
# Compliance exports - get all data for one tenant
export = client.get_memory_export(filters={"AND": [{"app_id": "sports_brand_portal"}]})
```
## Complete Example
Putting it all together - here's how to properly scope memories:
```python
# Store memories with all identifiers
client.add(
[{"role": "user", "content": "I need a hotel near the conference center."}],
user_id="exec_123",
agent_id="booking_assistant",
app_id="enterprise_portal",
run_id="trip-2025-03"
)
# Retrieve with the same scope
filters = {
"AND": [
{"user_id": "exec_123"},
{"app_id": "enterprise_portal"},
{"run_id": "trip-2025-03"}
]
}
# Alternative: Use wildcards if you're not sure about some fields
# filters = {
# "AND": [
# {"user_id": "exec_123"},
# {"agent_id": "*"}, # Match any agent
# {"app_id": "enterprise_portal"},
# {"run_id": "*"} # Match any run
# ]
# }
results = client.search("Hotels near conference", filters=filters)
# Debug: Print the filter you're using
print(f"Searching with filters: {filters}")
# If no results, try a broader search to see what's stored
if not results["results"]:
print("No results found! Trying broader search...")
broader = client.get_all(filters={"user_id": "exec_123"})
print(broader)
print(results["results"][0]["memory"])
```
**Output:**
```
I need a hotel near the conference center.
```
## When to Use Each Identifier
| Identifier | When to Use | Example Values |
|------------|-------------|----------------|
| `user_id` | Individual preferences that persist across all interactions | `cam_traveler`, `sarah_exec`, `team_alpha` |
| `agent_id` | Different AI roles need separate context | `travel_agent`, `concierge`, `customer_support` |
| `app_id` | White-label deployments or separate products | `travel_app_ios`, `enterprise_portal`, `partner_integration` |
| `run_id` | Temporary sessions that should be isolated | `support_ticket_9234`, `chat_session_456`, `booking_flow_789` |
## Troubleshooting Common Issues
### My search returns empty results!
**Problem**: Using `AND` with exact matches but some fields might be `null`.
**Solution**:
```python
# If this returns nothing:
filters = {"AND": [{"user_id": "u1"}, {"agent_id": "a1"}]}
# Try using wildcards:
filters = {"AND": [{"user_id": "u1"}, {"agent_id": "*"}]}
# Or don't include fields you don't need:
filters = {"AND": [{"user_id": "u1"}]}
```
### OR gives results but AND doesn't
This confirms you have a **field mismatch**. The memory exists but some identifier values don't match exactly.
**Always check what's actually stored:**
```python
# Get all memories for the user to see the actual field values
all_mems = client.get_all(filters={"user_id": "your_user_id"})
print(json.dumps(all_mems, indent=2))
```
## Best Practices
1. **Use consistent identifier formats**
```python
# Good: consistent patterns
user_id = "cam_traveler"
agent_id = "travel_agent_v1"
app_id = "nora_concierge_app"
run_id = "tokyo_trip_2025_03"
# Avoid: mixed patterns
# user_id = "123", agent_id = "agent2", app_id = "app"
```
2. **Print filters when debugging**
```python
filters = {"AND": [{"user_id": "cam", "agent_id": "chef"}]}
print(f"Searching with filters: {filters}") # Helps catch typos
```
3. **Clean up temporary sessions**
```python
# After a support ticket closes
client.delete_all(user_id="customer_123", run_id="ticket_456")
```
## Summary
You learned how to:
- Store memories with proper entity scoping using `user_id`, `agent_id`, `app_id`, and `run_id`
- Prevent memory leaks between different agents and applications
- Clean up data for specific tenants or sessions
- Use wildcards to query across scoped memories
## Next Steps
<CardGroup cols={2}>
<Card
title="Deep Dive: Memory Filters v2"
description="Layer entity filters with JSON logic to answer complex queries."
icon="sliders"
href="/platform/features/v2-memory-filters"
/>
<Card
title="Control Memory Ingestion"
description="Pair scoped storage with rules that block low-quality facts."
icon="shield-check"
href="/cookbooks/essentials/controlling-memory-ingestion"
/>
</CardGroup>
@@ -0,0 +1,289 @@
---
title: Export Stored Memories
description: "Retrieve, review, and migrate user memories with structured exports."
---
Mem0 is a dynamic memory store that gives you full control over your data. Along with storing memories, it gives you the ability to retrieve, export, and migrate your data whenever you need.
This cookbook shows you how to retrieve and export your data for inspection, migration, or compliance.
---
## Setup
```python
from mem0 import MemoryClient
client = MemoryClient(api_key="your-api-key")
```
<Note>
Your API key needs export permissions to download memory data. Check your project settings on the [dashboard](https://app.mem0.ai) if export operations fail with authentication errors.
</Note>
Let's add some sample memories to work with:
```python
# Dev's work history
client.add(
"Dev works at TechCorp as a senior engineer",
user_id="dev",
metadata={"type": "professional"}
)
# Arjun's preferences
client.add(
"Arjun prefers morning meetings and async communication",
user_id="arjun",
metadata={"type": "preference"}
)
# Carl's project notes
client.add(
"Carl is leading the API redesign project, targeting Q2 launch",
user_id="carl",
metadata={"type": "project"}
)
```
---
## Getting All Memories
Use `get_all()` with filters to retrieve everything for a specific user:
```python
dev_memories = client.get_all(
filters={"user_id": "dev"},
page_size=50
)
print(f"Total memories: {dev_memories['count']}")
print(f"First memory: {dev_memories['results'][0]['memory']}")
```
**Output:**
```
Total memories: 1
First memory: Dev works at TechCorp as a senior engineer
```
<Info>
**Expected output:** `get_all()` retrieved Dev's complete memory record. This method returns everything matching your filters—no semantic search, no ranking, just raw retrieval. Perfect for exports and audits.
</Info>
You can filter by metadata to get specific types:
```python
carl_projects = client.get_all(
filters={
"AND": [
{"user_id": "carl"},
{"metadata": {"type": "project"}}
]
}
)
for memory in carl_projects['results']:
print(memory['memory'])
```
**Output:**
```
Carl is leading the API redesign project, targeting Q2 launch
```
---
## Searching Memories
When you need semantic search instead of retrieving everything, use `search()`:
```python
results = client.search(
query="What does Dev do for work?",
filters={"user_id": "dev"},
top_k=5
)
for result in results['results']:
print(f"{result['memory']} (score: {result['score']:.2f})")
```
**Output:**
```
Dev works at TechCorp as a senior engineer (score: 0.89)
```
Search works across all memory fields and ranks by relevance. Use it when you have a specific question, use `get_all()` when you need everything.
---
## Exporting to Structured Format
For migrations or compliance, you can export memories into a structured schema using Pydantic-style JSON schemas.
### Step 1: Define the schema
```python
professional_profile_schema = {
"properties": {
"full_name": {
"type": "string",
"description": "The person's full name"
},
"current_role": {
"type": "string",
"description": "Current job title or role"
},
"company": {
"type": "string",
"description": "Current employer"
}
},
"title": "ProfessionalProfile",
"type": "object"
}
```
### Step 2: Create export job
```python
export_job = client.create_memory_export(
schema=professional_profile_schema,
filters={"user_id": "dev"}
)
print(f"Export ID: {export_job['id']}")
print(f"Status: {export_job['status']}")
```
**Output:**
```
Export ID: exp_abc123
Status: processing
```
<Info>
**Export initiated:** Status is "processing". Large exports may take a few seconds. Poll with `get_memory_export()` until status changes to "completed" before downloading data.
</Info>
### Step 3: Download the export
```python
# Get by ID
export_data = client.get_memory_export(
memory_export_id=export_job['id']
)
print(export_data['data'])
```
**Output:**
```json
{
"full_name": "Dev",
"current_role": "senior engineer",
"company": "TechCorp"
}
```
You can also retrieve exports by filters:
```python
# Get latest export matching filters
export_by_filters = client.get_memory_export(
filters={"user_id": "dev"}
)
print(export_by_filters['data'])
```
---
## Adding Export Instructions
Guide how Mem0 resolves conflicts or formats the export:
```python
export_with_instructions = client.create_memory_export(
schema=professional_profile_schema,
filters={"user_id": "arjun"},
export_instructions="""
1. Use the most recent information if there are conflicts
2. Only include confirmed facts, not speculation
3. Return null for missing fields rather than guessing
"""
)
```
<Tip>
Always check export status before downloading. Call `get_memory_export()` in a loop with a short delay until `status == "completed"`. Attempting to download while still processing returns incomplete data.
</Tip>
---
## Platform Export
You can also export memories directly from the Mem0 platform UI:
1. Navigate to **Memory Exports** in your project dashboard
2. Click **Create Export**
3. Select your filters and schema
4. Download the completed export as JSON
This is useful for one-off exports or manual data reviews.
<Warning>
Exported data expires after 7 days. Download and store exports locally if you need long-term archives. After expiration, you'll need to recreate the export job.
</Warning>
---
## What You Built
A complete memory export system with multiple retrieval methods:
- **Bulk retrieval (get_all)** - Fetch all memories matching filters for comprehensive audits
- **Semantic search** - Query-based lookups with relevance scoring
- **Structured exports** - Pydantic-schema exports for migrations and compliance
- **Export instructions** - Guide conflict resolution and data formatting
- **Platform UI exports** - One-off manual downloads via dashboard
This covers data portability, GDPR compliance, system migrations, and manual reviews.
---
## Summary
Use **`get_all()`** for bulk retrieval, **`search()`** for specific questions, and **`create_memory_export()`** for structured data exports with custom schemas. Remember exports expire after 7 days—download them locally for long-term archives.
<CardGroup cols={2}>
<Card title="Expire Short-Term Data" icon="timer" href="/cookbooks/essentials/memory-expiration-short-and-long-term">
Keep exports lean by clearing session context before you archive it.
</Card>
<Card title="Control Memory Ingestion" icon="filter" href="/cookbooks/essentials/controlling-memory-ingestion">
Ensure only verified insights make it into your export pipeline.
</Card>
</CardGroup>
@@ -0,0 +1,277 @@
---
title: Set Memory Expiration
description: "Define short-term versus long-term retention so the store stays fresh."
---
While building memory systems, we realized their size grows fast. Session notes, temporary context, chat history - everything starts accumulating and bogging down the system. This pollutes search results and increase storage costs. Not every memory needs to persist forever.
In this cookbook, we'll go through how to use short-term vs long-term memories and see where it's best to use them.
---
## Overview
By default, Mem0 memories persist forever. This works for user preferences and core facts, but temporary data should expire automatically.
In this tutorial, we will:
- Understand default (permanent) memory behavior
- Add expiration dates for temporary memories
- Decide what should be temporary vs permanent
---
## Setup
```python
from mem0 import MemoryClient
from datetime import datetime, timedelta
client = MemoryClient(api_key="your-api-key")
```
<Note>
Import `datetime` and `timedelta` to calculate expiration dates. Without these imports, you'll need to manually format ISO timestamps—error-prone and harder to read.
</Note>
---
## Default Behavior: Everything Persists
By default, all memories persist forever:
```python
# Store user preference
client.add("User prefers dark mode", user_id="sarah")
# Store session context
client.add("Currently browsing electronics category", user_id="sarah")
# 6 months later - both still exist
results = client.get_all(filters={"user_id": "sarah"})
print(f"Total memories: {len(results['results'])}")
```
**Output:**
```
Total memories: 2
```
Both the preference and session context persist. The preference is useful, but the 6-month-old session context is not.
---
## The Problem: Memory Bloat
Without expiration, memories accumulate forever. Session notes from weeks ago mix with current preferences. Storage grows, search results get polluted with irrelevant old context, and retrieval quality degrades.
<Warning>
Memory bloat degrades search quality. When "User prefers dark mode" competes with "Currently browsing electronics" from 6 months ago, semantic search returns stale session data instead of actual preferences. Old memories pollute retrieval.
</Warning>
---
## Short-Term Memories: Adding Expiration
Set `expiration_date` to make memories temporary:
```python
from datetime import datetime, timedelta
# Session context - expires in 7 days
expires_at = (datetime.now() + timedelta(days=7)).isoformat()
client.add(
"Currently browsing electronics category",
user_id="sarah",
expiration_date=expires_at
)
# User preference - no expiration, persists forever
client.add(
"User prefers dark mode",
user_id="sarah"
)
```
<Info icon="check">
**Expected behavior:** After 7 days, the session context automatically disappears—no cron jobs, no manual cleanup. The preference persists forever. Mem0 handles expiration transparently.
</Info>
Memories with `expiration_date` are automatically removed after expiring. No cleanup job needed - Mem0 handles it.
<Tip>
Start conservative with short expiration windows (7 days), then extend them based on usage patterns. It's easier to increase retention than to clean up over-retained stale data. Monitor search quality to find the right balance.
</Tip>
---
## When to Use Each
### Permanent Memories (no expiration_date):
**Use for:**
- User preferences and settings
- Account information
- Important facts and milestones
- Historical data that matters long-term
```python
client.add("User prefers email notifications", user_id="sarah")
client.add("User's birthday is March 15th", user_id="sarah")
client.add("User completed onboarding on Jan 5th", user_id="sarah")
```
### Temporary Memories (with expiration_date):
**Use for:**
- Session context (current page, browsing history)
- Temporary reminders
- Recent chat history
- Cached data
```python
expires_7d = (datetime.now() + timedelta(days=7)).isoformat()
client.add(
"Currently viewing product ABC123",
user_id="sarah",
expiration_date=expires_7d
)
client.add(
"Asked about return policy",
user_id="sarah",
expiration_date=expires_7d
)
```
---
## Setting Different Expiration Periods
Different data needs different lifetimes:
```python
# Session context - 7 days
expires_7d = (datetime.now() + timedelta(days=7)).isoformat()
client.add("Browsing electronics", user_id="sarah", expiration_date=expires_7d)
# Recent chat - 30 days
expires_30d = (datetime.now() + timedelta(days=30)).isoformat()
client.add("User asked about warranty", user_id="sarah", expiration_date=expires_30d)
# Important preference - no expiration
client.add("User prefers dark mode", user_id="sarah")
```
---
## Using Metadata to Track Memory Types
Tag memories to make filtering easier:
```python
expires_7d = (datetime.now() + timedelta(days=7)).isoformat()
# Tag session context
client.add(
"Browsing electronics",
user_id="sarah",
expiration_date=expires_7d,
metadata={"type": "session"}
)
# Tag preference
client.add(
"User prefers dark mode",
user_id="sarah",
metadata={"type": "preference"}
)
# Query only preferences
preferences = client.get_all(
filters={
"AND": [
{"user_id": "sarah"},
{"metadata": {"type": "preference"}}
]
}
)
```
---
## Checking Expiration Status
See which memories will expire and when:
```python
results = client.get_all(filters={"user_id": "sarah"})
for memory in results['results']:
exp_date = memory.get('expiration_date')
if exp_date:
print(f"Temporary: {memory['memory']}")
print(f" Expires: {exp_date}\\n")
else:
print(f"Permanent: {memory['memory']}\\n")
```
**Output:**
```
Temporary: Browsing electronics
Expires: 2025-11-01T10:30:00Z
Temporary: Viewed MacBook Pro and Dell XPS
Expires: 2025-11-01T10:30:00Z
Permanent: User prefers dark mode
Permanent: User prefers email notifications
```
---
## What You Built
A self-cleaning memory system with automatic retention policies:
- **Automatic expiration** - Memories self-destruct after defined periods, no cron jobs needed
- **Tiered retention** - 7-day session context, 30-day chat history, permanent preferences
- **Metadata tagging** - Classify memories by type (session, preference, chat) for filtered retrieval
- **Expiration tracking** - Check which memories will expire and when using `get_all()`
This pattern keeps storage costs low and search quality high as your memory store scales.
---
## Summary
Memory expiration keeps storage clean and search results relevant. Use **`expiration_date`** for temporary data (session context, recent chats), skip it for permanent facts (preferences, account info). Mem0 handles cleanup automatically—no background jobs required.
Start by identifying what's temporary versus permanent, then set conservative expiration windows and adjust based on retrieval quality.
<CardGroup cols={2}>
<Card title="Control Memory Ingestion" icon="filter" href="/cookbooks/essentials/controlling-memory-ingestion">
Pair expirations with ingestion rules so only trusted context persists.
</Card>
<Card title="Export Memories Safely" icon="download" href="/cookbooks/essentials/exporting-memories">
Build compliant archives once your retention windows are dialed in.
</Card>
</CardGroup>

Some files were not shown because too many files have changed in this diff Show More