feat: let the AI pick a sub-agent model from a list set by admins

Sub-agents always ran on the model of the chat that started them, so delegated work could not go to a cheaper, faster or stronger model. Admin Settings > Sub-agents now has a Models list (also settable as a JSON list with `SUBAGENTS_MODELS`), where each model can get a short note on what it is good or bad at. The AI sees the listed models the user is allowed to use, with that note or else the model's own description, and picks one per sub-agent or leaves it out to stay on the current chat model; a pick outside that set gets an error naming the valid choices. The main chat always continues on its own model, for foreground and background sub-agents alike. With an empty list nothing changes: no choice is offered and sub-agents run on the chat's model as before.

Closes #27598
This commit is contained in:
Classic298 2026-10-29 09:48:00 +01:00
parent 77398eba31
commit 59f62fb866
7 changed files with 144 additions and 9 deletions

View file

@ -2092,6 +2092,16 @@ ENABLE_CALENDAR = os.getenv('ENABLE_CALENDAR', 'True').lower() == 'true'
ENABLE_AUTOMATIONS = os.getenv('ENABLE_AUTOMATIONS', 'True').lower() == 'true'
ENABLE_SUBAGENTS = os.getenv('ENABLE_SUBAGENTS', 'False').lower() == 'true'
try:
subagents_models = JSONCodec.loads(os.getenv('SUBAGENTS_MODELS', '[]'))
if not isinstance(subagents_models, list) or not all(isinstance(entry, dict) for entry in subagents_models):
raise ValueError('SUBAGENTS_MODELS must be a JSON list of objects')
except Exception as e:
log.exception('Error loading SUBAGENTS_MODELS: %s', e)
subagents_models = []
SUBAGENTS_MODELS = subagents_models
SUBAGENTS_BACKGROUND_ENABLED = os.getenv('SUBAGENTS_BACKGROUND_ENABLED', 'False').lower() == 'true'
SUBAGENTS_MAX_CONCURRENT = int(os.getenv('SUBAGENTS_MAX_CONCURRENT', '20'))
SUBAGENTS_MAX_ASYNC = int(os.getenv('SUBAGENTS_MAX_ASYNC', '20'))
@ -3221,6 +3231,7 @@ DEFAULT_CONFIG = {
'calendar.enable': ENABLE_CALENDAR,
'automations.enable': ENABLE_AUTOMATIONS,
'subagents.enable': ENABLE_SUBAGENTS,
'subagents.models': SUBAGENTS_MODELS,
'subagents.background_enabled': SUBAGENTS_BACKGROUND_ENABLED,
'subagents.max_concurrent': SUBAGENTS_MAX_CONCURRENT,
'subagents.max_async': SUBAGENTS_MAX_ASYNC,

View file

@ -68,6 +68,7 @@ MODELS_CONFIG_KEYS = {
}
SUBAGENTS_CONFIG_KEYS = {
'ENABLE_SUBAGENTS': 'subagents.enable',
'SUBAGENTS_MODELS': 'subagents.models',
'SUBAGENTS_BACKGROUND_ENABLED': 'subagents.background_enabled',
'SUBAGENTS_MAX_CONCURRENT': 'subagents.max_concurrent',
'SUBAGENTS_MAX_ASYNC': 'subagents.max_async',
@ -810,6 +811,7 @@ async def set_models_config(request: Request, form_data: ModelsConfigForm, user=
class SubagentsConfigForm(BaseModel):
ENABLE_SUBAGENTS: bool
SUBAGENTS_MODELS: list[dict]
SUBAGENTS_BACKGROUND_ENABLED: bool
SUBAGENTS_MAX_CONCURRENT: int
SUBAGENTS_MAX_ASYNC: int

View file

@ -1810,6 +1810,7 @@ async def delegate_task(
context: str = '',
file_ids: list[str] | None = None,
background: bool = False,
model_id: str | None = None,
__request__: Request = None,
__user__: dict = None,
__metadata__: dict = None,
@ -1824,6 +1825,7 @@ async def delegate_task(
:param file_ids: Attached file IDs the sub-agent needs. Use this for images or files;
do not put file IDs only in context.
:param background: Return immediately and continue this chat when the sub-agent finishes
:param model_id: ID of one of the listed sub-agent models. Omit to use the current model
:return: Foreground result text, or a JSON dispatch handle for background work
"""
if __request__ is None:
@ -1838,6 +1840,7 @@ async def delegate_task(
context,
background,
file_ids=file_ids,
model_id=model_id,
request=__request__,
user_data=__user__ or {},
metadata=__metadata__ or {},

View file

@ -18,6 +18,7 @@ from open_webui.tasks import create_task, has_active_tasks
from open_webui.utils.auth import VERIFIED_USER_ROLES, create_token
from open_webui.utils.json_codec import JSONCodec
from open_webui.utils.misc import add_or_update_system_message, get_message_list, is_raster_image_content_type
from open_webui.utils.models import get_all_models, get_filtered_models
from open_webui.utils.tool_search import strip_deferred_tools_manifest
from starlette.datastructures import Headers
@ -73,6 +74,21 @@ async def _build_request(source: Request, user_id: str, *, internal: bool) -> Re
return request
async def get_subagent_models(request: Request, user: UserModel) -> list[dict]:
configured_models = await Config.get('subagents.models')
if not configured_models:
return []
models = {model['id']: model for model in await get_filtered_models(await get_all_models(request, user=user), user)}
subagent_models = []
for entry in configured_models:
model = models.get(entry.get('id'))
if not model:
continue
description = entry.get('description') or model.get('info', {}).get('meta', {}).get('description') or ''
subagent_models.append({'id': model['id'], 'name': model.get('name', model['id']), 'description': description})
return subagent_models
async def process_pending_internal_messages(
source_request: Request,
parent_chat_id: str,
@ -282,6 +298,7 @@ async def delegate(
background: bool,
*,
file_ids: list[str] | None = None,
model_id: str | None = None,
request: Request,
user_data: dict,
metadata: dict,
@ -343,6 +360,16 @@ async def delegate(
return 'Error: model context is required.'
if run.get('direct'):
return 'Error: sub-agents are unavailable for direct connections.'
if model_id:
subagent_models = await get_subagent_models(request, UserModel(**user_data))
available_model_ids = [model['id'] for model in subagent_models]
if model_id not in available_model_ids:
return (
f'Error: model {model_id} is not available for sub-agents. '
f'Available models: {", ".join(available_model_ids) or "none"}. '
'Omit model_id to use the current model.'
)
model_id = model_id or run['model_id']
delegated_files = []
if file_ids:
requested_file_ids = {str(file_id) for file_id in file_ids if file_id}
@ -405,7 +432,7 @@ async def delegate(
'role': 'user',
'content': prompt,
'timestamp': int(time.time()),
'models': [run['model_id']],
'models': [model_id],
**({'files': prompt_files} if prompt_files else {}),
}
chat = await Chats.insert_new_chat(
@ -415,7 +442,7 @@ async def delegate(
chat={
'id': chat_id,
'title': f'Sub-agent: {task[:60]}',
'models': [run['model_id']],
'models': [model_id],
'history': {
'currentId': assistant_message_id,
'messages': {
@ -427,7 +454,7 @@ async def delegate(
'role': 'assistant',
'content': '',
'done': False,
'model': run['model_id'],
'model': model_id,
'timestamp': int(time.time()),
},
},
@ -472,7 +499,7 @@ async def delegate(
str(config.get('subagents.system_prompt') or '').strip() or DEFAULT_SUBAGENT_SYSTEM_PROMPT
)
form_data = {
'model': run['model_id'],
'model': model_id,
'params': chat_context.get('params') or {},
'messages': add_or_update_system_message(
subagent_system_prompt,

View file

@ -913,11 +913,22 @@ async def get_builtin_tools(
)
spec = get_builtin_tool_spec(func)
if func.__name__ == 'delegate_task' and not config.get('subagents.background_enabled'):
if func.__name__ == 'delegate_task':
from open_webui.utils.subagents import get_subagent_models
parameters = spec.get('parameters', {})
parameters.get('properties', {}).pop('background', None)
if isinstance(parameters.get('required'), list):
parameters['required'] = [name for name in parameters['required'] if name != 'background']
properties = parameters.get('properties', {})
if not config.get('subagents.background_enabled'):
properties.pop('background', None)
if isinstance(parameters.get('required'), list):
parameters['required'] = [name for name in parameters['required'] if name != 'background']
subagent_models = await get_subagent_models(request, UserModel(**user)) if user else []
if subagent_models:
properties['model_id']['enum'] = [subagent_model['id'] for subagent_model in subagent_models]
spec['description'] += f' Available models: {JSONCodec.dumps(subagent_models)}'
else:
properties.pop('model_id', None)
tools_dict[func.__name__] = {
'tool_id': f'builtin:{func.__name__}',

View file

@ -4,16 +4,22 @@
import { toast } from 'svelte-sonner';
import { config as appConfig } from '$lib/stores';
import { getBackendConfig } from '$lib/apis';
import { getBackendConfig, getModels } from '$lib/apis';
import { getSubagentsConfig, setSubagentsConfig } from '$lib/apis/configs';
import SettingsSelect from '$lib/components/common/SettingsSelect.svelte';
import Spinner from '$lib/components/common/Spinner.svelte';
import Switch from '$lib/components/common/Switch.svelte';
import Tooltip from '$lib/components/common/Tooltip.svelte';
import Plus from '$lib/components/icons/Plus.svelte';
import XMark from '$lib/components/icons/XMark.svelte';
const i18n = getContext('i18n');
let loading = true;
let saving = false;
let enabled = false;
let subagentModels: { id: string; description: string }[] = [];
let models: any[] = [];
let backgroundEnabled = false;
let maxConcurrent = 20;
let maxAsync = 20;
@ -25,12 +31,14 @@
try {
const config = await getSubagentsConfig(localStorage.token);
enabled = config?.ENABLE_SUBAGENTS ?? false;
subagentModels = config?.SUBAGENTS_MODELS ?? [];
backgroundEnabled = config?.SUBAGENTS_BACKGROUND_ENABLED ?? false;
maxConcurrent = Number(config?.SUBAGENTS_MAX_CONCURRENT) || 20;
maxAsync = Number(config?.SUBAGENTS_MAX_ASYNC) || 20;
maxIterations = Number(config?.SUBAGENTS_MAX_ITERATIONS) || 30;
maxOutput = Number(config?.SUBAGENTS_MAX_OUTPUT) || 30000;
systemPrompt = config?.SUBAGENTS_SYSTEM_PROMPT ?? '';
models = await getModels(localStorage.token, null, false);
} catch (error) {
toast.error(`${error}`);
} finally {
@ -43,6 +51,7 @@
try {
await setSubagentsConfig(localStorage.token, {
ENABLE_SUBAGENTS: enabled,
SUBAGENTS_MODELS: subagentModels,
SUBAGENTS_BACKGROUND_ENABLED: backgroundEnabled,
SUBAGENTS_MAX_CONCURRENT: maxConcurrent,
SUBAGENTS_MAX_ASYNC: maxAsync,
@ -83,6 +92,74 @@
</p>
{#if enabled}
<div>
<div class="mb-1 flex min-h-7 w-full items-center justify-between gap-2">
<div class="min-w-0 text-xs text-gray-600 dark:text-gray-400">
{$i18n.t('settings.admin.subagents.models.label')}
</div>
<button
class="flex size-6 shrink-0 items-center justify-center text-gray-400 dark:text-gray-600"
type="button"
aria-label={$i18n.t('settings.admin.subagents.addModel.label')}
on:click={() => {
if (
subagentModels.length === 0 ||
subagentModels[subagentModels.length - 1]?.id !== ''
) {
subagentModels = [...subagentModels, { id: '', description: '' }];
}
}}
>
<Plus />
</button>
</div>
<div class="mb-2 text-[0.6875rem] text-gray-400 dark:text-gray-600">
{$i18n.t('settings.admin.subagents.models.description')}
</div>
<div class="flex flex-col gap-1.5">
{#each subagentModels as subagentModel, subagentModelIdx}
<div
class="flex items-center gap-1 rounded-lg border border-gray-100/40 bg-transparent px-2 py-1 transition focus-within:border-blue-400 dark:border-gray-850/50 dark:focus-within:border-blue-500"
>
<SettingsSelect
bind:value={subagentModel.id}
required
ariaLabel={$i18n.t('Model')}
className="w-48 max-w-full shrink-0"
selectClassName="!h-6 !border-transparent !bg-transparent !ps-1 dark:!bg-transparent"
>
<option value="" disabled hidden>{$i18n.t('Select a model')}</option>
{#each models.filter((model: any) => model.id === subagentModel.id || !subagentModels.some((otherModel) => otherModel.id === model.id)) as model}
<option value={model.id} class="bg-gray-100 dark:bg-gray-700">
{model.name}
{model?.connection_type === 'local' ? `(${$i18n.t('Local')})` : ''}
</option>
{/each}
</SettingsSelect>
<input
class="min-w-0 flex-1 bg-transparent text-[0.8125rem] leading-5 text-gray-700 outline-hidden placeholder:text-gray-300 dark:text-gray-200 dark:placeholder:text-gray-700"
aria-label={$i18n.t('Description')}
placeholder={$i18n.t('settings.admin.subagents.modelDescription.placeholder')}
bind:value={subagentModel.description}
/>
<Tooltip content={$i18n.t('Delete')}>
<button
class="flex size-6 shrink-0 items-center justify-center text-gray-400 opacity-70 transition hover:text-gray-700 hover:opacity-100 dark:text-gray-600 dark:hover:text-gray-300"
type="button"
aria-label={$i18n.t('Delete')}
on:click={() => {
subagentModels.splice(subagentModelIdx, 1);
subagentModels = subagentModels;
}}
>
<XMark className="size-3.5" />
</button>
</Tooltip>
</div>
{/each}
</div>
</div>
<div>
<label class="text-xs text-gray-600 dark:text-gray-400" for="sa-concurrent">
{$i18n.t('settings.admin.subagents.maxConcurrent.label')}

View file

@ -3213,6 +3213,7 @@
"settings.admin.pipelines.title": "Pipelines",
"settings.admin.pipelines.uploadPipeline.description": "Upload a local Python pipeline file to the selected server.",
"settings.admin.pipelines.uploadPipeline.label": "Upload Pipeline",
"settings.admin.subagents.addModel.label": "Add model",
"settings.admin.subagents.enableBackgroundSubAgents.description": "Allow delegated sub-agents to keep running while the parent chat continues.",
"settings.admin.subagents.enableBackgroundSubAgents.label": "Enable background sub-agents",
"settings.admin.subagents.enableSubAgents.label": "Enable sub-agents",
@ -3220,6 +3221,9 @@
"settings.admin.subagents.maxConcurrent.label": "Max concurrent",
"settings.admin.subagents.maxIterations.label": "Max iterations",
"settings.admin.subagents.maxOutput.label": "Max output",
"settings.admin.subagents.modelDescription.placeholder": "What is this model good for?",
"settings.admin.subagents.models.description": "Models the AI can choose for sub-agents, each with an optional note on what it is good or bad at. Without a choice, sub-agents use the current chat model.",
"settings.admin.subagents.models.label": "Models",
"settings.admin.subagents.systemPrompt.description": "Leave empty for the built-in default.",
"settings.admin.subagents.systemPrompt.label": "System prompt",
"settings.admin.subagents.title": "Sub-agents",