Add persistent memory to Vercel AI SDK applications.

Vercel AI SDK Integration

CortexDB integrates with the Vercel AI SDK to provide persistent memory for chat applications, AI assistants, and agents built with Next.js.

Installation

npm install @cortexdb/vercel-ai

Setup

import { openai } from "@ai-sdk/openai";
import { streamText } from "ai";
import { cortexMemory } from "@cortexdb/vercel-ai";

const memory = cortexMemory({
  apiKey: process.env.CORTEX_API_KEY!,
  tenantId: "my-app",
});

export async function POST(req: Request) {
  const { messages } = await req.json();

  // Recall relevant context
  const lastMessage = messages[messages.length - 1].content;
  const context = await memory.recall(lastMessage);

  // Augment the system message with memory
  const augmentedMessages = [
    {
      role: "system" as const,
      content: `You are a helpful assistant. Here is relevant context from previous conversations:\n${context}`,
    },
    ...messages,
  ];

  const result = streamText({
    model: openai("gpt-4o"),
    messages: augmentedMessages,
    onFinish: async ({ text }) => {
      // Store the exchange in memory
      await memory.remember(lastMessage, { author: "user" });
      await memory.remember(text, { author: "assistant" });
    },
  });

  return result.toDataStreamResponse();
}

With useChat Hook

// app/page.tsx
"use client";

import { useChat } from "ai/react";

export default function Chat() {
  const { messages, input, handleInputChange, handleSubmit } = useChat({
    api: "/api/chat",
  });

  return (
    <div>
      {messages.map((m) => (
        <div key={m.id}>
          <strong>{m.role}:</strong> {m.content}
        </div>
      ))}
      <form onSubmit={handleSubmit}>
        <input value={input} onChange={handleInputChange} />
        <button type="submit">Send</button>
      </form>
    </div>
  );
}

Configuration

| Parameter | Default | Description | |---|---|---| | apiKey | $CORTEX_API_KEY | CortexDB API key | | baseUrl | https://api.cortexdb.io | Server URL | | tenantId | Required | Tenant identifier | | namespace | undefined | Memory namespace | | topK | 10 | Results per recall |