Class RagStore
A pre-built, shareable RAG index. Build once, share across multiple AIService instances.
public class RagStore
- Inheritance
-
RagStore
- Inherited Members
- Extension Methods
Methods
BuildAsync(Action<RagBuilder>, Func<IReadOnlyList<VectorRecord>, Task>?, CancellationToken)
Builds a RagStore by loading, splitting, embedding, and indexing all configured documents. The resulting store can be shared across multiple AIService instances.
public static Task<RagStore> BuildAsync(Action<RagBuilder> configure, Func<IReadOnlyList<VectorRecord>, Task>? onDocumentEmbedded = null, CancellationToken cancellationToken = default)
Parameters
configureAction<RagBuilder>Configuration delegate for the RAG builder.
onDocumentEmbeddedFunc<IReadOnlyList<VectorRecord>, Task>Optional callback invoked after each document's embedding is complete. When provided, replaces the default
UpsertBatchAsynccall โ the callback receives the generated VectorRecord list and decides how to persist them. When omitted, records are saved to the configured store automatically.cancellationTokenCancellationTokenCancellation token.
Returns
Examples
// Default: embed and save to store automatically
var ragStore = await RagStore.BuildAsync(config => config
.AddDocuments("./knowledge-base/")
.UseOpenAIEmbedding(apiKey)
);
// With callback: replace vectors atomically per document
var ragStore = await RagStore.BuildAsync(config => config
.AddDocuments(loader, filePath)
.UseEmbedding(embeddingProvider)
.UseStore(vectorStore),
onDocumentEmbedded: records =>
vectorStore.ReplaceByFilterAsync(
VectorFilter.ByMetadata("full_path", filePath), records),
);
QueryAsync(string, RagQueryOptions?, CancellationToken)
Processes a query through the RAG pipeline with per-request query overrides.
public Task<RagProcessedQuery> QueryAsync(string query, RagQueryOptions? options, CancellationToken cancellationToken = default)
Parameters
querystringoptionsRagQueryOptionscancellationTokenCancellationToken
Returns
QueryAsync(string, IReadOnlyList<ConversationTurn>?, RagQueryOptions?, CancellationToken)
Processes a query through the RAG pipeline with automatic query rewriting and per-request query overrides.
public Task<RagProcessedQuery> QueryAsync(string query, IReadOnlyList<ConversationTurn>? conversationHistory, RagQueryOptions? options, CancellationToken cancellationToken = default)
Parameters
querystringconversationHistoryIReadOnlyList<ConversationTurn>optionsRagQueryOptionscancellationTokenCancellationToken
Returns
QueryAsync(string, IReadOnlyList<ConversationTurn>?, CancellationToken)
Processes a query through the RAG pipeline with automatic query rewriting for multi-turn conversations. If a Mythosia.AI.Rag.RagStore.QueryRewriter is configured and conversation history is provided, the query is rewritten into retrieval-ready form before search, and retrieval-oriented keywords may also be derived.
public Task<RagProcessedQuery> QueryAsync(string query, IReadOnlyList<ConversationTurn>? conversationHistory, CancellationToken cancellationToken = default)
Parameters
querystringThe current user query.
conversationHistoryIReadOnlyList<ConversationTurn>Previous conversation turns for context, or null to skip rewriting.
cancellationTokenCancellationTokenCancellation token.
Returns
QueryAsync(string, CancellationToken)
Processes a query through the RAG pipeline: embed โ search โ build context. Returns the request message content and references without calling an LLM.
public Task<RagProcessedQuery> QueryAsync(string query, CancellationToken cancellationToken = default)
Parameters
querystringcancellationTokenCancellationToken
Returns
SetQueryRewriter(IQueryRewriter?)
Sets or clears the query rewriter at runtime. Pass null to disable query rewriting and retrieval keyword derivation.
public void SetQueryRewriter(IQueryRewriter? queryRewriter)
Parameters
queryRewriterIQueryRewriter
UpdateOptions(Action<RagPipelineOptions>)
Updates pipeline options at runtime without rebuilding the index.
public bool UpdateOptions(Action<RagPipelineOptions> configure)
Parameters
configureAction<RagPipelineOptions>
Returns
Examples
store.UpdateOptions(opt =>
{
opt.TopK = 8;
opt.MinScore = 0.4;
opt.RetrievalMultiplier = 3;
opt.PromptTemplate = "Based on:\n{context}\n\nQuestion: {question}";
});
UpdateRetrievalStrategy(bool, float)
Switches the retrieval strategy at runtime between vector-only and hybrid search without rebuilding the entire pipeline or re-indexing documents. Returns false if the underlying pipeline does not support runtime updates.
public bool UpdateRetrievalStrategy(bool useHybridSearch, float vectorWeight = 0.5)
Parameters
useHybridSearchboolTrue to enable hybrid (vector + BM25) search, false for vector-only.
vectorWeightfloatWeight for vector similarity [0, 1] when hybrid is enabled. 0.5 = equal weight.