123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618 |
- <script lang="ts">
- import { DropdownMenu } from 'bits-ui';
- import { marked } from 'marked';
- import Fuse from 'fuse.js';
- import { flyAndScale } from '$lib/utils/transitions';
- import { createEventDispatcher, onMount, getContext, tick } from 'svelte';
- import ChevronDown from '$lib/components/icons/ChevronDown.svelte';
- import Check from '$lib/components/icons/Check.svelte';
- import Search from '$lib/components/icons/Search.svelte';
- import { deleteModel, getOllamaVersion, pullModel } from '$lib/apis/ollama';
- import {
- user,
- MODEL_DOWNLOAD_POOL,
- models,
- mobile,
- temporaryChatEnabled,
- settings,
- config
- } from '$lib/stores';
- import { toast } from 'svelte-sonner';
- import { capitalizeFirstLetter, sanitizeResponseContent, splitStream } from '$lib/utils';
- import { getModels } from '$lib/apis';
- import Tooltip from '$lib/components/common/Tooltip.svelte';
- import Switch from '$lib/components/common/Switch.svelte';
- import ChatBubbleOval from '$lib/components/icons/ChatBubbleOval.svelte';
- import { goto } from '$app/navigation';
- const i18n = getContext('i18n');
- const dispatch = createEventDispatcher();
- export let id = '';
- export let value = '';
- export let placeholder = 'Select a model';
- export let searchEnabled = true;
- export let searchPlaceholder = $i18n.t('Search a model');
- export let showTemporaryChatControl = false;
- export let items: {
- label: string;
- value: string;
- model: Model;
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- [key: string]: any;
- }[] = [];
- export let className = 'w-[32rem]';
- export let triggerClassName = 'text-lg';
- let show = false;
- let selectedModel = '';
- $: selectedModel = items.find((item) => item.value === value) ?? '';
- let searchValue = '';
- let ollamaVersion = null;
- let selectedModelIdx = 0;
- const fuse = new Fuse(
- items.map((item) => {
- const _item = {
- ...item,
- modelName: item.model?.name,
- tags: item.model?.info?.meta?.tags?.map((tag) => tag.name).join(' '),
- desc: item.model?.info?.meta?.description
- };
- return _item;
- }),
- {
- keys: ['value', 'tags', 'modelName'],
- threshold: 0.4
- }
- );
- $: filteredItems = searchValue
- ? fuse.search(searchValue).map((e) => {
- return e.item;
- })
- : items;
- const pullModelHandler = async () => {
- const sanitizedModelTag = searchValue.trim().replace(/^ollama\s+(run|pull)\s+/, '');
- console.log($MODEL_DOWNLOAD_POOL);
- if ($MODEL_DOWNLOAD_POOL[sanitizedModelTag]) {
- toast.error(
- $i18n.t(`Model '{{modelTag}}' is already in queue for downloading.`, {
- modelTag: sanitizedModelTag
- })
- );
- return;
- }
- if (Object.keys($MODEL_DOWNLOAD_POOL).length === 3) {
- toast.error(
- $i18n.t('Maximum of 3 models can be downloaded simultaneously. Please try again later.')
- );
- return;
- }
- const [res, controller] = await pullModel(localStorage.token, sanitizedModelTag, '0').catch(
- (error) => {
- toast.error(`${error}`);
- return null;
- }
- );
- if (res) {
- const reader = res.body
- .pipeThrough(new TextDecoderStream())
- .pipeThrough(splitStream('\n'))
- .getReader();
- MODEL_DOWNLOAD_POOL.set({
- ...$MODEL_DOWNLOAD_POOL,
- [sanitizedModelTag]: {
- ...$MODEL_DOWNLOAD_POOL[sanitizedModelTag],
- abortController: controller,
- reader,
- done: false
- }
- });
- while (true) {
- try {
- const { value, done } = await reader.read();
- if (done) break;
- let lines = value.split('\n');
- for (const line of lines) {
- if (line !== '') {
- let data = JSON.parse(line);
- console.log(data);
- if (data.error) {
- throw data.error;
- }
- if (data.detail) {
- throw data.detail;
- }
- if (data.status) {
- if (data.digest) {
- let downloadProgress = 0;
- if (data.completed) {
- downloadProgress = Math.round((data.completed / data.total) * 1000) / 10;
- } else {
- downloadProgress = 100;
- }
- MODEL_DOWNLOAD_POOL.set({
- ...$MODEL_DOWNLOAD_POOL,
- [sanitizedModelTag]: {
- ...$MODEL_DOWNLOAD_POOL[sanitizedModelTag],
- pullProgress: downloadProgress,
- digest: data.digest
- }
- });
- } else {
- toast.success(data.status);
- MODEL_DOWNLOAD_POOL.set({
- ...$MODEL_DOWNLOAD_POOL,
- [sanitizedModelTag]: {
- ...$MODEL_DOWNLOAD_POOL[sanitizedModelTag],
- done: data.status === 'success'
- }
- });
- }
- }
- }
- }
- } catch (error) {
- console.log(error);
- if (typeof error !== 'string') {
- error = error.message;
- }
- toast.error(`${error}`);
- // opts.callback({ success: false, error, modelName: opts.modelName });
- break;
- }
- }
- if ($MODEL_DOWNLOAD_POOL[sanitizedModelTag].done) {
- toast.success(
- $i18n.t(`Model '{{modelName}}' has been successfully downloaded.`, {
- modelName: sanitizedModelTag
- })
- );
- models.set(
- await getModels(
- localStorage.token,
- $config?.features?.enable_direct_connections && ($settings?.directConnections ?? null)
- )
- );
- } else {
- toast.error($i18n.t('Download canceled'));
- }
- delete $MODEL_DOWNLOAD_POOL[sanitizedModelTag];
- MODEL_DOWNLOAD_POOL.set({
- ...$MODEL_DOWNLOAD_POOL
- });
- }
- };
- onMount(async () => {
- ollamaVersion = await getOllamaVersion(localStorage.token).catch((error) => false);
- });
- const cancelModelPullHandler = async (model: string) => {
- const { reader, abortController } = $MODEL_DOWNLOAD_POOL[model];
- if (abortController) {
- abortController.abort();
- }
- if (reader) {
- await reader.cancel();
- delete $MODEL_DOWNLOAD_POOL[model];
- MODEL_DOWNLOAD_POOL.set({
- ...$MODEL_DOWNLOAD_POOL
- });
- await deleteModel(localStorage.token, model);
- toast.success(`${model} download has been canceled`);
- }
- };
- </script>
- <DropdownMenu.Root
- bind:open={show}
- onOpenChange={async () => {
- searchValue = '';
- selectedModelIdx = 0;
- window.setTimeout(() => document.getElementById('model-search-input')?.focus(), 0);
- }}
- closeFocus={false}
- >
- <DropdownMenu.Trigger
- class="relative w-full font-primary"
- aria-label={placeholder}
- id="model-selector-{id}-button"
- >
- <div
- class="flex w-full text-left px-0.5 outline-hidden bg-transparent truncate {triggerClassName} justify-between font-medium placeholder-gray-400 focus:outline-hidden"
- >
- {#if selectedModel}
- {selectedModel.label}
- {:else}
- {placeholder}
- {/if}
- <ChevronDown className=" self-center ml-2 size-3" strokeWidth="2.5" />
- </div>
- </DropdownMenu.Trigger>
- <DropdownMenu.Content
- class=" z-40 {$mobile
- ? `w-full`
- : `${className}`} max-w-[calc(100vw-1rem)] justify-start rounded-xl bg-white dark:bg-gray-850 dark:text-white shadow-lg outline-hidden"
- transition={flyAndScale}
- side={$mobile ? 'bottom' : 'bottom-start'}
- sideOffset={3}
- >
- <slot>
- {#if searchEnabled}
- <div class="flex items-center gap-2.5 px-5 mt-3.5 mb-3">
- <Search className="size-4" strokeWidth="2.5" />
- <input
- id="model-search-input"
- bind:value={searchValue}
- class="w-full text-sm bg-transparent outline-hidden"
- placeholder={searchPlaceholder}
- autocomplete="off"
- on:keydown={(e) => {
- if (e.code === 'Enter' && filteredItems.length > 0) {
- value = filteredItems[selectedModelIdx].value;
- show = false;
- return; // dont need to scroll on selection
- } else if (e.code === 'ArrowDown') {
- selectedModelIdx = Math.min(selectedModelIdx + 1, filteredItems.length - 1);
- } else if (e.code === 'ArrowUp') {
- selectedModelIdx = Math.max(selectedModelIdx - 1, 0);
- } else {
- // if the user types something, reset to the top selection.
- selectedModelIdx = 0;
- }
- const item = document.querySelector(`[data-arrow-selected="true"]`);
- item?.scrollIntoView({ block: 'center', inline: 'nearest', behavior: 'instant' });
- }}
- />
- </div>
- <hr class="border-gray-100 dark:border-gray-850" />
- {/if}
- <div class="px-3 my-2 max-h-64 overflow-y-auto scrollbar-hidden group">
- {#each filteredItems as item, index}
- <button
- aria-label="model-item"
- class="flex w-full text-left font-medium line-clamp-1 select-none items-center rounded-button py-2 pl-3 pr-1.5 text-sm text-gray-700 dark:text-gray-100 outline-hidden transition-all duration-75 hover:bg-gray-100 dark:hover:bg-gray-800 rounded-lg cursor-pointer data-highlighted:bg-muted {index ===
- selectedModelIdx
- ? 'bg-gray-100 dark:bg-gray-800 group-hover:bg-transparent'
- : ''}"
- data-arrow-selected={index === selectedModelIdx}
- on:click={() => {
- value = item.value;
- selectedModelIdx = index;
- show = false;
- }}
- >
- <div class="flex flex-col">
- {#if $mobile && (item?.model?.info?.meta?.tags ?? []).length > 0}
- <div class="flex gap-0.5 self-start h-full mb-1.5 -translate-x-1">
- {#each item.model?.info?.meta.tags as tag}
- <div
- class=" text-xs font-bold px-1 rounded-sm uppercase line-clamp-1 bg-gray-500/20 text-gray-700 dark:text-gray-200"
- >
- {tag.name}
- </div>
- {/each}
- </div>
- {/if}
- <div class="flex items-center gap-2">
- <div class="flex items-center min-w-fit">
- <div class="line-clamp-1">
- <div class="flex items-center min-w-fit">
- <Tooltip
- content={$user?.role === 'admin' ? (item?.value ?? '') : ''}
- placement="top-start"
- >
- <img
- src={item.model?.info?.meta?.profile_image_url ?? '/static/favicon.png'}
- alt="Model"
- class="rounded-full size-5 flex items-center mr-2"
- />
- {item.label}
- </Tooltip>
- </div>
- </div>
- {#if item.model.owned_by === 'ollama' && (item.model.ollama?.details?.parameter_size ?? '') !== ''}
- <div class="flex ml-1 items-center translate-y-[0.5px]">
- <Tooltip
- content={`${
- item.model.ollama?.details?.quantization_level
- ? item.model.ollama?.details?.quantization_level + ' '
- : ''
- }${
- item.model.ollama?.size
- ? `(${(item.model.ollama?.size / 1024 ** 3).toFixed(1)}GB)`
- : ''
- }`}
- className="self-end"
- >
- <span
- class=" text-xs font-medium text-gray-600 dark:text-gray-400 line-clamp-1"
- >{item.model.ollama?.details?.parameter_size ?? ''}</span
- >
- </Tooltip>
- </div>
- {/if}
- </div>
- <!-- {JSON.stringify(item.info)} -->
- {#if item.model?.direct}
- <Tooltip content={`${'Direct'}`}>
- <div class="translate-y-[1px]">
- <svg
- xmlns="http://www.w3.org/2000/svg"
- viewBox="0 0 16 16"
- fill="currentColor"
- class="size-3"
- >
- <path
- fill-rule="evenodd"
- d="M2 2.75A.75.75 0 0 1 2.75 2C8.963 2 14 7.037 14 13.25a.75.75 0 0 1-1.5 0c0-5.385-4.365-9.75-9.75-9.75A.75.75 0 0 1 2 2.75Zm0 4.5a.75.75 0 0 1 .75-.75 6.75 6.75 0 0 1 6.75 6.75.75.75 0 0 1-1.5 0C8 10.35 5.65 8 2.75 8A.75.75 0 0 1 2 7.25ZM3.5 11a1.5 1.5 0 1 0 0 3 1.5 1.5 0 0 0 0-3Z"
- clip-rule="evenodd"
- />
- </svg>
- </div>
- </Tooltip>
- {:else if item.model.owned_by === 'openai'}
- <Tooltip content={`${'External'}`}>
- <div class="translate-y-[1px]">
- <svg
- xmlns="http://www.w3.org/2000/svg"
- viewBox="0 0 16 16"
- fill="currentColor"
- class="size-3"
- >
- <path
- fill-rule="evenodd"
- d="M8.914 6.025a.75.75 0 0 1 1.06 0 3.5 3.5 0 0 1 0 4.95l-2 2a3.5 3.5 0 0 1-5.396-4.402.75.75 0 0 1 1.251.827 2 2 0 0 0 3.085 2.514l2-2a2 2 0 0 0 0-2.828.75.75 0 0 1 0-1.06Z"
- clip-rule="evenodd"
- />
- <path
- fill-rule="evenodd"
- d="M7.086 9.975a.75.75 0 0 1-1.06 0 3.5 3.5 0 0 1 0-4.95l2-2a3.5 3.5 0 0 1 5.396 4.402.75.75 0 0 1-1.251-.827 2 2 0 0 0-3.085-2.514l-2 2a2 2 0 0 0 0 2.828.75.75 0 0 1 0 1.06Z"
- clip-rule="evenodd"
- />
- </svg>
- </div>
- </Tooltip>
- {/if}
- {#if item.model?.info?.meta?.description}
- <Tooltip
- content={`${marked.parse(
- sanitizeResponseContent(item.model?.info?.meta?.description).replaceAll(
- '\n',
- '<br>'
- )
- )}`}
- >
- <div class=" translate-y-[1px]">
- <svg
- xmlns="http://www.w3.org/2000/svg"
- fill="none"
- viewBox="0 0 24 24"
- stroke-width="1.5"
- stroke="currentColor"
- class="w-4 h-4"
- >
- <path
- stroke-linecap="round"
- stroke-linejoin="round"
- d="m11.25 11.25.041-.02a.75.75 0 0 1 1.063.852l-.708 2.836a.75.75 0 0 0 1.063.853l.041-.021M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Zm-9-3.75h.008v.008H12V8.25Z"
- />
- </svg>
- </div>
- </Tooltip>
- {/if}
- {#if !$mobile && (item?.model?.info?.meta?.tags ?? []).length > 0}
- <div class="flex gap-0.5 self-center items-center h-full translate-y-[0.5px]">
- {#each item.model?.info?.meta.tags as tag}
- <Tooltip content={tag.name}>
- <div
- class=" text-xs font-bold px-1 rounded-sm uppercase line-clamp-1 bg-gray-500/20 text-gray-700 dark:text-gray-200"
- >
- {tag.name}
- </div>
- </Tooltip>
- {/each}
- </div>
- {/if}
- </div>
- </div>
- {#if value === item.value}
- <div class="ml-auto pl-2 pr-2 md:pr-0">
- <Check />
- </div>
- {/if}
- </button>
- {:else}
- <div>
- <div class="block px-3 py-2 text-sm text-gray-700 dark:text-gray-100">
- {$i18n.t('No results found')}
- </div>
- </div>
- {/each}
- {#if !(searchValue.trim() in $MODEL_DOWNLOAD_POOL) && searchValue && ollamaVersion && $user.role === 'admin'}
- <Tooltip
- content={$i18n.t(`Pull "{{searchValue}}" from Ollama.com`, {
- searchValue: searchValue
- })}
- placement="top-start"
- >
- <button
- class="flex w-full font-medium line-clamp-1 select-none items-center rounded-button py-2 pl-3 pr-1.5 text-sm text-gray-700 dark:text-gray-100 outline-hidden transition-all duration-75 hover:bg-gray-100 dark:hover:bg-gray-800 rounded-lg cursor-pointer data-highlighted:bg-muted"
- on:click={() => {
- pullModelHandler();
- }}
- >
- <div class=" truncate">
- {$i18n.t(`Pull "{{searchValue}}" from Ollama.com`, { searchValue: searchValue })}
- </div>
- </button>
- </Tooltip>
- {/if}
- {#each Object.keys($MODEL_DOWNLOAD_POOL) as model}
- <div
- class="flex w-full justify-between font-medium select-none rounded-button py-2 pl-3 pr-1.5 text-sm text-gray-700 dark:text-gray-100 outline-hidden transition-all duration-75 rounded-lg cursor-pointer data-highlighted:bg-muted"
- >
- <div class="flex">
- <div class="-ml-2 mr-2.5 translate-y-0.5">
- <svg
- class="size-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>
- <div class="flex flex-col self-start">
- <div class="flex gap-1">
- <div class="line-clamp-1">
- Downloading "{model}"
- </div>
- <div class="shrink-0">
- {'pullProgress' in $MODEL_DOWNLOAD_POOL[model]
- ? `(${$MODEL_DOWNLOAD_POOL[model].pullProgress}%)`
- : ''}
- </div>
- </div>
- {#if 'digest' in $MODEL_DOWNLOAD_POOL[model] && $MODEL_DOWNLOAD_POOL[model].digest}
- <div class="-mt-1 h-fit text-[0.7rem] dark:text-gray-500 line-clamp-1">
- {$MODEL_DOWNLOAD_POOL[model].digest}
- </div>
- {/if}
- </div>
- </div>
- <div class="mr-2 ml-1 translate-y-0.5">
- <Tooltip content={$i18n.t('Cancel')}>
- <button
- class="text-gray-800 dark:text-gray-100"
- on:click={() => {
- cancelModelPullHandler(model);
- }}
- >
- <svg
- class="w-4 h-4 text-gray-800 dark:text-white"
- aria-hidden="true"
- xmlns="http://www.w3.org/2000/svg"
- width="24"
- height="24"
- fill="currentColor"
- viewBox="0 0 24 24"
- >
- <path
- stroke="currentColor"
- stroke-linecap="round"
- stroke-linejoin="round"
- stroke-width="2"
- d="M6 18 17.94 6M18 18 6.06 6"
- />
- </svg>
- </button>
- </Tooltip>
- </div>
- </div>
- {/each}
- </div>
- {#if showTemporaryChatControl}
- <hr class="border-gray-100 dark:border-gray-850" />
- <div class="flex items-center mx-2 my-2">
- <button
- class="flex justify-between w-full font-medium line-clamp-1 select-none items-center rounded-button py-2 px-3 text-sm text-gray-700 dark:text-gray-100 outline-hidden transition-all duration-75 hover:bg-gray-100 dark:hover:bg-gray-800 rounded-lg cursor-pointer data-highlighted:bg-muted"
- on:click={async () => {
- temporaryChatEnabled.set(!$temporaryChatEnabled);
- await goto('/');
- const newChatButton = document.getElementById('new-chat-button');
- setTimeout(() => {
- newChatButton?.click();
- }, 0);
- // add 'temporary-chat=true' to the URL
- if ($temporaryChatEnabled) {
- history.replaceState(null, '', '?temporary-chat=true');
- } else {
- history.replaceState(null, '', location.pathname);
- }
- show = false;
- }}
- >
- <div class="flex gap-2.5 items-center">
- <ChatBubbleOval className="size-4" strokeWidth="2.5" />
- {$i18n.t(`Temporary Chat`)}
- </div>
- <div>
- <Switch state={$temporaryChatEnabled} />
- </div>
- </button>
- </div>
- {/if}
- <div class="hidden w-[42rem]" />
- <div class="hidden w-[32rem]" />
- </slot>
- </DropdownMenu.Content>
- </DropdownMenu.Root>
|