Advanced Configuration

Use SmartRAG's advanced features to create more reliable and performant systems:

Fallback Providers

Fallback Provider Configuration

builder.Services.AddSmartRag(configuration, options =>
{
    // Primary AI provider
    options.AIProvider = AIProvider.OpenAI;
    
    // Enable fallback providers
    options.EnableFallbackProviders = true;
    options.FallbackProviders = new List<AIProvider>
    {
        AIProvider.Anthropic,    // First fallback
        AIProvider.Gemini,       // Second fallback
        AIProvider.Custom        // Last fallback (Ollama)
    };
    
    // Retry configuration
    options.MaxRetryAttempts = 3;
    options.RetryDelayMs = 1000;
    options.RetryPolicy = RetryPolicy.ExponentialBackoff;
});

Fallback Scenarios

// Scenario 1: OpenAI → Anthropic → Gemini
options.FallbackProviders = new List<AIProvider>
{
    AIProvider.Anthropic,
    AIProvider.Gemini
};

// Scenario 2: Azure OpenAI → OpenAI → Anthropic
options.FallbackProviders = new List<AIProvider>
{
    AIProvider.OpenAI,
    AIProvider.Anthropic
};

// Scenario 3: Cloud → On-premise
options.FallbackProviders = new List<AIProvider>
{
    AIProvider.Custom  // Ollama/LM Studio
};

// Scenario 4: Premium → Budget
options.FallbackProviders = new List<AIProvider>
{
    AIProvider.Gemini  // More cost-effective
};

Retry Policies

RetryPolicy Options

// Scenario 1: Fast retry
options.RetryPolicy = RetryPolicy.FixedDelay;
options.RetryDelayMs = 500;  // Fixed 500ms wait

// Scenario 2: Linear increasing wait
options.RetryPolicy = RetryPolicy.LinearBackoff;
options.RetryDelayMs = 1000;  // 1s, 2s, 3s, 4s...

// Scenario 3: Exponential increasing wait (recommended)
options.RetryPolicy = RetryPolicy.ExponentialBackoff;
options.RetryDelayMs = 1000;  // 1s, 2s, 4s, 8s...

// Scenario 4: No retry
options.RetryPolicy = RetryPolicy.None;

Custom Retry Logic

// Aggressive retry for critical applications
options.MaxRetryAttempts = 5;
options.RetryDelayMs = 200;
options.RetryPolicy = RetryPolicy.ExponentialBackoff;

// Minimal retry for test environments
options.MaxRetryAttempts = 1;
options.RetryDelayMs = 1000;
options.RetryPolicy = RetryPolicy.FixedDelay;

Performance Optimization

Chunk Size Optimization

// Scenario 1: Fast search (small chunks)
options.MaxChunkSize = 500;
options.MinChunkSize = 100;
options.ChunkOverlap = 75;

// Scenario 2: Context preservation (large chunks)
options.MaxChunkSize = 2000;
options.MinChunkSize = 200;
options.ChunkOverlap = 400;

// Scenario 3: Balance (recommended)
options.MaxChunkSize = 1000;
options.MinChunkSize = 100;
options.ChunkOverlap = 200;

Storage Optimization

// Scenario 1: Maximum performance
options.StorageProvider = StorageProvider.Qdrant;
options.ConversationStorageProvider = ConversationStorageProvider.Redis;

// Scenario 2: Cost optimization
options.StorageProvider = StorageProvider.Redis;
options.ConversationStorageProvider = ConversationStorageProvider.InMemory;

// Scenario 3: Hybrid approach
options.StorageProvider = StorageProvider.Redis;
options.ConversationStorageProvider = ConversationStorageProvider.SQLite;

Security Configuration

API Key Management

// Use environment variables (recommended)
builder.Services.AddSmartRag(configuration, options =>
{
    options.AIProvider = AIProvider.OpenAI;
    // API key automatically loaded from environment
});

// Use appsettings.json (for development)
{
  "AI": {
    "OpenAI": {
      "ApiKey": "sk-proj-YOUR_KEY"
    }
  }
}

Database Connection Configuration

// Database connection configuration
options.DatabaseConnections = new List<DatabaseConnectionConfig>
{
    new DatabaseConnectionConfig
    {
        Name = "Secure Database",
        DatabaseType = DatabaseType.SqlServer,
        ConnectionString = "Server=localhost;Database=SecureDB;...",
        Description = "Production database with sensitive data",
        Enabled = true,
        MaxRowsPerQuery = 1000,
        QueryTimeoutSeconds = 30,
        SchemaRefreshIntervalMinutes = 60,
        IncludedTables = new string[] { "Orders", "Customers" },
        ExcludedTables = new string[] { "Logs", "TempData" }
    }
};

Note: SanitizeSensitiveData and MaxRowsPerTable are configured in DatabaseConfig, not in DatabaseConnectionConfig. These settings apply globally to all database connections.

