Skip to content

RetainDB/retaindb-express-starter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

retaindb-express-starter

Express.js chat API with persistent memory powered by RetainDB and the Vercel AI SDK.

Drop-in backend for any frontend using useChat — memories survive restarts and new sessions automatically.

Stack

  • Express.js — HTTP server
  • Vercel AI SDKstreamText + data stream protocol
  • OpenAI gpt-4o-mini — chat completions
  • RetainDB — persistent memory via withRetainDB adapter

Quickstart

# 1. Clone
git clone https://github.com/Alinxus/retaindb-express-starter
cd retaindb-express-starter

# 2. Install
npm install

# 3. Configure
cp .env.example .env
# Edit .env with your keys

# 4. Run (dev, with hot reload)
npm run dev

API

POST /api/chat

Compatible with Vercel AI SDK's useChat hook on the frontend.

curl -X POST http://localhost:3001/api/chat \
  -H "Content-Type: application/json" \
  -d '{
    "userId": "alice",
    "messages": [{"role": "user", "content": "My favourite language is TypeScript"}]
  }'

Response streams as Vercel AI data stream protocol (text/plain + X-Vercel-AI-Data-Stream: v1).

How it works

The withRetainDB adapter wraps the OpenAI model bidirectionally:

POST /api/chat
  │
  ├─► withRetainDB intercepts doStream
  │     ├─► Retrieves relevant memories from RetainDB
  │     └─► Injects them into the system prompt
  │
  ├─► gpt-4o-mini streams reply
  │
  └─► withRetainDB fires background remember() after streaming
        └─► Stores the user turn in RetainDB for future sessions

Connecting a frontend

Use useChat from ai/react with your Express server URL:

import { useChat } from 'ai/react';

export function Chat() {
  const { messages, input, handleSubmit, handleInputChange } = useChat({
    api: 'http://localhost:3001/api/chat',
    body: { userId: 'alice' },
  });
  // ...
}

Environment variables

Variable Description
RETAINDB_API_KEY Get free at retaindb.com
OPENAI_API_KEY Get at platform.openai.com
PORT Server port (default 3001)

Related starters

Releases

No releases published

Packages

 
 
 

Contributors