SourceRagService class

High-level service for source-based RAG operations.

Constructors

SourceRagService({required String dbPath, String? modelPath, int maxChunkChars = 500, int overlapChars = 50})

Properties

dbPath String
final
hashCode int
The hash code for this object.
no setterinherited
isIndexReady bool
no setter
maxChunkChars int
Maximum characters per chunk (default: 500)
final
modelPath String?
Path to the ONNX model file.
final
overlapChars int
Overlap characters between chunks for context continuity
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
warmupFuture Future<void>
no setter

Methods

addSourceWithChunking(String content, {String? metadata, String? name, String? filePath, ChunkingStrategy? strategy, Duration? chunkDelay, void onProgress(int done, int total)?}) Future<SourceAddResult>
Add a source document with automatic chunking and embedding.
dispose() Future<void>
Dispose resources held by this service. Call this when the service is no longer needed.
formatPrompt(String query, RagSearchResult result) String
Format search results as an LLM prompt.
getAdjacentChunks({required int sourceId, required int minIndex, required int maxIndex}) Future<List<ChunkSearchResult>>
Fetch adjacent chunks for a given source and index range.
getSourceChunkCount({required int sourceId}) Future<int>
Get the total number of chunks for a specific source.
getSourceChunks({required int sourceId}) Future<List<String>>
Get all chunk texts for a specific source.
getSourceDocument({required int sourceId}) Future<String?>
Get the original source document content by source ID.
getSourceForChunk(ChunkSearchResult chunk) Future<String?>
Get the original source document for a chunk.
getStats() Future<SourceStats>
Get statistics about stored sources and chunks.
init({bool deferIndexWarmup = false}) Future<void>
Initialize the source database.
listSources() Future<List<SourceEntry>>
Get a list of all stored sources.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
rebuildIndex({bool force = false}) Future<void>
Rebuild the HNSW and BM25 indexes after adding sources. force - If true, rebuilds even if no changes were detected (default: false).
regenerateAllEmbeddings({void onProgress(int done, int total)?}) Future<void>
Regenerate embeddings for all existing chunks using the current model. This is needed when the embedding model or tokenizer has been updated.
removeSource(int sourceId) Future<void>
Remove a source and all its chunks from the database.
saveIndex() Future<void>
Save HNSW index marker to disk.
Search for relevant chunks and assemble context for LLM.
searchHybrid(String query, {int topK = 10, double vectorWeight = 0.2, double bm25Weight = 0.8, List<int>? sourceIds}) Future<List<HybridSearchResult>>
Hybrid search combining vector and keyword (BM25) search.
searchHybridWithContext(String query, {int topK = 10, int tokenBudget = 2000, ContextStrategy strategy = ContextStrategy.relevanceFirst, double vectorWeight = 0.2, double bm25Weight = 0.8, List<int>? sourceIds, int adjacentChunks = 0, bool singleSourceMode = false}) Future<RagSearchResult>
Hybrid search with context assembly for LLM.
toString() String
A string representation of this object.
inherited
tryLoadCachedIndex() Future<bool>
Try to load cached HNSW index.

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Methods

detectChunkingStrategy(String? filePath) ChunkingStrategy
Detect the appropriate chunking strategy based on file extension.