open-webui/src/lib/components/chat/ModelSelector.svelte

119 lines
3.3 KiB
Svelte
Raw Normal View History

2023-11-20 01:47:07 +00:00
<script lang="ts">
2024-08-19 14:49:40 +00:00
import { models, showSettings, settings, user, mobile, config } from '$lib/stores';
2024-03-01 04:40:36 +00:00
import { onMount, tick, getContext } from 'svelte';
2024-03-01 09:18:07 +00:00
import { toast } from 'svelte-sonner';
import Selector from './ModelSelector/Selector.svelte';
2024-03-30 00:20:07 +00:00
import Tooltip from '../common/Tooltip.svelte';
2023-11-20 01:47:07 +00:00
2024-05-27 05:47:42 +00:00
import { setDefaultModels } from '$lib/apis/configs';
import { updateUserSettings } from '$lib/apis/users';
2024-03-01 04:40:36 +00:00
const i18n = getContext('i18n');
2023-11-20 01:47:07 +00:00
export let selectedModels = [''];
export let disabled = false;
export let showSetDefault = true;
2024-01-03 01:26:19 +00:00
const saveDefaultModel = async () => {
2023-12-30 07:35:08 +00:00
const hasEmptyModel = selectedModels.filter((it) => it === '');
if (hasEmptyModel.length) {
toast.error($i18n.t('Choose a model before saving...'));
return;
}
2023-11-20 01:47:07 +00:00
settings.set({ ...$settings, models: selectedModels });
2024-05-27 05:47:42 +00:00
await updateUserSettings(localStorage.token, { ui: $settings });
2024-01-03 01:26:19 +00:00
toast.success($i18n.t('Default model updated'));
2023-11-20 01:47:07 +00:00
};
2024-01-03 00:06:11 +00:00
$: if (selectedModels.length > 0 && $models.length > 0) {
selectedModels = selectedModels.map((model) =>
2024-02-22 12:12:26 +00:00
$models.map((m) => m.id).includes(model) ? model : ''
2024-01-03 00:06:11 +00:00
);
}
2023-11-20 01:47:07 +00:00
</script>
2024-07-08 23:55:12 +00:00
<div class="flex flex-col w-full items-start">
2023-11-20 01:47:07 +00:00
{#each selectedModels as selectedModel, selectedModelIdx}
2024-05-02 19:33:04 +00:00
<div class="flex w-full max-w-fit">
2024-03-24 22:28:36 +00:00
<div class="overflow-hidden w-full">
2024-05-02 19:33:04 +00:00
<div class="mr-1 max-w-full">
<Selector
2024-03-24 22:28:36 +00:00
placeholder={$i18n.t('Select a model')}
2024-05-24 10:02:56 +00:00
items={$models.map((model) => ({
value: model.id,
label: model.name,
model: model
}))}
2024-08-19 14:49:40 +00:00
showTemporaryChatControl={$user.role === 'user'
? ($config?.permissions?.chat?.temporary ?? true)
: true}
2024-03-24 22:28:36 +00:00
bind:value={selectedModel}
/>
</div>
</div>
2023-11-20 01:47:07 +00:00
{#if selectedModelIdx === 0}
2024-09-05 15:11:55 +00:00
<div
class=" self-center mr-2 disabled:text-gray-600 disabled:hover:text-gray-600 -translate-y-[0.75px]"
>
<Tooltip content={$i18n.t('Add Model')}>
2024-03-30 00:20:07 +00:00
<button
class=" "
{disabled}
on:click={() => {
selectedModels = [...selectedModels, ''];
}}
aria-label="Add Model"
2024-03-30 00:20:07 +00:00
>
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
2024-05-02 19:33:04 +00:00
stroke-width="2"
2024-03-30 00:20:07 +00:00
stroke="currentColor"
2024-05-02 19:33:04 +00:00
class="size-3.5"
2024-03-30 00:20:07 +00:00
>
<path stroke-linecap="round" stroke-linejoin="round" d="M12 6v12m6-6H6" />
</svg>
</button>
</Tooltip>
</div>
2023-11-20 01:47:07 +00:00
{:else}
2024-09-05 15:11:55 +00:00
<div
class=" self-center disabled:text-gray-600 disabled:hover:text-gray-600 mr-2 -translate-y-[0.75px]"
>
<Tooltip content={$i18n.t('Remove Model')}>
2024-03-30 00:20:07 +00:00
<button
{disabled}
on:click={() => {
selectedModels.splice(selectedModelIdx, 1);
selectedModels = selectedModels;
}}
aria-label="Remove Model"
2024-03-30 00:20:07 +00:00
>
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
2024-05-02 19:33:04 +00:00
stroke-width="2"
2024-03-30 00:20:07 +00:00
stroke="currentColor"
2024-05-02 19:33:04 +00:00
class="size-3.5"
2024-03-30 00:20:07 +00:00
>
<path stroke-linecap="round" stroke-linejoin="round" d="M19.5 12h-15" />
</svg>
</button>
</Tooltip>
</div>
2023-11-20 01:47:07 +00:00
{/if}
</div>
{/each}
</div>
2024-09-05 15:11:55 +00:00
{#if showSetDefault}
<div class=" absolute text-left mt-[1px] ml-1 text-[0.7rem] text-gray-500 font-primary">
<button on:click={saveDefaultModel}> {$i18n.t('Set as default')}</button>
</div>
{/if}