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

116 lines
3.3 KiB
Svelte
Raw Normal View History

2023-11-20 01:47:07 +00:00
<script lang="ts">
2024-03-30 00:20:07 +00:00
import { Collapsible } from 'bits-ui';
2024-01-03 01:26:19 +00:00
import { setDefaultModels } from '$lib/apis/configs';
2024-05-15 06:16:22 +00:00
import { models, showSettings, settings, user, mobile } 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-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 });
localStorage.setItem('settings', JSON.stringify($settings));
2024-01-03 01:26:19 +00:00
if ($user.role === 'admin') {
2024-01-03 01:30:09 +00:00
console.log('setting default models globally');
2024-01-03 01:26:19 +00:00
await setDefaultModels(localStorage.token, selectedModels.join(','));
}
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-05-14 22:09:30 +00:00
<div class="flex flex-col w-full items-center md: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')}
items={$models
.filter((model) => model.name !== 'hr')
.map((model) => ({
value: model.id,
label: model.custom_info?.name ?? model.name,
2024-03-25 06:26:00 +00:00
info: model
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-03-30 00:20:07 +00:00
<div class=" self-center mr-2 disabled:text-gray-600 disabled:hover:text-gray-600">
<Tooltip content={$i18n.t('Add Model')}>
2024-03-30 00:20:07 +00:00
<button
class=" "
{disabled}
on:click={() => {
selectedModels = [...selectedModels, ''];
}}
>
<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-03-30 00:20:07 +00:00
<div class=" self-center disabled:text-gray-600 disabled:hover:text-gray-600 mr-2">
<Tooltip content={$i18n.t('Remove Model')}>
2024-03-30 00:20:07 +00:00
<button
{disabled}
on:click={() => {
selectedModels.splice(selectedModelIdx, 1);
selectedModels = selectedModels;
}}
>
<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-05-15 06:16:22 +00:00
{#if showSetDefault && !$mobile}
<div class="text-left mt-0.5 ml-1 text-[0.7rem] text-gray-500">
<button on:click={saveDefaultModel}> {$i18n.t('Set as default')}</button>
</div>
{/if}