Docs/AI SDK

Memory

RAG

Last updated March 3, 2026

Retrieval-Augmented Generation for context-aware AI responses.

RAG (Retrieval-Augmented Generation) combines memory search with AI generation for context-aware responses.

How It Works

  1. User sends a query
  2. Relevant memories are retrieved
  3. Context is injected into the prompt
  4. AI generates a grounded response

Basic Usage

Codetext
const response = await cencori.ai.rag({
  model: 'gpt-4o',
  messages: [{ role: 'user', content: 'What is the refund policy?' }],
  namespace: 'docs'
});
 
console.log(response.content);
// "Based on your documentation, the refund policy allows returns within 30 days..."
 
console.log(response.sources);
// [{ id: '...', content: '...', score: 0.95 }]

Parameters

ParameterTypeRequiredDescription
modelstringYesChat model
messagesarrayYesConversation
namespacestringYesMemory namespace
limitnumberNoMax context items (default: 5)
thresholdnumberNoMin relevance score
filterobjectNoMetadata filter

Custom System Prompt

Codetext
const response = await cencori.ai.rag({
  model: 'gpt-4o',
  messages: [{ role: 'user', content: 'What is the refund policy?' }],
  namespace: 'docs',
  systemPrompt: `You are a helpful customer support agent. 
    Answer questions based on the provided context. 
    If you don't know, say so.`
});

With Metadata Filter

Codetext
const response = await cencori.ai.rag({
  model: 'gpt-4o',
  messages: [{ role: 'user', content: 'How do I get a refund?' }],
  namespace: 'docs',
  filter: {
    category: 'refunds',
    language: 'en'
  }
});

Manual RAG

For more control, implement RAG manually:

Codetext
// 1. Search for relevant context
const context = await cencori.memory.search({
  namespace: 'docs',
  query: 'refund policy',
  limit: 5
});
 
// 2. Build context string
const contextText = context
  .map(r => r.content)
  .join('\n\n');
 
// 3. Generate with context
const response = await cencori.ai.chat({
  model: 'gpt-4o',
  messages: [
    {
      role: 'system',
      content: `Answer based on this context:\n\n${contextText}`
    },
    { role: 'user', content: 'What is the refund policy?' }
  ]
});

Best Practices

PracticeDescription
Chunk contentSplit long documents into smaller pieces
Include metadataAdd source, date, category for filtering
Set thresholdFilter low-relevance results
Use citationsShow users where answers came from