OrkaJS
Orka.JS

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 base
const 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 search
const 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 filters
const 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 RAG
const 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 sources
if (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