Examples
Ready-to-use code examples for common AI use cases
AI Chatbot with RAG
Build a knowledge-grounded chatbot that answers questions from your documentation
@rana/rag@rana/promptsNext.js
import { RAGPresets } from '@rana/rag';
import { PromptManager } from '@rana/prompts';
// Setup RAG pipeline
const rag = RAGPresets.chat();
await rag.index(documents);
// Setup prompt management
const pm = new PromptManager({ workspace: 'chatbot' });
await pm.register('chat', {
template: `Context: {{context}}
Question: {{question}}
Answer helpfully based on the context.`,
});
// Handle user query
export async function chat(question: string) {
const ragResult = await rag.query({ query: question });
const response = await pm.execute('chat', {
variables: {
context: ragResult.citations.map(c => c.text).join('\n'),
question,
},
});
return {
answer: response.response,
sources: ragResult.sources,
};
}Document Summarizer
Summarize long documents with customizable styles and automatic caching
@rana/helpersCaching
import { summarize, extract } from '@rana/helpers';
// Summarize a document
const summary = await summarize(longDocument, {
style: 'brief', // 'brief' | 'detailed' | 'bullet'
maxLength: 500,
});
// Extract key points
const keyPoints = await extract(longDocument, {
title: 'string',
mainPoints: 'string[]',
conclusion: 'string',
sentiment: "'positive' | 'negative' | 'neutral'",
});
console.log(summary);
console.log(keyPoints);Email Classifier
Automatically classify and route incoming emails to the right department
@rana/helpersClassification
import { classify, extract, sentiment } from '@rana/helpers';
async function processEmail(email: string) {
// Classify into departments
const department = await classify(email, [
'support',
'sales',
'billing',
'feedback',
'spam'
]);
// Extract key information
const data = await extract(email, {
senderIntent: 'string',
urgency: "'high' | 'medium' | 'low'",
actionRequired: 'boolean',
topics: 'string[]',
});
// Analyze sentiment
const mood = await sentiment(email);
return {
department,
...data,
sentiment: mood,
};
}Multi-Language Support
Translate content to multiple languages with preserved formatting
@rana/helpersTranslation
import { translate, rewrite } from '@rana/helpers';
const content = "Welcome to our platform!";
// Translate to multiple languages
const translations = await Promise.all([
translate(content, { to: 'es' }), // Spanish
translate(content, { to: 'fr' }), // French
translate(content, { to: 'de' }), // German
translate(content, { to: 'ja' }), // Japanese
translate(content, { to: 'zh' }), // Chinese
]);
// Localize tone for different markets
const usVersion = await rewrite(content, {
style: 'casual',
audience: 'US market'
});
const ukVersion = await rewrite(content, {
style: 'formal',
audience: 'UK market'
});Code Documentation RAG
Search and answer questions about your codebase
@rana/ragCode Search
import { RAGPresets, CodeChunker } from '@rana/rag';
// Create code-optimized pipeline
const pipeline = RAGPresets.code('typescript');
// Index your codebase
const files = await glob('src/**/*.ts');
const documents = await Promise.all(
files.map(async (file) => ({
id: file,
content: await readFile(file, 'utf-8'),
metadata: { path: file },
}))
);
await pipeline.index(documents);
// Query about your code
const result = await pipeline.query({
query: 'How does authentication work?',
});
console.log(result.answer);
console.log('Relevant files:', result.sources);A/B Testing Prompts
Test different prompt variants and measure performance
@rana/promptsA/B Testing
import { PromptManager } from '@rana/prompts';
const pm = new PromptManager({
workspace: 'my-app',
analytics: true,
});
// Register base prompt
await pm.register('product-description', {
template: 'Write a description for {{product}}',
});
// Create A/B test
const testId = await pm.createABTest('product-description', {
name: 'Tone Test',
variants: [
{
name: 'professional',
template: 'Write a professional description for {{product}}.'
},
{
name: 'casual',
template: 'Write a fun, casual description for {{product}}!'
},
],
trafficSplit: [50, 50],
metric: 'conversion_rate',
});
// Execute (automatically assigns variant)
const result = await pm.execute('product-description', {
variables: { product: 'Wireless Headphones' },
userId: 'user-123',
});
// Record outcome
await pm.recordOutcome(testId, result.variantId, {
converted: true,
});
// Get test results
const results = await pm.getABTestResults(testId);Want more examples? Check out our GitHub repository
View All Examples on GitHub