The RAG System uses a unified configuration management system built with Pydantic BaseSettings. This provides type-safe configuration with validation, supporting multiple configuration sources with clear priority.
Configuration values are loaded in the following order (highest to lowest priority):
.env File - Medium priorityConfiguration is organized by functional domains:
from src.config import get_settings
# Get settings singleton
settings = get_settings()
# Access configuration
print(settings.app_name)
print(settings.database.host)
print(settings.vector_db_type)
print(settings.api.port)
# Database configuration
db_config = settings.database
print(f"Connecting to {db_config.host}:{db_config.port}")
# Vector database configuration
if settings.vector_db_type == "infinity":
infinity_config = settings.infinity
print(f"Using Infinity at {infinity_config.host}:{infinity_config.port}")
elif settings.vector_db_type == "elasticsearch":
es_config = settings.elasticsearch
print(f"Using Elasticsearch at {es_config.nodes}")
# Model configuration
model_config = settings.model
print(f"Using {model_config.provider} with {model_config.chat_model_name}")
settings = get_settings()
if settings.environment == "production":
# Production-specific logic
assert not settings.api.debug, "Debug mode should be disabled in production"
elif settings.environment == "development":
# Development-specific logic
pass
Copy the example file:
cp .env.example .env
Edit .env with your values:
# Application
APP_NAME=My RAG System
ENVIRONMENT=production
# Database
DB_HOST=db.example.com
DB_PORT=3306
DB_USERNAME=myuser
DB_PASSWORD=mypassword
DB_DATABASE=rag_db
# Vector Database
VECTOR_DB_TYPE=elasticsearch
ES_NODES=["http://es1.example.com:9200", "http://es2.example.com:9200"]
ES_USERNAME=elastic
ES_PASSWORD=changeme
# API
API_PORT=8000
API_LOG_LEVEL=INFO
API_WORKERS=4
You can override any configuration using environment variables:
# Linux/Mac
export DB_HOST=localhost
export API_PORT=9000
export VECTOR_DB_TYPE=infinity
# Windows PowerShell
$env:DB_HOST="localhost"
$env:API_PORT="9000"
$env:VECTOR_DB_TYPE="infinity"
# Windows CMD
set DB_HOST=localhost
set API_PORT=9000
set VECTOR_DB_TYPE=infinity
The configuration system validates all values on load:
# Valid
DB_PORT=3306
API_PORT=8000
# Invalid - will raise ValidationError
DB_PORT=99999 # Port must be between 1 and 65535
# Valid
API_LOG_LEVEL=INFO
API_LOG_LEVEL=DEBUG
# Invalid - will raise ValidationError
API_LOG_LEVEL=INVALID # Must be DEBUG, INFO, WARNING, ERROR, or CRITICAL
# Valid
VECTOR_DB_TYPE=infinity
VECTOR_DB_TYPE=elasticsearch
VECTOR_DB_TYPE=es # Normalized to 'elasticsearch'
# Invalid - will raise ValidationError
VECTOR_DB_TYPE=invalid # Must be infinity, elasticsearch, or es
# Valid
ENVIRONMENT=development
ENVIRONMENT=testing
ENVIRONMENT=production
# Invalid - will raise ValidationError
ENVIRONMENT=staging # Must be development, testing, or production
In tests, you may need to reload configuration:
from src.config import get_settings, clear_settings_cache
# Clear cache to reload settings
clear_settings_cache()
settings = get_settings()
import os
from src.config import get_settings, clear_settings_cache
# Set test environment variables
os.environ['DB_HOST'] = 'test-db'
os.environ['VECTOR_DB_TYPE'] = 'elasticsearch'
# Clear cache and reload
clear_settings_cache()
settings = get_settings()
# Run tests
assert settings.database.host == 'test-db'
assert settings.vector_db_type == 'elasticsearch'
If you're migrating from the old configuration system (src/conf/settings.py), here's the mapping:
from src.conf.settings import model_settings, vector_db_settings
model_name = model_settings.chat_model_name
infinity_host = vector_db_settings.infinity_host
from src.config import get_settings
settings = get_settings()
model_name = settings.model.chat_model_name
infinity_host = settings.infinity.host
| Old Variable | New Variable |
|---|---|
MODEL_PROVIDER |
MODEL_PROVIDER (same) |
CHAT_MODEL_NAME |
MODEL_CHAT_MODEL_NAME |
EMBEDDING_MODEL_NAME |
MODEL_EMBEDDING_MODEL_NAME |
INFINITY_HOST |
INFINITY_HOST (same) |
MYSQL_HOST |
DB_HOST |
MYSQL_PORT |
DB_PORT |
MYSQL_USER |
DB_USERNAME |
MYSQL_PASSWORD |
DB_PASSWORD |
MYSQL_DATABASE |
DB_DATABASE |
LOG_LEVEL |
API_LOG_LEVEL |
.env file - It contains sensitive information.env.example - Keep it updated with all configuration options.env.example when adding new configuration.env file exists and is in the project root# Example error
ValidationError: 1 validation error for Settings
vector_db_type
Value error, Vector DB type must be one of ['infinity', 'elasticsearch', 'es']
Solution: Check the value matches one of the allowed options.
If configuration changes aren't reflected:
from src.config import clear_settings_cache
clear_settings_cache()
For complete configuration options, see:
src/config/settings.py - Configuration class definitions.env.example - All available configuration options with descriptions