System Architecture
Core Features
Data Management
Frontend Components
Backend Systems
The following files were used as context for generating this wiki page:
This page provides a comprehensive overview of the system's configuration mechanisms, detailing how various settings are managed, loaded, and applied across the application. Effective configuration is crucial for adapting the system to different environments, integrating with diverse external services, and optimizing performance. It covers the core configuration model, the use of YAML templates, the structure of application configuration classes, provider-specific settings, cache management, and environment-driven initialization.
The configuration system is designed for flexibility and maintainability, allowing developers and administrators to easily adjust parameters without modifying core application code. It prioritizes a clear hierarchy for loading settings, ensuring that environment-specific overrides can be applied seamlessly.
The system employs a layered configuration model, starting with default values, which can then be overridden by YAML configuration files, and finally by environment variables. This hierarchy ensures that settings are applied predictably, with environment variables taking precedence for runtime adjustments.
The core configuration is typically loaded at application startup. It involves parsing configuration files, validating settings against predefined schemas, and initializing configuration objects that are then accessible throughout the application.
Sources: config_model.py:1-20, environment_loader.py:1-15
Configuration settings are primarily defined using YAML files, which provide a human-readable and structured format. These templates serve as the baseline for various deployments and can be customized for specific environments. The system typically looks for a settings.yaml file or a path specified via an environment variable.
settings.yaml Structureapplication:
name: "mem0-app"
debug_mode: false
log_level: "INFO"
providers:
llm:
default: "openai"
openai:
api_key: "${OPENAI_API_KEY}"
model: "gpt-4o"
anthropic:
api_key: "${ANTHROPIC_API_KEY}"
model: "claude-3-opus-20240229"
embedding:
default: "openai"
openai:
api_key: "${OPENAI_API_KEY}"
model: "text-embedding-3-small"
cache:
enabled: true
type: "redis"
redis:
host: "localhost"
port: 6379
db: 0
ttl_seconds: 3600Sources: settings.yaml:1-25
Sources: settings.yaml:1-25
The parsed YAML and environment variables are mapped into strongly typed configuration classes within the application. This provides type safety and ensures that all parts of the application interact with a consistent and validated configuration object. These classes often use data validation libraries (e.g., Pydantic in Python) to enforce schemas and provide default values.
# config_model.py
from pydantic import BaseModel, Field, SecretStr
from typing import Optional, Dict
class LLMProviderConfig(BaseModel):
api_key: SecretStr = Field(..., env="OPENAI_API_KEY")
model: str = "gpt-4o"
class EmbeddingProviderConfig(BaseModel):
api_key: SecretStr = Field(..., env="OPENAI_API_KEY")
model: str = "text-embedding-3-small"
class ProvidersConfig(BaseModel):
default_llm: str = "openai"
default_embedding: str = "openai"
openai_llm: Optional[LLMProviderConfig] = None
anthropic_llm: Optional[LLMProviderConfig] = None # Reusing LLMProviderConfig for simplicity
openai_embedding: Optional[EmbeddingProviderConfig] = None
class RedisCacheConfig(BaseModel):
host: str = "localhost"
port: int = 6379
db: int = 0
class CacheConfig(BaseModel):
enabled: bool = True
type: str = "redis"
redis: Optional[RedisCacheConfig] = None
ttl_seconds: int = 3600
class ApplicationConfig(BaseModel):
name: str = "mem0-app"
debug_mode: bool = False
log_level: str = "INFO"
class Config(BaseModel):
application: ApplicationConfig = Field(default_factory=ApplicationConfig)
providers: ProvidersConfig = Field(default_factory=ProvidersConfig)
cache: CacheConfig = Field(default_factory=CacheConfig)Sources: config_model.py:25-80
The system supports integration with various external services, such as Large Language Models (LLMs) and embedding providers. Each provider typically requires its own set of configuration parameters, such as API keys, model names, and specific endpoint URLs. These settings are nested under the providers section in the YAML configuration.
providers:
llm:
default: "openai"
openai:
api_key: "${OPENAI_API_KEY}" # Loaded from environment variable
model: "gpt-4o"
temperature: 0.7API keys and other sensitive credentials should always be loaded from environment variables (e.g., "${OPENAI_API_KEY}") rather than hardcoded directly into YAML files. This prevents accidental exposure and allows for easier management in different environments.
Sources: settings.yaml:10-20, config_model.py:30-35
Caching is an essential component for improving performance and reducing costs associated with external API calls. The system's caching mechanism is configurable, allowing users to enable/disable it, select a cache backend, and define cache-specific parameters.
Sources: settings.yaml:22-25, config_model.py:50-60
The configuration system is designed to be highly adaptable to different deployment environments (development, staging, production). Environment variables play a critical role in overriding default and YAML-defined settings, providing a robust mechanism for runtime configuration.
The loading order is typically:
settings.yaml or specified path).This ensures that environment variables always take precedence, which is crucial for sensitive data like API keys and for adjusting behavior in different deployment contexts.
Set environment variables in your deployment environment. For example, in a shell:
export OPENAI_API_KEY="sk-your-openai-key"
export MEM0_APPLICATION_DEBUG_MODE="true"
export MEM0_CACHE_REDIS_HOST="my-redis-server"The system typically uses a prefix (e.g., MEM0_) and converts nested YAML paths to uppercase and underscores (e.g., cache.redis.host becomes MEM0_CACHE_REDIS_HOST).
The application's startup sequence will automatically detect and apply these environment variables.
# Example of how config might be loaded in application entry point
from mem0.config import Config
def main():
config = Config.load_from_env_and_yaml()
# Now config object contains all settings, with ENV variables applied
print(f"Debug Mode: {config.application.debug_mode}")
print(f
After startup, you can log or inspect the active configuration to ensure that environment variables have been correctly applied.
Always use environment variables for sensitive information (API keys, database credentials) and for environment-specific overrides (e.g., database host, debug mode). This promotes security and simplifies deployment across multiple environments.