open-webui/src/lib/components/admin/Settings/Evaluations.svelte

186 lines
5 KiB
Svelte
Raw Normal View History

2024-10-22 10:16:48 +00:00
<script lang="ts">
import { toast } from 'svelte-sonner';
2025-02-12 09:41:59 +00:00
import { models, settings, user, config } from '$lib/stores';
2024-10-22 10:16:48 +00:00
import { createEventDispatcher, onMount, getContext, tick } from 'svelte';
const dispatch = createEventDispatcher();
import { getModels } from '$lib/apis';
2024-11-26 08:58:40 +00:00
import { getConfig, updateConfig } from '$lib/apis/evaluations';
2024-10-22 10:16:48 +00:00
import Switch from '$lib/components/common/Switch.svelte';
import Spinner from '$lib/components/common/Spinner.svelte';
import Tooltip from '$lib/components/common/Tooltip.svelte';
import Plus from '$lib/components/icons/Plus.svelte';
import Model from './Evaluations/Model.svelte';
2024-11-18 13:22:13 +00:00
import ArenaModelModal from './Evaluations/ArenaModelModal.svelte';
2024-10-22 10:16:48 +00:00
const i18n = getContext('i18n');
2025-02-12 09:41:59 +00:00
let evaluationConfig = null;
2024-10-22 10:16:48 +00:00
let showAddModel = false;
const submitHandler = async () => {
2025-02-12 09:41:59 +00:00
evaluationConfig = await updateConfig(localStorage.token, evaluationConfig).catch((err) => {
2024-10-22 10:16:48 +00:00
toast.error(err);
return null;
});
2025-02-12 09:41:59 +00:00
if (evaluationConfig) {
2024-10-22 10:16:48 +00:00
toast.success('Settings saved successfully');
2025-02-12 09:22:53 +00:00
models.set(
await getModels(
localStorage.token,
2025-02-12 09:32:49 +00:00
$config?.features?.enable_direct_connections && ($settings?.directConnections ?? null)
2025-02-12 09:22:53 +00:00
)
);
2024-10-22 10:16:48 +00:00
}
};
const addModelHandler = async (model) => {
2025-02-12 09:41:59 +00:00
evaluationConfig.EVALUATION_ARENA_MODELS.push(model);
evaluationConfig.EVALUATION_ARENA_MODELS = [...evaluationConfig.EVALUATION_ARENA_MODELS];
2024-10-22 10:16:48 +00:00
await submitHandler();
2025-02-12 09:22:53 +00:00
models.set(
await getModels(
localStorage.token,
2025-02-12 09:32:49 +00:00
$config?.features?.enable_direct_connections && ($settings?.directConnections ?? null)
2025-02-12 09:22:53 +00:00
)
);
2024-10-22 10:16:48 +00:00
};
const editModelHandler = async (model, modelIdx) => {
2025-02-12 09:41:59 +00:00
evaluationConfig.EVALUATION_ARENA_MODELS[modelIdx] = model;
evaluationConfig.EVALUATION_ARENA_MODELS = [...evaluationConfig.EVALUATION_ARENA_MODELS];
2024-10-22 10:16:48 +00:00
await submitHandler();
2025-02-12 09:22:53 +00:00
models.set(
await getModels(
localStorage.token,
2025-02-12 09:32:49 +00:00
$config?.features?.enable_direct_connections && ($settings?.directConnections ?? null)
2025-02-12 09:22:53 +00:00
)
);
2024-10-22 10:16:48 +00:00
};
const deleteModelHandler = async (modelIdx) => {
2025-02-12 09:41:59 +00:00
evaluationConfig.EVALUATION_ARENA_MODELS = evaluationConfig.EVALUATION_ARENA_MODELS.filter(
2024-10-22 10:16:48 +00:00
(m, mIdx) => mIdx !== modelIdx
);
await submitHandler();
2025-02-12 09:22:53 +00:00
models.set(
await getModels(
localStorage.token,
2025-02-12 09:32:49 +00:00
$config?.features?.enable_direct_connections && ($settings?.directConnections ?? null)
2025-02-12 09:22:53 +00:00
)
);
2024-10-22 10:16:48 +00:00
};
onMount(async () => {
2025-04-01 03:32:12 +00:00
if ($user?.role === 'admin') {
2025-02-12 09:41:59 +00:00
evaluationConfig = await getConfig(localStorage.token).catch((err) => {
2024-10-22 10:16:48 +00:00
toast.error(err);
return null;
});
}
});
</script>
2024-11-18 13:22:13 +00:00
<ArenaModelModal
2024-10-22 10:16:48 +00:00
bind:show={showAddModel}
on:submit={async (e) => {
addModelHandler(e.detail);
}}
/>
<form
class="flex flex-col h-full justify-between text-sm"
on:submit|preventDefault={() => {
submitHandler();
dispatch('save');
}}
>
<div class="overflow-y-scroll scrollbar-hidden h-full">
2025-02-12 09:41:59 +00:00
{#if evaluationConfig !== null}
2024-10-22 10:16:48 +00:00
<div class="">
2025-02-27 07:58:43 +00:00
<div class="mb-3">
<div class=" mb-2.5 text-base font-medium">{$i18n.t('General')}</div>
2024-10-22 10:16:48 +00:00
2025-02-27 07:58:43 +00:00
<hr class=" border-gray-100 dark:border-gray-850 my-2" />
<div class="mb-2.5 flex w-full justify-between">
2024-10-22 10:16:48 +00:00
<div class=" text-xs font-medium">{$i18n.t('Arena Models')}</div>
<Tooltip content={$i18n.t(`Message rating should be enabled to use this feature`)}>
2025-02-12 09:41:59 +00:00
<Switch bind:state={evaluationConfig.ENABLE_EVALUATION_ARENA_MODELS} />
</Tooltip>
2024-10-22 10:16:48 +00:00
</div>
</div>
2025-02-12 09:41:59 +00:00
{#if evaluationConfig.ENABLE_EVALUATION_ARENA_MODELS}
2025-02-27 07:58:43 +00:00
<div class="mb-3">
<div class=" mb-2.5 text-base font-medium flex justify-between items-center">
<div>
{$i18n.t('Manage')}
</div>
2024-10-22 10:16:48 +00:00
2025-02-27 07:58:43 +00:00
<div>
<Tooltip content={$i18n.t('Add Arena Model')}>
<button
class="p-1"
type="button"
on:click={() => {
showAddModel = true;
}}
>
<Plus />
</button>
</Tooltip>
2024-10-22 10:16:48 +00:00
</div>
2025-02-27 07:58:43 +00:00
</div>
<hr class=" border-gray-100 dark:border-gray-850 my-2" />
<div class="flex flex-col gap-2">
{#if (evaluationConfig?.EVALUATION_ARENA_MODELS ?? []).length > 0}
{#each evaluationConfig.EVALUATION_ARENA_MODELS as model, index}
<Model
{model}
on:edit={(e) => {
editModelHandler(e.detail, index);
}}
on:delete={(e) => {
deleteModelHandler(index);
}}
/>
{/each}
{:else}
<div class=" text-center text-xs text-gray-500">
{$i18n.t(
`Using the default arena model with all models. Click the plus button to add custom models.`
)}
</div>
{/if}
</div>
2024-10-22 10:16:48 +00:00
</div>
{/if}
</div>
{:else}
<div class="flex h-full justify-center">
<div class="my-auto">
<Spinner className="size-6" />
</div>
</div>
{/if}
</div>
<div class="flex justify-end pt-3 text-sm font-medium">
<button
class="px-3.5 py-1.5 text-sm font-medium bg-black hover:bg-gray-900 text-white dark:bg-white dark:text-black dark:hover:bg-gray-100 transition rounded-full"
type="submit"
>
{$i18n.t('Save')}
</button>
</div>
</form>