Class RagBuilder
Fluent builder for configuring a RAG pipeline. Follows the same builder pattern as Mythosia.AI's WithFunction, WithSystemMessage, etc.
public class RagBuilder
- Inheritance
-
RagBuilder
- Inherited Members
Methods
AddDocument(string)
Adds a single document file to the RAG index. Automatically selects a loader based on file extension (.docx/.xlsx/.pptx/.pdf).
public RagBuilder AddDocument(string filePath)
Parameters
filePathstring
Returns
AddDocuments(IDocumentLoader, string)
Adds documents using a custom IDocumentLoader.
public RagBuilder AddDocuments(IDocumentLoader loader, string source)
Parameters
loaderIDocumentLoadersourcestring
Returns
AddDocuments(IDocumentLoader, string, ITextSplitter)
Adds documents using a custom IDocumentLoader with a per-source text splitter.
public RagBuilder AddDocuments(IDocumentLoader loader, string source, ITextSplitter textSplitter)
Parameters
loaderIDocumentLoadersourcestringtextSplitterITextSplitter
Returns
AddDocuments(string)
Adds all supported text files from a directory (recursive).
public RagBuilder AddDocuments(string directoryPath)
Parameters
directoryPathstring
Returns
AddDocuments(string, Action<DocumentSourceBuilder>)
Adds documents from a directory with per-extension routing (loader/splitter).
public RagBuilder AddDocuments(string directoryPath, Action<DocumentSourceBuilder> configure)
Parameters
directoryPathstringconfigureAction<DocumentSourceBuilder>
Returns
AddExcel(string, IDocumentParser?, OfficeParserOptions?)
Adds an Excel document file to the RAG index.
public RagBuilder AddExcel(string filePath, IDocumentParser? parser = null, OfficeParserOptions? options = null)
Parameters
filePathstringparserIDocumentParseroptionsOfficeParserOptions
Returns
AddPowerPoint(string, IDocumentParser?, OfficeParserOptions?)
Adds a PowerPoint document file to the RAG index.
public RagBuilder AddPowerPoint(string filePath, IDocumentParser? parser = null, OfficeParserOptions? options = null)
Parameters
filePathstringparserIDocumentParseroptionsOfficeParserOptions
Returns
AddText(string, string?)
Adds raw text content directly to the RAG index.
public RagBuilder AddText(string text, string? id = null)
Parameters
Returns
AddUrl(string)
Adds a document from a URL. Content is fetched via HTTP GET.
public RagBuilder AddUrl(string url)
Parameters
urlstring
Returns
AddWord(string, IDocumentParser?, OfficeParserOptions?)
Adds a Word document file to the RAG index.
public RagBuilder AddWord(string filePath, IDocumentParser? parser = null, OfficeParserOptions? options = null)
Parameters
filePathstringparserIDocumentParseroptionsOfficeParserOptions
Returns
UseEmbedding(IEmbeddingProvider)
Uses a custom embedding provider.
public RagBuilder UseEmbedding(IEmbeddingProvider provider)
Parameters
providerIEmbeddingProvider
Returns
UseFileStore(string)
Uses a file-based vector store for persistence across restarts.
public RagBuilder UseFileStore(string directory)
Parameters
directorystring
Returns
UseHybridSearch(float, double?)
Enables hybrid search combining BM25 keyword matching with dense vector similarity. Uses Reciprocal Rank Fusion (RRF) to merge results.
When the underlying store supports IVectorStore.HybridSearchAsync,
the query is delegated natively. Otherwise, BM25 is computed at the application level.
public RagBuilder UseHybridSearch(float vectorWeight = 0.5, double? minScore = null)
Parameters
vectorWeightfloatWeight for vector similarity vs keyword matching. Range [0, 1]. 0.5 = equal weight (default), 1.0 = pure vector, 0.0 = pure keyword.
minScoredouble?Optional minimum score threshold applied after retrieval. When set, results below this score are discarded.
Returns
UseInMemoryStore()
Uses the in-memory vector store. This is the default. Data is lost when the process exits.
public RagBuilder UseInMemoryStore()
Returns
UseLocalEmbedding(int)
Uses the local feature-hashing embedding provider (no API key required). This is the default if no embedding provider is specified.
public RagBuilder UseLocalEmbedding(int dimensions = 1024)
Parameters
dimensionsint
Returns
UseOpenAIEmbedding(string, string, int)
Uses OpenAI's embedding API.
public RagBuilder UseOpenAIEmbedding(string apiKey, string model = "text-embedding-3-small", int dimensions = 1536)
Parameters
apiKeystringOpenAI API key.
modelstringEmbedding model. Default is "text-embedding-3-small".
dimensionsintVector dimensions. Default is 1536.
Returns
UseStore(IVectorStore)
Uses a custom vector store implementation (e.g., Qdrant, Chroma, Pinecone).
public RagBuilder UseStore(IVectorStore store)
Parameters
storeIVectorStore
Returns
UseVectorSearch()
Uses the default pure vector search strategy. This is the default.
public RagBuilder UseVectorSearch()
Returns
WithChunkOverlap(int)
Sets the overlap between consecutive chunks. Default is 30.
public RagBuilder WithChunkOverlap(int overlap)
Parameters
overlapint
Returns
WithChunkSize(int)
Sets the chunk size in characters. Default is 300.
public RagBuilder WithChunkSize(int chunkSize)
Parameters
chunkSizeint
Returns
WithContextBuilder(IContextBuilder)
Sets a custom context builder implementation.
public RagBuilder WithContextBuilder(IContextBuilder contextBuilder)
Parameters
contextBuilderIContextBuilder
Returns
WithFinalSelectionPolicy(RagFinalSelectionMode, double)
Configures how final references are selected after optional re-ranking. By default the pipeline preserves the current reranker-only behavior.
public RagBuilder WithFinalSelectionPolicy(RagFinalSelectionMode mode, double retrievalWeight = 0.65)
Parameters
modeRagFinalSelectionModeretrievalWeightdouble
Returns
WithPromptTemplate(string)
Sets a custom prompt template. Use {context} and {question} placeholders.
public RagBuilder WithPromptTemplate(string template)
Parameters
templatestring
Returns
Examples
.WithPromptTemplate(@"
[์ฐธ๊ณ ๋ฌธ์]
{context}
[์ง๋ฌธ]
{question}
๋ฐ๋์ ๋ฌธ์ ๋ด์ฉ์ ๊ทผ๊ฑฐ๋ก ๋ต๋ณํ์ธ์.
")
WithQueryRewriter()
Enables automatic query rewriting for multi-turn conversations. When enabled, follow-up queries like "tell me more about that" are rewritten into retrieval-ready form using the conversation history, and the rewriter may also derive keyword terms for hybrid/text search. The inner AIService is used as the LLM for rewriting.
public RagBuilder WithQueryRewriter()
Returns
WithQueryRewriter(IQueryRewriter)
Enables query rewriting with a custom IQueryRewriter implementation. Custom rewriters may both rewrite semantic queries and shape retrieval keywords.
public RagBuilder WithQueryRewriter(IQueryRewriter queryRewriter)
Parameters
queryRewriterIQueryRewriter
Returns
WithQueryRewriter(uint)
Enables query rewriting with a custom max token limit for the LLM response.
public RagBuilder WithQueryRewriter(uint maxTokens)
Parameters
maxTokensuintMaximum tokens for the rewriter LLM response.
Returns
WithReranker(IReranker)
Registers a re-ranker to refine search results after initial retrieval. When set, the retrieval results are passed through the re-ranker before context building.
public RagBuilder WithReranker(IReranker reranker)
Parameters
rerankerIReranker
Returns
WithRetrievalMinScore(double)
public RagBuilder WithRetrievalMinScore(double threshold)
Parameters
thresholddouble
Returns
WithRetrievalMultiplier(int)
public RagBuilder WithRetrievalMultiplier(int multiplier)
Parameters
multiplierint
Returns
WithScoreThreshold(double)
Sets the minimum similarity score threshold. Results below this are discarded.
public RagBuilder WithScoreThreshold(double threshold)
Parameters
thresholddouble
Returns
WithTextSplitter(ITextSplitter)
Sets a custom text splitter implementation.
public RagBuilder WithTextSplitter(ITextSplitter textSplitter)
Parameters
textSplitterITextSplitter
Returns
WithTopK(int)
Sets the number of top results to retrieve. Default is 3.
public RagBuilder WithTopK(int topK)
Parameters
topKint