splinter-keep/tools/engine_lib/config.py
2026-06-30 20:03:53 +02:00

75 lines
2.1 KiB
Python

#!/usr/bin/env python3
"""
config.py — LLM configuration loading and accessors for The Chaos engine.
"""
from __future__ import annotations
import json
import sys
from pathlib import Path
from .paths import CONFIG_PATH
DEFAULT_CONFIG: dict = {
"llm": {
"model": "ollama/llama3.1",
"api_key": None,
"api_base": None,
"temperature": 0.8,
"max_tokens": 300,
}
}
def load_config(path: Path = CONFIG_PATH) -> dict:
"""Load config from path, creating default if missing. Returns config dict."""
if not path.exists():
print(
"No session/config.json found. Creating default.\n"
"Edit the model field (e.g. 'ollama/llama3.1', 'openai/gpt-4', "
"'anthropic/claude-sonnet-4-20250514') and set api_key if needed.",
file=sys.stderr,
)
cfg = dict(DEFAULT_CONFIG)
save_config(cfg, path)
return cfg
raw = path.read_text()
cfg = json.loads(raw)
llm = cfg.get("llm", {})
if not llm.get("api_key"):
llm["api_key"] = None
return cfg
def save_config(config: dict, path: Path = CONFIG_PATH) -> None:
"""Save config dict to path."""
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(json.dumps(config, indent=2) + "\n")
# ── Accessors ──────────────────────────────────────────────────────────────
def get_model(config: dict) -> str:
return config.get("llm", {}).get("model", "ollama/llama3.1")
def get_api_key(config: dict) -> str | None:
return config.get("llm", {}).get("api_key")
def get_api_base(config: dict) -> str | None:
return config.get("llm", {}).get("api_base")
def get_temperature(config: dict) -> float:
return config.get("llm", {}).get("temperature", 0.8)
def get_max_tokens(config: dict) -> int:
return config.get("llm", {}).get("max_tokens", 512)
def get_timeout(config: dict) -> int:
return config.get("llm", {}).get("timeout", 120)