Files
quality_analyse/scripts/nao_model_settings.py
T

26 lines
1.2 KiB
Python

"""Read the UI-managed, private NAO model configuration with legacy bootstrap."""
import json
from pathlib import Path
from urllib.parse import urlsplit
ROOT = Path(__file__).resolve().parents[1]
SETTINGS_FILE = ROOT / '.runtime/nao-model-config.json'
def model_settings():
if not SETTINGS_FILE.exists():
from dbgpt_trial import model_settings as legacy_settings
return legacy_settings()
if SETTINGS_FILE.is_symlink() or SETTINGS_FILE.parent.is_symlink() or SETTINGS_FILE.stat().st_size > 32768:
raise ValueError('Invalid private model configuration')
value = json.loads(SETTINGS_FILE.read_text(encoding='utf-8'))
base, key, model = (value.get(name) for name in ('baseUrl', 'apiKey', 'model'))
if not all(isinstance(item, str) and item.strip() for item in (base, key, model)):
raise ValueError('Incomplete model configuration')
u = urlsplit(base)
if u.scheme not in ('http', 'https') or not u.hostname or u.username or u.password or u.query or u.fragment:
raise ValueError('Invalid model endpoint')
if len(key) > 4096 or any(c.isspace() or ord(c) < 32 for c in key):
raise ValueError('Invalid model credential')
return base.rstrip('/'), key, model