File size: 4,036 Bytes
501bc8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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)