RAG & Knowledge Base
Build intelligent Q&A systems with Retrieval-Augmented Generation.
RAG combines the power of semantic search with LLM generation to provide accurate, context-aware answers based on your own data.
ORKA β RAG PIPELINE ARCHITECTURE
π User Question
Embedding
OpenAI text-embedding-3-small
Vector Search
knowledge.search(topK: 3)
β
chunk_1 (0.92)
chunk_2 (0.87)
chunk_3 (0.81)
Context Assembly
System Prompt + Retrieved Context + Question
LLM Generation
GPT-4o / Claude / Mistral
AskResult
answer: stringcontext: Chunk[]usage: TokenUsage
LLM
Vector DB
Processing
1. Create a Knowledge Base
Ingest documents with automatic chunking and embedding:
create-knowledge.ts
import { createOrka, OpenAIAdapter, MemoryVectorAdapter } from 'orkajs';Β const orka = createOrka({ llm: new OpenAIAdapter({ apiKey: process.env.OPENAI_API_KEY! }), vectorDB: new MemoryVectorAdapter(), defaults: { chunkSize: 500, // Chunk size in characters chunkOverlap: 100, // Overlap between chunks topK: 3, // Number of results by default },});Β // Create a knowledge baseconst result = await orka.knowledge.create({ name: 'faq', source: [ { text: 'Orka JS is a TypeScript framework for building production-ready LLM systems.', metadata: { category: 'general' }, }, { text: 'To install Orka JS, use npm install orkajs.', metadata: { category: 'installation' }, }, { text: 'Orka JS supports Pinecone, Qdrant, Chroma and an in-memory adapter.', metadata: { category: 'features' }, }, ],});Β console.log(`Base created: ${result.documentCount} documents, ${result.chunkCount} chunks`);2. Semantic Search
Search your knowledge base with natural language queries:
semantic-search.ts
// Simple semantic searchconst results = await orka.knowledge.search('faq', 'how to install', { topK: 2 });Β for (const r of results) { console.log(`[Score: ${r.score.toFixed(3)}] ${r.content}`); console.log(` Metadata: ${JSON.stringify(r.metadata)}`);}Β // Search with metadata filtersconst filtered = await orka.knowledge.search('faq', 'features', { topK: 5, filter: { category: 'features' },});3. Ask with RAG
Get AI-generated answers grounded in your knowledge base:
ask-rag.ts
// Question with RAGconst answer = await orka.ask({ knowledge: 'faq', question: 'Which vector databases are supported ?', includeContext: true, // Include sources in the response systemPrompt: 'You are a technical assistant. Respond concisely.', temperature: 0.3,});Β console.log(`Answer: ${answer.answer}`);console.log(`Tokens: ${answer.usage.totalTokens}`);console.log(`Latency: ${answer.latencyMs}ms`);Β // Display used sourcesif (answer.context) { console.log('Sources:'); for (const source of answer.context) { console.log(` - [${source.score.toFixed(2)}] ${source.content.slice(0, 80)}...`); }}4. Complete Example
rag-complete.ts
import { createOrka, OpenAIAdapter, MemoryVectorAdapter } from 'orkajs';Β async function main() { const orka = createOrka({ llm: new OpenAIAdapter({ apiKey: process.env.OPENAI_API_KEY! }), vectorDB: new MemoryVectorAdapter(), defaults: { chunkSize: 500, chunkOverlap: 100, topK: 3 }, });Β console.log('π Creating the knowledge base......');Β await orka.knowledge.create({ name: 'docs', source: [ { text: 'Orka JS is a TypeScript framework for LLM systems.', metadata: { topic: 'intro' } }, { text: 'Installation: npm install orkajs', metadata: { topic: 'setup' } }, { text: 'Supported providers: OpenAI, Anthropic, Mistral, Ollama.', metadata: { topic: 'providers' } }, { text: 'Vector databases: Pinecone, Qdrant, Chroma, in-memory.', metadata: { topic: 'vectordb' } }, ], });Β console.log('π Semantic search...'); const searchResults = await orka.knowledge.search('docs', 'how to install', { topK: 2 });Β for (const r of searchResults) { console.log(` - [${r.score.toFixed(3)}] ${r.content}`); }Β console.log('\nπ¬ Question with RAG...'); const answer = await orka.ask({ knowledge: 'docs', question: 'Which LLM providers are supported?', includeContext: true, });Β console.log(`Answer: ${answer.answer}`); console.log(`Tokens: ${answer.usage.totalTokens}, Latency: ${answer.latencyMs}ms`);Β if (answer.context) { console.log(`Sources used: ${answer.context.length}`); }Β // Nettoyage await orka.knowledge.delete('docs');}Β main().catch(console.error);Key Features
π Automatic Chunking
Documents are automatically split into optimal chunks with configurable size and overlap
π― Semantic Search
Find relevant content based on meaning, not just keywords
π Metadata Filtering
Filter search results by custom metadata fields
π Source Attribution
Get the exact sources used to generate each answer