Back to AI Recipes

Retrieval-Augmented Generation

RAGKnowledge

Knowledge

Ground LLM responses in external knowledge bases for accurate, up-to-date answers.

Summary

Retrieval-Augmented Generation (RAG) combines information retrieval with text generation. Relevant documents are retrieved from a knowledge base based on user queries, then provided as context for LLM response generation. This grounding reduces hallucinations and ensures responses reflect current, authoritative information.

How it works

  1. Query Processing: Transform user query into retrieval format
  2. Document Retrieval: Search knowledge base for relevant passages
  3. Context Assembly: Combine retrieved documents with user query
  4. Generation: LLM produces grounded response from augmented prompt
  5. Verification: Optional citation or source attribution

RAG architectures

  • Naive RAG: Retrieve-then-read pipeline
  • Agentic RAG: Iterative retrieval with planning and tool use
  • Hybrid RAG: Combine dense and sparse retrieval
  • Graph RAG: Leverage knowledge graph relationships

Component considerations

  • Retriever: BM25, dense embeddings, hybrid approaches
  • Index: FAISS, Pinecone, Weaviate, Elasticsearch
  • Chunking: Fixed-size, semantic, recursive strategies
  • Retrieval: Top-k, MMR, similarity thresholds
Code View

Retrieval-Augmented Generation Implementation

// Retrieval-Augmented Generation recipe using OpenAI
// Install: bun add openai

import OpenAI from "openai";

async function main() {
  const input = "Add your prompt here.";
  const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
  const system = "You are a senior AI engineer and technical writer. Apply the agent recipe to structure the reasoning and produce a useful result. Recipe: Retrieval-Augmented Generation. Description: Ground LLM responses in external knowledge bases for accurate, up-to-date answers. Focus: Knowledge Provide actionable, implementation-ready guidance.";
  const user = `Request: ${input}`;

  const openaiResponse = await openai.chat.completions.create({
    model: "gpt-4o-mini",
    messages: [
      { role: "system", content: system },
      { role: "user", content: user },
    ],
  });

  const openaiText = openaiResponse.choices[0]?.message?.content?.trim() ?? "";

  console.log(openaiText);
}

main().catch((error) => {
  console.error(error);
  process.exitCode = 1;
});