open-webui/src/routes/(app)/workspace/models/edit/+page.svelte

494 lines
13 KiB
Svelte
Raw Normal View History

2023-12-04 03:13:42 +00:00
<script>
import { v4 as uuidv4 } from 'uuid';
2024-03-01 09:18:07 +00:00
import { toast } from 'svelte-sonner';
2023-12-04 03:13:42 +00:00
import { goto } from '$app/navigation';
import { onMount, getContext } from 'svelte';
2023-12-04 03:13:42 +00:00
import { page } from '$app/stores';
2024-05-25 01:26:36 +00:00
import { settings, user, config, models } from '$lib/stores';
import { splitStream } from '$lib/utils';
2024-05-24 09:17:48 +00:00
import { getModelInfos, updateModelById } from '$lib/apis/models';
2024-02-05 09:58:54 +00:00
import AdvancedParams from '$lib/components/chat/Settings/Advanced/AdvancedParams.svelte';
2024-05-25 01:26:36 +00:00
import { getModels } from '$lib/apis';
const i18n = getContext('i18n');
2023-12-04 03:13:42 +00:00
let loading = false;
2024-05-25 01:26:36 +00:00
let success = false;
2023-12-04 03:13:42 +00:00
let filesInputElement;
let inputFiles;
2024-05-25 01:26:36 +00:00
2023-12-04 03:13:42 +00:00
let digest = '';
let pullProgress = null;
2024-05-25 03:29:13 +00:00
let showAdvanced = false;
let showPreview = false;
2023-12-04 03:13:42 +00:00
// ///////////
2024-05-25 01:26:36 +00:00
// model
2023-12-04 03:13:42 +00:00
// ///////////
2024-05-25 01:26:36 +00:00
let model = null;
let info = {
id: '',
base_model_id: null,
name: '',
meta: {
profile_image_url: '/favicon.png',
description: '',
content: '',
suggestion_prompts: []
},
2024-05-25 03:29:13 +00:00
params: {
system: ''
}
2023-12-04 03:13:42 +00:00
};
2024-05-25 03:29:13 +00:00
let params = {};
2023-12-04 03:13:42 +00:00
const updateHandler = async () => {
loading = true;
2024-05-25 01:26:36 +00:00
const res = await updateModelById(localStorage.token, info.id, info);
2023-12-04 03:13:42 +00:00
2024-05-25 01:26:36 +00:00
if (res) {
await goto('/workspace/models');
await models.set(await getModels(localStorage.token));
2023-12-04 03:13:42 +00:00
}
loading = false;
success = false;
};
2024-05-25 01:26:36 +00:00
onMount(() => {
const id = $page.url.searchParams.get('id');
if (id) {
model = $models.find((m) => m.id === id);
if (model) {
info = {
...info,
...JSON.parse(
JSON.stringify(
model?.info
? model?.info
: {
id: model.id,
name: model.name
}
)
)
};
2024-05-25 03:29:13 +00:00
if (model.preset && model.owned_by === 'ollama' && !info.base_model_id.includes(':')) {
info.base_model_id = `${info.base_model_id}:latest`;
}
2024-05-25 01:26:36 +00:00
console.log(model);
} else {
goto('/workspace/models');
}
} else {
goto('/workspace/models');
}
});
2023-12-04 03:13:42 +00:00
</script>
2024-05-15 08:09:32 +00:00
<div class="w-full max-h-full">
<input
bind:this={filesInputElement}
bind:files={inputFiles}
type="file"
hidden
accept="image/*"
on:change={() => {
let reader = new FileReader();
reader.onload = (event) => {
let originalImageUrl = `${event.target.result}`;
const img = new Image();
img.src = originalImageUrl;
img.onload = function () {
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
// Calculate the aspect ratio of the image
const aspectRatio = img.width / img.height;
// Calculate the new width and height to fit within 100x100
let newWidth, newHeight;
if (aspectRatio > 1) {
newWidth = 100 * aspectRatio;
newHeight = 100;
} else {
newWidth = 100;
newHeight = 100 / aspectRatio;
}
2023-12-04 03:13:42 +00:00
2024-05-15 08:09:32 +00:00
// Set the canvas size
canvas.width = 100;
canvas.height = 100;
2023-12-04 03:13:42 +00:00
2024-05-15 08:09:32 +00:00
// Calculate the position to center the image
const offsetX = (100 - newWidth) / 2;
const offsetY = (100 - newHeight) / 2;
2023-12-04 03:13:42 +00:00
2024-05-15 08:09:32 +00:00
// Draw the image on the canvas
ctx.drawImage(img, offsetX, offsetY, newWidth, newHeight);
2023-12-04 03:13:42 +00:00
2024-05-15 08:09:32 +00:00
// Get the base64 representation of the compressed image
const compressedSrc = canvas.toDataURL('image/jpeg');
2023-12-04 03:13:42 +00:00
2024-05-15 08:09:32 +00:00
// Display the compressed image
2024-05-25 01:26:36 +00:00
info.meta.profile_image_url = compressedSrc;
2023-12-04 03:13:42 +00:00
2024-05-15 08:09:32 +00:00
inputFiles = null;
};
};
2023-12-04 03:13:42 +00:00
2024-05-15 08:09:32 +00:00
if (
inputFiles &&
inputFiles.length > 0 &&
2024-05-19 11:10:00 +00:00
['image/gif', 'image/webp', 'image/jpeg', 'image/png'].includes(inputFiles[0]['type'])
2024-05-15 08:09:32 +00:00
) {
reader.readAsDataURL(inputFiles[0]);
} else {
console.log(`Unsupported File Type '${inputFiles[0]['type']}'.`);
inputFiles = null;
}
}}
/>
<button
class="flex space-x-1"
on:click={() => {
history.back();
}}
>
<div class=" self-center">
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 20 20"
fill="currentColor"
class="w-4 h-4"
2023-12-04 03:13:42 +00:00
>
2024-05-15 08:09:32 +00:00
<path
fill-rule="evenodd"
d="M17 10a.75.75 0 01-.75.75H5.612l4.158 3.96a.75.75 0 11-1.04 1.08l-5.5-5.25a.75.75 0 010-1.08l5.5-5.25a.75.75 0 111.04 1.08L5.612 9.25H16.25A.75.75 0 0117 10z"
clip-rule="evenodd"
/>
</svg>
</div>
<div class=" self-center font-medium text-sm">{$i18n.t('Back')}</div>
</button>
2024-05-25 01:26:36 +00:00
{#if model}
<form
class="flex flex-col max-w-2xl mx-auto mt-4 mb-10"
on:submit|preventDefault={() => {
updateHandler();
}}
>
<div class="flex justify-center my-4">
<div class="self-center">
<button
class=" {info?.meta?.profile_image_url
? ''
: 'p-6'} rounded-full dark:bg-gray-700 border border-dashed border-gray-200"
type="button"
on:click={() => {
filesInputElement.click();
}}
>
{#if info?.meta?.profile_image_url}
<img
src={info?.meta?.profile_image_url}
alt="modelfile profile"
class=" rounded-full w-20 h-20 object-cover"
/>
{:else}
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
fill="currentColor"
class="w-8"
>
<path
fill-rule="evenodd"
d="M12 3.75a.75.75 0 01.75.75v6.75h6.75a.75.75 0 010 1.5h-6.75v6.75a.75.75 0 01-1.5 0v-6.75H4.5a.75.75 0 010-1.5h6.75V4.5a.75.75 0 01.75-.75z"
clip-rule="evenodd"
/>
</svg>
{/if}
</button>
2023-12-04 03:13:42 +00:00
</div>
2024-05-15 08:09:32 +00:00
</div>
2024-05-25 01:26:36 +00:00
<div class="my-2 flex space-x-2">
<div class="flex-1">
<div class=" text-sm font-semibold mb-2">{$i18n.t('Name')}*</div>
2024-05-15 08:09:32 +00:00
2024-05-25 01:26:36 +00:00
<div>
<input
class="px-3 py-1.5 text-sm w-full bg-transparent border dark:border-gray-600 outline-none rounded-lg"
placeholder={$i18n.t('Name your model')}
bind:value={info.name}
required
/>
</div>
2023-12-04 03:13:42 +00:00
</div>
2024-05-15 08:09:32 +00:00
2024-05-25 01:26:36 +00:00
<div class="flex-1">
<div class=" text-sm font-semibold mb-2">{$i18n.t('Model ID')}*</div>
2023-12-04 03:13:42 +00:00
2024-05-25 01:26:36 +00:00
<div>
<input
class="px-3 py-1.5 text-sm w-full bg-transparent disabled:text-gray-500 border dark:border-gray-600 outline-none rounded-lg"
placeholder={$i18n.t('Add a model id')}
value={info.id}
disabled
required
/>
</div>
</div>
2024-05-15 08:09:32 +00:00
</div>
2023-12-04 03:13:42 +00:00
2024-05-25 03:29:13 +00:00
{#if model.preset}
<div class="my-2">
<div class=" text-sm font-semibold mb-2">{$i18n.t('Base Model (From)')}</div>
<div>
<select
class="px-3 py-1.5 text-sm w-full bg-transparent border dark:border-gray-600 outline-none rounded-lg"
placeholder="Select a base model (e.g. llama3, gpt-4o)"
bind:value={info.base_model_id}
required
>
<option value={null} class=" placeholder:text-gray-500"
>{$i18n.t('Select a base model')}</option
>
{#each $models.filter((m) => m.id !== model.id) as model}
<option value={model.id}>{model.name}</option>
{/each}
</select>
</div>
</div>
{/if}
2024-05-25 01:26:36 +00:00
<div class="my-2">
2024-05-25 03:29:13 +00:00
<div class=" text-sm font-semibold mb-2">{$i18n.t('Description')}*</div>
2024-05-15 08:09:32 +00:00
<div>
2024-05-25 01:26:36 +00:00
<input
2024-05-15 08:09:32 +00:00
class="px-3 py-1.5 text-sm w-full bg-transparent border dark:border-gray-600 outline-none rounded-lg"
2024-05-25 01:26:36 +00:00
placeholder={$i18n.t('Add a short description about what this model does')}
bind:value={info.meta.description}
2024-05-15 08:09:32 +00:00
required
/>
2023-12-04 03:13:42 +00:00
</div>
2024-05-15 08:09:32 +00:00
</div>
2023-12-04 03:13:42 +00:00
2024-05-25 01:26:36 +00:00
<div class="my-2">
<div class="flex w-full justify-between">
2024-05-25 03:29:13 +00:00
<div class=" self-center text-sm font-semibold">{$i18n.t('Model Params')}</div>
2024-05-25 01:26:36 +00:00
</div>
2023-12-04 03:13:42 +00:00
2024-05-25 01:26:36 +00:00
<!-- <div class=" text-sm font-semibold mb-2"></div> -->
2023-12-04 03:13:42 +00:00
2024-05-25 01:26:36 +00:00
<div class="mt-2">
2024-05-25 03:29:13 +00:00
<div class="my-1">
<div class=" text-xs font-semibold mb-2">{$i18n.t('System Prompt')}</div>
<div>
<textarea
class="px-3 py-1.5 text-sm w-full bg-transparent border dark:border-gray-600 outline-none rounded-lg -mb-1"
placeholder={`Write your model system prompt content here\ne.g.) You are Mario from Super Mario Bros, acting as an assistant.`}
rows="4"
bind:value={info.params.system}
/>
</div>
</div>
2023-12-04 03:13:42 +00:00
2024-05-25 03:29:13 +00:00
<div class="flex w-full justify-between">
<div class=" self-center text-sm font-semibold">
{$i18n.t('Advanced Params')}
</div>
<button
class="p-1 px-3 text-xs flex rounded transition"
type="button"
on:click={() => {
showAdvanced = !showAdvanced;
}}
>
{#if showAdvanced}
<span class="ml-2 self-center">{$i18n.t('Hide')}</span>
{:else}
<span class="ml-2 self-center">{$i18n.t('Show')}</span>
{/if}
</button>
2024-05-15 08:09:32 +00:00
</div>
2024-05-25 03:29:13 +00:00
{#if showAdvanced}
<div class="my-2">
<AdvancedParams bind:params />
</div>
{/if}
2024-05-25 01:26:36 +00:00
</div>
2024-05-15 08:09:32 +00:00
</div>
2023-12-04 03:13:42 +00:00
2024-05-15 08:09:32 +00:00
<div class="my-2">
2024-05-25 01:26:36 +00:00
<div class="flex w-full justify-between mb-2">
<div class=" self-center text-sm font-semibold">{$i18n.t('Prompt suggestions')}</div>
<button
class="p-1 px-3 text-xs flex rounded transition"
type="button"
on:click={() => {
if (
info.meta.suggestion_prompts.length === 0 ||
info.meta.suggestion_prompts.at(-1).content !== ''
) {
info.meta.suggestion_prompts = [...info.meta.suggestion_prompts, { content: '' }];
}
}}
2024-05-15 08:09:32 +00:00
>
2024-05-25 01:26:36 +00:00
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 20 20"
fill="currentColor"
class="w-4 h-4"
>
<path
d="M10.75 4.75a.75.75 0 00-1.5 0v4.5h-4.5a.75.75 0 000 1.5h4.5v4.5a.75.75 0 001.5 0v-4.5h4.5a.75.75 0 000-1.5h-4.5v-4.5z"
/>
</svg>
</button>
2023-12-04 03:13:42 +00:00
</div>
2024-05-25 01:26:36 +00:00
<div class="flex flex-col space-y-1">
{#each info.meta.suggestion_prompts as prompt, promptIdx}
<div class=" flex border dark:border-gray-600 rounded-lg">
<input
class="px-3 py-1.5 text-sm w-full bg-transparent outline-none border-r dark:border-gray-600"
placeholder={$i18n.t('Write a prompt suggestion (e.g. Who are you?)')}
bind:value={prompt.content}
/>
<button
class="px-2"
type="button"
on:click={() => {
info.meta.suggestion_prompts.splice(promptIdx, 1);
info.meta.suggestion_prompts = info.meta.suggestion_prompts;
}}
>
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 20 20"
fill="currentColor"
class="w-4 h-4"
>
<path
d="M6.28 5.22a.75.75 0 00-1.06 1.06L8.94 10l-3.72 3.72a.75.75 0 101.06 1.06L10 11.06l3.72 3.72a.75.75 0 101.06-1.06L11.06 10l3.72-3.72a.75.75 0 00-1.06-1.06L10 8.94 6.28 5.22z"
/>
</svg>
</button>
</div>
{/each}
2024-05-15 08:09:32 +00:00
</div>
</div>
2024-05-25 03:29:13 +00:00
<div class="my-2 text-gray-500">
<div class="flex w-full justify-between mb-2">
<div class=" self-center text-sm font-semibold">{$i18n.t('JSON Preview')}</div>
<button
class="p-1 px-3 text-xs flex rounded transition"
type="button"
on:click={() => {
showPreview = !showPreview;
}}
>
{#if showPreview}
<span class="ml-2 self-center">{$i18n.t('Hide')}</span>
{:else}
<span class="ml-2 self-center">{$i18n.t('Show')}</span>
{/if}
</button>
</div>
{#if showPreview}
<div>
<textarea
class="px-3 py-1.5 text-sm w-full bg-transparent border dark:border-gray-600 outline-none rounded-lg"
rows="10"
value={JSON.stringify(info, null, 2)}
disabled
readonly
/>
</div>
{/if}
</div>
2024-05-25 01:26:36 +00:00
{#if pullProgress !== null}
<div class="my-2">
<div class=" text-sm font-semibold mb-2">{$i18n.t('Pull Progress')}</div>
<div class="w-full rounded-full dark:bg-gray-800">
<div
class="dark:bg-gray-600 text-xs font-medium text-blue-100 text-center p-0.5 leading-none rounded-full"
style="width: {Math.max(15, pullProgress ?? 0)}%"
2024-05-15 08:09:32 +00:00
>
2024-05-25 01:26:36 +00:00
{pullProgress ?? 0}%
</div>
2023-12-04 03:13:42 +00:00
</div>
2024-05-25 01:26:36 +00:00
<div class="mt-1 text-xs dark:text-gray-500" style="font-size: 0.5rem;">
{digest}
</div>
</div>
{/if}
<div class="my-2 flex justify-end">
<button
class=" text-sm px-3 py-2 transition rounded-xl {loading
? ' cursor-not-allowed bg-gray-100 dark:bg-gray-800'
: ' bg-gray-50 hover:bg-gray-100 dark:bg-gray-700 dark:hover:bg-gray-800'} flex"
type="submit"
disabled={loading}
>
<div class=" self-center font-medium">{$i18n.t('Save & Update')}</div>
{#if loading}
<div class="ml-1.5 self-center">
<svg
class=" w-4 h-4"
viewBox="0 0 24 24"
fill="currentColor"
xmlns="http://www.w3.org/2000/svg"
><style>
.spinner_ajPY {
transform-origin: center;
animation: spinner_AtaB 0.75s infinite linear;
}
@keyframes spinner_AtaB {
100% {
transform: rotate(360deg);
}
}
</style><path
d="M12,1A11,11,0,1,0,23,12,11,11,0,0,0,12,1Zm0,19a8,8,0,1,1,8-8A8,8,0,0,1,12,20Z"
opacity=".25"
/><path
d="M10.14,1.16a11,11,0,0,0-9,8.92A1.59,1.59,0,0,0,2.46,12,1.52,1.52,0,0,0,4.11,10.7a8,8,0,0,1,6.66-6.61A1.42,1.42,0,0,0,12,2.69h0A1.57,1.57,0,0,0,10.14,1.16Z"
class="spinner_ajPY"
/></svg
>
</div>
{/if}
</button>
</div>
</form>
{/if}
2023-12-04 03:13:42 +00:00
</div>