Mem0
Learn how to use the Mem0 AI SDK provider for the AI SDK.
The Mem0 Provider is a library developed by Mem0 to integrate with the AI SDK. This library brings enhanced AI interaction capabilities to your applications by introducing persistent memory functionality.
🎉 Exciting news! Mem0 AI SDK now supports Tools Call.
Setup
The Mem0 provider is available in the @mem0/vercel-ai-provider module. You can install it with:
$ pnpm add @mem0/vercel-ai-provider
$ npm install @mem0/vercel-ai-provider
$ yarn add @mem0/vercel-ai-provider
$ bun add @mem0/vercel-ai-provider
Provider Instance
First, get your Mem0 API Key from the Mem0 Dashboard.
Then initialize the Mem0 Client in your application:
The openai provider is set as default. Consider using MEM0_API_KEY and
OPENAI_API_KEY as environment variables for security.
The mem0Config is optional. It is used to set the global config for the Mem0
Client (eg. user_id, agent_id, app_id, run_id, org_id, project_id
etc).
- Add Memories to Enhance Context:
Features
Adding and Retrieving Memories
retrieveMemories(): Retrieves memory context for prompts.getMemories(): Get memories from your profile in array format.addMemories(): Adds user memories to enhance contextual responses.
For standalone features, such as addMemories, retrieveMemories, and
getMemories, you must either set MEM0_API_KEY as an environment variable
or pass it directly in the function call.
getMemories will return raw memories in the form of an array of objects,
while retrieveMemories will return a response in string format with a system
prompt ingested with the retrieved memories.
Generate Text with Memory Context
You can use language models from OpenAI, Anthropic, Cohere, and Groq to generate text with the generateText function:
Structured Message Format with Memory
Streaming Responses with Memory Context
Generate Responses with Tools Call
Get sources from memory
This same functionality is available in the streamText function.
Supported LLM Providers
The Mem0 provider supports the following LLM providers:
| Provider | Configuration Value |
|---|---|
| OpenAI | openai |
| Anthropic | anthropic |
google | |
| Groq | groq |
| Cohere | cohere |
Best Practices
- User Identification: Use a unique
user_idfor consistent memory retrieval. - Memory Cleanup: Regularly clean up unused memory data.
We also have support for agent_id, app_id, and run_id. Refer
Docs.
Help
- For more details on Vercel AI SDK, visit the Vercel AI SDK documentation.
- For Mem0 documentation, refer to the Mem0 Platform.
- If you need further assistance, please feel free to reach out to us through following methods: