Models.svelte 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. <script lang="ts">
  2. import { marked } from 'marked';
  3. import fileSaver from 'file-saver';
  4. const { saveAs } = fileSaver;
  5. import { onMount, getContext, tick } from 'svelte';
  6. const i18n = getContext('i18n');
  7. import { WEBUI_NAME, config, mobile, models as _models, settings, user } from '$lib/stores';
  8. import {
  9. createNewModel,
  10. deleteAllModels,
  11. getBaseModels,
  12. toggleModelById,
  13. updateModelById
  14. } from '$lib/apis/models';
  15. import { getModels } from '$lib/apis';
  16. import Search from '$lib/components/icons/Search.svelte';
  17. import Tooltip from '$lib/components/common/Tooltip.svelte';
  18. import Switch from '$lib/components/common/Switch.svelte';
  19. import Spinner from '$lib/components/common/Spinner.svelte';
  20. import ModelEditor from '$lib/components/workspace/Models/ModelEditor.svelte';
  21. import { toast } from 'svelte-sonner';
  22. import ConfirmDialog from '$lib/components/common/ConfirmDialog.svelte';
  23. import Cog6 from '$lib/components/icons/Cog6.svelte';
  24. import ConfigureModelsModal from './Models/ConfigureModelsModal.svelte';
  25. import Wrench from '$lib/components/icons/Wrench.svelte';
  26. import ArrowDownTray from '$lib/components/icons/ArrowDownTray.svelte';
  27. import ManageModelsModal from './Models/ManageModelsModal.svelte';
  28. let importFiles;
  29. let modelsImportInputElement: HTMLInputElement;
  30. let models = null;
  31. let workspaceModels = null;
  32. let baseModels = null;
  33. let filteredModels = [];
  34. let selectedModelId = null;
  35. let showConfigModal = false;
  36. let showManageModal = false;
  37. $: if (models) {
  38. filteredModels = models
  39. .filter((m) => searchValue === '' || m.name.toLowerCase().includes(searchValue.toLowerCase()))
  40. .sort((a, b) => {
  41. // // Check if either model is inactive and push them to the bottom
  42. // if ((a.is_active ?? true) !== (b.is_active ?? true)) {
  43. // return (b.is_active ?? true) - (a.is_active ?? true);
  44. // }
  45. // If both models' active states are the same, sort alphabetically
  46. return a.name.localeCompare(b.name);
  47. });
  48. }
  49. let searchValue = '';
  50. const downloadModels = async (models) => {
  51. let blob = new Blob([JSON.stringify(models)], {
  52. type: 'application/json'
  53. });
  54. saveAs(blob, `models-export-${Date.now()}.json`);
  55. };
  56. const init = async () => {
  57. workspaceModels = await getBaseModels(localStorage.token);
  58. baseModels = await getModels(localStorage.token, null, true);
  59. models = baseModels.map((m) => {
  60. const workspaceModel = workspaceModels.find((wm) => wm.id === m.id);
  61. if (workspaceModel) {
  62. return {
  63. ...m,
  64. ...workspaceModel
  65. };
  66. } else {
  67. return {
  68. ...m,
  69. id: m.id,
  70. name: m.name,
  71. is_active: true
  72. };
  73. }
  74. });
  75. };
  76. const upsertModelHandler = async (model) => {
  77. model.base_model_id = null;
  78. if (workspaceModels.find((m) => m.id === model.id)) {
  79. const res = await updateModelById(localStorage.token, model.id, model).catch((error) => {
  80. return null;
  81. });
  82. if (res) {
  83. toast.success($i18n.t('Model updated successfully'));
  84. }
  85. } else {
  86. const res = await createNewModel(localStorage.token, model).catch((error) => {
  87. return null;
  88. });
  89. if (res) {
  90. toast.success($i18n.t('Model updated successfully'));
  91. }
  92. }
  93. _models.set(
  94. await getModels(
  95. localStorage.token,
  96. $config?.features?.enable_direct_connections && ($settings?.directConnections ?? null)
  97. )
  98. );
  99. await init();
  100. };
  101. const toggleModelHandler = async (model) => {
  102. if (!Object.keys(model).includes('base_model_id')) {
  103. await createNewModel(localStorage.token, {
  104. id: model.id,
  105. name: model.name,
  106. base_model_id: null,
  107. meta: {},
  108. params: {},
  109. access_control: {},
  110. is_active: model.is_active
  111. }).catch((error) => {
  112. return null;
  113. });
  114. } else {
  115. await toggleModelById(localStorage.token, model.id);
  116. }
  117. // await init();
  118. _models.set(
  119. await getModels(
  120. localStorage.token,
  121. $config?.features?.enable_direct_connections && ($settings?.directConnections ?? null)
  122. )
  123. );
  124. };
  125. onMount(async () => {
  126. init();
  127. });
  128. </script>
  129. <ConfigureModelsModal bind:show={showConfigModal} initHandler={init} />
  130. <ManageModelsModal bind:show={showManageModal} />
  131. {#if models !== null}
  132. {#if selectedModelId === null}
  133. <div class="flex flex-col gap-1 mt-1.5 mb-2">
  134. <div class="flex justify-between items-center">
  135. <div class="flex items-center md:self-center text-xl font-medium px-0.5">
  136. {$i18n.t('Models')}
  137. <div class="flex self-center w-[1px] h-6 mx-2.5 bg-gray-50 dark:bg-gray-850" />
  138. <span class="text-lg font-medium text-gray-500 dark:text-gray-300"
  139. >{filteredModels.length}</span
  140. >
  141. </div>
  142. <div class="flex items-center gap-1.5">
  143. <Tooltip content={$i18n.t('Manage Models')}>
  144. <button
  145. class=" p-1 rounded-full flex gap-1 items-center"
  146. type="button"
  147. on:click={() => {
  148. showManageModal = true;
  149. }}
  150. >
  151. <ArrowDownTray />
  152. </button>
  153. </Tooltip>
  154. <Tooltip content={$i18n.t('Settings')}>
  155. <button
  156. class=" p-1 rounded-full flex gap-1 items-center"
  157. type="button"
  158. on:click={() => {
  159. showConfigModal = true;
  160. }}
  161. >
  162. <Cog6 />
  163. </button>
  164. </Tooltip>
  165. </div>
  166. </div>
  167. <div class=" flex flex-1 items-center w-full space-x-2">
  168. <div class="flex flex-1 items-center">
  169. <div class=" self-center ml-1 mr-3">
  170. <Search className="size-3.5" />
  171. </div>
  172. <input
  173. class=" w-full text-sm py-1 rounded-r-xl outline-hidden bg-transparent"
  174. bind:value={searchValue}
  175. placeholder={$i18n.t('Search Models')}
  176. />
  177. </div>
  178. </div>
  179. </div>
  180. <div class=" my-2 mb-5" id="model-list">
  181. {#if models.length > 0}
  182. {#each filteredModels as model, modelIdx (model.id)}
  183. <div
  184. class=" flex space-x-4 cursor-pointer w-full px-3 py-2 dark:hover:bg-white/5 hover:bg-black/5 rounded-lg transition"
  185. id="model-item-{model.id}"
  186. >
  187. <button
  188. class=" flex flex-1 text-left space-x-3.5 cursor-pointer w-full"
  189. type="button"
  190. on:click={() => {
  191. selectedModelId = model.id;
  192. }}
  193. >
  194. <div class=" self-center w-8">
  195. <div
  196. class=" rounded-full object-cover {(model?.is_active ?? true)
  197. ? ''
  198. : 'opacity-50 dark:opacity-50'} "
  199. >
  200. <img
  201. src={model?.meta?.profile_image_url ?? '/static/favicon.png'}
  202. alt="modelfile profile"
  203. class=" rounded-full w-full h-auto object-cover"
  204. />
  205. </div>
  206. </div>
  207. <div class=" flex-1 self-center {(model?.is_active ?? true) ? '' : 'text-gray-500'}">
  208. <Tooltip
  209. content={marked.parse(
  210. !!model?.meta?.description
  211. ? model?.meta?.description
  212. : model?.ollama?.digest
  213. ? `${model?.ollama?.digest} **(${model?.ollama?.modified_at})**`
  214. : model.id
  215. )}
  216. className=" w-fit"
  217. placement="top-start"
  218. >
  219. <div class=" font-semibold line-clamp-1">{model.name}</div>
  220. </Tooltip>
  221. <div class=" text-xs overflow-hidden text-ellipsis line-clamp-1 text-gray-500">
  222. <span class=" line-clamp-1">
  223. {!!model?.meta?.description
  224. ? model?.meta?.description
  225. : model?.ollama?.digest
  226. ? `${model.id} (${model?.ollama?.digest})`
  227. : model.id}
  228. </span>
  229. </div>
  230. </div>
  231. </button>
  232. <div class="flex flex-row gap-0.5 items-center self-center">
  233. <button
  234. class="self-center w-fit text-sm px-2 py-2 dark:text-gray-300 dark:hover:text-white hover:bg-black/5 dark:hover:bg-white/5 rounded-xl"
  235. type="button"
  236. on:click={() => {
  237. selectedModelId = model.id;
  238. }}
  239. >
  240. <svg
  241. xmlns="http://www.w3.org/2000/svg"
  242. fill="none"
  243. viewBox="0 0 24 24"
  244. stroke-width="1.5"
  245. stroke="currentColor"
  246. class="w-4 h-4"
  247. >
  248. <path
  249. stroke-linecap="round"
  250. stroke-linejoin="round"
  251. d="m16.862 4.487 1.687-1.688a1.875 1.875 0 1 1 2.652 2.652L6.832 19.82a4.5 4.5 0 0 1-1.897 1.13l-2.685.8.8-2.685a4.5 4.5 0 0 1 1.13-1.897L16.863 4.487Zm0 0L19.5 7.125"
  252. />
  253. </svg>
  254. </button>
  255. <div class="ml-1">
  256. <Tooltip
  257. content={(model?.is_active ?? true) ? $i18n.t('Enabled') : $i18n.t('Disabled')}
  258. >
  259. <Switch
  260. bind:state={model.is_active}
  261. on:change={async () => {
  262. toggleModelHandler(model);
  263. }}
  264. />
  265. </Tooltip>
  266. </div>
  267. </div>
  268. </div>
  269. {/each}
  270. {:else}
  271. <div class="flex flex-col items-center justify-center w-full h-20">
  272. <div class="text-gray-500 dark:text-gray-400 text-xs">
  273. {$i18n.t('No models found')}
  274. </div>
  275. </div>
  276. {/if}
  277. </div>
  278. {#if $user?.role === 'admin'}
  279. <div class=" flex justify-end w-full mb-3">
  280. <div class="flex space-x-1">
  281. <input
  282. id="models-import-input"
  283. bind:this={modelsImportInputElement}
  284. bind:files={importFiles}
  285. type="file"
  286. accept=".json"
  287. hidden
  288. on:change={() => {
  289. console.log(importFiles);
  290. let reader = new FileReader();
  291. reader.onload = async (event) => {
  292. let savedModels = JSON.parse(event.target.result);
  293. console.log(savedModels);
  294. for (const model of savedModels) {
  295. if (Object.keys(model).includes('base_model_id')) {
  296. if (model.base_model_id === null) {
  297. upsertModelHandler(model);
  298. }
  299. } else {
  300. if (model?.info ?? false) {
  301. if (model.info.base_model_id === null) {
  302. upsertModelHandler(model.info);
  303. }
  304. }
  305. }
  306. }
  307. await _models.set(
  308. await getModels(
  309. localStorage.token,
  310. $config?.features?.enable_direct_connections &&
  311. ($settings?.directConnections ?? null)
  312. )
  313. );
  314. init();
  315. };
  316. reader.readAsText(importFiles[0]);
  317. }}
  318. />
  319. <button
  320. class="flex text-xs items-center space-x-1 px-3 py-1.5 rounded-xl bg-gray-50 hover:bg-gray-100 dark:bg-gray-800 dark:hover:bg-gray-700 dark:text-gray-200 transition"
  321. on:click={() => {
  322. modelsImportInputElement.click();
  323. }}
  324. >
  325. <div class=" self-center mr-2 font-medium line-clamp-1">
  326. {$i18n.t('Import Presets')}
  327. </div>
  328. <div class=" self-center">
  329. <svg
  330. xmlns="http://www.w3.org/2000/svg"
  331. viewBox="0 0 16 16"
  332. fill="currentColor"
  333. class="w-3.5 h-3.5"
  334. >
  335. <path
  336. fill-rule="evenodd"
  337. d="M4 2a1.5 1.5 0 0 0-1.5 1.5v9A1.5 1.5 0 0 0 4 14h8a1.5 1.5 0 0 0 1.5-1.5V6.621a1.5 1.5 0 0 0-.44-1.06L9.94 2.439A1.5 1.5 0 0 0 8.878 2H4Zm4 9.5a.75.75 0 0 1-.75-.75V8.06l-.72.72a.75.75 0 0 1-1.06-1.06l2-2a.75.75 0 0 1 1.06 0l2 2a.75.75 0 1 1-1.06 1.06l-.72-.72v2.69a.75.75 0 0 1-.75.75Z"
  338. clip-rule="evenodd"
  339. />
  340. </svg>
  341. </div>
  342. </button>
  343. <button
  344. class="flex text-xs items-center space-x-1 px-3 py-1.5 rounded-xl bg-gray-50 hover:bg-gray-100 dark:bg-gray-800 dark:hover:bg-gray-700 dark:text-gray-200 transition"
  345. on:click={async () => {
  346. downloadModels(models);
  347. }}
  348. >
  349. <div class=" self-center mr-2 font-medium line-clamp-1">
  350. {$i18n.t('Export Presets')}
  351. </div>
  352. <div class=" self-center">
  353. <svg
  354. xmlns="http://www.w3.org/2000/svg"
  355. viewBox="0 0 16 16"
  356. fill="currentColor"
  357. class="w-3.5 h-3.5"
  358. >
  359. <path
  360. fill-rule="evenodd"
  361. d="M4 2a1.5 1.5 0 0 0-1.5 1.5v9A1.5 1.5 0 0 0 4 14h8a1.5 1.5 0 0 0 1.5-1.5V6.621a1.5 1.5 0 0 0-.44-1.06L9.94 2.439A1.5 1.5 0 0 0 8.878 2H4Zm4 3.5a.75.75 0 0 1 .75.75v2.69l.72-.72a.75.75 0 1 1 1.06 1.06l-2 2a.75.75 0 0 1-1.06 0l-2-2a.75.75 0 0 1 1.06-1.06l.72.72V6.25A.75.75 0 0 1 8 5.5Z"
  362. clip-rule="evenodd"
  363. />
  364. </svg>
  365. </div>
  366. </button>
  367. </div>
  368. </div>
  369. {/if}
  370. {:else}
  371. <ModelEditor
  372. edit
  373. model={models.find((m) => m.id === selectedModelId)}
  374. preset={false}
  375. onSubmit={(model) => {
  376. console.log(model);
  377. upsertModelHandler(model);
  378. selectedModelId = null;
  379. }}
  380. onBack={() => {
  381. selectedModelId = null;
  382. }}
  383. />
  384. {/if}
  385. {:else}
  386. <div class=" h-full w-full flex justify-center items-center">
  387. <Spinner />
  388. </div>
  389. {/if}