File size: 2,559 Bytes
cfc8e23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from functools import lru_cache
from pathlib import Path

from pydantic_settings import BaseSettings


class Settings(BaseSettings):
    """Application configuration settings"""

    api_title: str = "NEXUS"
    api_version: str = "0.1.0"

    host: str = "0.0.0.0"
    port: int = 8000

    chunk_size: int = 512
    chunk_overlap: int = 256

    llm_model: str = "gpt-4o"
    rerank_model: str = "cross-encoder/ms-marco-MiniLM-L-6-v2"

    use_colbert: bool = True
    colbert_model: str = "answerdotai/answerai-colbert-small-v1"
    colbert_top_k: int = 20
    colbert_only: bool = False

    dense_top_k: int = 20
    bm25_top_k: int = 20
    final_top_k: int = 10
    bm25_weight: float = 0.55
    colbert_weight: float = 0.45
    use_reranking: bool = True

    data_dir: str = "./data"
    index_path: str = "./data/indices"
    colbert_index_path: str = "./data/colbert_index"
    uploads_dir: str = "./data/uploads"

    openai_api_key: str | None = None

    log_level: str = "INFO"
    log_file: str | None = None

    max_concurrent_index_jobs: int = 1
    job_timeout_minutes: int = 60

    class Config:
        env_file = ".env"
        case_sensitive = False
        extra = "ignore"

    def model_post_init(self, __context) -> None:
        """Create necessary directories after model initialization"""
        for path in [
            self.data_dir,
            self.index_path,
            self.colbert_index_path,
            self.uploads_dir,
        ]:
            Path(path).mkdir(parents=True, exist_ok=True)

    @property
    def rag_config_dict(self) -> dict:
        """Get RAG configuration as dictionary for compatibility with existing code"""
        return {
            "chunk_size": self.chunk_size,
            "chunk_overlap": self.chunk_overlap,
            "rerank_model": self.rerank_model,
            "llm_model": self.llm_model,
            "use_colbert": self.use_colbert,
            "colbert_model": self.colbert_model,
            "colbert_top_k": self.colbert_top_k,
            "colbert_only": self.colbert_only,
            "dense_top_k": self.dense_top_k,
            "bm25_top_k": self.bm25_top_k,
            "final_top_k": self.final_top_k,
            "bm25_weight": self.bm25_weight,
            "colbert_weight": self.colbert_weight,
            "use_reranking": self.use_reranking,
            "index_path": self.index_path,
            "colbert_index_path": self.colbert_index_path,
            "openai_api_key": self.openai_api_key,
        }


@lru_cache
def get_settings():
    return Settings()