This commit is contained in:
Timothy Jaeryang Baek
2026-06-29 11:38:59 -05:00
parent 7572283517
commit b696c5deff

View File

@@ -238,6 +238,28 @@ def get_microsoft_entra_id_access_token():
router = APIRouter()
LLAMACPP_LOADED_STATES = {'loaded', 'sleeping'}
LLAMACPP_UNLOADED_STATES = {'loading', 'unloaded'}
def get_llamacpp_model_loaded_state(model: dict, provider: str, manual_model_ids: bool = False) -> bool | None:
if provider != 'llama.cpp':
return None
status = model.get('status')
if isinstance(status, dict):
value = status.get('value')
if value in LLAMACPP_LOADED_STATES:
return True
if value in LLAMACPP_UNLOADED_STATES:
return False
if not manual_model_ids and 'status' not in model:
return True
return None
OPENAI_CONFIG_KEYS = {
'ENABLE_OPENAI_API': 'openai.enable',
'OPENAI_API_BASE_URLS': 'openai.api_base_urls',
@@ -529,7 +551,7 @@ async def get_filtered_models(models, user, db=None):
async def get_all_models(request: Request, user: UserModel) -> dict[str, list]:
log.info('get_all_models()')
enable_openai_api, api_base_urls, _, _ = await get_openai_runtime_config()
enable_openai_api, api_base_urls, _, api_configs = await get_openai_runtime_config()
if not enable_openai_api:
return {'data': []}
@@ -573,21 +595,25 @@ async def get_all_models(request: Request, user: UserModel) -> dict[str, list]:
continue
if model_id and model_id not in models:
api_config = api_configs.get(str(idx), api_configs.get(base_url, {}))
provider = model.get('provider', '')
merged = {
**model,
'name': model.get('name', model_id),
'owned_by': 'openai',
'openai': model,
'connection_type': model.get('connection_type', 'external'),
'provider': model.get('provider', ''),
'provider': provider,
'urlIdx': idx,
}
# llama.cpp router mode: derive loaded state from
# the status object returned by GET /v1/models.
status = model.get('status')
if isinstance(status, dict) and 'value' in status:
merged['loaded'] = status['value'] in ('loaded', 'sleeping')
loaded = get_llamacpp_model_loaded_state(
model,
provider,
manual_model_ids=bool(api_config.get('model_ids')),
)
if loaded is not None:
merged['loaded'] = loaded
models[model_id] = merged