接入 OpenAI 官方 API 并为 Codex 订阅配置同型号回退
This commit is contained in:
@@ -91,6 +91,39 @@ def add_bailian_routes(config, env, vocabulary_path):
|
||||
return config
|
||||
|
||||
|
||||
def add_openai_routes(config, catalog, env):
|
||||
"""Official API is separate from OAuth and third-party OpenAI-compatible APIs."""
|
||||
if not env.get('OPENAI_API_KEY'):
|
||||
return config
|
||||
models = dict(catalog.get('openai', {}))
|
||||
for alias, model in catalog.get('chatgpt', {}).items():
|
||||
name = alias if alias.endswith('-latest') else model
|
||||
if name in models and models[name] != model:
|
||||
raise ValueError('Official API/OAuth model mismatch: ' + name)
|
||||
models[name] = model
|
||||
by_model = {}
|
||||
for alias, model in models.items():
|
||||
name = 'openai/' + alias
|
||||
config['model_list'].append({'model_name': name, 'litellm_params': {
|
||||
'model': 'openai/' + model, 'api_key': 'os.environ/OPENAI_API_KEY',
|
||||
'api_base': 'https://api.openai.com/v1', 'max_retries': 0}})
|
||||
by_model[model] = name
|
||||
for route in list(config['model_list']):
|
||||
params = route['litellm_params']
|
||||
if not params['model'].startswith('chatgpt/'):
|
||||
continue
|
||||
backup = by_model.get(params['model'].removeprefix('chatgpt/'))
|
||||
if not backup:
|
||||
raise ValueError('Missing same-model official API fallback')
|
||||
params['max_retries'] = 0
|
||||
route['model_info'] = {'cooldown_time': 60, 'allowed_fails_policy': {
|
||||
'RateLimitErrorAllowedFails': 0, 'AuthenticationErrorAllowedFails': 0,
|
||||
'InternalServerErrorAllowedFails': 0, 'ServiceUnavailableErrorAllowedFails': 0,
|
||||
'TimeoutErrorAllowedFails': 0}}
|
||||
config['router_settings']['fallbacks'].append({route['model_name']: [backup]})
|
||||
return config
|
||||
|
||||
|
||||
def build_config(catalog, env, exists, namespaced=True):
|
||||
routes = []
|
||||
fallbacks = []
|
||||
@@ -159,11 +192,13 @@ def build_config(catalog, env, exists, namespaced=True):
|
||||
'disable_env_credential_login': True,
|
||||
},
|
||||
'litellm_settings': {'drop_params': False, 'set_verbose': False},
|
||||
# Only explicitly approved open-model routes have cross-provider fallbacks.
|
||||
# Official OpenAI fallbacks are appended after namespace expansion.
|
||||
'router_settings': {'num_retries': 1, 'timeout': 180, 'fallbacks': fallbacks},
|
||||
}
|
||||
return add_bailian_routes(namespace_config(config), env,
|
||||
'/etc/litellm/personal-vocabulary.json') if namespaced else config
|
||||
if namespaced:
|
||||
config = add_bailian_routes(namespace_config(config), env,
|
||||
'/etc/litellm/personal-vocabulary.json')
|
||||
return add_openai_routes(config, catalog, env)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
Reference in New Issue
Block a user