Skip to content

Commit 6a00643

Browse files
authored
Docs: Integration/Vercel AI SDK (mem0ai#2009)
1 parent 2e74667 commit 6a00643

File tree

3 files changed

+170
-1
lines changed

3 files changed

+170
-1
lines changed

docs/integrations/vercel-ai-sdk.mdx

+168
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
---
2+
title: Vercel AI SDK
3+
---
4+
5+
The [**Mem0 AI SDK Provider**](https://www.npmjs.com/package/@mem0/vercel-ai-provider) is a library developed by **Mem0** to integrate with the Vercel AI SDK. This library brings enhanced AI interaction capabilities to your applications by introducing persistent memory functionality.
6+
7+
<Note type="info">
8+
🎉 Exciting news! Mem0 AI SDK now supports **OpenAI**, **Anthropic**, **Cohere**, and **Groq** providers.
9+
</Note>
10+
11+
## Overview
12+
13+
In this guide, we'll create a Travel Agent AI that:
14+
1. 🧠 Offers persistent memory storage for conversational AI
15+
2. 🔄 Enables smooth integration with the Vercel AI SDK
16+
3. 🚀 Ensures compatibility with multiple LLM providers
17+
4. 📝 Supports structured message formats for clarity
18+
5. ⚡ Facilitates streaming response capabilities
19+
20+
## Setup and Configuration
21+
22+
Install the SDK provider using npm:
23+
24+
```bash
25+
npm install @mem0/vercel-ai-provider
26+
```
27+
28+
## Getting Started
29+
30+
### Setting Up Mem0
31+
32+
1. Get your **Mem0 API Key** from the [Mem0 Dashboard](https://app.mem0.ai/dashboard/api-keys).
33+
34+
2. Initialize the Mem0 Client in your application:
35+
36+
```typescript
37+
import { createMem0 } from "@mem0/vercel-ai-provider";
38+
39+
const mem0 = createMem0({
40+
provider: "openai",
41+
mem0ApiKey: "m0-xxx",
42+
apiKey: "provider-api-key",
43+
config: {
44+
compatibility: "strict",
45+
},
46+
});
47+
```
48+
49+
> **Note**: The `openai` provider is set as default. Consider using `MEM0_API_KEY` and `OPENAI_API_KEY` as environment variables for security.
50+
51+
3. Add Memories to Enhance Context:
52+
53+
```typescript
54+
import { LanguageModelV1Prompt } from "ai";
55+
import { addMemories } from "@mem0/vercel-ai-provider";
56+
57+
const messages: LanguageModelV1Prompt = [
58+
{ role: "user", content: [{ type: "text", text: "I love red cars." }] },
59+
];
60+
61+
await addMemories(messages, { user_id: "borat" });
62+
```
63+
64+
### Standalone Features:
65+
66+
```typescript
67+
await addMemories(messages, { user_id: "borat", mem0ApiKey: "m0-xxx" });
68+
await retrieveMemories(prompt, { user_id: "borat", mem0ApiKey: "m0-xxx" });
69+
```
70+
> **Note**: For standalone features, such as `addMemories` and `retrieveMemories`, you must either set `MEM0_API_KEY` as an environment variable or pass it directly in the function call.
71+
72+
### 1. Basic Text Generation with Memory Context
73+
74+
```typescript
75+
import { generateText } from "ai";
76+
import { createMem0 } from "@mem0/vercel-ai-provider";
77+
78+
const mem0 = createMem0();
79+
80+
const { text } = await generateText({
81+
model: mem0("gpt-4-turbo", { user_id: "borat" }),
82+
prompt: "Suggest me a good car to buy!",
83+
});
84+
```
85+
86+
### 2. Combining OpenAI Provider with Memory Utils
87+
88+
```typescript
89+
import { generateText } from "ai";
90+
import { openai } from "@ai-sdk/openai";
91+
import { retrieveMemories } from "@mem0/vercel-ai-provider";
92+
93+
const prompt = "Suggest me a good car to buy.";
94+
const memories = await retrieveMemories(prompt, { user_id: "borat" });
95+
96+
const { text } = await generateText({
97+
model: openai("gpt-4-turbo"),
98+
prompt: prompt,
99+
system: memories,
100+
});
101+
```
102+
103+
### 3. Structured Message Format with Memory
104+
105+
```typescript
106+
import { generateText } from "ai";
107+
import { createMem0 } from "@mem0/vercel-ai-provider";
108+
109+
const mem0 = createMem0();
110+
111+
const { text } = await generateText({
112+
model: mem0("gpt-4-turbo", { user_id: "borat" }),
113+
messages: [
114+
{
115+
role: "user",
116+
content: [
117+
{ type: "text", text: "Suggest me a good car to buy." },
118+
{ type: "text", text: "Why is it better than the other cars for me?" },
119+
],
120+
},
121+
],
122+
});
123+
```
124+
125+
### 3. Streaming Responses with Memory Context
126+
127+
```typescript
128+
import { streamText } from "ai";
129+
import { createMem0 } from "@mem0/vercel-ai-provider";
130+
131+
const mem0 = createMem0();
132+
133+
const { textStream } = await streamText({
134+
model: mem0("gpt-4-turbo", {
135+
user_id: "borat",
136+
}),
137+
prompt: "Suggest me a good car to buy! Why is it better than the other cars for me? Give options for every price range.",
138+
});
139+
140+
for await (const textPart of textStream) {
141+
process.stdout.write(textPart);
142+
}
143+
```
144+
145+
## Key Features
146+
147+
- `createMem0()`: Initializes a new Mem0 provider instance.
148+
- `retrieveMemories()`: Retrieves memory context for prompts.
149+
- `addMemories()`: Adds user memories to enhance contextual responses.
150+
151+
## Best Practices
152+
153+
1. **User Identification**: Use a unique `user_id` for consistent memory retrieval.
154+
2. **Memory Cleanup**: Regularly clean up unused memory data.
155+
156+
> **Note**: We also have support for `agent_id`, `app_id`, and `run_id`. Refer [Docs](/api-reference/memory/add-memories).
157+
158+
## Conclusion
159+
160+
Mem0’s Vercel AI SDK enables the creation of intelligent, context-aware applications with persistent memory and seamless integration.
161+
162+
## Help
163+
164+
- For more details on LangGraph, visit the [Vercel AI SDK documentation](https://sdk.vercel.ai/docs/introduction).
165+
- For Mem0 documentation, refer to the [Mem0 Platform](https://app.mem0.ai/).
166+
- If you need further assistance, please feel free to reach out to us through following methods:
167+
168+
<Snippet file="get-help.mdx" />

docs/mint.json

+1
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@
207207
{
208208
"group": "Integrations",
209209
"pages": [
210+
"integrations/vercel-ai-sdk",
210211
"integrations/multion",
211212
"integrations/autogen",
212213
"integrations/langchain",

docs/overview.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Overview
33
---
44

55
<Note type="info">
6-
🎉 Exciting news! Mem0 now supports all the latest [Claude models](https://www.anthropic.com/news/3-5-models-and-computer-use).
6+
🎉 Exciting news! Mem0 now supports [Vercel's AI SDK](/integrations/vercel-ai-sdk).
77
</Note>
88

99
[Mem0](https://mem0.dev/wd) (pronounced "mem-zero") enhances AI assistants and agents with an intelligent memory layer, enabling personalized AI interactions. Mem0 remembers user preferences and traits and continuously updates over time, making it ideal for applications like customer support chatbots and AI assistants.

0 commit comments

Comments
 (0)