Add multi-tenancy support and memory storage infrastructure:
- Add ContextVar-based request context (src/core/context.py)
- Async-safe user/conversation tracking via contextvars
- RequestContext manager for clean setup/teardown
- get_user(), get_conversation_id() helpers
- Add multi-tenancy utilities (src/core/multi_tenancy.py)
- User ID sanitization for collection/key names
- get_memory_collection_name(), get_session_key() helpers
- Add Ollama embedding client (src/core/embeddings.py)
- nomic-embed-text model (768 dimensions)
- embed(), embed_batch(), health_check() methods
- Add Qdrant client wrapper (src/core/qdrant.py)
- Per-user collection pattern: memories_{user}
- upsert_memory(), search_memories(), delete_memory()
- Type-based filtering support
- Add Redis memory cache (src/core/memory_cache.py)
- Session context with 24h TTL
- Recent entities tracking
- Separate from benchmarks (db=2)
- Update config with memory settings
- QDRANT_HOST, QDRANT_PORT, QDRANT_EMBEDDING_DIM
- OLLAMA_EMBEDDING_MODEL
- REDIS_MEMORY_DB, REDIS_MEMORY_TTL_HOURS
- Add user field to ResponseRequest (OpenAI standard)
- Set context in router, reset in finally block
- Update librarian client to use get_user() (12 methods)
All 333 unit tests pass.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
208 lines
5.9 KiB
Python
208 lines
5.9 KiB
Python
"""
|
|
Global application configuration.
|
|
Following best practice of splitting config across domains.
|
|
"""
|
|
from enum import Enum
|
|
from functools import lru_cache
|
|
from pathlib import Path
|
|
|
|
from pydantic import Field, HttpUrl
|
|
from pydantic_settings import BaseSettings, SettingsConfigDict
|
|
|
|
|
|
def _get_version_from_pyproject() -> str:
|
|
"""
|
|
Load version from pyproject.toml.
|
|
|
|
Falls back to "unknown" if file cannot be read.
|
|
"""
|
|
try:
|
|
# Find pyproject.toml relative to this file
|
|
config_dir = Path(__file__).parent
|
|
pyproject_path = config_dir.parent.parent / "pyproject.toml"
|
|
|
|
if pyproject_path.exists():
|
|
content = pyproject_path.read_text()
|
|
for line in content.splitlines():
|
|
if line.strip().startswith("version"):
|
|
# Parse: version = "1.0.0"
|
|
return line.split("=", 1)[1].strip().strip('"').strip("'")
|
|
except Exception:
|
|
pass
|
|
return "unknown"
|
|
|
|
|
|
class Environment(str, Enum):
|
|
"""Application environment."""
|
|
DEVELOPMENT = "development"
|
|
PRODUCTION = "production"
|
|
TESTING = "testing"
|
|
|
|
|
|
class Config(BaseSettings):
|
|
"""
|
|
Global application configuration.
|
|
|
|
Loads from environment variables and .env file.
|
|
Domain-specific configs should be in their respective modules.
|
|
"""
|
|
model_config = SettingsConfigDict(
|
|
env_file=".env",
|
|
env_file_encoding="utf-8",
|
|
case_sensitive=True,
|
|
extra="ignore",
|
|
)
|
|
|
|
# Application
|
|
APP_NAME: str = "OpenAI-Compatible API"
|
|
APP_VERSION: str = Field(default_factory=_get_version_from_pyproject)
|
|
ENVIRONMENT: Environment = Environment.DEVELOPMENT
|
|
DEBUG: bool = Field(default=False, description="Debug mode")
|
|
|
|
# API Configuration
|
|
API_HOST: str = Field(default="0.0.0.0", description="API host")
|
|
API_PORT: int = Field(default=8000, description="API port")
|
|
API_PREFIX: str = Field(default="/v1", description="API route prefix")
|
|
|
|
# Ollama Configuration
|
|
OLLAMA_HOST: HttpUrl = Field(
|
|
default="http://localhost:11434",
|
|
description="Ollama server URL"
|
|
)
|
|
OLLAMA_DEFAULT_MODEL: str = Field(
|
|
default="mistral-nemo:latest",
|
|
description="Default Ollama model"
|
|
)
|
|
OLLAMA_TIMEOUT: int = Field(
|
|
default=120,
|
|
description="Ollama request timeout in seconds"
|
|
)
|
|
STREAM_TIMEOUT: int = Field(
|
|
default=20,
|
|
description="Timeout for each streaming turn in seconds"
|
|
)
|
|
|
|
# SearXNG Configuration
|
|
SEARXNG_HOST: HttpUrl = Field(
|
|
default="http://localhost:8087",
|
|
description="SearXNG server URL"
|
|
)
|
|
SEARXNG_TIMEOUT: int = Field(
|
|
default=30,
|
|
description="SearXNG request timeout in seconds"
|
|
)
|
|
|
|
# Redis Configuration
|
|
REDIS_HOST: str = Field(
|
|
default="localhost",
|
|
description="Redis server host"
|
|
)
|
|
REDIS_PORT: int = Field(
|
|
default=6379,
|
|
description="Redis server port"
|
|
)
|
|
REDIS_DB: int = Field(
|
|
default=1,
|
|
description="Redis database number"
|
|
)
|
|
REDIS_TIMEOUT: int = Field(
|
|
default=5,
|
|
description="Redis connection timeout in seconds"
|
|
)
|
|
|
|
# Library-Desk Configuration (The Librarian backend)
|
|
LIBRARY_DESK_HOST: HttpUrl = Field(
|
|
default="http://localhost:8089",
|
|
description="Library-Desk API URL"
|
|
)
|
|
LIBRARY_DESK_API_KEY: str = Field(
|
|
default="",
|
|
description="API key for Library-Desk authentication"
|
|
)
|
|
LIBRARY_DESK_TIMEOUT: int = Field(
|
|
default=60,
|
|
description="Library-Desk request timeout in seconds"
|
|
)
|
|
|
|
# Qdrant Configuration (Memory vector storage)
|
|
QDRANT_HOST: str = Field(
|
|
default="localhost",
|
|
description="Qdrant server host"
|
|
)
|
|
QDRANT_PORT: int = Field(
|
|
default=6333,
|
|
description="Qdrant server port"
|
|
)
|
|
QDRANT_EMBEDDING_DIM: int = Field(
|
|
default=768,
|
|
description="Embedding dimension (768 for nomic-embed-text)"
|
|
)
|
|
|
|
# Ollama Embedding Configuration
|
|
OLLAMA_EMBEDDING_MODEL: str = Field(
|
|
default="nomic-embed-text",
|
|
description="Ollama model for embeddings"
|
|
)
|
|
|
|
# Redis Memory Database (separate from benchmarks)
|
|
REDIS_MEMORY_DB: int = Field(
|
|
default=2,
|
|
description="Redis database number for memory cache"
|
|
)
|
|
REDIS_MEMORY_TTL_HOURS: int = Field(
|
|
default=24,
|
|
description="TTL for session context in hours"
|
|
)
|
|
|
|
# Logging
|
|
LOG_LEVEL: str = Field(default="INFO", description="Logging level")
|
|
ENABLE_BENCHMARKS: bool = Field(default=True, description="Enable performance benchmarking")
|
|
|
|
# CORS
|
|
CORS_ORIGINS: list[str] = Field(
|
|
default=["*"],
|
|
description="Allowed CORS origins"
|
|
)
|
|
CORS_ALLOW_CREDENTIALS: bool = True
|
|
CORS_ALLOW_METHODS: list[str] = ["*"]
|
|
CORS_ALLOW_HEADERS: list[str] = ["*"]
|
|
|
|
@property
|
|
def redis_url(self) -> str:
|
|
"""Construct Redis connection URL for benchmarks."""
|
|
return f"redis://{self.REDIS_HOST}:{self.REDIS_PORT}/{self.REDIS_DB}"
|
|
|
|
@property
|
|
def redis_memory_url(self) -> str:
|
|
"""Construct Redis connection URL for memory cache."""
|
|
return f"redis://{self.REDIS_HOST}:{self.REDIS_PORT}/{self.REDIS_MEMORY_DB}"
|
|
|
|
@property
|
|
def qdrant_url(self) -> str:
|
|
"""Construct Qdrant server URL."""
|
|
return f"http://{self.QDRANT_HOST}:{self.QDRANT_PORT}"
|
|
|
|
@property
|
|
def log_format(self) -> str:
|
|
"""
|
|
Determine log format based on environment.
|
|
|
|
- production: JSON format for machine parsing
|
|
- development/testing: Console format for human readability
|
|
"""
|
|
return "json" if self.ENVIRONMENT == Environment.PRODUCTION else "console"
|
|
|
|
|
|
@lru_cache
|
|
def get_config() -> Config:
|
|
"""
|
|
Get cached configuration instance.
|
|
|
|
Uses lru_cache to ensure config is loaded once and reused.
|
|
"""
|
|
return Config()
|
|
|
|
|
|
# Global config instance
|
|
config = get_config()
|