2024-03-03 22:42:01 +00:00
|
|
|
<script lang="ts">
|
2024-10-21 12:09:28 +00:00
|
|
|
import { toast } from 'svelte-sonner';
|
2024-03-02 11:01:44 +00:00
|
|
|
|
2024-10-21 12:09:28 +00:00
|
|
|
import { goto } from '$app/navigation';
|
2024-03-05 10:44:37 +00:00
|
|
|
import { onMount, tick, getContext } from 'svelte';
|
2024-03-02 11:01:44 +00:00
|
|
|
|
2024-10-21 12:09:28 +00:00
|
|
|
import {
|
|
|
|
OLLAMA_API_BASE_URL,
|
|
|
|
OPENAI_API_BASE_URL,
|
|
|
|
WEBUI_API_BASE_URL,
|
|
|
|
WEBUI_BASE_URL
|
|
|
|
} from '$lib/constants';
|
2024-10-21 22:24:59 +00:00
|
|
|
import { WEBUI_NAME, config, user, models, settings } from '$lib/stores';
|
2024-03-02 11:01:44 +00:00
|
|
|
|
2024-03-03 02:16:02 +00:00
|
|
|
import { generateOpenAIChatCompletion } from '$lib/apis/openai';
|
|
|
|
|
2024-03-02 11:01:44 +00:00
|
|
|
import { splitStream } from '$lib/utils';
|
2024-10-21 22:24:59 +00:00
|
|
|
import Collapsible from '../common/Collapsible.svelte';
|
|
|
|
|
2024-10-21 22:43:15 +00:00
|
|
|
import Messages from '$lib/components/playground/Chat/Messages.svelte';
|
2024-10-21 22:24:59 +00:00
|
|
|
import ChevronUp from '../icons/ChevronUp.svelte';
|
|
|
|
import ChevronDown from '../icons/ChevronDown.svelte';
|
|
|
|
import Pencil from '../icons/Pencil.svelte';
|
|
|
|
import Cog6 from '../icons/Cog6.svelte';
|
|
|
|
import Sidebar from '../common/Sidebar.svelte';
|
|
|
|
import ArrowRight from '../icons/ArrowRight.svelte';
|
2024-03-02 11:01:44 +00:00
|
|
|
|
2024-03-05 10:44:37 +00:00
|
|
|
const i18n = getContext('i18n');
|
|
|
|
|
2024-03-02 11:01:44 +00:00
|
|
|
let loaded = false;
|
|
|
|
|
2024-03-03 02:16:02 +00:00
|
|
|
let selectedModelId = '';
|
2024-03-02 11:01:44 +00:00
|
|
|
let loading = false;
|
|
|
|
let stopResponseFlag = false;
|
|
|
|
|
2024-03-03 22:42:01 +00:00
|
|
|
let messagesContainerElement: HTMLDivElement;
|
|
|
|
|
2024-10-21 22:24:59 +00:00
|
|
|
let showSystem = false;
|
|
|
|
let showSettings = false;
|
|
|
|
|
2024-03-03 01:11:48 +00:00
|
|
|
let system = '';
|
2024-10-21 22:24:59 +00:00
|
|
|
|
|
|
|
let role = 'user';
|
|
|
|
let message = '';
|
|
|
|
|
|
|
|
let messages = [];
|
2024-03-03 01:11:48 +00:00
|
|
|
|
2024-03-02 11:01:44 +00:00
|
|
|
const scrollToBottom = () => {
|
2024-10-21 12:09:28 +00:00
|
|
|
const element = messagesContainerElement;
|
2024-03-03 02:16:02 +00:00
|
|
|
|
|
|
|
if (element) {
|
|
|
|
element.scrollTop = element?.scrollHeight;
|
|
|
|
}
|
2024-03-02 11:01:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const stopResponse = () => {
|
|
|
|
stopResponseFlag = true;
|
|
|
|
console.log('stopResponse');
|
|
|
|
};
|
|
|
|
|
2024-03-03 02:16:02 +00:00
|
|
|
const chatCompletionHandler = async () => {
|
|
|
|
const model = $models.find((model) => model.id === selectedModelId);
|
|
|
|
|
2024-04-20 20:11:02 +00:00
|
|
|
const [res, controller] = await generateOpenAIChatCompletion(
|
2024-03-03 02:16:02 +00:00
|
|
|
localStorage.token,
|
|
|
|
{
|
|
|
|
model: model.id,
|
|
|
|
stream: true,
|
|
|
|
messages: [
|
|
|
|
system
|
|
|
|
? {
|
|
|
|
role: 'system',
|
|
|
|
content: system
|
2024-08-13 10:12:35 +00:00
|
|
|
}
|
2024-03-03 02:16:02 +00:00
|
|
|
: undefined,
|
|
|
|
...messages
|
|
|
|
].filter((message) => message)
|
|
|
|
},
|
2024-10-21 12:09:28 +00:00
|
|
|
`${WEBUI_BASE_URL}/api`
|
2024-03-03 02:16:02 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
let responseMessage;
|
|
|
|
if (messages.at(-1)?.role === 'assistant') {
|
|
|
|
responseMessage = messages.at(-1);
|
|
|
|
} else {
|
|
|
|
responseMessage = {
|
|
|
|
role: 'assistant',
|
|
|
|
content: ''
|
|
|
|
};
|
|
|
|
messages.push(responseMessage);
|
|
|
|
messages = messages;
|
|
|
|
}
|
|
|
|
|
|
|
|
await tick();
|
|
|
|
const textareaElement = document.getElementById(`assistant-${messages.length - 1}-textarea`);
|
|
|
|
|
|
|
|
if (res && res.ok) {
|
|
|
|
const reader = res.body
|
|
|
|
.pipeThrough(new TextDecoderStream())
|
|
|
|
.pipeThrough(splitStream('\n'))
|
|
|
|
.getReader();
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
const { value, done } = await reader.read();
|
|
|
|
if (done || stopResponseFlag) {
|
2024-03-05 09:41:22 +00:00
|
|
|
if (stopResponseFlag) {
|
2024-04-20 20:11:02 +00:00
|
|
|
controller.abort('User: Stop Response');
|
2024-03-05 09:41:22 +00:00
|
|
|
}
|
2024-03-03 02:16:02 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
let lines = value.split('\n');
|
|
|
|
|
|
|
|
for (const line of lines) {
|
|
|
|
if (line !== '') {
|
|
|
|
console.log(line);
|
|
|
|
if (line === 'data: [DONE]') {
|
|
|
|
// responseMessage.done = true;
|
|
|
|
messages = messages;
|
|
|
|
} else {
|
|
|
|
let data = JSON.parse(line.replace(/^data: /, ''));
|
|
|
|
console.log(data);
|
|
|
|
|
2024-06-10 00:38:08 +00:00
|
|
|
if (responseMessage.content == '' && data.choices[0].delta.content == '\n') {
|
|
|
|
continue;
|
2024-03-03 02:16:02 +00:00
|
|
|
} else {
|
2024-06-10 00:38:08 +00:00
|
|
|
textareaElement.style.height = textareaElement.scrollHeight + 'px';
|
2024-03-02 11:01:44 +00:00
|
|
|
|
2024-06-10 00:38:08 +00:00
|
|
|
responseMessage.content += data.choices[0].delta.content ?? '';
|
|
|
|
messages = messages;
|
2024-03-03 02:16:02 +00:00
|
|
|
|
2024-06-10 00:38:08 +00:00
|
|
|
textareaElement.style.height = textareaElement.scrollHeight + 'px';
|
2024-03-03 02:16:02 +00:00
|
|
|
|
2024-06-10 00:38:08 +00:00
|
|
|
await tick();
|
2024-03-03 02:16:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.log(error);
|
2024-03-02 11:01:44 +00:00
|
|
|
}
|
2024-03-03 02:16:02 +00:00
|
|
|
|
|
|
|
scrollToBottom();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-10-21 22:24:59 +00:00
|
|
|
const addHandler = async () => {
|
|
|
|
if (message) {
|
|
|
|
messages.push({
|
|
|
|
role: role,
|
|
|
|
content: message
|
|
|
|
});
|
|
|
|
messages = messages;
|
|
|
|
message = '';
|
|
|
|
await tick();
|
|
|
|
scrollToBottom();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-03-03 02:16:02 +00:00
|
|
|
const submitHandler = async () => {
|
|
|
|
if (selectedModelId) {
|
2024-10-21 22:37:50 +00:00
|
|
|
await addHandler();
|
|
|
|
|
2024-03-03 02:16:02 +00:00
|
|
|
loading = true;
|
2024-10-21 12:09:28 +00:00
|
|
|
await chatCompletionHandler();
|
2024-03-02 11:01:44 +00:00
|
|
|
|
|
|
|
loading = false;
|
|
|
|
stopResponseFlag = false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
onMount(async () => {
|
|
|
|
if ($user?.role !== 'admin') {
|
|
|
|
await goto('/');
|
|
|
|
}
|
|
|
|
|
2024-03-03 01:11:48 +00:00
|
|
|
if ($settings?.models) {
|
2024-03-03 02:16:02 +00:00
|
|
|
selectedModelId = $settings?.models[0];
|
2024-03-03 01:11:48 +00:00
|
|
|
} else if ($config?.default_models) {
|
2024-03-03 02:16:02 +00:00
|
|
|
selectedModelId = $config?.default_models.split(',')[0];
|
2024-03-03 01:11:48 +00:00
|
|
|
} else {
|
2024-03-03 02:16:02 +00:00
|
|
|
selectedModelId = '';
|
2024-03-03 01:11:48 +00:00
|
|
|
}
|
2024-03-02 11:01:44 +00:00
|
|
|
loaded = true;
|
|
|
|
});
|
|
|
|
</script>
|
|
|
|
|
2024-05-15 06:16:22 +00:00
|
|
|
<div class=" flex flex-col justify-between w-full overflow-y-auto h-full">
|
2024-10-21 22:24:59 +00:00
|
|
|
<div class="mx-auto w-full md:px-0 h-full relative">
|
2024-10-22 04:50:34 +00:00
|
|
|
<Sidebar bind:show={showSettings} className=" bg-white dark:bg-gray-900" width="300px">
|
2024-10-21 22:24:59 +00:00
|
|
|
<div class="flex flex-col px-5 py-3 text-sm">
|
|
|
|
<div class="flex justify-between items-center mb-2">
|
|
|
|
<div class=" font-medium text-base">Settings</div>
|
|
|
|
|
2024-10-21 22:30:30 +00:00
|
|
|
<div class=" translate-x-1.5">
|
|
|
|
<button
|
|
|
|
class="p-1.5 bg-transparent hover:bg-white/5 transition rounded-lg"
|
|
|
|
on:click={() => {
|
|
|
|
showSettings = !showSettings;
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<ArrowRight className="size-3" strokeWidth="2.5" />
|
2024-10-21 22:24:59 +00:00
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
2024-10-22 04:50:34 +00:00
|
|
|
<div class="mt-1">
|
2024-10-21 22:24:59 +00:00
|
|
|
<div>
|
|
|
|
<div class=" text-xs font-medium mb-1">Model</div>
|
|
|
|
|
|
|
|
<div class="w-full">
|
2024-10-21 22:30:30 +00:00
|
|
|
<select
|
2024-10-22 04:50:34 +00:00
|
|
|
class="w-full bg-transparent border border-gray-50 dark:border-gray-850 rounded-lg py-1 px-2 -mx-0.5 text-sm outline-none"
|
2024-10-21 22:24:59 +00:00
|
|
|
bind:value={selectedModelId}
|
2024-10-21 22:30:30 +00:00
|
|
|
>
|
|
|
|
{#each $models as model}
|
2024-10-23 09:17:10 +00:00
|
|
|
<option value={model.id} class="bg-gray-50 dark:bg-gray-700">{model.name}</option>
|
2024-10-21 22:30:30 +00:00
|
|
|
{/each}
|
|
|
|
</select>
|
2024-03-28 20:49:53 +00:00
|
|
|
</div>
|
2024-10-21 06:53:36 +00:00
|
|
|
</div>
|
2024-03-02 11:01:44 +00:00
|
|
|
</div>
|
2024-05-15 06:16:22 +00:00
|
|
|
</div>
|
2024-10-21 22:24:59 +00:00
|
|
|
</Sidebar>
|
|
|
|
|
|
|
|
<div class=" flex flex-col h-full px-4 py-1">
|
|
|
|
<div class="flex w-full items-start gap-1.5">
|
|
|
|
<Collapsible
|
|
|
|
className="w-full flex-1"
|
|
|
|
bind:open={showSystem}
|
|
|
|
buttonClassName="w-full rounded-lg text-sm border border-gray-50 dark:border-gray-850 w-full py-1 px-1.5"
|
|
|
|
grow={true}
|
|
|
|
>
|
|
|
|
<div class="flex gap-2 justify-between items-center">
|
|
|
|
<div class=" flex-shrink-0 font-medium ml-1.5">
|
|
|
|
{$i18n.t('System Instructions')}
|
|
|
|
</div>
|
|
|
|
|
|
|
|
{#if !showSystem}
|
|
|
|
<div class=" flex-1 text-gray-500 line-clamp-1">
|
|
|
|
{system}
|
|
|
|
</div>
|
|
|
|
{/if}
|
|
|
|
|
|
|
|
<div class="flex-shrink-0">
|
|
|
|
<button class="p-1.5 bg-transparent hover:bg-white/5 transition rounded-lg">
|
|
|
|
{#if showSystem}
|
|
|
|
<ChevronUp className="size-3.5" />
|
|
|
|
{:else}
|
|
|
|
<Pencil className="size-3.5" />
|
|
|
|
{/if}
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</div>
|
2024-03-02 11:01:44 +00:00
|
|
|
|
2024-10-21 22:24:59 +00:00
|
|
|
<div slot="content">
|
|
|
|
<div class="pt-1 px-1.5">
|
|
|
|
<textarea
|
|
|
|
id="system-textarea"
|
|
|
|
class="w-full h-full bg-transparent resize-none outline-none text-sm"
|
|
|
|
bind:value={system}
|
|
|
|
placeholder={$i18n.t("You're a helpful assistant.")}
|
|
|
|
rows="4"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</Collapsible>
|
|
|
|
|
|
|
|
<div class="translate-y-1">
|
|
|
|
<button
|
|
|
|
class="p-1.5 bg-transparent hover:bg-white/5 transition rounded-lg"
|
|
|
|
on:click={() => {
|
|
|
|
showSettings = !showSettings;
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Cog6 />
|
|
|
|
</button>
|
2024-05-15 06:16:22 +00:00
|
|
|
</div>
|
2024-10-21 12:09:28 +00:00
|
|
|
</div>
|
2024-10-21 22:24:59 +00:00
|
|
|
|
2024-05-15 06:16:22 +00:00
|
|
|
<div
|
|
|
|
class=" pb-2.5 flex flex-col justify-between w-full flex-auto overflow-auto h-0"
|
|
|
|
id="messages-container"
|
|
|
|
bind:this={messagesContainerElement}
|
|
|
|
>
|
|
|
|
<div class=" h-full w-full flex flex-col">
|
|
|
|
<div class="flex-1 p-1">
|
2024-10-21 22:24:59 +00:00
|
|
|
<Messages bind:messages />
|
2024-03-02 11:01:44 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
2024-05-15 06:16:22 +00:00
|
|
|
</div>
|
2024-03-03 01:11:48 +00:00
|
|
|
|
2024-10-21 22:24:59 +00:00
|
|
|
<div class="pb-3">
|
2024-10-22 04:50:34 +00:00
|
|
|
<div class="text-xs font-medium text-gray-500 px-2 py-1">
|
|
|
|
{selectedModelId}
|
|
|
|
</div>
|
2024-10-21 22:24:59 +00:00
|
|
|
<div class="border border-gray-50 dark:border-gray-850 w-full px-3 py-2.5 rounded-xl">
|
|
|
|
<div class="py-0.5">
|
|
|
|
<!-- $i18n.t('a user') -->
|
|
|
|
<!-- $i18n.t('an assistant') -->
|
|
|
|
<textarea
|
|
|
|
bind:value={message}
|
|
|
|
class=" w-full h-full bg-transparent resize-none outline-none text-sm"
|
|
|
|
placeholder={$i18n.t(`Enter {{role}} message here`, {
|
|
|
|
role: role === 'user' ? $i18n.t('a user') : $i18n.t('an assistant')
|
|
|
|
})}
|
|
|
|
on:input={(e) => {
|
|
|
|
e.target.style.height = '';
|
|
|
|
e.target.style.height = Math.min(e.target.scrollHeight, 150) + 'px';
|
|
|
|
}}
|
|
|
|
on:focus={(e) => {
|
|
|
|
e.target.style.height = '';
|
|
|
|
e.target.style.height = Math.min(e.target.scrollHeight, 150) + 'px';
|
|
|
|
}}
|
|
|
|
rows="2"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div class="flex justify-between">
|
|
|
|
<div>
|
|
|
|
<button
|
|
|
|
class="px-3.5 py-1.5 text-sm font-medium bg-gray-50 hover:bg-gray-100 text-gray-900 dark:bg-gray-850 dark:hover:bg-gray-800 dark:text-gray-200 transition rounded-lg"
|
|
|
|
on:click={() => {
|
|
|
|
role = role === 'user' ? 'assistant' : 'user';
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{#if role === 'user'}
|
|
|
|
{$i18n.t('User')}
|
|
|
|
{:else}
|
|
|
|
{$i18n.t('Assistant')}
|
|
|
|
{/if}
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div>
|
|
|
|
{#if !loading}
|
|
|
|
<button
|
|
|
|
disabled={message === ''}
|
|
|
|
class="px-3.5 py-1.5 text-sm font-medium disabled:bg-gray-50 dark:disabled:hover:bg-gray-850 disabled:cursor-not-allowed bg-gray-50 hover:bg-gray-100 text-gray-900 dark:bg-gray-850 dark:hover:bg-gray-800 dark:text-gray-200 transition rounded-lg"
|
|
|
|
on:click={() => {
|
|
|
|
addHandler();
|
2024-10-21 22:37:50 +00:00
|
|
|
role = role === 'user' ? 'assistant' : 'user';
|
2024-10-21 22:24:59 +00:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
{$i18n.t('Add')}
|
|
|
|
</button>
|
|
|
|
|
|
|
|
<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-lg"
|
|
|
|
on:click={() => {
|
|
|
|
submitHandler();
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{$i18n.t('Run')}
|
|
|
|
</button>
|
|
|
|
{:else}
|
|
|
|
<button
|
|
|
|
class="px-3 py-1.5 text-sm font-medium bg-gray-300 text-black transition rounded-lg"
|
|
|
|
on:click={() => {
|
|
|
|
stopResponse();
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{$i18n.t('Cancel')}
|
|
|
|
</button>
|
|
|
|
{/if}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
2024-03-02 11:01:44 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|