Welcome to RAG.NET, my latest adventure into building a powerful Retrieval-Augmented Generation framework for .NET! If you've ever wanted to build intelligent document search with fuzzy matching and ML-powered concept understanding, you're in the right place.
RAG.NET brings together some seriously cool features:
- π§ Smart Concept Understanding: Uses TF-IDF vectorization to understand document relationships
- π Fuzzy Search Magic: Handles typos and variations like a champ
- π― Concept-Based Retrieval: Finds relevant documents even when queries aren't exact matches
- β‘ Blazing Fast: Built with parallel processing and smart caching
Ever tried searching for "machine lerning" and got zero results? Frustrating, right? RAG.NET handles these scenarios beautifully:
// Even with typos, we'll find the right content!
var results = await searchService.SearchAsync("artficial intellignce");
// Results will include matches for "artificial intelligence"
// thanks to our fuzzy matching system!The heart of RAG.NET is its concept-document relationship system. Think of concepts as smart containers that understand their content:
// Create a concept for Machine Learning
var mlConcept = new Concept
{
Id = "ml",
Name = "Machine Learning",
Description = "AI and ML concepts",
Metadata = new Dictionary<string, string>
{
{ "category", "artificial-intelligence" },
{ "difficulty", "advanced" }
}
};
// Add related documents to the concept
mlConcept.Documents.Add(new Document
{
Id = "ml-intro",
Content = "Machine learning enables computers to learn from data...",
Metadata = new Dictionary<string, string>
{
{ "author", "John Doe" },
{ "last_updated", "2024-01-01" }
}
});
// Add to your concept store
conceptStore.AddConcept(mlConcept);Concepts in RAG.NET aren't just folders for documents - they're intelligent collections that:
- Group Related Content: Documents about similar topics stay together
- Share Context: All documents in a concept contribute to its understanding
- Enable Smart Search: When you search for "ML", we know to look in related documents too
- Support Metadata: Both concepts and documents can have metadata for extra context
// 1. Set up your concept store
var mlConcept = new Concept
{
Id = "ml",
Name = "Machine Learning",
Description = "AI and ML concepts"
};
mlConcept.Documents.Add(new Document
{
Content = "Machine learning enables computers to learn from data..."
});
conceptStore.AddConcept(mlConcept);
// 2. Build the vector store
await vectorStore.BuildVectorStoreAsync();
// 3. Start searching!
var results = await searchService.SearchAsync("neural neworks");We use Term Frequency-Inverse Document Frequency (TF-IDF) to understand document importance:
var vectorizer = new TfIdfVectorizer();
vectorizer.Fit(documents); // Learns from your document collection
var documentVector = vectorizer.Transform(newDocument);Our Levenshtein-based matching handles various text variations:
var fuzzyService = new FuzzyMatchService(matcher, options);
var expandedTerms = fuzzyService.ExpandTerms("neural", vocabulary);
// Returns: ["neural", "neuronal", "neurons", ...]Instead of simple keyword matching, we understand relationships between concepts:
var searchOptions = new SearchOptions
{
MinimumScore = 0.1,
MaxResults = 5
};
var results = await searchService.SearchAsync(
"machine learning algorithms",
searchOptions
);RAG.NET isn't just another search engine - it's your AI's best friend! Here's why it's a game-changer for your LLM pipeline:
graph LR
A[User Query] --> B[RAG.NET]
B --> C[Document Processing]
C --> D[Vector Store]
D --> E[Smart Retrieval]
E --> F[Context Selection]
F --> G[LLM Input]
G --> H[AI Response]
style B fill:#f96,stroke:#333
style G fill:#6af,stroke:#333
Traditional search might miss crucial context, but RAG.NET ensures your LLM gets the most relevant information:
// Example: Processing a user query for an LLM
var query = "What are the latest deep learning developments in our project?";
// Get highly relevant context using RAG.NET
var searchResults = await searchService.SearchAsync(query);
// Format context for your LLM
var llmContext = searchResults.Predictions
.OrderByDescending(p => p.Score)
.Take(3) // Top 3 most relevant concepts
.SelectMany(p => p.Concept.Documents)
.Select(d => d.Content);-
Reduced Token Usage:
- Only send the most relevant context to your LLM
- Save on API costs by filtering out noise
-
Improved Answer Quality:
- Better context = better answers
- Reduced hallucinations through relevant document grounding
-
Smart Context Ranking:
- Use scores to prioritize most relevant information
- Dynamically adjust context based on relevance
Here's how RAG.NET fits into a complete LLM pipeline:
public async Task<string> GetAIResponse(string userQuery)
{
// 1. Get relevant context using RAG.NET
var searchResults = await searchService.SearchAsync(
userQuery,
new SearchOptions
{
MinimumScore = 0.6,
MaxResults = 3
}
);
// 2. Prepare context for LLM
var relevantDocs = searchResults.Predictions
.OrderByDescending(p => p.Score)
.SelectMany(p => p.Concept.Documents)
.Select(d => d.Content);
// 3. Format your LLM prompt
var prompt = $"""
You are an AI assistant with access to the following relevant documentation:
{string.Join("\n---\n", relevantDocs)}
Using ONLY the information provided above, please answer:
{userQuery}
If the provided context doesn't contain enough information to answer,
please say so rather than making assumptions.
""";
// 4. Send to your favorite LLM
return await yourLLMService.GetCompletion(prompt);
}Ready to supercharge your document search? Clone this repo and dive in! Check out the examples folder for more detailed usage scenarios.
git clone https://github.com/yourusername/rag-net.git
cd rag-net
dotnet buildBuilt with β€οΈ using .NET 8.0 and Microsoft.ML