Skip to content

Commit 06df6ca

Browse files
Add Mem0 Memory Integration (langchain-ai#7865)
Co-authored-by: jacoblee93 <jacoblee93@gmail.com>
1 parent 39dc373 commit 06df6ca

File tree

10 files changed

+711
-10
lines changed

10 files changed

+711
-10
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
hide_table_of_contents: true
3+
---
4+
5+
import CodeBlock from "@theme/CodeBlock";
6+
7+
# Mem0 Memory
8+
9+
[Mem0](https://github.com/mem0ai/mem0) is a self-improving memory layer for LLM applications, enabling personalized AI experiences that save costs and delight users.
10+
11+
## Setup
12+
13+
Goto [Mem0 Dashboard](https://app.mem0.ai) to get API keys for Mem0.
14+
15+
## Usage
16+
17+
import Example from "@examples/memory/mem0.ts";
18+
19+
import IntegrationInstallTooltip from "@mdx_components/integration_install_tooltip.mdx";
20+
21+
<IntegrationInstallTooltip></IntegrationInstallTooltip>
22+
23+
```bash npm2yarn
24+
npm install @langchain/openai @langchain/core @langchain/community
25+
```
26+
27+
<CodeBlock language="typescript">{Example}</CodeBlock>

examples/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
"langchain": "workspace:*",
9797
"langsmith": ">=0.2.8 <0.4.0",
9898
"mariadb": "^3.4.0",
99+
"mem0ai": "^2.1.8",
99100
"mongodb": "^6.3.0",
100101
"pg": "^8.11.0",
101102
"pickleparser": "^0.2.1",

examples/src/memory/mem0.ts

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { ChatOpenAI } from "@langchain/openai";
2+
import { ConversationChain } from "langchain/chains";
3+
import { Mem0Memory } from "@langchain/community/memory/mem0";
4+
import { randomUUID } from "crypto";
5+
6+
const sessionId = randomUUID(); // This should be unique for each user or each user's session.
7+
8+
const memory = new Mem0Memory({
9+
apiKey: "your-api-key",
10+
sessionId,
11+
memoryOptions: {
12+
run_id: "run123", // Optional, if you want to save the conversation to a specific run.
13+
},
14+
});
15+
16+
const model = new ChatOpenAI({
17+
modelName: "gpt-3.5-turbo",
18+
temperature: 0,
19+
});
20+
21+
const chain = new ConversationChain({ llm: model, memory });
22+
console.log("Memory Keys:", memory.memoryKeys);
23+
24+
const res1 = await chain.invoke({
25+
input: "Hi! I am Jim and I live in Finland",
26+
});
27+
console.log({ res1 });
28+
/*
29+
{
30+
res1: {
31+
response: "Hello Jim! It's nice to meet you. My name is AI. How may I assist you today?"
32+
}
33+
}
34+
*/
35+
36+
const res2 = await chain.invoke({ input: "What did I just say my name was?" });
37+
console.log({ res2 });
38+
39+
/*
40+
{
41+
res2: {
42+
response: "You said your name was Jim."
43+
}
44+
}
45+
*/
46+
47+
const res3 = await chain.invoke({ input: "Where do I live?" });
48+
console.log({ res3 });
49+
50+
/*
51+
{
52+
res3: {
53+
response: "You live in Finland, Jim."
54+
}
55+
}
56+
*/
57+
58+
console.log("Session ID: ", sessionId);
59+
console.log("Memory: ", await memory.loadMemoryVariables({}));

libs/langchain-community/.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -870,6 +870,10 @@ memory/chat_memory.cjs
870870
memory/chat_memory.js
871871
memory/chat_memory.d.ts
872872
memory/chat_memory.d.cts
873+
memory/mem0.cjs
874+
memory/mem0.js
875+
memory/mem0.d.ts
876+
memory/mem0.d.cts
873877
memory/motorhead_memory.cjs
874878
memory/motorhead_memory.js
875879
memory/motorhead_memory.d.ts

libs/langchain-community/langchain.config.js

+2
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ export const config = {
269269
"stores/message/zep_cloud": "stores/message/zep_cloud",
270270
// memory
271271
"memory/chat_memory": "memory/chat_memory",
272+
"memory/mem0": "memory/mem0",
272273
"memory/motorhead_memory": "memory/motorhead_memory",
273274
"memory/zep": "memory/zep",
274275
"memory/zep_cloud": "memory/zep_cloud",
@@ -501,6 +502,7 @@ export const config = {
501502
"stores/message/xata",
502503
"stores/message/zep_cloud",
503504
// memory
505+
"memory/mem0",
504506
"memory/motorhead_memory",
505507
"memory/zep",
506508
"memory/zep_cloud",

libs/langchain-community/package.json

+18
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@
190190
"lunary": "^0.7.10",
191191
"mammoth": "^1.6.0",
192192
"mariadb": "^3.4.0",
193+
"mem0ai": "^2.1.8",
193194
"mongodb": "^5.2.0",
194195
"mysql2": "^3.9.8",
195196
"neo4j-driver": "^5.17.0",
@@ -323,6 +324,7 @@
323324
"lunary": "^0.7.10",
324325
"mammoth": "^1.6.0",
325326
"mariadb": "^3.4.0",
327+
"mem0ai": "^2.1.8",
326328
"mongodb": ">=5.2.0",
327329
"mysql2": "^3.9.8",
328330
"neo4j-driver": "*",
@@ -640,6 +642,9 @@
640642
"mariadb": {
641643
"optional": true
642644
},
645+
"mem0ai": {
646+
"optional": true
647+
},
643648
"mongodb": {
644649
"optional": true
645650
},
@@ -2685,6 +2690,15 @@
26852690
"import": "./memory/chat_memory.js",
26862691
"require": "./memory/chat_memory.cjs"
26872692
},
2693+
"./memory/mem0": {
2694+
"types": {
2695+
"import": "./memory/mem0.d.ts",
2696+
"require": "./memory/mem0.d.cts",
2697+
"default": "./memory/mem0.d.ts"
2698+
},
2699+
"import": "./memory/mem0.js",
2700+
"require": "./memory/mem0.cjs"
2701+
},
26882702
"./memory/motorhead_memory": {
26892703
"types": {
26902704
"import": "./memory/motorhead_memory.d.ts",
@@ -4119,6 +4133,10 @@
41194133
"memory/chat_memory.js",
41204134
"memory/chat_memory.d.ts",
41214135
"memory/chat_memory.d.cts",
4136+
"memory/mem0.cjs",
4137+
"memory/mem0.js",
4138+
"memory/mem0.d.ts",
4139+
"memory/mem0.d.cts",
41224140
"memory/motorhead_memory.cjs",
41234141
"memory/motorhead_memory.js",
41244142
"memory/motorhead_memory.d.ts",

libs/langchain-community/src/load/import_constants.ts

+1
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ export const optionalImportEntrypoints: string[] = [
141141
"langchain_community/stores/message/upstash_redis",
142142
"langchain_community/stores/message/xata",
143143
"langchain_community/stores/message/zep_cloud",
144+
"langchain_community/memory/mem0",
144145
"langchain_community/memory/motorhead_memory",
145146
"langchain_community/memory/zep",
146147
"langchain_community/memory/zep_cloud",

0 commit comments

Comments
 (0)