Core Interfaces

SmartRAG provides several core interfaces for document processing and management.

IDocumentService

The main service for document operations.

public interface IDocumentService
{
    Task<Document> UploadDocumentAsync(IFormFile file);
    Task<IEnumerable<Document>> GetAllDocumentsAsync();
    Task<Document> GetDocumentByIdAsync(string id);
    Task<bool> DeleteDocumentAsync(string id);
    Task<IEnumerable<DocumentChunk>> SearchDocumentsAsync(string query, int maxResults = 10);
}

IDocumentSearchService

The service for AI-powered question answering with automatic session management.

public interface IDocumentSearchService
{
    Task<List<DocumentChunk>> SearchDocumentsAsync(string query, int maxResults = 5);
    Task<RagResponse> GenerateRagAnswerAsync(string query, int maxResults = 5, bool startNewConversation = false);
}

GenerateRagAnswerAsync

Generates AI-powered answers with automatic session management and conversation history.

Task<RagResponse> GenerateRagAnswerAsync(string query, int maxResults = 5, bool startNewConversation = false)

Parameters:

  • query (string): The user's question
  • maxResults (int): Maximum number of document chunks to retrieve (default: 5)
  • startNewConversation (bool): Start a new conversation session (default: false)

Returns: RagResponse with AI answer, sources, and metadata

// Basic usage
var response = await documentSearchService.GenerateRagAnswerAsync("What is the weather?");

// Start new conversation
var response = await documentSearchService.GenerateRagAnswerAsync("/new");

Other Key Interfaces

Additional services for document processing and storage.

// Document parsing and processing
IDocumentParserService - Parse documents and extract text
IDocumentRepository - Document storage operations
IAIService - AI provider communication
IAudioParserService - Audio transcription (Google Speech-to-Text)

Key Models

Essential data models for SmartRAG operations.

// Main response model
public class RagResponse
{
    public string Query { get; set; }
    public string Answer { get; set; }
    public List<SearchSource> Sources { get; set; }
    public DateTime SearchedAt { get; set; }
}

// Document chunk for search results
public class DocumentChunk
{
    public string Id { get; set; }
    public string DocumentId { get; set; }
    public string Content { get; set; }
    public double RelevanceScore { get; set; }
}

Configuration

Key configuration options for SmartRAG.

// AI Providers
AIProvider.Anthropic    // Claude models
AIProvider.OpenAI       // GPT models
AIProvider.Gemini       // Google models

// Storage Providers  
StorageProvider.Qdrant  // Vector database
StorageProvider.Redis   // High-performance cache
StorageProvider.Sqlite  // Local database

Quick Start

Get started with SmartRAG in minutes.

// 1. Register services
services.AddSmartRag(configuration, options =>
{
    options.AIProvider = AIProvider.Anthropic;
    options.StorageProvider = StorageProvider.Redis;
});

// 2. Inject and use
public class MyController : ControllerBase
{
    private readonly IDocumentSearchService _searchService;
    
    public async Task Ask(string question)
    {
        var response = await _searchService.GenerateRagAnswerAsync(question);
        return Ok(response);
    }
}</code></pre>
                    </div>
                </div>
            </div>
        </section>

        
        

Common Patterns

Frequently used patterns and configurations.

// Document upload
var document = await _documentService.UploadDocumentAsync(file);

// Document search  
var results = await _searchService.SearchDocumentsAsync(query, 10);

// RAG conversation
var response = await _searchService.GenerateRagAnswerAsync(question);

// Configuration
services.AddSmartRag(configuration, options =>
{
    options.AIProvider = AIProvider.Anthropic;
    options.StorageProvider = StorageProvider.Redis;
});

Error Handling

Common exceptions and error handling patterns.

try
{
    var response = await _searchService.GenerateRagAnswerAsync(query);
    return Ok(response);
}
catch (SmartRagException ex)
{
    return BadRequest(ex.Message);
}
catch (Exception ex)
{
    return StatusCode(500, "Internal server error");
}

Performance Tips

Optimize SmartRAG performance with these tips.

  • Chunk Size: 500-1000 characters for optimal balance
  • Batch Operations: Process multiple documents together
  • Caching: Use Redis for better performance
  • Vector Storage: Qdrant for production use
</div> </div>