File size: 2,842 Bytes
613af80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Settings of the system and access to secrets.

All settings were chosen on the tuning half of RareBench and then frozen.
API keys are never written in the code: they are read from environment
variables (or Colab / Kaggle secrets when running there).
"""
from __future__ import annotations

import os
import sys
from dataclasses import dataclass, field
from pathlib import Path


def default_data_dir() -> Path:
    """Folder for downloaded data, vectors and the LLM cache (never committed)."""
    if "google.colab" in sys.modules:
        return Path("/content/drive/MyDrive/rag-maladies-rares/data")
    if os.path.exists("/kaggle/working"):
        return Path("/kaggle/temp/data")
    return Path(os.environ.get("RAG_DATA_DIR", "./data"))


def get_secret(name: str) -> str | None:
    """Read an API key from the environment, Colab secrets or Kaggle secrets."""
    value = os.environ.get(name)
    if value:
        return value
    if "google.colab" in sys.modules:
        try:
            from google.colab import userdata  # type: ignore
            return userdata.get(name)
        except Exception:
            return None
    if os.path.exists("/kaggle/working"):
        try:
            from kaggle_secrets import UserSecretsClient  # type: ignore
            return UserSecretsClient().get_secret(name)
        except Exception:
            return None
    return None


@dataclass
class Settings:
    """Frozen settings of the official system (see the notebook, section 1)."""

    data_dir: Path = field(default_factory=default_data_dir)
    llm_model: str = "openai/gpt-oss-120b"          # free LLM through Groq
    embedding_model: str = "BAAI/bge-small-en-v1.5"
    n_candidates: int = 20                           # diseases kept by the RAG
    alpha: float = 0.25                              # penalty for very long disease profiles
    metabolic_threshold: float = 0.15                # routing: share of metabolic findings
    llm_weight_metabolic: float = 1.0                # LLM weight for "biological" patients
    llm_weight_clinical: float = 0.5                 # LLM weight for "clinical" patients
    rrf_constant: int = 1                            # Reciprocal Rank Fusion constant
    by_family: bool = False                          # strict scoring (official test configuration)
    hpo_threshold: float = 0.75                      # min similarity text -> HPO code
    name_threshold: float = 0.85                     # min similarity disease name -> code
    to_verify_threshold: float = 0.85                # below this, a symptom is flagged "to verify"
    decontaminate: bool = False                      # leave-publication-out base (Phenopacket Store)

    def __post_init__(self) -> None:
        self.data_dir = Path(self.data_dir)
        self.data_dir.mkdir(parents=True, exist_ok=True)