|
| 1 | +--- |
| 2 | +title: 'Supermemory' |
| 3 | +description: 'Learn how to use the Supermemory AI SDK provider for the Vercel AI SDK.' |
| 4 | +--- |
| 5 | + |
| 6 | +# Supermemory |
| 7 | + |
| 8 | +[Supermemory](https://supermemory.ai) is a long-term memory platform that adds persistent, self-growing memory to your AI applications. The Supermemory provider for the AI SDK enables you to build AI applications with memory that works like the human brain: |
| 9 | + |
| 10 | +- **Persistent Memory**: Long-term storage that grows with each interaction |
| 11 | +- **Semantic Search**: Find relevant memories using natural language queries |
| 12 | +- **Automatic Memory Management**: AI automatically saves and retrieves relevant information |
| 13 | +- **Easy Integration**: Simple setup with existing AI SDK applications |
| 14 | +- **Memory Router**: Direct integration with language model providers |
| 15 | +- **Free Tier Available**: Get started with a free API key |
| 16 | + |
| 17 | +Learn more about Supermemory's capabilities in the [Supermemory Documentation](https://supermemory.ai/docs/ai-sdk/overview). |
| 18 | + |
| 19 | +## Setup |
| 20 | + |
| 21 | +The Supermemory provider is available in the `@supermemory/tools` module. You can install it with: |
| 22 | + |
| 23 | +<Tabs items={['pnpm', 'npm', 'yarn', 'bun']}> |
| 24 | + <Tab> |
| 25 | + <Snippet text="pnpm add @supermemory/tools" dark /> |
| 26 | + </Tab> |
| 27 | + <Tab> |
| 28 | + <Snippet text="npm install @supermemory/tools" dark /> |
| 29 | + </Tab> |
| 30 | + <Tab> |
| 31 | + <Snippet text="yarn add @supermemory/tools" dark /> |
| 32 | + </Tab> |
| 33 | + <Tab> |
| 34 | + <Snippet text="bun add @supermemory/tools" dark /> |
| 35 | + </Tab> |
| 36 | +</Tabs> |
| 37 | + |
| 38 | +## Provider Instance |
| 39 | + |
| 40 | +You can obtain your Supermemory API key for free at [https://console.supermemory.ai](https://console.supermemory.ai). |
| 41 | + |
| 42 | +There are two ways to integrate Supermemory with your AI applications: |
| 43 | + |
| 44 | +**1. Using Supermemory Tools** |
| 45 | + |
| 46 | +Import and use Supermemory tools with your existing AI SDK setup: |
| 47 | + |
| 48 | +```typescript |
| 49 | +import { supermemoryTools } from '@supermemory/tools/ai-sdk'; |
| 50 | +``` |
| 51 | + |
| 52 | +**2. Using the Memory Router** |
| 53 | + |
| 54 | +Use the Memory Router for direct integration with language model providers: |
| 55 | + |
| 56 | +```typescript |
| 57 | +import { createAnthropic } from '@ai-sdk/anthropic'; |
| 58 | + |
| 59 | +const supermemoryRouter = createAnthropic({ |
| 60 | + baseUrl: 'https://api.supermemory.ai/v3/https://api.anthropic.com/v1', |
| 61 | + apiKey: 'your-provider-api-key', |
| 62 | + headers: { |
| 63 | + 'x-supermemory-api-key': 'supermemory-api-key', |
| 64 | + 'x-sm-conversation-id': 'conversation-id', |
| 65 | + }, |
| 66 | +}); |
| 67 | +``` |
| 68 | + |
| 69 | +## Examples |
| 70 | + |
| 71 | +Here are examples of using Supermemory with the AI SDK: |
| 72 | + |
| 73 | +### `generateText` with Tools |
| 74 | + |
| 75 | +```javascript |
| 76 | +import { generateText } from 'ai'; |
| 77 | +import { createOpenAI } from '@ai-sdk/openai'; |
| 78 | +import { supermemoryTools } from '@supermemory/tools/ai-sdk'; |
| 79 | + |
| 80 | +const openai = createOpenAI({ |
| 81 | + apiKey: 'YOUR_OPENAI_KEY', |
| 82 | +}); |
| 83 | + |
| 84 | +const { text } = await generateText({ |
| 85 | + model: openai('gpt-4-turbo'), |
| 86 | + prompt: 'Remember that my name is Alice', |
| 87 | + tools: supermemoryTools('YOUR_SUPERMEMORY_KEY'), |
| 88 | +}); |
| 89 | + |
| 90 | +console.log(text); |
| 91 | +``` |
| 92 | + |
| 93 | +### `streamText` with Automatic Memory |
| 94 | + |
| 95 | +```javascript |
| 96 | +import { streamText } from 'ai'; |
| 97 | +import { createOpenAI } from '@ai-sdk/openai'; |
| 98 | +import { supermemoryTools } from '@supermemory/tools/ai-sdk'; |
| 99 | + |
| 100 | +const openai = createOpenAI({ |
| 101 | + apiKey: 'YOUR_OPENAI_KEY', |
| 102 | +}); |
| 103 | + |
| 104 | +const result = streamText({ |
| 105 | + model: openai('gpt-4'), |
| 106 | + prompt: 'What are my dietary preferences?', |
| 107 | + tools: supermemoryTools('YOUR_SUPERMEMORY_KEY'), |
| 108 | +}); |
| 109 | + |
| 110 | +// The AI will automatically call searchMemories tool |
| 111 | +// Example tool call: |
| 112 | +// searchMemories({ informationToGet: "dietary preferences and restrictions" }) |
| 113 | +// OR |
| 114 | +// addMemory({ memory: "User is allergic to peanuts" }) |
| 115 | + |
| 116 | +for await (const chunk of result.textStream) { |
| 117 | + console.log(chunk); |
| 118 | +} |
| 119 | +``` |
| 120 | + |
| 121 | +### Using Memory Router |
| 122 | + |
| 123 | +```javascript |
| 124 | +import { streamText } from 'ai'; |
| 125 | +import { createAnthropic } from '@ai-sdk/anthropic'; |
| 126 | + |
| 127 | +const supermemoryRouter = createAnthropic({ |
| 128 | + baseUrl: 'https://api.supermemory.ai/v3/https://api.anthropic.com/v1', |
| 129 | + apiKey: 'your-provider-api-key', |
| 130 | + headers: { |
| 131 | + 'x-supermemory-api-key': 'supermemory-api-key', |
| 132 | + 'x-sm-conversation-id': 'conversation-id', |
| 133 | + }, |
| 134 | +}); |
| 135 | + |
| 136 | +const result = streamText({ |
| 137 | + model: supermemoryRouter('claude-3-sonnet'), |
| 138 | + messages: [ |
| 139 | + { role: 'user', content: 'Hello! Remember that I love TypeScript.' }, |
| 140 | + ], |
| 141 | +}); |
| 142 | +``` |
| 143 | + |
| 144 | +For more information about these features and advanced configuration options, visit the [Supermemory Documentation](https://supermemory.ai/docs/). |
| 145 | + |
| 146 | +## Additional Resources |
| 147 | + |
| 148 | +- [Supermemory Documentation](https://supermemory.ai/docs/?ref=ai-sdk) |
| 149 | +- [AI SDK Integration Cookbook](https://supermemory.ai/docs/cookbook/ai-sdk-integration) |
| 150 | +- [Supermemory Console](https://console.supermemory.ai) |
| 151 | +- [Memory Engine Blog Post](https://supermemory.ai/blog/memory-engine/) |
0 commit comments