diff --git a/backend/open_webui/main.py b/backend/open_webui/main.py index 914c0620b8..36043d071a 100644 --- a/backend/open_webui/main.py +++ b/backend/open_webui/main.py @@ -587,7 +587,7 @@ app.state.SCIM_TOKEN = SCIM_TOKEN ######################################## app.state.BASE_MODELS = [] -app.state.BASE_MODELS_EPOCH = None +app.state.BASE_MODELS_EPOCH = '' ######################################## # diff --git a/backend/open_webui/routers/openai.py b/backend/open_webui/routers/openai.py index 7ea9302d47..fc6a6ee180 100644 --- a/backend/open_webui/routers/openai.py +++ b/backend/open_webui/routers/openai.py @@ -345,7 +345,6 @@ async def get_openai_connection(idx: int) -> tuple[str, str, dict]: async def clear_openai_model_cache(request: Request): - await Config.upsert({'models.base_models_cache_epoch': str(uuid4())}) await get_all_models.cache.clear() request.app.state.BASE_MODELS = [] request.app.state.OPENAI_MODELS = {} @@ -570,10 +569,18 @@ async def update_config(request: Request, form_data: OpenAIConfigForm, user=Depe 'openai.api_base_urls': form_data.OPENAI_API_BASE_URLS, 'openai.api_keys': api_keys, 'openai.api_configs': api_configs, + 'models.base_models_cache_epoch': str(uuid4()), } ) - await clear_openai_model_cache(request) + await get_all_models.cache.clear() + request.app.state.BASE_MODELS = [] + request.app.state.OPENAI_MODELS = {} + models = getattr(request.app.state, 'MODELS', None) + if hasattr(models, 'clear'): + models.clear() + else: + request.app.state.MODELS = {} await publish_event( request, diff --git a/backend/open_webui/utils/models.py b/backend/open_webui/utils/models.py index d55c5b52bc..89e104a869 100644 --- a/backend/open_webui/utils/models.py +++ b/backend/open_webui/utils/models.py @@ -72,25 +72,26 @@ async def get_all_models(request, refresh: bool = False, user: UserModel = None) 'evaluation.arena.models', 'models.default_metadata', ) - # A changed epoch means another instance edited a provider, so our cached models are stale. cache_epoch = config.get('models.base_models_cache_epoch') - stale = request.app.state.BASE_MODELS_EPOCH != cache_epoch + if request.app.state.BASE_MODELS_EPOCH != cache_epoch: + # Another instance changed a provider connection, so our cached models are outdated. + refresh = True - if refresh or stale: + if refresh: await openai.get_all_models.cache.clear() await ollama.get_all_models.cache.clear() if ( request.app.state.MODELS and request.app.state.BASE_MODELS - and (config.get('models.base_models_cache') and not (refresh or stale)) + and (config.get('models.base_models_cache') and not refresh) ): base_models = request.app.state.BASE_MODELS else: base_models = await get_all_base_models(request, user=user) - request.app.state.BASE_MODELS_EPOCH = cache_epoch if base_models: request.app.state.BASE_MODELS = base_models + request.app.state.BASE_MODELS_EPOCH = cache_epoch else: base_models = request.app.state.BASE_MODELS