AutoRag Agent
Automatic retrieval-augmented generation with smart query detection
Automatically retrieve relevant context based on query analysis with AutoRagAgent.
Quick Start
- Create AutoRagAgent
typescript import { AutoRagAgent, RetrievalPolicy } from 'praisonai'; const autoRag = new AutoRagAgent({ retrievalPolicy: RetrievalPolicy.AUTO, topK: 5, rerank: true }); - Check if Retrieval Needed
typescript // Queries that trigger retrieval autoRag.shouldRetrieve("What is machine learning?"); // true autoRag.shouldRetrieve("Explain the document"); // true autoRag.shouldRetrieve("Find information about AI"); // true // Queries that don't trigger retrieval autoRag.shouldRetrieve("Hi"); // false autoRag.shouldRetrieve("Thanks"); // false
Retrieval Policies
import { RetrievalPolicy } from 'praisonai';
RetrievalPolicy.AUTO // Smart detection based on query
RetrievalPolicy.ALWAYS // Always retrieve context
RetrievalPolicy.NEVER // Never retrieve contextConfiguration
import { AutoRagAgent, AutoRagAgentConfig, RetrievalPolicy } from 'praisonai';
const config: AutoRagAgentConfig = {
retrievalPolicy: RetrievalPolicy.AUTO,
topK: 5, // Number of results to retrieve
hybrid: false, // Use hybrid search
rerank: true, // Rerank results
includeCitations: true, // Include citations in response
citationsMode: 'append', // 'append' | 'hidden' | 'none'
maxContextTokens: 4000, // Max tokens for context
autoMinLength: 10 // Min query length for auto mode
};
const autoRag = new AutoRagAgent(config);Configuration Options
| Option | Type | Default | Description |
|---|---|---|---|
retrievalPolicy | RetrievalPolicy | AUTO | When to retrieve context |
topK | number | 5 | Number of results to retrieve |
hybrid | boolean | false | Use hybrid (keyword + semantic) search |
rerank | boolean | false | Rerank results for relevance |
includeCitations | boolean | true | Include source citations |
citationsMode | string | 'append' | How to show citations |
maxContextTokens | number | 4000 | Max context window |
autoMinLength | number | 10 | Min query length for AUTO |
Auto Keywords
Queries containing these keywords trigger retrieval in AUTO mode:
import { DEFAULT_AUTO_KEYWORDS } from 'praisonai';
// Default keywords that trigger retrieval
const keywords = [
'what', 'how', 'why', 'when', 'where', 'who', 'which',
'explain', 'describe', 'summarize', 'find', 'search',
'tell me', 'show me', 'according to', 'based on',
'cite', 'source', 'reference', 'document', 'paper'
];Custom Keywords
const autoRag = new AutoRagAgent({
retrievalPolicy: RetrievalPolicy.AUTO,
autoKeywords: new Set([
'what', 'how', 'explain',
'my custom keyword',
'another trigger'
])
});Methods
shouldRetrieve
Check if a query should trigger retrieval:
const autoRag = new AutoRagAgent();
// Returns true for questions
autoRag.shouldRetrieve("What is the capital of France?"); // true
// Returns true for search queries
autoRag.shouldRetrieve("Find documents about AI"); // true
// Returns false for short queries
autoRag.shouldRetrieve("Hi"); // false
// Returns false for non-question statements
autoRag.shouldRetrieve("Thanks for your help"); // falsegetConfig
Get the current configuration:
const config = autoRag.getConfig();
console.log(config.topK); // 5
console.log(config.retrievalPolicy); // 'auto'Common Patterns
With Knowledge Base
import { AutoRagAgent, RetrievalPolicy, Knowledge } from 'praisonai';
const knowledge = new Knowledge();
await knowledge.add('./docs', { userId: 'u1' });
const autoRag = new AutoRagAgent({
retrievalPolicy: RetrievalPolicy.AUTO,
topK: 5,
rerank: true
});
async function answer(query: string) {
if (autoRag.shouldRetrieve(query)) {
const found = await knowledge.search(query, { userId: 'u1', limit: 5 });
return generateWithContext(query, found.results);
}
return generateDirect(query);
}With Agent
import { Agent, AutoRagAgent, RetrievalPolicy } from 'praisonai';
const autoRag = new AutoRagAgent({
retrievalPolicy: RetrievalPolicy.AUTO
});
const agent = new Agent({
name: "RAG Assistant",
instructions: "Answer questions using retrieved context"
});
async function chat(query: string) {
const needsContext = autoRag.shouldRetrieve(query);
if (needsContext) {
// Add context to the query
const context = await retrieveContext(query);
return agent.start(`Context: ${context}\n\nQuestion: ${query}`);
}
return agent.start(query);
}API Reference
Best Practices
Use AUTO for general chatbots
AUTO mode balances retrieval costs with response quality for most use cases.
Use ALWAYS for document Q&A
When every query relates to documents, use ALWAYS to ensure context is included.
Enable reranking for accuracy
Reranking improves result relevance at a small latency cost.
Tune autoMinLength
Increase autoMinLength to avoid retrieval for very short queries.