This commit is contained in:
Timothy Jaeryang Baek 2026-10-05 10:48:41 +04:00
parent 7e7aeb48cd
commit cf5755f949
3 changed files with 28 additions and 18 deletions

View file

@ -1019,6 +1019,8 @@ async def unload_model(request: Request, form_data: ModelUnloadForm, user=Depend
log.exception(f'Failed to unload model on Ollama node {idx}: {e}')
errors.append({'url_idx': idx, 'error': str(e)})
await ollama.clear_models_cache(request)
if errors:
raise HTTPException(
status_code=500,
@ -1056,12 +1058,14 @@ async def unload_model(request: Request, form_data: ModelUnloadForm, user=Depend
if not r.ok:
detail = await r.text()
raise HTTPException(status_code=r.status, detail=detail)
return await r.json()
result = await r.json()
except HTTPException:
raise
except Exception as e:
log.exception(f'Failed to unload model via llama.cpp: {e}')
raise HTTPException(status_code=500, detail=str(e))
await openai.clear_models_cache(request)
return result
else:
raise HTTPException(
status_code=400,

View file

@ -291,6 +291,20 @@ class OllamaConfigForm(BaseModel):
OLLAMA_API_CONFIGS: dict
async def clear_models_cache(request: Request):
await get_all_models.cache.clear()
redis = getattr(request.app.state, 'redis', None)
if redis is not None:
await redis.delete(BASE_MODELS_CACHE_KEY)
request.app.state.BASE_MODELS = []
request.app.state.OLLAMA_MODELS = {}
models = getattr(request.app.state, 'MODELS', None)
if hasattr(models, 'clear'):
models.clear()
else:
request.app.state.MODELS = {}
@router.post('/config/update')
async def update_config(
request: Request,
@ -309,17 +323,7 @@ async def update_config(
}
)
await get_all_models.cache.clear()
redis = getattr(request.app.state, 'redis', None)
if redis is not None:
await redis.delete(BASE_MODELS_CACHE_KEY)
request.app.state.BASE_MODELS = []
request.app.state.OLLAMA_MODELS = {}
models = getattr(request.app.state, 'MODELS', None)
if hasattr(models, 'clear'):
models.clear()
else:
request.app.state.MODELS = {}
await clear_models_cache(request)
await publish_event(
request,
@ -649,6 +653,8 @@ async def unload_model(
log.exception(f'Failed to unload model on node {idx}: {e}')
errors.append({'url_idx': idx, 'success': False, 'error': str(e)})
await clear_models_cache(request)
if len(errors) > 0:
raise HTTPException(
status_code=500,

View file

@ -260,7 +260,7 @@ async def get_openai_connection(idx: int) -> tuple[str, str, dict]:
return url, key, api_config
async def clear_openai_model_cache(request: Request):
async def clear_models_cache(request: Request):
await get_all_models.cache.clear()
redis = getattr(request.app.state, 'redis', None)
if redis is not None:
@ -491,7 +491,7 @@ async def update_config(request: Request, form_data: OpenAIConfigForm, user=Depe
}
)
await clear_openai_model_cache(request)
await clear_models_cache(request)
await publish_event(
request,
@ -880,7 +880,7 @@ async def download_provider_model(
payload['model'] = strip_provider_model_prefix(payload['model'], api_config.get('prefix_id'))
result = await send_model_management_request(request, url_idx, 'download', 'POST', payload, user=user)
await clear_openai_model_cache(request)
await clear_models_cache(request)
await publish_event(
request,
EVENTS.MODEL_PROVIDER_MODEL_CREATED,
@ -919,7 +919,7 @@ async def load_provider_model(
payload['model'] = strip_provider_model_prefix(payload['model'], api_config.get('prefix_id'))
result = await send_model_management_request(request, url_idx, 'load', 'POST', payload, user=user)
await clear_openai_model_cache(request)
await clear_models_cache(request)
return result
@ -935,7 +935,7 @@ async def unload_provider_model(
payload['model'] = strip_provider_model_prefix(payload['model'], api_config.get('prefix_id'))
result = await send_model_management_request(request, url_idx, 'unload', 'POST', payload, user=user)
await clear_openai_model_cache(request)
await clear_models_cache(request)
return result
@ -962,7 +962,7 @@ async def delete_provider_model(
query={'model': actual_model},
user=user,
)
await clear_openai_model_cache(request)
await clear_models_cache(request)
await publish_event(
request,
EVENTS.MODEL_PROVIDER_MODEL_DELETED,