feat(tools-pve): prepare LiteLLM gateway with persistent OAuth routes
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
"""Render only credential-ready routes; credentials remain environment references."""
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
import yaml
|
||||
|
||||
|
||||
def build_config(catalog, env, exists):
|
||||
routes = []
|
||||
|
||||
def add(alias, model, key=None, **params):
|
||||
if key:
|
||||
params['api_key'] = 'os.environ/' + key
|
||||
routes.append({'model_name': alias, 'litellm_params': {'model': model, **params}})
|
||||
|
||||
for key, suffix in [('ZENMUX_SUBSCRIPTION_API_KEY', ''), ('ZENMUX_PAYG_API_KEY', '-payg')]:
|
||||
if env.get(key):
|
||||
for alias, model in catalog['zenmux'].items():
|
||||
# Strip only the LiteLLM provider prefix, retaining ZenMux's vendor prefix.
|
||||
add(alias + suffix, 'anthropic/' + model, key,
|
||||
api_base='https://zenmux.ai/api/anthropic')
|
||||
if env.get('OPENROUTER_API_KEY'):
|
||||
for alias, model in catalog['openrouter'].items():
|
||||
add(alias, 'openrouter/' + model, 'OPENROUTER_API_KEY')
|
||||
if env.get('GOOGLE_AI_STUDIO_API_KEY'):
|
||||
for alias, model in catalog['google_ai_studio'].items():
|
||||
add(alias, 'gemini/' + model, 'GOOGLE_AI_STUDIO_API_KEY')
|
||||
for provider, token_var, default_dir in [
|
||||
('chatgpt', 'CHATGPT_TOKEN_DIR', '/tokens/chatgpt'),
|
||||
('xai', 'XAI_OAUTH_TOKEN_DIR', '/tokens/xai'),
|
||||
]:
|
||||
if exists(Path(env.get(token_var, default_dir)) / 'auth.json'):
|
||||
for alias, model in catalog[provider].items():
|
||||
extra = {'use_xai_oauth': True} if provider == 'xai' else {}
|
||||
add(alias, provider + '/' + model, **extra)
|
||||
return {
|
||||
'model_list': routes,
|
||||
'general_settings': {
|
||||
'master_key': 'os.environ/LITELLM_MASTER_KEY',
|
||||
'database_url': 'os.environ/DATABASE_URL',
|
||||
'store_model_in_db': False,
|
||||
},
|
||||
'litellm_settings': {'drop_params': False, 'set_verbose': False},
|
||||
# No cross-provider or subscription-to-PAYG fallback without an explicit decision.
|
||||
'router_settings': {'num_retries': 1, 'timeout': 180},
|
||||
}
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
catalog = yaml.safe_load(Path('/etc/litellm/models.yaml').read_text())
|
||||
config = build_config(catalog, os.environ, Path.is_file)
|
||||
output = Path('/tmp/litellm.yaml')
|
||||
output.write_text(yaml.safe_dump(config, allow_unicode=True, sort_keys=False))
|
||||
output.chmod(0o600)
|
||||
print('Configured routes:', ', '.join(route['model_name'] for route in config['model_list']))
|
||||
Reference in New Issue
Block a user