refac: ENABLE_MODEL_LIST_CACHE -> ENABLE_BASE_MODELS_CACHE

This commit is contained in:
Timothy Jaeryang Baek 2025-06-30 13:27:07 +04:00
parent eac2f36f4f
commit 8a334decf6
5 changed files with 209 additions and 207 deletions

View file

@ -931,13 +931,13 @@ OPENAI_API_BASE_URL = "https://api.openai.com/v1"
#################################### ####################################
# MODEL_LIST # MODELS
#################################### ####################################
ENABLE_MODEL_LIST_CACHE = PersistentConfig( ENABLE_BASE_MODELS_CACHE = PersistentConfig(
"ENABLE_MODEL_LIST_CACHE", "ENABLE_BASE_MODELS_CACHE",
"models.cache", "models.base_models_cache",
os.environ.get("ENABLE_MODEL_LIST_CACHE", "False").lower() == "true", os.environ.get("ENABLE_BASE_MODELS_CACHE", "False").lower() == "true",
) )

View file

@ -117,7 +117,7 @@ from open_webui.config import (
# Direct Connections # Direct Connections
ENABLE_DIRECT_CONNECTIONS, ENABLE_DIRECT_CONNECTIONS,
# Model list # Model list
ENABLE_MODEL_LIST_CACHE, ENABLE_BASE_MODELS_CACHE,
# Thread pool size for FastAPI/AnyIO # Thread pool size for FastAPI/AnyIO
THREAD_POOL_SIZE, THREAD_POOL_SIZE,
# Tool Server Configs # Tool Server Configs
@ -537,7 +537,7 @@ async def lifespan(app: FastAPI):
asyncio.create_task(periodic_usage_pool_cleanup()) asyncio.create_task(periodic_usage_pool_cleanup())
if app.state.config.ENABLE_MODEL_LIST_CACHE: if app.state.config.ENABLE_BASE_MODELS_CACHE:
await get_all_models( await get_all_models(
Request( Request(
# Creating a mock request object to pass to get_all_models # Creating a mock request object to pass to get_all_models
@ -643,11 +643,12 @@ app.state.config.ENABLE_DIRECT_CONNECTIONS = ENABLE_DIRECT_CONNECTIONS
######################################## ########################################
# #
# MODEL LIST # MODELS
# #
######################################## ########################################
app.state.config.ENABLE_MODEL_LIST_CACHE = ENABLE_MODEL_LIST_CACHE app.state.config.ENABLE_BASE_MODELS_CACHE = ENABLE_BASE_MODELS_CACHE
app.state.BASE_MODELS = []
######################################## ########################################
# #

View file

@ -45,14 +45,14 @@ async def export_config(user=Depends(get_admin_user)):
class ConnectionsConfigForm(BaseModel): class ConnectionsConfigForm(BaseModel):
ENABLE_DIRECT_CONNECTIONS: bool ENABLE_DIRECT_CONNECTIONS: bool
ENABLE_MODEL_LIST_CACHE: bool ENABLE_BASE_MODELS_CACHE: bool
@router.get("/connections", response_model=ConnectionsConfigForm) @router.get("/connections", response_model=ConnectionsConfigForm)
async def get_connections_config(request: Request, user=Depends(get_admin_user)): async def get_connections_config(request: Request, user=Depends(get_admin_user)):
return { return {
"ENABLE_DIRECT_CONNECTIONS": request.app.state.config.ENABLE_DIRECT_CONNECTIONS, "ENABLE_DIRECT_CONNECTIONS": request.app.state.config.ENABLE_DIRECT_CONNECTIONS,
"ENABLE_MODEL_LIST_CACHE": request.app.state.config.ENABLE_MODEL_LIST_CACHE, "ENABLE_BASE_MODELS_CACHE": request.app.state.config.ENABLE_BASE_MODELS_CACHE,
} }
@ -65,11 +65,13 @@ async def set_connections_config(
request.app.state.config.ENABLE_DIRECT_CONNECTIONS = ( request.app.state.config.ENABLE_DIRECT_CONNECTIONS = (
form_data.ENABLE_DIRECT_CONNECTIONS form_data.ENABLE_DIRECT_CONNECTIONS
) )
request.app.state.config.ENABLE_MODEL_LIST_CACHE = form_data.ENABLE_MODEL_LIST_CACHE request.app.state.config.ENABLE_BASE_MODELS_CACHE = (
form_data.ENABLE_BASE_MODELS_CACHE
)
return { return {
"ENABLE_DIRECT_CONNECTIONS": request.app.state.config.ENABLE_DIRECT_CONNECTIONS, "ENABLE_DIRECT_CONNECTIONS": request.app.state.config.ENABLE_DIRECT_CONNECTIONS,
"ENABLE_MODEL_LIST_CACHE": request.app.state.config.ENABLE_MODEL_LIST_CACHE, "ENABLE_BASE_MODELS_CACHE": request.app.state.config.ENABLE_BASE_MODELS_CACHE,
} }

View file

@ -77,12 +77,15 @@ async def get_all_base_models(request: Request, user: UserModel = None):
async def get_all_models(request, refresh: bool = False, user: UserModel = None): async def get_all_models(request, refresh: bool = False, user: UserModel = None):
if request.app.state.MODELS and ( if (
request.app.state.config.ENABLE_MODEL_LIST_CACHE and not refresh request.app.state.MODELS
and request.app.state.BASE_MODELS
and (request.app.state.config.ENABLE_BASE_MODELS_CACHE and not refresh)
): ):
return list(request.app.state.MODELS.values()) models = request.app.state.BASE_MODELS
else: else:
models = await get_all_base_models(request, user=user) models = await get_all_base_models(request, user=user)
request.app.state.BASE_MODELS = models
# If there are no models, return an empty list # If there are no models, return an empty list
if len(models) == 0: if len(models) == 0:
@ -265,16 +268,12 @@ async def get_all_models(request, refresh: bool = False, user: UserModel = None)
for model in models: for model in models:
action_ids = [ action_ids = [
action_id action_id
for action_id in list( for action_id in list(set(model.pop("action_ids", []) + global_action_ids))
set(model.pop("action_ids", []) + global_action_ids)
)
if action_id in enabled_action_ids if action_id in enabled_action_ids
] ]
filter_ids = [ filter_ids = [
filter_id filter_id
for filter_id in list( for filter_id in list(set(model.pop("filter_ids", []) + global_filter_ids))
set(model.pop("filter_ids", []) + global_filter_ids)
)
if filter_id in enabled_filter_ids if filter_id in enabled_filter_ids
] ]

View file

@ -386,12 +386,12 @@
<div class="my-2"> <div class="my-2">
<div class="flex justify-between items-center text-sm"> <div class="flex justify-between items-center text-sm">
<div class=" text-xs font-medium">{$i18n.t('Cache Model List')}</div> <div class=" text-xs font-medium">{$i18n.t('Cache Base Model List')}</div>
<div class="flex items-center"> <div class="flex items-center">
<div class=""> <div class="">
<Switch <Switch
bind:state={connectionsConfig.ENABLE_MODEL_LIST_CACHE} bind:state={connectionsConfig.ENABLE_BASE_MODELS_CACHE}
on:change={async () => { on:change={async () => {
updateConnectionsHandler(); updateConnectionsHandler();
}} }}
@ -402,7 +402,7 @@
<div class="mt-1 text-xs text-gray-400 dark:text-gray-500"> <div class="mt-1 text-xs text-gray-400 dark:text-gray-500">
{$i18n.t( {$i18n.t(
'Model List Cache speeds up access by fetching models only at startup or on settings save—faster, but may not show recent model changes.' 'Base Model List Cache speeds up access by fetching base models only at startup or on settings save—faster, but may not show recent base model changes.'
)} )}
</div> </div>
</div> </div>