File size: 1,233 Bytes
ed656e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Reads config/config.yaml; explicit arguments always win."""
import os
from functools import lru_cache

import yaml

CONFIG_PATH = os.environ.get("VRAG_CONFIG", "config/config.yaml")


@lru_cache(maxsize=1)
def load_config(path: str = CONFIG_PATH) -> dict:
    """The parsed config file (read once and cached); an empty dict when the file is missing."""
    if not os.path.exists(path):
        print(f"[Config] {path} not found — falling back to built-in defaults.")
        return {}
    with open(path, "r", encoding="utf-8") as f:
        return yaml.safe_load(f) or {}


def get(dotted_key: str, default=None):
    """get('retrieval.top_k_rerank', 3) -> value from config.yaml, or the default."""
    # Read the module attribute at call time so VRAG_CONFIG (and tests) can redirect it.
    node = load_config(CONFIG_PATH)
    for part in dotted_key.split("."):
        if not isinstance(node, dict) or part not in node:
            return default
        node = node[part]
    return default if node is None else node


def pick(explicit, dotted_key: str, default):
    """Explicit argument wins; otherwise config; otherwise the built-in default."""
    return explicit if explicit is not None else get(dotted_key, default)