"""Load ``config.yaml`` and read values with the same keys the ROS nodes use.
The ROS nodes read parameters such as ``models/llm_provider`` from the parameter server, which ``tcc_ros.launch`` fills from ``tcc_ros/config/config.yaml``. :func:`get` reads the same slash-separated
keys from dictionary, so the toolkit and the robot agree.
"""
import os
from typing import Any, Dict, Optional
import yaml
_DATA_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "data")
#: Environment variable that holds the API key for each provider.
API_KEY_ENV = {
"openai": "OPENAI_API_KEY",
"deepseek": "DEEPSEEK_API_KEY",
"claude": "ANTHROPIC_API_KEY",
"gemini": "GEMINI_API_KEY",
"llama.cpp": None, # local server, no key
}
[docs]
def data_path(name: str) -> str:
"""Path of a file shipped in ``tcc_ironl/data``."""
return os.path.join(_DATA_DIR, name)
[docs]
def default_config_path() -> str:
"""Path of the ``config.yaml`` shipped with the package, identical to ``tcc_ros/config/config.yaml``."""
return data_path("config.yaml")
[docs]
def load_config(path: Optional[str] = None) -> Dict[str, Any]:
"""Load a TCC-IRoNL ``config.yaml``. Without ``path``, load the bundled one."""
with open(os.path.expanduser(path or default_config_path()), encoding="utf-8") as f:
return yaml.safe_load(f) or {}
[docs]
def get(cfg: Dict[str, Any], key: str, default: Any = None) -> Any:
"""Read a slash-separated key, e.g. ``get(cfg, "models/llm_name")``."""
node: Any = cfg
for part in key.strip("/").split("/"):
if not isinstance(node, dict) or part not in node:
return default
node = node[part]
return node
[docs]
def resolve_api_key(provider: str, configured_key: Optional[str] = None) -> Optional[str]:
"""Return the API key for ``provider``.
A non-empty ``models/llm_api_key`` from the config wins; otherwise the
provider's environment variable (see :data:`API_KEY_ENV`) is used.
Returns ``None`` for providers that need no key, or when none is set.
"""
if provider not in API_KEY_ENV:
raise ValueError(
f"Unsupported LLM provider: {provider!r}. Choose one of: {', '.join(API_KEY_ENV)}"
)
if configured_key:
return configured_key
env_var = API_KEY_ENV[provider]
return os.getenv(env_var) if env_var else None