Day 04 Advanced Patterns

RAG: Answer Questions from Your Own Documents

RAG (Retrieval-Augmented Generation) lets your chatbot answer questions based on your specific documents — not just what Claude already knows. Day 4 builds

~1 hour Hands-on Precision AI Academy

Today's Objective

Learn the core concepts of RAG: Answer Questions from Your Own Documents and apply them in practical exercises.

01

What RAG Is

RAG works in two steps: first, find the relevant parts of your documents that match the user's question (retrieval). Second, include those parts in the prompt and ask Claude to answer based on them (generation). The simplest implementation uses text search rather than vector embeddings — good enough for most use cases.

Terminal — Install Dependencies
Terminal — Install Dependencies
npm install natural
rag.js — Document Store and Retrieval
rag.js — Document Store and Retrieval
// Simple keyword-based retrieval
class DocumentStore {
  constructor() {
    this.chunks = [];
  }
  
  addDocument(text, metadata = {}) {
    // Split into ~500 character chunks with overlap
    const words = text.split(/\s+/);
    const chunkSize = 80; // words
    const overlap = 20;
    
    for (let i = 0; i < words.length; i += (chunkSize - overlap)) {
      const chunk = words.slice(i, i + chunkSize).join(' ');
      if (chunk.trim()) {
        this.chunks.push({ text: chunk, metadata });
      }
    }
  }
  
  retrieve(query, topK = 3) {
    const queryWords = new Set(
      query.toLowerCase().split(/\W+/).filter(w => w.length > 3)
    );
    
    const scored = this.chunks.map(chunk => {
      const chunkWords = new Set(
        chunk.text.toLowerCase().split(/\W+/).filter(w => w.length > 3)
      );
      let score = 0;
      for (const word of queryWords) {
        if (chunkWords.has(word)) score++;
      }
      return { ...chunk, score };
    });
    
    return scored
      .sort((a, b) => b.score - a.score)
      .slice(0, topK)
      .filter(c => c.score > 0);
  }
}

module.exports = DocumentStore;
server.js — Add RAG to the /chat Route
server.js — Add RAG to the /chat Route
const DocumentStore = require('./rag');
const docStore = new DocumentStore();

// Load your documents at startup
docStore.addDocument(`
  Your company knowledge base text goes here.
  This can be product documentation, FAQs,
  policies, or any text you want the bot to
  know about. Add as much as you need.
`, { source: 'knowledge-base' });

// Update the /chat route to use RAG
app.post('/chat', async (req, res) => {
  const { message, sessionId } = req.body;
  
  if (!conversations[sessionId]) {
    conversations[sessionId] = [];
  }
  
  // Retrieve relevant chunks
  const chunks = docStore.retrieve(message, 3);
  const context = chunks.length > 0
    ? 'Relevant information:
' + chunks.map(c => c.text).join('

')
    : '';
  
  conversations[sessionId].push({
    role: 'user',
    content: message
  });
  
  const systemPrompt = `You are a helpful assistant.
${context ? context + '

Use the above information to answer if relevant.' : ''}
If you don't know something, say so honestly.`;
  
  const response = await client.messages.create({
    model: 'claude-opus-4-5',
    max_tokens: 1024,
    system: systemPrompt,
    messages: conversations[sessionId]
  });
  
  const reply = response.content[0].text;
  conversations[sessionId].push({ role: 'assistant', content: reply });
  
  res.json({ reply, sourcesFound: chunks.length });
});

Supporting References & Reading

Go deeper with these external resources.

Anthropic Docs
RAG: Answer Questions from Your Own Documents Official Claude API documentation and guides.
YouTube
RAG: Answer Questions from Your Own Documents AI chatbot tutorials
MDN
MDN Web Docs Comprehensive web technology reference

Day 4 Checkpoint

Before moving on, confirm understanding of these key concepts:

Continue To Day 5
Day 5 of the Build an AI Chatbot course