import os import logging import time from dataclasses import dataclass from pathlib import Path from typing import Optional from dotenv import load_dotenv # Load environment variables once — .env/local.env for local dev, fallback for production _env_path = Path(__file__).parent.parent.parent / '.env' / 'local.env' if _env_path.exists(): load_dotenv(dotenv_path=_env_path) else: load_dotenv() logger = logging.getLogger("Config") # Project paths — single source of truth PROJECT_ROOT = Path(__file__).parent.parent.parent DATA_DIR = PROJECT_ROOT / "data" @dataclass(frozen=True) class SearchScoring: """Scoring constants for fuzzy search — extracted from retriever magic numbers.""" # Fuzzy matching min_score: int = 80 high_confidence_threshold: int = 90 fuzzy_title_threshold: int = 85 fuzzy_researcher_threshold: int = 90 fuzzy_method_threshold: int = 90 # Boosts title_boost: int = 400 researcher_boost: int = 300 methodology_boost: int = 200 filter_force_boost: int = 500 exact_id_boost: int = 1000 multi_keyword_bonus: int = 20 max_score_cap: int = 500 # Chat history max_history_messages: int = 6 # Semantic search semantic_search_enabled: bool = True rrf_k: int = 60 embedding_batch_size: int = 100 max_embedding_text_length: int = 2000 # Default scoring instance SCORING = SearchScoring() @dataclass(frozen=True) class Config: # Google AI (required) google_api_key: str google_api_key_label: str = "default" # Slack (optional — Slack 봇 병행 운영 시 필요) slack_bot_token: Optional[str] = None slack_app_token: Optional[str] = None # Supabase (optional — logging only) supabase_url: Optional[str] = None supabase_key: Optional[str] = None # HuggingFace (optional — data sync only) hf_token: Optional[str] = None hf_dataset_repo: Optional[str] = None # LLM llm_model: str = "gemini-2.5-pro" llm_fast_model: str = "gemini-2.5-flash" llm_agent_model: str = "gemini-2.5-flash" # agentic retriever용 (속도 우선) llm_temperature: float = 0 # Anthropic (optional — Claude 사용 시) anthropic_api_key: Optional[str] = None # LLM Cache llm_cache_path: str = ".langchain_cache.db" @classmethod def from_env(cls) -> "Config": google_api_key = os.getenv("GOOGLE_API_KEY", "") if not google_api_key: raise EnvironmentError("Required environment variable missing: GOOGLE_API_KEY") return cls( google_api_key=google_api_key, google_api_key_label=os.getenv("GOOGLE_API_KEY_LABEL", "default"), slack_bot_token=os.getenv("SLACK_BOT_TOKEN") or None, slack_app_token=os.getenv("SLACK_APP_TOKEN") or None, supabase_url=os.getenv("SUPABASE_URL"), supabase_key=os.getenv("SUPABASE_KEY"), hf_token=os.getenv("HF_TOKEN"), hf_dataset_repo=os.getenv("HF_DATASET_REPO"), anthropic_api_key=os.getenv("ANTHROPIC_API_KEY"), ) @property def langsmith_metadata(self) -> dict: return {"api_key_label": self.google_api_key_label} # Singleton instance — import this from other modules config = Config.from_env() # ── LLM Cache (SQLiteCache) ────────────────────────────────────────────────── from langchain_community.cache import SQLiteCache from langchain_core.globals import set_llm_cache _cache_path = str(PROJECT_ROOT / config.llm_cache_path) # Startup cleanup: remove cache file older than 72 hours if os.path.exists(_cache_path): _age_hours = (time.time() - os.path.getmtime(_cache_path)) / 3600 if _age_hours > 72: os.remove(_cache_path) logger.info("Stale LLM cache removed (%.0fh old)", _age_hours) set_llm_cache(SQLiteCache(database_path=_cache_path)) logger.info("LLM cache enabled: %s", _cache_path)