diff --git a/backend/open_webui/config.py b/backend/open_webui/config.py index 7d3caf0d9f..8b71335d46 100644 --- a/backend/open_webui/config.py +++ b/backend/open_webui/config.py @@ -3166,6 +3166,7 @@ DEFAULT_CONFIG = { 'image_generation.size': IMAGE_SIZE, 'image_generation.steps': IMAGE_STEPS, 'image_generation.prompt.enable': ENABLE_IMAGE_PROMPT_GENERATION, + 'image_generation.default_model': '', 'image_generation.automatic1111.base_url': AUTOMATIC1111_BASE_URL, 'image_generation.automatic1111.api_auth': AUTOMATIC1111_API_AUTH, 'image_generation.automatic1111.api_params': AUTOMATIC1111_PARAMS, @@ -3183,6 +3184,7 @@ DEFAULT_CONFIG = { 'images.edit.enable': ENABLE_IMAGE_EDIT, 'images.edit.engine': IMAGE_EDIT_ENGINE, 'images.edit.model': IMAGE_EDIT_MODEL, + 'images.edit.default_model': '', 'images.edit.size': IMAGE_EDIT_SIZE, 'images.edit.openai.api_base_url': IMAGES_EDIT_OPENAI_API_BASE_URL, 'images.edit.openai.api_version': IMAGES_EDIT_OPENAI_API_VERSION, diff --git a/backend/open_webui/main.py b/backend/open_webui/main.py index fd7094b099..aa1293fba5 100644 --- a/backend/open_webui/main.py +++ b/backend/open_webui/main.py @@ -249,6 +249,7 @@ from open_webui.utils.chat_variables import ( ) from open_webui.utils.embeddings import generate_embeddings from open_webui.utils.headers import get_headers_and_cookies +from open_webui.utils.images.connections import migrate_legacy_image_config from open_webui.utils.json_codec import JSONCodec from open_webui.utils.json_response import apply_orjson_http_json from open_webui.utils.logger import start_logger @@ -395,6 +396,7 @@ async def lifespan(app: FastAPI): await validate_mfa_configuration() await initialize_runtime_config(app) await migrate_legacy_webhook_config() + await migrate_legacy_image_config() request = Request( { 'type': 'http', diff --git a/backend/open_webui/routers/images.py b/backend/open_webui/routers/images.py index 3d90c6cf49..e64aa71a67 100644 --- a/backend/open_webui/routers/images.py +++ b/backend/open_webui/routers/images.py @@ -42,10 +42,20 @@ from open_webui.utils.images.comfyui import ( comfyui_edit_image, comfyui_upload_image, ) +from open_webui.utils.images.connections import ( + DEFAULT_GEMINI_IMAGE_MODEL, + DEFAULT_OPENAI_IMAGE_MODEL, + IMAGE_CONNECTIONS_CONFIG_KEY, + IMAGE_EDIT_DEFAULT_MODEL_CONFIG_KEY, + IMAGE_GENERATION_DEFAULT_MODEL_CONFIG_KEY, + get_image_models, + normalize_image_connection, + resolve_image_model, +) from open_webui.utils.json_codec import JSONCodec from open_webui.utils.session_pool import get_session from PIL import Image, ImageOps -from pydantic import BaseModel +from pydantic import BaseModel, ConfigDict from sqlalchemy.ext.asyncio import AsyncSession log = logging.getLogger(__name__) @@ -57,8 +67,6 @@ IMAGE_CACHE_DIR.mkdir(parents=True, exist_ok=True) router = APIRouter() -DEFAULT_OPENAI_IMAGE_MODEL = 'gpt-image-2' - IMAGE_FILE_EXTENSIONS = { 'image/jpeg': '.jpg', 'image/jpg': '.jpg', @@ -70,37 +78,10 @@ IMAGE_FILE_EXTENSIONS = { IMAGE_CONFIG_KEYS = { 'ENABLE_IMAGE_GENERATION': 'image_generation.enable', 'ENABLE_IMAGE_PROMPT_GENERATION': 'image_generation.prompt.enable', - 'IMAGE_GENERATION_ENGINE': 'image_generation.engine', - 'IMAGE_GENERATION_MODEL': 'image_generation.model', - 'IMAGE_SIZE': 'image_generation.size', - 'IMAGE_STEPS': 'image_generation.steps', - 'IMAGES_OPENAI_API_BASE_URL': 'image_generation.openai.api_base_url', - 'IMAGES_OPENAI_API_KEY': 'image_generation.openai.api_key', - 'IMAGES_OPENAI_API_VERSION': 'image_generation.openai.api_version', - 'IMAGES_OPENAI_API_PARAMS': 'image_generation.openai.params', - 'AUTOMATIC1111_BASE_URL': 'image_generation.automatic1111.base_url', - 'AUTOMATIC1111_API_AUTH': 'image_generation.automatic1111.api_auth', - 'AUTOMATIC1111_PARAMS': 'image_generation.automatic1111.api_params', - 'COMFYUI_BASE_URL': 'image_generation.comfyui.base_url', - 'COMFYUI_API_KEY': 'image_generation.comfyui.api_key', - 'COMFYUI_WORKFLOW': 'image_generation.comfyui.workflow', - 'COMFYUI_WORKFLOW_NODES': 'image_generation.comfyui.nodes', - 'IMAGES_GEMINI_API_BASE_URL': 'image_generation.gemini.api_base_url', - 'IMAGES_GEMINI_API_KEY': 'image_generation.gemini.api_key', - 'IMAGES_GEMINI_ENDPOINT_METHOD': 'image_generation.gemini.endpoint_method', 'ENABLE_IMAGE_EDIT': 'images.edit.enable', - 'IMAGE_EDIT_ENGINE': 'images.edit.engine', - 'IMAGE_EDIT_MODEL': 'images.edit.model', - 'IMAGE_EDIT_SIZE': 'images.edit.size', - 'IMAGES_EDIT_OPENAI_API_BASE_URL': 'images.edit.openai.api_base_url', - 'IMAGES_EDIT_OPENAI_API_KEY': 'images.edit.openai.api_key', - 'IMAGES_EDIT_OPENAI_API_VERSION': 'images.edit.openai.api_version', - 'IMAGES_EDIT_GEMINI_API_BASE_URL': 'images.edit.gemini.api_base_url', - 'IMAGES_EDIT_GEMINI_API_KEY': 'images.edit.gemini.api_key', - 'IMAGES_EDIT_COMFYUI_BASE_URL': 'images.edit.comfyui.base_url', - 'IMAGES_EDIT_COMFYUI_API_KEY': 'images.edit.comfyui.api_key', - 'IMAGES_EDIT_COMFYUI_WORKFLOW': 'images.edit.comfyui.workflow', - 'IMAGES_EDIT_COMFYUI_WORKFLOW_NODES': 'images.edit.comfyui.nodes', + 'IMAGE_GENERATION_DEFAULT_MODEL': IMAGE_GENERATION_DEFAULT_MODEL_CONFIG_KEY, + 'IMAGE_EDIT_DEFAULT_MODEL': IMAGE_EDIT_DEFAULT_MODEL_CONFIG_KEY, + 'IMAGE_CONNECTIONS': IMAGE_CONNECTIONS_CONFIG_KEY, 'USER_PERMISSIONS': 'user.permissions', } @@ -187,144 +168,80 @@ def get_image_file_item(base64_string, param_name='image'): ) -async def set_image_model(request: Request, model: str): - log.info('Setting image model to %s', model) - await Config.upsert({'image_generation.model': model}) - image_config = await get_image_config() - if image_config.IMAGE_GENERATION_ENGINE in ['', 'automatic1111']: - api_auth = get_automatic1111_api_auth(image_config) - - try: - session = await get_session() - async with session.get( - url=f'{image_config.AUTOMATIC1111_BASE_URL}/sdapi/v1/options', - headers={'authorization': api_auth}, - ssl=AIOHTTP_CLIENT_SESSION_SSL, - ) as r: - options = await r.json() - if model != options['sd_model_checkpoint']: - options['sd_model_checkpoint'] = model - async with session.post( - url=f'{image_config.AUTOMATIC1111_BASE_URL}/sdapi/v1/options', - json=options, - headers={'authorization': api_auth}, - ssl=AIOHTTP_CLIENT_SESSION_SSL, - ) as r: - r.raise_for_status() - except Exception as e: - log.debug('%s', e) - - return image_config.IMAGE_GENERATION_MODEL +def get_automatic1111_api_auth(api_auth: str | None) -> str: + if not api_auth: + return '' + return f'Basic {base64.b64encode(api_auth.encode("utf-8")).decode("utf-8")}' -async def get_image_model(request): - image_config = await get_image_config() - if image_config.IMAGE_GENERATION_ENGINE == 'openai': - return ( - image_config.IMAGE_GENERATION_MODEL if image_config.IMAGE_GENERATION_MODEL else DEFAULT_OPENAI_IMAGE_MODEL - ) - elif image_config.IMAGE_GENERATION_ENGINE == 'gemini': - return image_config.IMAGE_GENERATION_MODEL if image_config.IMAGE_GENERATION_MODEL else 'imagen-3.0-generate-002' - elif image_config.IMAGE_GENERATION_ENGINE == 'comfyui': - return image_config.IMAGE_GENERATION_MODEL if image_config.IMAGE_GENERATION_MODEL else '' - elif image_config.IMAGE_GENERATION_ENGINE == 'automatic1111' or image_config.IMAGE_GENERATION_ENGINE == '': - try: - session = await get_session() - async with session.get( - url=f'{image_config.AUTOMATIC1111_BASE_URL}/sdapi/v1/options', - headers={'authorization': get_automatic1111_api_auth(image_config)}, - ssl=AIOHTTP_CLIENT_SESSION_SSL, - ) as r: - options = await r.json() - return options['sd_model_checkpoint'] - except Exception as e: - log.exception(f'Failed to get default model from automatic1111: {e}') - raise HTTPException( - status_code=400, - detail=ERROR_MESSAGES.DEFAULT(e, 'Failed to connect to the image generation engine'), - ) +async def set_automatic1111_model(connection: dict, model: str) -> None: + """AUTOMATIC1111 holds one checkpoint instance-wide; switch it when the request needs another.""" + if not model: + return + + headers = {'authorization': get_automatic1111_api_auth(connection.get('key'))} + session = await get_session() + async with session.get( + url=f'{connection["url"]}/sdapi/v1/options', + headers=headers, + ssl=AIOHTTP_CLIENT_SESSION_SSL, + ) as r: + r.raise_for_status() + options = await r.json() + + if model != options.get('sd_model_checkpoint'): + log.info('Switching AUTOMATIC1111 checkpoint to %s', model) + async with session.post( + url=f'{connection["url"]}/sdapi/v1/options', + json={**options, 'sd_model_checkpoint': model}, + headers=headers, + ssl=AIOHTTP_CLIENT_SESSION_SSL, + ) as r: + r.raise_for_status() + + +class ImageConnection(BaseModel): + id: str | None = None + name: str = '' + type: str = 'openai' + url: str = '' + key: str = '' + config: dict = {} + + model_config = ConfigDict(extra='allow') class ImagesConfig(BaseModel): ENABLE_IMAGE_GENERATION: bool ENABLE_IMAGE_PROMPT_GENERATION: bool - - IMAGE_GENERATION_ENGINE: str - IMAGE_GENERATION_MODEL: str - IMAGE_SIZE: str | None - IMAGE_STEPS: int | None - - IMAGES_OPENAI_API_BASE_URL: str - IMAGES_OPENAI_API_KEY: str - IMAGES_OPENAI_API_VERSION: str - IMAGES_OPENAI_API_PARAMS: dict | str | None - - AUTOMATIC1111_BASE_URL: str - AUTOMATIC1111_API_AUTH: dict | str | None - AUTOMATIC1111_PARAMS: dict | str | None - - COMFYUI_BASE_URL: str - COMFYUI_API_KEY: str - COMFYUI_WORKFLOW: str - COMFYUI_WORKFLOW_NODES: list[dict] - - IMAGES_GEMINI_API_BASE_URL: str - IMAGES_GEMINI_API_KEY: str - IMAGES_GEMINI_ENDPOINT_METHOD: str - ENABLE_IMAGE_EDIT: bool - IMAGE_EDIT_ENGINE: str - IMAGE_EDIT_MODEL: str - IMAGE_EDIT_SIZE: str | None - - IMAGES_EDIT_OPENAI_API_BASE_URL: str - IMAGES_EDIT_OPENAI_API_KEY: str - IMAGES_EDIT_OPENAI_API_VERSION: str - IMAGES_EDIT_GEMINI_API_BASE_URL: str - IMAGES_EDIT_GEMINI_API_KEY: str - IMAGES_EDIT_COMFYUI_BASE_URL: str - IMAGES_EDIT_COMFYUI_API_KEY: str - IMAGES_EDIT_COMFYUI_WORKFLOW: str - IMAGES_EDIT_COMFYUI_WORKFLOW_NODES: list[dict] + IMAGE_GENERATION_DEFAULT_MODEL: str = '' + IMAGE_EDIT_DEFAULT_MODEL: str = '' + IMAGE_CONNECTIONS: list[ImageConnection] = [] @router.get('/config', response_model=ImagesConfig) async def get_config(request: Request, user=Depends(get_admin_user)): - return await get_config_values(IMAGE_CONFIG_KEYS) + values = await get_config_values(IMAGE_CONFIG_KEYS) + return {**values, 'IMAGE_CONNECTIONS': values.get('IMAGE_CONNECTIONS') or []} @router.post('/config/update') async def update_config(request: Request, form_data: ImagesConfig, user=Depends(get_admin_user)): - # Check the model requests will actually use, so an empty OpenAI model field (gpt-image default) can use auto. - image_model = form_data.IMAGE_GENERATION_MODEL or ( - DEFAULT_OPENAI_IMAGE_MODEL if form_data.IMAGE_GENERATION_ENGINE == 'openai' else '' - ) - if form_data.IMAGE_SIZE == 'auto' and not re.match(IMAGE_AUTO_SIZE_MODELS_REGEX_PATTERN, image_model): - raise HTTPException( - status_code=400, - detail=ERROR_MESSAGES.INCORRECT_FORMAT( - f' (auto is only allowed with models matching {IMAGE_AUTO_SIZE_MODELS_REGEX_PATTERN}).' - ), - ) + connections = [] + try: + for connection in form_data.IMAGE_CONNECTIONS: + connections.append(normalize_image_connection(connection.model_dump())) + except (ValueError, TypeError) as e: + raise HTTPException(status_code=400, detail=ERROR_MESSAGES.DEFAULT(e)) - pattern = r'^\d+x\d+$' - if not (form_data.IMAGE_SIZE == 'auto' or form_data.IMAGE_SIZE == '' or re.match(pattern, form_data.IMAGE_SIZE)): - raise HTTPException( - status_code=400, - detail=ERROR_MESSAGES.INCORRECT_FORMAT(' (e.g., 512x512).'), - ) + if len({connection['id'] for connection in connections}) != len(connections): + raise HTTPException(status_code=400, detail=ERROR_MESSAGES.DEFAULT('Image connection IDs must be unique')) - if form_data.IMAGE_STEPS < 0: - raise HTTPException( - status_code=400, - detail=ERROR_MESSAGES.INCORRECT_FORMAT(' (e.g., 50).'), - ) - - updates = config_updates(form_data.model_dump(), IMAGE_CONFIG_KEYS) - updates['image_generation.comfyui.base_url'] = form_data.COMFYUI_BASE_URL.strip('/') - updates['images.edit.comfyui.base_url'] = form_data.IMAGES_EDIT_COMFYUI_BASE_URL.strip('/') + updates = config_updates(form_data.model_dump(exclude={'IMAGE_CONNECTIONS'}), IMAGE_CONFIG_KEYS) + updates[IMAGE_CONNECTIONS_CONFIG_KEY] = connections await Config.upsert(updates) - await set_image_model(request, form_data.IMAGE_GENERATION_MODEL) + values = await get_config_values(IMAGE_CONFIG_KEYS) await publish_event( request, @@ -334,133 +251,162 @@ async def update_config(request: Request, form_data: ImagesConfig, user=Depends( data={ 'image_generation_enabled': values.get('ENABLE_IMAGE_GENERATION'), 'image_edit_enabled': values.get('ENABLE_IMAGE_EDIT'), - 'image_generation_engine': values.get('IMAGE_GENERATION_ENGINE'), - 'image_edit_engine': values.get('IMAGE_EDIT_ENGINE'), + 'image_connections': [ + {'id': connection['id'], 'name': connection['name'], 'type': connection['type']} + for connection in connections + ], }, ) - return values + return {**values, 'IMAGE_CONNECTIONS': connections} -def get_automatic1111_api_auth(image_config): - if image_config.AUTOMATIC1111_API_AUTH is None: - return '' - else: - auth1111_byte_string = image_config.AUTOMATIC1111_API_AUTH.encode('utf-8') - auth1111_base64_encoded_bytes = base64.b64encode(auth1111_byte_string) - auth1111_base64_encoded_string = auth1111_base64_encoded_bytes.decode('utf-8') - return f'Basic {auth1111_base64_encoded_string}' +async def get_openai_connection_models(connection: dict) -> list[dict]: + headers = {'Authorization': f'Bearer {connection.get("key", "")}'} if connection.get('key') else {} + session = await get_session() + async with session.get(url=f'{connection["url"]}/models', headers=headers, ssl=AIOHTTP_CLIENT_SESSION_SSL) as r: + await raise_for_provider_status(r) + res = await r.json(content_type=None) + + models = [ + {'id': model['id'], 'name': model['id']} + for model in (res.get('data') or []) + if isinstance(model, dict) and model.get('id') + ] + + # LiteLLM labels each model with its mode, which tells image models apart from chat models. + try: + async with session.get( + url=f'{connection["url"]}/model/info', headers=headers, ssl=AIOHTTP_CLIENT_SESSION_SSL + ) as r: + info = await r.json(content_type=None) if r.ok else {} + modes = { + entry.get('model_name'): (entry.get('model_info') or {}).get('mode') + for entry in (info.get('data') or []) + if isinstance(entry, dict) + } + if any(modes.values()): + for model in models: + model['image'] = modes.get(model['id']) in ('image_generation', 'image_edit') + except Exception as e: + log.debug('No model info at %s: %s', connection['url'], e) + + return models -class ConnectionVerificationForm(BaseModel): - engine: str - url: str - key: str | None = None +async def get_gemini_connection_models(connection: dict) -> list[dict]: + session = await get_session() + async with session.get( + url=f'{connection["url"]}/models', + headers={'x-goog-api-key': connection.get('key', '')}, + ssl=AIOHTTP_CLIENT_SESSION_SSL, + ) as r: + await raise_for_provider_status(r) + res = await r.json(content_type=None) + + models = [] + for model in res.get('models') or []: + model_id = str(model.get('name') or '').removeprefix('models/') + if not model_id: + continue + methods = model.get('supportedGenerationMethods') or [] + models.append( + { + 'id': model_id, + 'name': model.get('displayName') or model_id, + 'image': 'predict' in methods or 'image' in model_id, + } + ) + return models or [{'id': DEFAULT_GEMINI_IMAGE_MODEL, 'name': DEFAULT_GEMINI_IMAGE_MODEL, 'image': True}] -@router.post('/verify') -async def verify_connection(form_data: ConnectionVerificationForm, user=Depends(get_admin_user)): - url = form_data.url.rstrip('/') - headers = {} - if form_data.engine == 'automatic1111': - url = f'{url}/sdapi/v1/options' - if form_data.key is not None: - headers['Authorization'] = f'Basic {base64.b64encode(form_data.key.encode("utf-8")).decode("utf-8")}' - elif form_data.engine == 'comfyui': - url = f'{url}/object_info' - if form_data.key: - headers['Authorization'] = f'Bearer {form_data.key}' - else: - raise HTTPException(status_code=400, detail='Unsupported image engine') +async def get_comfyui_connection_models(connection: dict) -> list[dict]: + config = connection.get('config') or {} + headers = {'Authorization': f'Bearer {connection["key"]}'} if connection.get('key') else {} + session = await get_session() + async with session.get( + url=f'{connection["url"]}/object_info', headers=headers, ssl=AIOHTTP_CLIENT_SESSION_SSL + ) as r: + await raise_for_provider_status(r) + info = await r.json(content_type=None) + + def options(class_type: str, key: str | None = None) -> list[dict]: + required = ((info.get(class_type) or {}).get('input') or {}).get('required') or {} + key = key or next((name for name in required if '_name' in name), None) + values = (required.get(key) or [[]])[0] if key else [] + return [{'id': value, 'name': value} for value in values if isinstance(value, str)] + + workflow_json = config.get('workflow') or config.get('edit_workflow') + nodes = config.get('nodes') if config.get('workflow') else config.get('edit_nodes') + model_node_id = next( + (node['node_ids'][0] for node in nodes or [] if node.get('type') == 'model' and node.get('node_ids')), + None, + ) + if workflow_json and model_node_id: + workflow = JSONCodec.loads(workflow_json) + class_type = (workflow.get(model_node_id) or {}).get('class_type') + if class_type: + return options(class_type) + return options('CheckpointLoaderSimple', 'ckpt_name') + + +async def get_automatic1111_connection_models(connection: dict) -> list[dict]: + session = await get_session() + async with session.get( + url=f'{connection["url"]}/sdapi/v1/sd-models', + headers={'authorization': get_automatic1111_api_auth(connection.get('key'))}, + ssl=AIOHTTP_CLIENT_SESSION_SSL, + ) as r: + await raise_for_provider_status(r) + models = await r.json(content_type=None) + return [{'id': model['title'], 'name': model['model_name']} for model in models] + + +@router.post('/connections/verify') +async def verify_image_connection(form_data: ImageConnection, user=Depends(get_admin_user)): + """Reach a connection before saving it and list the models it serves.""" + try: + connection = normalize_image_connection(form_data.model_dump()) + except (ValueError, TypeError) as e: + raise HTTPException(status_code=400, detail=ERROR_MESSAGES.DEFAULT(e)) + + if not connection['url']: + raise HTTPException(status_code=400, detail=ERROR_MESSAGES.INVALID_URL) try: - session = await get_session() - async with session.get(url=url, headers=headers, ssl=AIOHTTP_CLIENT_SESSION_SSL) as r: - r.raise_for_status() - return True - except Exception: - raise HTTPException(status_code=400, detail=ERROR_MESSAGES.INVALID_URL) + if connection['type'] == 'openai': + return await get_openai_connection_models(connection) + if connection['type'] == 'gemini': + return await get_gemini_connection_models(connection) + if connection['type'] == 'comfyui': + return await get_comfyui_connection_models(connection) + return await get_automatic1111_connection_models(connection) + except Exception as e: + log.error( + f'Failed to verify image connection {connection["url"]}: {str(e) or type(e).__name__}', + exc_info=not isinstance(e, (aiohttp.ClientConnectionError, aiohttp.ClientResponseError, TimeoutError)), + ) + error = e.message if isinstance(e, aiohttp.ClientResponseError) else e + raise HTTPException( + status_code=400, + detail=ERROR_MESSAGES.DEFAULT(error, 'Failed to connect to the image generation engine'), + ) @router.get('/models') async def get_models(request: Request, user=Depends(get_verified_user)): + """Image models this user can pick from, with what each can do.""" image_config = await get_image_config() - try: - if image_config.IMAGE_GENERATION_ENGINE == 'openai': - return [ - {'id': 'gpt-image-1', 'name': 'GPT-IMAGE 1'}, - {'id': 'gpt-image-1.5', 'name': 'GPT-IMAGE 1.5'}, - {'id': 'gpt-image-2', 'name': 'GPT-IMAGE 2'}, - ] - elif image_config.IMAGE_GENERATION_ENGINE == 'gemini': - return [ - {'id': 'imagen-3.0-generate-002', 'name': 'imagen-3.0 generate-002'}, - ] - elif image_config.IMAGE_GENERATION_ENGINE == 'comfyui': - # TODO - get models from comfyui - headers = {'Authorization': f'Bearer {image_config.COMFYUI_API_KEY}'} - session = await get_session() - async with session.get( - url=f'{image_config.COMFYUI_BASE_URL}/object_info', - headers=headers, - ssl=AIOHTTP_CLIENT_SESSION_SSL, - ) as r: - info = await r.json() + if not (image_config.ENABLE_IMAGE_GENERATION or image_config.ENABLE_IMAGE_EDIT): + return [] - workflow = JSONCodec.loads(image_config.COMFYUI_WORKFLOW) - model_node_id = None + if user.role != 'admin' and not await has_permission( + user.id, 'features.image_generation', image_config.USER_PERMISSIONS + ): + return [] - for node in image_config.COMFYUI_WORKFLOW_NODES: - if node['type'] == 'model': - if node['node_ids']: - model_node_id = node['node_ids'][0] - break - - if model_node_id: - model_list_key = None - - log.info(workflow[model_node_id]['class_type']) - for key in info[workflow[model_node_id]['class_type']]['input']['required']: - if '_name' in key: - model_list_key = key - break - - if model_list_key: - return list( - map( - lambda model: {'id': model, 'name': model}, - info[workflow[model_node_id]['class_type']]['input']['required'][model_list_key][0], - ) - ) - else: - return list( - map( - lambda model: {'id': model, 'name': model}, - info['CheckpointLoaderSimple']['input']['required']['ckpt_name'][0], - ) - ) - elif image_config.IMAGE_GENERATION_ENGINE == 'automatic1111' or image_config.IMAGE_GENERATION_ENGINE == '': - session = await get_session() - async with session.get( - url=f'{image_config.AUTOMATIC1111_BASE_URL}/sdapi/v1/sd-models', - headers={'authorization': get_automatic1111_api_auth(image_config)}, - ssl=AIOHTTP_CLIENT_SESSION_SSL, - ) as r: - models = await r.json() - return list( - map( - lambda model: {'id': model['title'], 'name': model['model_name']}, - models, - ) - ) - except Exception as e: - log.error( - f'Failed to list image generation models: {str(e) or type(e).__name__}', - exc_info=not isinstance(e, (aiohttp.ClientConnectionError, TimeoutError)), - ) - raise HTTPException( - status_code=400, - detail=ERROR_MESSAGES.DEFAULT(e, 'Failed to retrieve image generation models'), - ) + return [ + {key: value for key, value in model.items() if key != 'connection'} for model in await get_image_models(user) + ] class CreateImageForm(BaseModel): @@ -632,7 +578,7 @@ async def generate_images(request: Request, form_data: CreateImageForm, user=Dep detail=ERROR_MESSAGES.ACCESS_PROHIBITED, ) - result = await image_generations(request, form_data, user=user) + result = await image_generations(request, form_data, user=user, strict=True) await publish_event( request, EVENTS.IMAGE_GENERATED, @@ -654,15 +600,17 @@ async def image_generations( form_data: CreateImageForm, metadata: dict | None = None, user=None, + strict: bool = False, ): - image_config = await get_image_config() - # if IMAGE_SIZE = 'auto', default WidthxHeight to the 512x512 default - # This is only relevant when the user has set IMAGE_SIZE to 'auto' with an - # image model other than gpt-image-1, which is warned about on settings save + connection, model = await resolve_image_model(user, 'image_generation', form_data.model, strict=strict) + connection_config = connection.get('config') or {} + connection_size = connection_config.get('size') or '' + connection_steps = connection_config.get('steps') + # An 'auto' size has no width and height, so engines that need them fall back to 512x512. size = '512x512' - if image_config.IMAGE_SIZE and 'x' in image_config.IMAGE_SIZE: - size = image_config.IMAGE_SIZE + if 'x' in connection_size: + size = connection_size if form_data.size and 'x' in form_data.size: size = form_data.size @@ -671,20 +619,19 @@ async def image_generations( metadata = metadata or {} - model = await get_image_model(request) - try: - if image_config.IMAGE_GENERATION_ENGINE == 'openai': + if connection['type'] == 'openai': + model = model or DEFAULT_OPENAI_IMAGE_MODEL headers = { - 'Authorization': f'Bearer {image_config.IMAGES_OPENAI_API_KEY}', + **({'Authorization': f'Bearer {connection["key"]}'} if connection['key'] else {}), 'Content-Type': 'application/json', } if ENABLE_FORWARD_USER_INFO_HEADERS: headers = include_user_info_headers(headers, user) - url = f'{image_config.IMAGES_OPENAI_API_BASE_URL}/images/generations' - openai_size = form_data.size or image_config.IMAGE_SIZE + url = f'{connection["url"]}/images/generations' + openai_size = form_data.size or connection_size # 512x512 is the stock IMAGE_SIZE, left over from DALL-E. GPT image models reject it, # so let them pick their own size unless the request asked for one. if ( @@ -693,8 +640,8 @@ async def image_generations( and re.match(IMAGE_AUTO_SIZE_MODELS_REGEX_PATTERN, model) ): openai_size = 'auto' - if image_config.IMAGES_OPENAI_API_VERSION: - url = f'{url}?api-version={image_config.IMAGES_OPENAI_API_VERSION}' + if connection_config.get('api_version'): + url = f'{url}?api-version={connection_config["api_version"]}' data = { 'model': model, @@ -709,7 +656,7 @@ async def image_generations( ) else {'response_format': 'b64_json'} ), - **({} if not image_config.IMAGES_OPENAI_API_PARAMS else image_config.IMAGES_OPENAI_API_PARAMS), + **(connection_config.get('params') or {}), } session = await get_session() @@ -729,7 +676,7 @@ async def image_generations( image_data, content_type = await get_image_data( image_url, {k: v for k, v in headers.items() if k != 'Content-Type'} - if _is_same_origin(image_url, image_config.IMAGES_OPENAI_API_BASE_URL) + if _is_same_origin(image_url, connection['url']) else None, ) else: @@ -739,18 +686,17 @@ async def image_generations( images.append(image_file) return images - elif image_config.IMAGE_GENERATION_ENGINE == 'gemini': + elif connection['type'] == 'gemini': + model = model or DEFAULT_GEMINI_IMAGE_MODEL headers = { 'Content-Type': 'application/json', - 'x-goog-api-key': image_config.IMAGES_GEMINI_API_KEY, + 'x-goog-api-key': connection['key'], } data = {} + endpoint_method = connection_config.get('endpoint_method') or '' - if ( - image_config.IMAGES_GEMINI_ENDPOINT_METHOD == '' - or image_config.IMAGES_GEMINI_ENDPOINT_METHOD == 'predict' - ): + if endpoint_method in ('', 'predict'): model = f'{model}:predict' data = { 'instances': {'prompt': form_data.prompt}, @@ -760,13 +706,13 @@ async def image_generations( }, } - elif image_config.IMAGES_GEMINI_ENDPOINT_METHOD == 'generateContent': + elif endpoint_method == 'generateContent': model = f'{model}:generateContent' data = {'contents': [{'parts': [{'text': form_data.prompt}]}]} session = await get_session() async with session.post( - url=f'{image_config.IMAGES_GEMINI_API_BASE_URL}/models/{model}', + url=f'{connection["url"]}/models/{model}', json=data, headers=headers, ssl=AIOHTTP_CLIENT_SESSION_SSL, @@ -797,7 +743,7 @@ async def image_generations( return images - elif image_config.IMAGE_GENERATION_ENGINE == 'comfyui': + elif connection['type'] == 'comfyui': data = { 'prompt': form_data.prompt, 'width': width, @@ -805,8 +751,8 @@ async def image_generations( 'n': form_data.n, } - if image_config.IMAGE_STEPS is not None or form_data.steps is not None: - data['steps'] = form_data.steps if form_data.steps is not None else image_config.IMAGE_STEPS + if connection_steps is not None or form_data.steps is not None: + data['steps'] = form_data.steps if form_data.steps is not None else connection_steps if form_data.negative_prompt is not None: data['negative_prompt'] = form_data.negative_prompt @@ -815,8 +761,8 @@ async def image_generations( **{ 'workflow': ComfyUIWorkflow( **{ - 'workflow': image_config.COMFYUI_WORKFLOW, - 'nodes': image_config.COMFYUI_WORKFLOW_NODES, + 'workflow': connection_config.get('workflow') or '', + 'nodes': connection_config.get('nodes') or [], } ), **data, @@ -826,8 +772,8 @@ async def image_generations( model, form_data, str(uuid.uuid4()), - image_config.COMFYUI_BASE_URL, - image_config.COMFYUI_API_KEY, + connection['url'], + connection['key'], ) log.debug('res: %s', res) @@ -835,13 +781,13 @@ async def image_generations( for image in res['data']: headers = None - if image_config.COMFYUI_API_KEY: - headers = {'Authorization': f'Bearer {image_config.COMFYUI_API_KEY}'} + if connection['key']: + headers = {'Authorization': f'Bearer {connection["key"]}'} image_data, content_type = await get_image_data( image['url'], headers, - trusted_base_url=image_config.COMFYUI_BASE_URL, + trusted_base_url=connection['url'], ) _, image_file = await upload_image( request, @@ -852,13 +798,10 @@ async def image_generations( ) images.append(image_file) return images - elif image_config.IMAGE_GENERATION_ENGINE == 'automatic1111' or image_config.IMAGE_GENERATION_ENGINE == '': - # Automatic1111 holds one checkpoint instance-wide, so set_image_model - # persists the global default and switches the shared backend. Only an - # admin may do that; a non-admin generates on the currently configured - # checkpoint. The model field is not a per-user selection on this backend. - if form_data.model and user.role == 'admin': - await set_image_model(request, form_data.model) + elif connection['type'] == 'automatic1111': + # The checkpoint is shared by everyone on this backend. Only models an admin listed on + # the connection reach here, so switching to one is what the admin allowed. + await set_automatic1111_model(connection, model) data = { 'prompt': form_data.prompt, @@ -867,20 +810,20 @@ async def image_generations( 'height': height, } - if image_config.IMAGE_STEPS is not None or form_data.steps is not None: - data['steps'] = form_data.steps if form_data.steps is not None else image_config.IMAGE_STEPS + if connection_steps is not None or form_data.steps is not None: + data['steps'] = form_data.steps if form_data.steps is not None else connection_steps if form_data.negative_prompt is not None: data['negative_prompt'] = form_data.negative_prompt - if image_config.AUTOMATIC1111_PARAMS: - data = {**data, **image_config.AUTOMATIC1111_PARAMS} + if connection_config.get('params'): + data = {**data, **connection_config['params']} session = await get_session() async with session.post( - url=f'{image_config.AUTOMATIC1111_BASE_URL}/sdapi/v1/txt2img', + url=f'{connection["url"]}/sdapi/v1/txt2img', json=data, - headers={'authorization': get_automatic1111_api_auth(image_config)}, + headers={'authorization': get_automatic1111_api_auth(connection['key'])}, ssl=AIOHTTP_CLIENT_SESSION_SSL, ) as r: res = await r.json(content_type=None) @@ -937,7 +880,7 @@ async def edit_images(request: Request, form_data: EditImageForm, user=Depends(g detail=ERROR_MESSAGES.ACCESS_PROHIBITED, ) - result = await image_edits(request, form_data, user=user) + result = await image_edits(request, form_data, user=user, strict=True) await publish_event( request, EVENTS.IMAGE_EDITED, @@ -959,20 +902,19 @@ async def image_edits( form_data: EditImageForm, metadata: dict | None = None, user=Depends(get_verified_user), + strict: bool = False, ): - image_config = await get_image_config() + connection, model = await resolve_image_model(user, 'image_edit', form_data.model, strict=strict) + connection_config = connection.get('config') or {} + connection_size = connection_config.get('edit_size') or '' size = None width, height = None, None metadata = metadata or {} - if (image_config.IMAGE_EDIT_SIZE and 'x' in image_config.IMAGE_EDIT_SIZE) or ( - form_data.size and 'x' in form_data.size - ): - size = form_data.size if form_data.size else image_config.IMAGE_EDIT_SIZE + if 'x' in connection_size or (form_data.size and 'x' in form_data.size): + size = form_data.size if form_data.size else connection_size width, height = tuple(map(int, size.split('x'))) - model = image_config.IMAGE_EDIT_MODEL if form_data.model is None else form_data.model - try: async def load_url_image(data): @@ -1040,10 +982,9 @@ async def image_edits( ) try: - if image_config.IMAGE_EDIT_ENGINE == 'openai': - headers = { - 'Authorization': f'Bearer {image_config.IMAGES_EDIT_OPENAI_API_KEY}', - } + if connection['type'] == 'openai': + model = model or DEFAULT_OPENAI_IMAGE_MODEL + headers = {'Authorization': f'Bearer {connection["key"]}'} if connection['key'] else {} if ENABLE_FORWARD_USER_INFO_HEADERS: headers = include_user_info_headers(headers, user) @@ -1077,8 +1018,8 @@ async def image_edits( files.append(get_image_file_item(img, 'image[]')) url_search_params = '' - if image_config.IMAGES_EDIT_OPENAI_API_VERSION: - url_search_params += f'?api-version={image_config.IMAGES_EDIT_OPENAI_API_VERSION}' + if connection_config.get('api_version'): + url_search_params += f'?api-version={connection_config["api_version"]}' # Build multipart form data for aiohttp form = aiohttp.FormData() @@ -1097,7 +1038,7 @@ async def image_edits( session = await get_session() async with session.post( - url=f'{image_config.IMAGES_EDIT_OPENAI_API_BASE_URL}/images/edits{url_search_params}', + url=f'{connection["url"]}/images/edits{url_search_params}', headers=headers, data=form, ssl=AIOHTTP_CLIENT_SESSION_SSL, @@ -1111,7 +1052,7 @@ async def image_edits( image_data, content_type = await get_image_data( image_url, {k: v for k, v in headers.items() if k != 'Content-Type'} - if _is_same_origin(image_url, image_config.IMAGES_EDIT_OPENAI_API_BASE_URL) + if _is_same_origin(image_url, connection['url']) else None, ) else: @@ -1121,10 +1062,10 @@ async def image_edits( images.append(image_file) return images - elif image_config.IMAGE_EDIT_ENGINE == 'gemini': + elif connection['type'] == 'gemini': headers = { 'Content-Type': 'application/json', - 'x-goog-api-key': image_config.IMAGES_EDIT_GEMINI_API_KEY, + 'x-goog-api-key': connection['key'], } model = f'{model}:generateContent' @@ -1154,7 +1095,7 @@ async def image_edits( session = await get_session() async with session.post( - url=f'{image_config.IMAGES_EDIT_GEMINI_API_BASE_URL}/models/{model}', + url=f'{connection["url"]}/models/{model}', json=data, headers=headers, ssl=AIOHTTP_CLIENT_SESSION_SSL, @@ -1178,7 +1119,7 @@ async def image_edits( return images - elif image_config.IMAGE_EDIT_ENGINE == 'comfyui': + elif connection['type'] == 'comfyui': try: files = [] if isinstance(form_data.image, str): @@ -1192,8 +1133,8 @@ async def image_edits( for file_item in files: res = await comfyui_upload_image( file_item, - image_config.IMAGES_EDIT_COMFYUI_BASE_URL, - image_config.IMAGES_EDIT_COMFYUI_API_KEY, + connection['url'], + connection['key'], ) comfyui_images.append(res.get('name', file_item[1][0])) except Exception as e: @@ -1212,8 +1153,8 @@ async def image_edits( **{ 'workflow': ComfyUIWorkflow( **{ - 'workflow': image_config.IMAGES_EDIT_COMFYUI_WORKFLOW, - 'nodes': image_config.IMAGES_EDIT_COMFYUI_WORKFLOW_NODES, + 'workflow': connection_config.get('edit_workflow') or '', + 'nodes': connection_config.get('edit_nodes') or [], } ), **data, @@ -1223,8 +1164,8 @@ async def image_edits( model, form_data, str(uuid.uuid4()), - image_config.IMAGES_EDIT_COMFYUI_BASE_URL, - image_config.IMAGES_EDIT_COMFYUI_API_KEY, + connection['url'], + connection['key'], ) log.debug('res: %s', res) @@ -1243,13 +1184,13 @@ async def image_edits( for image_url in image_urls: headers = None - if image_config.IMAGES_EDIT_COMFYUI_API_KEY: - headers = {'Authorization': f'Bearer {image_config.IMAGES_EDIT_COMFYUI_API_KEY}'} + if connection['key']: + headers = {'Authorization': f'Bearer {connection["key"]}'} image_data, content_type = await get_image_data( image_url, headers, - trusted_base_url=image_config.IMAGES_EDIT_COMFYUI_BASE_URL, + trusted_base_url=connection['url'], ) _, image_file = await upload_image( request, diff --git a/backend/open_webui/tools/builtin.py b/backend/open_webui/tools/builtin.py index ec79e105b9..bfd1aa071a 100644 --- a/backend/open_webui/tools/builtin.py +++ b/backend/open_webui/tools/builtin.py @@ -37,6 +37,7 @@ from open_webui.routers.images import ( image_edits, image_generations, ) +from open_webui.utils.images.connections import get_requested_image_model from open_webui.routers.memories import ( AddMemoryForm, ListMemoryPathsForm, @@ -391,7 +392,7 @@ async def generate_image( images = await image_generations( request=__request__, - form_data=CreateImageForm(prompt=prompt, size=size), + form_data=CreateImageForm(prompt=prompt, size=size, model=get_requested_image_model(__metadata__)), metadata=( {'channel_id': __chat_id__.removeprefix('channel:'), 'message_id': __message_id__} if isinstance(__chat_id__, str) and __chat_id__.startswith('channel:') @@ -482,7 +483,9 @@ async def edit_image( resolved_images.append(url) images = await image_edits( request=__request__, - form_data=EditImageForm(prompt=prompt, image=resolved_images, size=size), + form_data=EditImageForm( + prompt=prompt, image=resolved_images, size=size, model=get_requested_image_model(__metadata__) + ), metadata=( {'channel_id': __chat_id__.removeprefix('channel:'), 'message_id': __message_id__} if isinstance(__chat_id__, str) and __chat_id__.startswith('channel:') diff --git a/backend/open_webui/utils/images/connections.py b/backend/open_webui/utils/images/connections.py new file mode 100644 index 0000000000..78e2697d12 --- /dev/null +++ b/backend/open_webui/utils/images/connections.py @@ -0,0 +1,466 @@ +"""Image connections: the configured backends that built-in image generation and editing run on. + +A connection is one backend (an OpenAI-compatible endpoint such as LiteLLM, Gemini, ComfyUI or +AUTOMATIC1111) with the image models it exposes. Users pick a model by its reference, +``{connection_id}:{model_id}``, and only see models their access grants allow. +""" + +from __future__ import annotations + +import logging +import uuid +from typing import Any, Literal + +from fastapi import HTTPException +from open_webui.constants import ERROR_MESSAGES +from open_webui.models.config import Config +from open_webui.models.groups import Groups +from open_webui.models.users import UserModel +from open_webui.utils.access_control import has_connection_access +from open_webui.utils.json_codec import JSONCodec + +log = logging.getLogger(__name__) + +IMAGE_CONNECTIONS_CONFIG_KEY = 'images.connections' +IMAGE_GENERATION_DEFAULT_MODEL_CONFIG_KEY = 'image_generation.default_model' +IMAGE_EDIT_DEFAULT_MODEL_CONFIG_KEY = 'images.edit.default_model' + +IMAGE_CONNECTION_TYPES = ('openai', 'gemini', 'comfyui', 'automatic1111') + +DEFAULT_OPENAI_IMAGE_MODEL = 'gpt-image-2' +DEFAULT_GEMINI_IMAGE_MODEL = 'imagen-3.0-generate-002' + +ImageCapability = Literal['image_generation', 'image_edit'] + +IMAGE_CAPABILITIES: tuple[ImageCapability, ...] = ('image_generation', 'image_edit') + +PUBLIC_ACCESS_GRANTS = [{'principal_type': 'user', 'principal_id': '*', 'permission': 'read'}] + + +def get_image_model_ref(connection_id: str, model_id: str) -> str: + return f'{connection_id}:{model_id}' + + +def parse_image_model_ref(ref: str) -> tuple[str, str] | None: + """Split a ``{connection_id}:{model_id}`` reference. Model ids may contain colons; connection ids never do.""" + if not ref or ':' not in ref: + return None + connection_id, model_id = ref.split(':', 1) + return connection_id, model_id + + +def _json_object(value: Any, label: str) -> dict: + if value in (None, ''): + return {} + if isinstance(value, str): + try: + value = JSONCodec.loads(value) + except JSONCodec.JSONDecodeError: + raise ValueError(f'Invalid JSON for {label}') + if not isinstance(value, dict): + raise ValueError(f'Invalid JSON for {label}') + return value + + +def _workflow(value: Any, label: str) -> str: + if value in (None, ''): + return '' + if isinstance(value, dict): + return JSONCodec.dumps(value) + _json_object(value, label) + return str(value) + + +def _size(value: Any) -> str: + size = str(value or '').strip() + if size and size != 'auto': + width, _, height = size.partition('x') + if not (width.isdigit() and height.isdigit()): + raise ValueError(f'Invalid image size: {size} (e.g. 512x512)') + return size + + +def _workflow_nodes(nodes: Any) -> list[dict]: + normalized = [] + for node in nodes or []: + if not isinstance(node, dict): + continue + node_ids = node.get('node_ids') or [] + if isinstance(node_ids, str): + node_ids = [node_id.strip() for node_id in node_ids.split(',') if node_id.strip()] + normalized.append( + { + 'type': str(node.get('type') or ''), + 'key': str(node.get('key') or ''), + 'node_ids': [str(node_id) for node_id in node_ids], + } + ) + return normalized + + +def normalize_image_connection(connection: dict[str, Any]) -> dict[str, Any]: + """Validate an image connection and fill in defaults. Raises ValueError on invalid input.""" + connection_type = str(connection.get('type') or 'openai') + if connection_type not in IMAGE_CONNECTION_TYPES: + raise ValueError(f'Unsupported image connection type: {connection_type}') + + connection_id = str(connection.get('id') or uuid.uuid4()) + if ':' in connection_id: + raise ValueError('Image connection IDs cannot contain ":"') + + config = connection.get('config') or {} + + model_ids = [] + for model_id in config.get('model_ids') or []: + model_id = str(model_id).strip() + if model_id and model_id not in model_ids: + model_ids.append(model_id) + + steps = config.get('steps') + if steps not in (None, ''): + steps = int(steps) + if steps < 0: + raise ValueError('Steps must be a positive number (e.g. 50)') + else: + steps = None + + return { + 'id': connection_id, + 'name': str(connection.get('name') or '').strip() or connection_type, + 'type': connection_type, + 'url': str(connection.get('url') or '').strip().rstrip('/'), + 'key': str(connection.get('key') or ''), + 'config': { + 'enable': bool(config.get('enable', True)), + 'capabilities': { + capability: bool((config.get('capabilities') or {}).get(capability, True)) + for capability in IMAGE_CAPABILITIES + }, + 'model_ids': model_ids, + 'access_grants': list(config.get('access_grants') or []), + 'size': _size(config.get('size')), + 'edit_size': _size(config.get('edit_size')), + 'steps': steps, + 'api_version': str(config.get('api_version') or ''), + 'params': _json_object(config.get('params'), 'additional parameters'), + 'endpoint_method': str(config.get('endpoint_method') or ''), + 'workflow': _workflow(config.get('workflow'), 'ComfyUI workflow'), + 'nodes': _workflow_nodes(config.get('nodes')), + 'edit_workflow': _workflow(config.get('edit_workflow'), 'ComfyUI edit workflow'), + 'edit_nodes': _workflow_nodes(config.get('edit_nodes')), + }, + } + + +def get_image_connection_capabilities(connection: dict) -> dict[str, bool]: + """What a connection's models can do: what its type supports (ComfyUI needs the matching + workflow, AUTOMATIC1111 cannot edit), narrowed by the capabilities the admin turned on.""" + config = connection.get('config') or {} + if connection.get('type') == 'automatic1111': + supported = {'image_generation': True, 'image_edit': False} + elif connection.get('type') == 'comfyui': + supported = {'image_generation': bool(config.get('workflow')), 'image_edit': bool(config.get('edit_workflow'))} + else: + supported = {'image_generation': True, 'image_edit': True} + + enabled = config.get('capabilities') or {} + return {capability: supported[capability] and bool(enabled.get(capability, True)) for capability in supported} + + +async def get_image_connections() -> list[dict]: + return await Config.get(IMAGE_CONNECTIONS_CONFIG_KEY) or [] + + +async def get_image_models(user: UserModel) -> list[dict]: + """Image models the user may use, across enabled connections. Each carries its connection for internal use. + + A connection without model IDs offers one model, its default: the engine's default model, + the model a ComfyUI workflow sets, or the checkpoint AUTOMATIC1111 has loaded. + """ + connections = await get_image_connections() + user_group_ids = {group.id for group in await Groups.get_groups_by_member_id(user.id, include_inherited=True)} + + models = [] + for connection in connections: + config = connection.get('config') or {} + if not config.get('enable', True): + continue + if not await has_connection_access(user, connection, user_group_ids): + continue + capabilities = get_image_connection_capabilities(connection) + for model_id in config.get('model_ids') or ['']: + models.append( + { + 'id': get_image_model_ref(connection['id'], model_id), + 'name': model_id or connection.get('name') or connection['id'], + 'model': model_id, + 'connection_id': connection['id'], + 'connection_name': connection.get('name') or connection['id'], + **capabilities, + 'connection': connection, + } + ) + return models + + +def get_requested_image_model(metadata: dict | None) -> str | None: + """The image model a chat asked for: the user's pick, else the chat model's default. + + The pick rides on the image feature itself, ``features.image_generation = {'model': ref}``; + plain ``true`` means no pick. + """ + metadata = metadata or {} + image_generation = (metadata.get('features') or {}).get('image_generation') + if isinstance(image_generation, dict) and isinstance(image_generation.get('model'), str): + if image_generation['model']: + return image_generation['model'] + model = metadata.get('model') or {} + return ((model.get('info') or {}).get('meta') or {}).get('imageModelId') or None + + +async def resolve_image_model( + user: UserModel, + capability: ImageCapability, + model: str | None = None, + *, + strict: bool = False, +) -> tuple[dict, str]: + """Pick the connection and model to run a request on. + + Order: the requested model (a reference or a bare model id), then the admin's default for + the capability, then the first model the user can use for it. A requested model the user + cannot use, or one that cannot do the capability (an edit sent to a generation-only model), + falls back to the default unless ``strict`` is set, as on the direct API routes. + """ + models = await get_image_models(user) + + def find(value: str | None) -> dict | None: + if not value: + return None + return next((m for m in models if m['id'] == value), None) or next( + (m for m in models if m['model'] == value), None + ) + + if model: + requested = find(model) + if requested and requested[capability]: + return requested['connection'], requested['model'] + if strict: + detail = ( + f'Image model "{model}" cannot be used for {capability.replace("_", " ")}' + if requested + else f'Image model "{model}" was not found' + ) + raise HTTPException(status_code=400, detail=detail) + log.debug('Image model %s is not available to %s for %s; using the default', model, user.id, capability) + + default_key = ( + IMAGE_GENERATION_DEFAULT_MODEL_CONFIG_KEY + if capability == 'image_generation' + else IMAGE_EDIT_DEFAULT_MODEL_CONFIG_KEY + ) + default = find(await Config.get(default_key)) + if default and default[capability]: + return default['connection'], default['model'] + + fallback = next((m for m in models if m[capability]), None) + if fallback: + return fallback['connection'], fallback['model'] + + raise HTTPException( + status_code=400, + detail=ERROR_MESSAGES.DEFAULT( + 'No image model is available' + if capability == 'image_generation' + else 'No image model that can edit images is available' + ), + ) + + +# Migration from the single-engine settings + + +def _legacy_generation_connection(values: dict) -> dict | None: + connection_type = values.get('image_generation.engine') or 'automatic1111' + enabled = bool(values.get('image_generation.enable')) + model = values.get('image_generation.model') or '' + config: dict[str, Any] = { + 'size': values.get('image_generation.size') or '', + 'steps': values.get('image_generation.steps'), + } + + if connection_type == 'openai': + url = values.get('image_generation.openai.api_base_url') or '' + key = values.get('image_generation.openai.api_key') or '' + config.update( + api_version=values.get('image_generation.openai.api_version') or '', + params=values.get('image_generation.openai.params') or {}, + ) + model = model or DEFAULT_OPENAI_IMAGE_MODEL + configured = bool(url) and (bool(key) or enabled) + elif connection_type == 'gemini': + url = values.get('image_generation.gemini.api_base_url') or '' + key = values.get('image_generation.gemini.api_key') or '' + config.update(endpoint_method=values.get('image_generation.gemini.endpoint_method') or '') + model = model or DEFAULT_GEMINI_IMAGE_MODEL + configured = bool(url) and (bool(key) or enabled) + elif connection_type == 'comfyui': + url = values.get('image_generation.comfyui.base_url') or '' + key = values.get('image_generation.comfyui.api_key') or '' + config.update( + workflow=values.get('image_generation.comfyui.workflow') or '', + nodes=values.get('image_generation.comfyui.nodes') or [], + ) + configured = bool(url) + else: + connection_type = 'automatic1111' + url = values.get('image_generation.automatic1111.base_url') or '' + key = values.get('image_generation.automatic1111.api_auth') or '' + config.update(params=values.get('image_generation.automatic1111.api_params') or {}) + configured = bool(url) + + if not configured: + return None + + config.update(model_ids=[model], capabilities={'image_generation': True, 'image_edit': False}) + return {'type': connection_type, 'url': url, 'key': key, 'config': config} + + +def _legacy_edit_connection(values: dict) -> dict | None: + connection_type = values.get('images.edit.engine') or 'openai' + enabled = bool(values.get('images.edit.enable')) + model = values.get('images.edit.model') or '' + config: dict[str, Any] = {'edit_size': values.get('images.edit.size') or ''} + + if connection_type == 'openai': + url = values.get('images.edit.openai.api_base_url') or '' + key = values.get('images.edit.openai.api_key') or '' + config.update(api_version=values.get('images.edit.openai.api_version') or '') + model = model or DEFAULT_OPENAI_IMAGE_MODEL + configured = bool(url) and (bool(key) or enabled) + elif connection_type == 'gemini': + url = values.get('images.edit.gemini.api_base_url') or '' + key = values.get('images.edit.gemini.api_key') or '' + # The edit route builds models/{model}:generateContent, so it never worked without a model. + configured = bool(url) and bool(model) and (bool(key) or enabled) + elif connection_type == 'comfyui': + url = values.get('images.edit.comfyui.base_url') or '' + key = values.get('images.edit.comfyui.api_key') or '' + config.update( + edit_workflow=values.get('images.edit.comfyui.workflow') or '', + edit_nodes=values.get('images.edit.comfyui.nodes') or [], + ) + configured = bool(url) + else: + return None + + if not configured: + return None + + config.update(model_ids=[model], capabilities={'image_generation': False, 'image_edit': True}) + return {'type': connection_type, 'url': url, 'key': key, 'config': config} + + +LEGACY_IMAGE_CONFIG_KEYS = ( + 'image_generation.enable', + 'image_generation.engine', + 'image_generation.model', + 'image_generation.size', + 'image_generation.steps', + 'image_generation.openai.api_base_url', + 'image_generation.openai.api_key', + 'image_generation.openai.api_version', + 'image_generation.openai.params', + 'image_generation.gemini.api_base_url', + 'image_generation.gemini.api_key', + 'image_generation.gemini.endpoint_method', + 'image_generation.comfyui.base_url', + 'image_generation.comfyui.api_key', + 'image_generation.comfyui.workflow', + 'image_generation.comfyui.nodes', + 'image_generation.automatic1111.base_url', + 'image_generation.automatic1111.api_auth', + 'image_generation.automatic1111.api_params', + 'images.edit.enable', + 'images.edit.engine', + 'images.edit.model', + 'images.edit.size', + 'images.edit.openai.api_base_url', + 'images.edit.openai.api_key', + 'images.edit.openai.api_version', + 'images.edit.gemini.api_base_url', + 'images.edit.gemini.api_key', + 'images.edit.comfyui.base_url', + 'images.edit.comfyui.api_key', + 'images.edit.comfyui.workflow', + 'images.edit.comfyui.nodes', +) + +LEGACY_IMAGE_CONNECTION_NAMES = { + 'openai': 'OpenAI', + 'gemini': 'Gemini', + 'comfyui': 'ComfyUI', + 'automatic1111': 'AUTOMATIC1111', +} + + +async def migrate_legacy_image_config() -> list[dict]: + """Turn the single generation and edit engine settings into image connections, once. + + The connections key is only written here or by an admin, so its absence marks a deployment + that has not been migrated. Both old engines keep working, with everyone who could use them + before keeping access. + """ + connections = await Config.get(IMAGE_CONNECTIONS_CONFIG_KEY) + if connections is not None: + return connections + + values = await Config.get_many(*LEGACY_IMAGE_CONFIG_KEYS) + generation = _legacy_generation_connection(values) + edit = _legacy_edit_connection(values) + + folded_edit_model = None + if ( + generation + and edit + and (generation['type'], generation['url'], generation['key']) + == ( + edit['type'], + edit['url'], + edit['key'], + ) + ): + # Same backend for both: one connection, with the edit model and settings folded in. + generation_model = generation['config']['model_ids'][0] + folded_edit_model = edit['config']['model_ids'][0] + for key, value in edit['config'].items(): + if key not in ('model_ids', 'capabilities'): + generation['config'].setdefault(key, value) + generation['config']['capabilities'] = {'image_generation': True, 'image_edit': True} + if folded_edit_model != generation_model: + generation['config']['model_ids'].append(folded_edit_model) + edit = None + + connections = [] + defaults = {IMAGE_GENERATION_DEFAULT_MODEL_CONFIG_KEY: '', IMAGE_EDIT_DEFAULT_MODEL_CONFIG_KEY: ''} + for legacy, default_key, suffix in ( + (generation, IMAGE_GENERATION_DEFAULT_MODEL_CONFIG_KEY, ''), + (edit, IMAGE_EDIT_DEFAULT_MODEL_CONFIG_KEY, ' (edit)'), + ): + if legacy is None: + continue + legacy['config']['access_grants'] = PUBLIC_ACCESS_GRANTS + connection = normalize_image_connection( + {**legacy, 'name': f'{LEGACY_IMAGE_CONNECTION_NAMES[legacy["type"]]}{suffix}'} + ) + connections.append(connection) + # An empty legacy model (ComfyUI, AUTOMATIC1111) becomes the connection's default model. + defaults[default_key] = get_image_model_ref(connection['id'], legacy['config']['model_ids'][0]) + + if folded_edit_model is not None: + defaults[IMAGE_EDIT_DEFAULT_MODEL_CONFIG_KEY] = get_image_model_ref(connections[0]['id'], folded_edit_model) + + await Config.upsert({IMAGE_CONNECTIONS_CONFIG_KEY: connections, **defaults}) + if connections: + log.info('Migrated image generation settings to %d image connection(s)', len(connections)) + return connections diff --git a/backend/open_webui/utils/middleware.py b/backend/open_webui/utils/middleware.py index c0a403007d..272ce61dfc 100644 --- a/backend/open_webui/utils/middleware.py +++ b/backend/open_webui/utils/middleware.py @@ -103,6 +103,7 @@ from open_webui.utils.filter import ( get_filter_functions, process_filter_functions, ) +from open_webui.utils.images.connections import get_requested_image_model from open_webui.utils.json_codec import JSONCodec, dumps_bytes from open_webui.utils.mcp.apps import MCPAppResult from open_webui.utils.memory import add_memory_context, recall_memories, review_memory_after_turn @@ -1521,7 +1522,7 @@ async def chat_image_generation_handler(request: Request, form_data: dict, extra try: images = await image_edits( request=request, - form_data=EditImageForm(**{'prompt': prompt, 'image': input_images}), + form_data=EditImageForm(prompt=prompt, image=input_images, model=get_requested_image_model(metadata)), metadata=image_metadata, user=user, ) @@ -1633,7 +1634,7 @@ async def chat_image_generation_handler(request: Request, form_data: dict, extra try: images = await image_generations( request=request, - form_data=CreateImageForm(**{'prompt': prompt}), + form_data=CreateImageForm(prompt=prompt, model=get_requested_image_model(metadata)), metadata=image_metadata, user=user, ) diff --git a/src/lib/apis/images/index.ts b/src/lib/apis/images/index.ts index 3717093798..3859b5570a 100644 --- a/src/lib/apis/images/index.ts +++ b/src/lib/apis/images/index.ts @@ -67,13 +67,10 @@ export const updateConfig = async (token: string = '', config: object) => { return res; }; -export const verifyConnection = async ( - token: string = '', - connection: { engine: string; url: string; key?: string | null } -) => { +export const verifyImageConnection = async (token: string = '', connection: object) => { let error = null; - const res = await fetch(`${IMAGES_API_BASE_URL}/verify`, { + const res = await fetch(`${IMAGES_API_BASE_URL}/connections/verify`, { method: 'POST', headers: { Accept: 'application/json', @@ -103,72 +100,7 @@ export const verifyConnection = async ( return res; }; -export const getImageGenerationConfig = async (token: string = '') => { - let error = null; - - const res = await fetch(`${IMAGES_API_BASE_URL}/image/config`, { - method: 'GET', - headers: { - Accept: 'application/json', - 'Content-Type': 'application/json', - ...(token && { authorization: `Bearer ${token}` }) - } - }) - .then(async (res) => { - if (!res.ok) throw await res.json(); - return res.json(); - }) - .catch((err) => { - console.error(err); - if ('detail' in err) { - error = err.detail; - } else { - error = 'Server connection failed'; - } - return null; - }); - - if (error) { - throw error; - } - - return res; -}; - -export const updateImageGenerationConfig = async (token: string = '', config: object) => { - let error = null; - - const res = await fetch(`${IMAGES_API_BASE_URL}/image/config/update`, { - method: 'POST', - headers: { - Accept: 'application/json', - 'Content-Type': 'application/json', - ...(token && { authorization: `Bearer ${token}` }) - }, - body: JSON.stringify({ ...config }) - }) - .then(async (res) => { - if (!res.ok) throw await res.json(); - return res.json(); - }) - .catch((err) => { - console.error(err); - if ('detail' in err) { - error = err.detail; - } else { - error = 'Server connection failed'; - } - return null; - }); - - if (error) { - throw error; - } - - return res; -}; - -export const getImageGenerationModels = async (token: string = '') => { +export const getImageModels = async (token: string = '') => { let error = null; const res = await fetch(`${IMAGES_API_BASE_URL}/models`, { diff --git a/src/lib/components/admin/Settings/Images.svelte b/src/lib/components/admin/Settings/Images.svelte index 35bff269a6..ce0743753c 100644 --- a/src/lib/components/admin/Settings/Images.svelte +++ b/src/lib/components/admin/Settings/Images.svelte @@ -1,324 +1,116 @@ -