Files
xc_agent_platform_demo/src/config_params.py

67 lines
2.8 KiB
Python
Raw Normal View History

"""Dry-run configuration: token-resolvable build-config first; failures fall back to sample with GAP."""
import os
from pathlib import Path
from src.build_config_client import build_config, BuildConfigError
from src.task_add_client import resolve_token
SOURCE = 'POST /api/adapt/task/build-config (discovered via capture); fallback templates/config_params.sample.yaml'
GAP = 'GAP: build-config API discovered via capture; sample is dry-run draft only, NOT production-ready. API foothold evidence: GAP (not wired).'
GPU_ENUMS = {
'壁砺': 'Biren_166m', '壁砺166M': 'Biren_166m', 'Biren_166m': 'Biren_166m',
'曦望': 'Sunrise_pt-200-x1', '曦望 S2': 'Sunrise_pt-200-x1',
'Sunrise_pt-200-x1': 'Sunrise_pt-200-x1',
}
def config_params_source():
"""Requested source preference for dry-run.
- CONFIG_PARAMS_SOURCE=sample → force offline sample
- CONFIG_PARAMS_SOURCE=build-config → force API path (with soft fallback in with_meta)
- default (empty): prefer build-config when resolve_token() succeeds; else sample
- ALLOW_BUILD_CONFIG=1 remains a legacy opt-in synonym when source empty and token ok
"""
source = os.environ.get('CONFIG_PARAMS_SOURCE', '').strip()
if source not in ('', 'sample', 'build-config'):
raise BuildConfigError('unsupported CONFIG_PARAMS_SOURCE')
if source:
return source
try:
resolve_token()
except (ValueError, OSError):
return 'sample'
# Token resolvable → prefer build-config (kickoff select-opt default).
return 'build-config'
def build_config_params_with_meta(gpu_type=None, framework='vllm', task_type='text-generation'):
"""Return (YAML, metadata); API failures explicitly fall back for dry-run.
scripts/build_config_dry.py calls build_config directly and remains strict.
"""
source = config_params_source()
meta = {'configParams_source': 'sample'}
try:
resolve_token()
except (ValueError, OSError):
meta['auth'] = 'missing'
else:
meta['auth'] = 'ok'
gpu_enum = GPU_ENUMS.get(gpu_type)
if source == 'build-config' and gpu_enum is None:
meta['configParams_fallback_reason'] = 'GAP: gpu enum unmapped'
elif source == 'build-config':
try:
yaml = build_config(gpu_enum, framework, task_type)
except BuildConfigError as error:
meta['configParams_fallback_reason'] = 'GAP: ' + error.error_attribution
else:
meta['configParams_source'] = 'build-config'
return yaml, meta
return (Path(__file__).resolve().parents[1] / 'templates/config_params.sample.yaml').read_text(), meta
def build_config_params(gpu_type=None, framework='vllm', task_type='text-generation'):
"""Compatible YAML-only dry-run wrapper, including explicit fallback."""
return build_config_params_with_meta(gpu_type, framework, task_type)[0]