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

84 lines
2.1 KiB
Svelte
Raw Normal View History

2023-12-03 00:05:01 +00:00
<script>
import { v4 as uuidv4 } from 'uuid';
2024-03-01 09:18:07 +00:00
import { toast } from 'svelte-sonner';
2023-12-03 00:05:01 +00:00
import { goto } from '$app/navigation';
2024-11-07 08:18:48 +00:00
import { models } from '$lib/stores';
2024-06-17 09:26:07 +00:00
import { onMount, tick, getContext } from 'svelte';
2024-11-15 10:05:43 +00:00
import { createNewModel, getModelById } from '$lib/apis/models';
2024-05-25 05:21:57 +00:00
import { getModels } from '$lib/apis';
2023-12-03 00:05:01 +00:00
2024-11-07 08:18:48 +00:00
import ModelEditor from '$lib/components/workspace/Models/ModelEditor.svelte';
2024-05-25 05:21:57 +00:00
const i18n = getContext('i18n');
2023-12-03 00:05:01 +00:00
2024-11-07 08:18:48 +00:00
const onSubmit = async (modelInfo) => {
if ($models.find((m) => m.id === modelInfo.id)) {
2023-12-04 06:21:17 +00:00
toast.error(
2024-11-07 08:18:48 +00:00
`Error: A model with the ID '${modelInfo.id}' already exists. Please select a different ID to proceed.`
2023-12-04 06:21:17 +00:00
);
2024-11-07 08:18:48 +00:00
return;
2023-12-03 00:33:53 +00:00
}
2024-11-30 08:29:27 +00:00
if (modelInfo.id === '') {
toast.error('Error: Model ID cannot be empty. Please enter a valid ID to proceed.');
return;
}
2024-11-07 08:18:48 +00:00
if (modelInfo) {
2024-11-15 10:05:43 +00:00
const res = await createNewModel(localStorage.token, {
2024-11-07 08:18:48 +00:00
...modelInfo,
2024-05-25 05:21:57 +00:00
meta: {
2024-11-07 08:18:48 +00:00
...modelInfo.meta,
profile_image_url: modelInfo.meta.profile_image_url ?? '/static/favicon.png',
suggestion_prompts: modelInfo.meta.suggestion_prompts
? modelInfo.meta.suggestion_prompts.filter((prompt) => prompt.content !== '')
2024-05-25 07:24:31 +00:00
: null
2024-05-25 05:21:57 +00:00
},
2024-11-07 08:18:48 +00:00
params: { ...modelInfo.params }
2024-11-15 11:00:18 +00:00
}).catch((error) => {
2025-01-21 06:41:32 +00:00
toast.error(`${error}`);
2024-11-15 11:00:18 +00:00
return null;
2024-05-25 05:21:57 +00:00
});
2023-12-03 01:11:52 +00:00
if (res) {
2025-02-12 09:22:53 +00:00
await 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-06-24 17:38:26 +00:00
toast.success($i18n.t('Model created successfully!'));
2024-05-25 05:21:57 +00:00
await goto('/workspace/models');
}
2023-12-03 00:05:01 +00:00
}
};
2023-12-04 07:44:12 +00:00
2024-11-07 08:18:48 +00:00
let model = null;
2024-05-25 06:42:27 +00:00
2024-02-18 23:53:54 +00:00
onMount(async () => {
2023-12-04 07:44:12 +00:00
window.addEventListener('message', async (event) => {
if (
2024-06-02 04:16:32 +00:00
!['https://openwebui.com', 'https://www.openwebui.com', 'http://localhost:5173'].includes(
event.origin
)
2023-12-04 07:44:12 +00:00
)
return;
2024-11-07 08:18:48 +00:00
model = JSON.parse(event.data);
2023-12-04 07:44:12 +00:00
});
2023-12-04 08:02:35 +00:00
if (window.opener ?? false) {
window.opener.postMessage('loaded', '*');
}
2024-02-18 23:53:54 +00:00
2024-05-25 05:21:57 +00:00
if (sessionStorage.model) {
2024-11-07 08:18:48 +00:00
model = JSON.parse(sessionStorage.model);
2024-05-25 05:21:57 +00:00
sessionStorage.removeItem('model');
2024-02-18 23:53:54 +00:00
}
2023-12-04 07:44:12 +00:00
});
2023-12-03 00:05:01 +00:00
</script>
2024-11-07 08:18:48 +00:00
{#key model}
<ModelEditor {model} {onSubmit} />
{/key}