Build a basic RAG (Retrieval-Augmented Generation) system that retrieves relevant documents and generates answers.

Code Playground
JavaScript (V8)
Loading...
Console Output
// Output will appear here...
// For React: ReactDOM.createRoot(rootElement).render(<App />);
Recommended Answer

Implementation:

import OpenAI from 'openai';

const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });

// Knowledge base
const documents = [
  "The Eiffel Tower is located in Paris, France. It was built in 1889.",
  "Python is a high-level programming language known for its simplicity.",
  "The Amazon rainforest produces 20% of the world's oxygen."
];

async function getEmbedding(text) {
  const response = await openai.embeddings.create({
    model: 'text-embedding-3-small',
    input: text,
  });
  return response.data[0].embedding;
}

function cosineSimilarity(a, b) {
  const dotProduct = a.reduce((sum, val, i) => sum + val * b[i], 0);
  const magA = Math.sqrt(a.reduce((sum, val) => sum + val * val, 0));
  const magB = Math.sqrt(b.reduce((sum, val) => sum + val * val, 0));
  return dotProduct / (magA * magB);
}

async function ragQuery(question) {
  // 1. Get query embedding
  const queryEmbedding = await getEmbedding(question);
  
  // 2. Find most relevant documents
  const docScores = await Promise.all(
    documents.map(async (doc) => ({
      doc,
      score: cosineSimilarity(queryEmbedding, await getEmbedding(doc))
    }))
  );
  
  const topDocs = docScores
    .sort((a, b) => b.score - a.score)
    .slice(0, 2)
    .map(d => d.doc);
  
  // 3. Generate answer using context
  const context = topDocs.join('\n');
  const completion = await openai.chat.completions.create({
    model: 'gpt-4',
    messages: [
      { role: 'system', content: 'Answer based on the provided context.' },
      { role: 'user', content: `Context: ${context}\n\nQuestion: ${question}` }
    ],
  });
  
  return completion.choices[0].message.content;
}

// Usage
const answer = await ragQuery("Where is the Eiffel Tower?");
console.log(answer);