@dooor-ai/toolkit
Version:
Guards, Evals & Observability for AI applications - works seamlessly with LangChain/LangGraph
67 lines (59 loc) • 2.21 kB
text/typescript
/**
* RAGContext class for DOOOR AI Toolkit
* Allows users to pass ephemeral documents, files, or embeddings for RAG
*/
import { RAGContextConfig, RAGStrategy, RAGFile, RAGDocument, RAGEmbedding } from "./types";
export class RAGContext {
public readonly files: RAGFile[];
public readonly documents: RAGDocument[];
public readonly embeddings: RAGEmbedding[];
public readonly embeddingProvider: string;
public readonly strategy: RAGStrategy;
public readonly topK: number;
public readonly chunkSize: number;
public readonly chunkOverlap: number;
public readonly similarityThreshold: number;
constructor(config: RAGContextConfig) {
this.files = config.files || [];
this.documents = config.documents || [];
this.embeddings = config.embeddings || [];
if (!config.embeddingProvider) {
throw new Error(
"embeddingProvider is required. Please configure an embedding provider at /settings/embeddings " +
"and pass its internal name (e.g., 'prod-gemini', 'myGemini')"
);
}
this.embeddingProvider = config.embeddingProvider;
this.strategy = config.strategy || RAGStrategy.SIMPLE;
this.topK = config.topK || 5;
this.chunkSize = config.chunkSize || 1000;
this.chunkOverlap = config.chunkOverlap || 200;
this.similarityThreshold = config.similarityThreshold || 0.7;
// Validate at least one source is provided
if (this.files.length === 0 && this.documents.length === 0 && this.embeddings.length === 0) {
throw new Error(
"RAGContext requires at least one source: files, documents, or embeddings"
);
}
}
/**
* Serialize RAGContext to JSON for API request
*/
toJSON(): Record<string, any> {
return {
files: this.files.map(f => ({
name: f.name,
data: Buffer.isBuffer(f.data) ? f.data.toString('base64') : f.data,
type: f.type,
})),
documents: this.documents,
embeddings: this.embeddings,
embedding_provider: this.embeddingProvider,
strategy: this.strategy,
top_k: this.topK,
chunk_size: this.chunkSize,
chunk_overlap: this.chunkOverlap,
similarity_threshold: this.similarityThreshold,
};
}
}