Monitoring and Logging

Detailed Logging

// Logging configuration
builder.Services.AddLogging(logging =>
{
    logging.AddConsole();
    logging.AddDebug();
    logging.SetMinimumLevel(LogLevel.Information);
});

// SmartRAG logging automatically enabled
builder.Services.AddSmartRag(configuration, options =>
{
    // Logging automatically enabled
});

Performance Monitoring

// For performance metrics
public class SmartRagMetrics
{
    public int TotalQueries { get; set; }
    public TimeSpan AverageResponseTime { get; set; }
    public int FallbackUsageCount { get; set; }
    public int RetryCount { get; set; }
}

Best Practices

Security

Security Best Practices

  • Never commit API keys to source control
  • Use environment variables for production
  • Configure database connections securely
  • Use HTTPS for external services
  • Prefer on-premise AI providers for sensitive data

Performance

Performance Best Practices

  • Use Qdrant or Redis for production
  • Configure appropriate chunk sizes
  • Enable fallback providers for reliability
  • Set reasonable MaxRowsPerQuery limits for database connections
  • Use ExponentialBackoff retry policy

Cost Optimization

Cost Optimization

  • Use Gemini or Custom providers for development
  • Prefer OpenAI, Azure OpenAI, or Anthropic for production
  • Use InMemory storage only for testing
  • Use Redis for cost-effective production storage
  • Ollama/LM Studio for 100% on-premise solutions

Custom Strategies

SmartRAG provides the Strategy Pattern for key components, allowing you to inject custom logic.

Registering Custom Strategies

You can implement your own strategies by implementing the corresponding interfaces. Here are examples of how you would register them:

// Example: Registering a custom SQL Dialect (e.g., if you implemented EnhancedPostgreSqlDialectStrategy)
services.AddSingleton<ISqlDialectStrategy, EnhancedPostgreSqlDialectStrategy>();

// Example: Registering a custom Scoring Strategy
services.AddSingleton<IScoringStrategy, CustomScoringStrategy>();

// Example: Registering a custom File Parser (e.g., for Markdown files)
services.AddSingleton<IFileParser, MarkdownFileParser>();

Example Configurations

Development Environment

builder.Services.AddSmartRag(configuration, options =>
{
    // For fast development
    options.AIProvider = AIProvider.Gemini;
    options.StorageProvider = StorageProvider.InMemory;
    options.MaxChunkSize = 500;
    options.ChunkOverlap = 100;
    options.MaxRetryAttempts = 1;
    options.RetryPolicy = RetryPolicy.FixedDelay;
});

Test Environment

builder.Services.AddSmartRag(configuration, options =>
{
    // Reliable configuration for testing
    options.AIProvider = AIProvider.OpenAI;
    options.StorageProvider = StorageProvider.Redis;
    options.ConversationStorageProvider = ConversationStorageProvider.SQLite;
    options.MaxChunkSize = 1000;
    options.ChunkOverlap = 200;
    options.MaxRetryAttempts = 3;
    options.RetryPolicy = RetryPolicy.ExponentialBackoff;
    options.EnableFallbackProviders = true;
    options.FallbackProviders = new List<AIProvider> { AIProvider.Gemini };
});

Production Environment

builder.Services.AddSmartRag(configuration, options =>
{
    // Reliable production configuration
    options.AIProvider = AIProvider.OpenAI;
    options.StorageProvider = StorageProvider.Qdrant;
    options.ConversationStorageProvider = ConversationStorageProvider.Redis;
    options.MaxChunkSize = 1000;
    options.ChunkOverlap = 200;
    options.MaxRetryAttempts = 5;
    options.RetryPolicy = RetryPolicy.ExponentialBackoff;
    options.EnableFallbackProviders = true;
    options.FallbackProviders = new List<AIProvider> 
    { 
        AIProvider.AzureOpenAI,  // First fallback (Azure)
        AIProvider.Anthropic,    // Second fallback
        AIProvider.Custom        // Last fallback (Ollama)
    };
    
    // Database configuration
    options.DatabaseConnections = new List<DatabaseConnectionConfig>
    {
        new DatabaseConnectionConfig
        {
            Name = "Production DB",
            DatabaseType = DatabaseType.SqlServer,
            ConnectionString = Environment.GetEnvironmentVariable("DB_CONNECTION_STRING"),
            Description = "Production database",
            Enabled = true,
            MaxRowsPerQuery = 5000,
            QueryTimeoutSeconds = 30,
            SchemaRefreshIntervalMinutes = 60
        }
    };
});

Next Steps

API Reference

Detailed API documentation and method references

API Reference

Examples

See practical examples and real-world usage scenarios

View Examples

Changelog

Track new features, improvements, and breaking changes across all versions.

View Changelog