+page.svelte 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. <script>
  2. import { v4 as uuidv4 } from 'uuid';
  3. import { toast } from 'svelte-sonner';
  4. import { goto } from '$app/navigation';
  5. import { settings, user, config, models } from '$lib/stores';
  6. import { onMount, tick, getContext } from 'svelte';
  7. import { addNewModel, getModelById, getModelInfos } from '$lib/apis/models';
  8. import { getModels } from '$lib/apis';
  9. import AdvancedParams from '$lib/components/chat/Settings/Advanced/AdvancedParams.svelte';
  10. import Checkbox from '$lib/components/common/Checkbox.svelte';
  11. const i18n = getContext('i18n');
  12. let filesInputElement;
  13. let inputFiles;
  14. let showAdvanced = false;
  15. let showPreview = false;
  16. let loading = false;
  17. let success = false;
  18. // ///////////
  19. // Model
  20. // ///////////
  21. let id = '';
  22. let name = '';
  23. let params = {};
  24. let capabilities = {
  25. vision: true
  26. };
  27. let info = {
  28. id: '',
  29. base_model_id: null,
  30. name: '',
  31. meta: {
  32. profile_image_url: null,
  33. description: '',
  34. suggestion_prompts: [
  35. {
  36. content: ''
  37. }
  38. ]
  39. },
  40. params: {
  41. system: ''
  42. }
  43. };
  44. $: if (name) {
  45. id = name.replace(/\s+/g, '-').toLowerCase();
  46. }
  47. let baseModel = null;
  48. $: {
  49. baseModel = $models.find((m) => m.id === info.base_model_id);
  50. console.log(baseModel);
  51. if (baseModel) {
  52. if (baseModel.owned_by === 'openai') {
  53. capabilities.usage = baseModel.info?.meta?.capabilities?.usage ?? false;
  54. } else {
  55. delete capabilities.usage;
  56. }
  57. capabilities = capabilities;
  58. }
  59. }
  60. const submitHandler = async () => {
  61. loading = true;
  62. info.id = id;
  63. info.name = name;
  64. info.meta.capabilities = capabilities;
  65. info.params.stop = params.stop ? params.stop.split(',').filter((s) => s.trim()) : null;
  66. if ($models.find((m) => m.id === info.id)) {
  67. toast.error(
  68. `Error: A model with the ID '${info.id}' already exists. Please select a different ID to proceed.`
  69. );
  70. loading = false;
  71. success = false;
  72. return success;
  73. }
  74. if (info) {
  75. const res = await addNewModel(localStorage.token, {
  76. ...info,
  77. meta: {
  78. ...info.meta,
  79. profile_image_url: info.meta.profile_image_url ?? '/favicon.png',
  80. suggestion_prompts: info.meta.suggestion_prompts
  81. ? info.meta.suggestion_prompts.filter((prompt) => prompt.content !== '')
  82. : null
  83. },
  84. params: { ...info.params, ...params }
  85. });
  86. if (res) {
  87. toast.success('Model created successfully!');
  88. await goto('/workspace/models');
  89. await models.set(await getModels(localStorage.token));
  90. }
  91. }
  92. loading = false;
  93. success = false;
  94. };
  95. const initModel = async (model) => {
  96. name = model.name;
  97. await tick();
  98. id = model.id;
  99. params = { ...params, ...model?.info?.params };
  100. params.stop = params?.stop ? (params?.stop ?? []).join(',') : null;
  101. capabilities = { ...capabilities, ...(model?.info?.meta?.capabilities ?? {}) };
  102. info = {
  103. ...info,
  104. ...model.info
  105. };
  106. };
  107. onMount(async () => {
  108. window.addEventListener('message', async (event) => {
  109. if (
  110. ![
  111. 'https://ollamahub.com',
  112. 'https://www.ollamahub.com',
  113. 'https://openwebui.com',
  114. 'https://www.openwebui.com',
  115. 'http://localhost:5173'
  116. ].includes(event.origin)
  117. )
  118. return;
  119. const model = JSON.parse(event.data);
  120. console.log(model);
  121. initModel(model);
  122. });
  123. if (window.opener ?? false) {
  124. window.opener.postMessage('loaded', '*');
  125. }
  126. if (sessionStorage.model) {
  127. const model = JSON.parse(sessionStorage.model);
  128. sessionStorage.removeItem('model');
  129. console.log(model);
  130. initModel(model);
  131. }
  132. });
  133. </script>
  134. <div class="w-full max-h-full">
  135. <input
  136. bind:this={filesInputElement}
  137. bind:files={inputFiles}
  138. type="file"
  139. hidden
  140. accept="image/*"
  141. on:change={() => {
  142. let reader = new FileReader();
  143. reader.onload = (event) => {
  144. let originalImageUrl = `${event.target.result}`;
  145. const img = new Image();
  146. img.src = originalImageUrl;
  147. img.onload = function () {
  148. const canvas = document.createElement('canvas');
  149. const ctx = canvas.getContext('2d');
  150. // Calculate the aspect ratio of the image
  151. const aspectRatio = img.width / img.height;
  152. // Calculate the new width and height to fit within 100x100
  153. let newWidth, newHeight;
  154. if (aspectRatio > 1) {
  155. newWidth = 100 * aspectRatio;
  156. newHeight = 100;
  157. } else {
  158. newWidth = 100;
  159. newHeight = 100 / aspectRatio;
  160. }
  161. // Set the canvas size
  162. canvas.width = 100;
  163. canvas.height = 100;
  164. // Calculate the position to center the image
  165. const offsetX = (100 - newWidth) / 2;
  166. const offsetY = (100 - newHeight) / 2;
  167. // Draw the image on the canvas
  168. ctx.drawImage(img, offsetX, offsetY, newWidth, newHeight);
  169. // Get the base64 representation of the compressed image
  170. const compressedSrc = canvas.toDataURL('image/jpeg');
  171. // Display the compressed image
  172. info.meta.profile_image_url = compressedSrc;
  173. inputFiles = null;
  174. };
  175. };
  176. if (
  177. inputFiles &&
  178. inputFiles.length > 0 &&
  179. ['image/gif', 'image/webp', 'image/jpeg', 'image/png'].includes(inputFiles[0]['type'])
  180. ) {
  181. reader.readAsDataURL(inputFiles[0]);
  182. } else {
  183. console.log(`Unsupported File Type '${inputFiles[0]['type']}'.`);
  184. inputFiles = null;
  185. }
  186. }}
  187. />
  188. <button
  189. class="flex space-x-1"
  190. on:click={() => {
  191. history.back();
  192. }}
  193. >
  194. <div class=" self-center">
  195. <svg
  196. xmlns="http://www.w3.org/2000/svg"
  197. viewBox="0 0 20 20"
  198. fill="currentColor"
  199. class="w-4 h-4"
  200. >
  201. <path
  202. fill-rule="evenodd"
  203. 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"
  204. clip-rule="evenodd"
  205. />
  206. </svg>
  207. </div>
  208. <div class=" self-center font-medium text-sm">{$i18n.t('Back')}</div>
  209. </button>
  210. <!-- <hr class="my-3 dark:border-gray-700" /> -->
  211. <form
  212. class="flex flex-col max-w-2xl mx-auto mt-4 mb-10"
  213. on:submit|preventDefault={() => {
  214. submitHandler();
  215. }}
  216. >
  217. <div class="flex justify-center my-4">
  218. <div class="self-center">
  219. <button
  220. class=" {info.meta.profile_image_url
  221. ? ''
  222. : 'p-6'} rounded-full dark:bg-gray-700 border border-dashed border-gray-200"
  223. type="button"
  224. on:click={() => {
  225. filesInputElement.click();
  226. }}
  227. >
  228. {#if info.meta.profile_image_url}
  229. <img
  230. src={info.meta.profile_image_url}
  231. alt="modelfile profile"
  232. class=" rounded-full w-20 h-20 object-cover"
  233. />
  234. {:else}
  235. <svg
  236. xmlns="http://www.w3.org/2000/svg"
  237. viewBox="0 0 24 24"
  238. fill="currentColor"
  239. class="size-8"
  240. >
  241. <path
  242. fill-rule="evenodd"
  243. 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"
  244. clip-rule="evenodd"
  245. />
  246. </svg>
  247. {/if}
  248. </button>
  249. </div>
  250. </div>
  251. <div class="my-2 flex space-x-2">
  252. <div class="flex-1">
  253. <div class=" text-sm font-semibold mb-2">{$i18n.t('Name')}*</div>
  254. <div>
  255. <input
  256. class="px-3 py-1.5 text-sm w-full bg-transparent border dark:border-gray-600 outline-none rounded-lg"
  257. placeholder={$i18n.t('Name your model')}
  258. bind:value={name}
  259. required
  260. />
  261. </div>
  262. </div>
  263. <div class="flex-1">
  264. <div class=" text-sm font-semibold mb-2">{$i18n.t('Model ID')}*</div>
  265. <div>
  266. <input
  267. class="px-3 py-1.5 text-sm w-full bg-transparent border dark:border-gray-600 outline-none rounded-lg"
  268. placeholder={$i18n.t('Add a model id')}
  269. bind:value={id}
  270. required
  271. />
  272. </div>
  273. </div>
  274. </div>
  275. <div class="my-2">
  276. <div class=" text-sm font-semibold mb-2">{$i18n.t('Base Model (From)')}</div>
  277. <div>
  278. <select
  279. class="px-3 py-1.5 text-sm w-full bg-transparent border dark:border-gray-600 outline-none rounded-lg"
  280. placeholder="Select a base model (e.g. llama3, gpt-4o)"
  281. bind:value={info.base_model_id}
  282. required
  283. >
  284. <option value={null} class=" text-gray-900">{$i18n.t('Select a base model')}</option>
  285. {#each $models.filter((m) => !m?.preset) as model}
  286. <option value={model.id} class=" text-gray-900">{model.name}</option>
  287. {/each}
  288. </select>
  289. </div>
  290. </div>
  291. <div class="my-2">
  292. <div class=" text-sm font-semibold mb-2">{$i18n.t('Description')}</div>
  293. <div>
  294. <input
  295. class="px-3 py-1.5 text-sm w-full bg-transparent border dark:border-gray-600 outline-none rounded-lg"
  296. placeholder={$i18n.t('Add a short description about what this model does')}
  297. bind:value={info.meta.description}
  298. />
  299. </div>
  300. </div>
  301. <div class="my-2">
  302. <div class="flex w-full justify-between">
  303. <div class=" self-center text-sm font-semibold">{$i18n.t('Model Params')}</div>
  304. </div>
  305. <div class="mt-2">
  306. <div class="my-1">
  307. <div class=" text-xs font-semibold mb-2">{$i18n.t('System Prompt')}</div>
  308. <div>
  309. <textarea
  310. class="px-3 py-1.5 text-sm w-full bg-transparent border dark:border-gray-600 outline-none rounded-lg -mb-1"
  311. placeholder={`Write your model system prompt content here\ne.g.) You are Mario from Super Mario Bros, acting as an assistant.`}
  312. rows="4"
  313. bind:value={info.params.system}
  314. />
  315. </div>
  316. </div>
  317. <div class="flex w-full justify-between">
  318. <div class=" self-center text-xs font-semibold">
  319. {$i18n.t('Advanced Params')}
  320. </div>
  321. <button
  322. class="p-1 px-3 text-xs flex rounded transition"
  323. type="button"
  324. on:click={() => {
  325. showAdvanced = !showAdvanced;
  326. }}
  327. >
  328. {#if showAdvanced}
  329. <span class="ml-2 self-center">{$i18n.t('Hide')}</span>
  330. {:else}
  331. <span class="ml-2 self-center">{$i18n.t('Show')}</span>
  332. {/if}
  333. </button>
  334. </div>
  335. {#if showAdvanced}
  336. <div class="my-2">
  337. <AdvancedParams
  338. bind:params
  339. on:change={(e) => {
  340. info.params = { ...info.params, ...params };
  341. }}
  342. />
  343. </div>
  344. {/if}
  345. </div>
  346. </div>
  347. <div class="my-2">
  348. <div class="flex w-full justify-between items-center">
  349. <div class="flex w-full justify-between items-center">
  350. <div class=" self-center text-sm font-semibold">{$i18n.t('Prompt suggestions')}</div>
  351. <button
  352. class="p-1 text-xs flex rounded transition"
  353. type="button"
  354. on:click={() => {
  355. if (info.meta.suggestion_prompts === null) {
  356. info.meta.suggestion_prompts = [{ content: '' }];
  357. } else {
  358. info.meta.suggestion_prompts = null;
  359. }
  360. }}
  361. >
  362. {#if info.meta.suggestion_prompts === null}
  363. <span class="ml-2 self-center">{$i18n.t('Default')}</span>
  364. {:else}
  365. <span class="ml-2 self-center">{$i18n.t('Custom')}</span>
  366. {/if}
  367. </button>
  368. </div>
  369. {#if info.meta.suggestion_prompts !== null}
  370. <button
  371. class="p-1 px-2 text-xs flex rounded transition"
  372. type="button"
  373. on:click={() => {
  374. if (
  375. info.meta.suggestion_prompts.length === 0 ||
  376. info.meta.suggestion_prompts.at(-1).content !== ''
  377. ) {
  378. info.meta.suggestion_prompts = [...info.meta.suggestion_prompts, { content: '' }];
  379. }
  380. }}
  381. >
  382. <svg
  383. xmlns="http://www.w3.org/2000/svg"
  384. viewBox="0 0 20 20"
  385. fill="currentColor"
  386. class="w-4 h-4"
  387. >
  388. <path
  389. 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"
  390. />
  391. </svg>
  392. </button>
  393. {/if}
  394. </div>
  395. {#if info.meta.suggestion_prompts}
  396. <div class="flex flex-col space-y-1 mt-2">
  397. {#if info.meta.suggestion_prompts.length > 0}
  398. {#each info.meta.suggestion_prompts as prompt, promptIdx}
  399. <div class=" flex border dark:border-gray-600 rounded-lg">
  400. <input
  401. class="px-3 py-1.5 text-sm w-full bg-transparent outline-none border-r dark:border-gray-600"
  402. placeholder={$i18n.t('Write a prompt suggestion (e.g. Who are you?)')}
  403. bind:value={prompt.content}
  404. />
  405. <button
  406. class="px-2"
  407. type="button"
  408. on:click={() => {
  409. info.meta.suggestion_prompts.splice(promptIdx, 1);
  410. info.meta.suggestion_prompts = info.meta.suggestion_prompts;
  411. }}
  412. >
  413. <svg
  414. xmlns="http://www.w3.org/2000/svg"
  415. viewBox="0 0 20 20"
  416. fill="currentColor"
  417. class="w-4 h-4"
  418. >
  419. <path
  420. 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"
  421. />
  422. </svg>
  423. </button>
  424. </div>
  425. {/each}
  426. {:else}
  427. <div class="text-xs text-center">No suggestion prompts</div>
  428. {/if}
  429. </div>
  430. {/if}
  431. </div>
  432. <div class="my-2">
  433. <div class="flex w-full justify-between">
  434. <div class=" self-center text-sm font-semibold">{$i18n.t('Capabilities')}</div>
  435. </div>
  436. <div class="flex flex-col">
  437. {#each Object.keys(capabilities) as capability}
  438. <div class=" flex items-center gap-2">
  439. <Checkbox
  440. state={capabilities[capability] ? 'checked' : 'unchecked'}
  441. on:change={(e) => {
  442. capabilities[capability] = e.detail === 'checked';
  443. }}
  444. />
  445. <div class=" py-1.5 text-sm w-full capitalize">
  446. {$i18n.t(capability)}
  447. </div>
  448. </div>
  449. {/each}
  450. </div>
  451. </div>
  452. <div class="my-2 text-gray-500">
  453. <div class="flex w-full justify-between mb-2">
  454. <div class=" self-center text-sm font-semibold">{$i18n.t('JSON Preview')}</div>
  455. <button
  456. class="p-1 px-3 text-xs flex rounded transition"
  457. type="button"
  458. on:click={() => {
  459. showPreview = !showPreview;
  460. }}
  461. >
  462. {#if showPreview}
  463. <span class="ml-2 self-center">{$i18n.t('Hide')}</span>
  464. {:else}
  465. <span class="ml-2 self-center">{$i18n.t('Show')}</span>
  466. {/if}
  467. </button>
  468. </div>
  469. {#if showPreview}
  470. <div>
  471. <textarea
  472. class="px-3 py-1.5 text-sm w-full bg-transparent border dark:border-gray-600 outline-none rounded-lg"
  473. rows="10"
  474. value={JSON.stringify(info, null, 2)}
  475. disabled
  476. readonly
  477. />
  478. </div>
  479. {/if}
  480. </div>
  481. <div class="my-2 flex justify-end mb-20">
  482. <button
  483. class=" text-sm px-3 py-2 transition rounded-xl {loading
  484. ? ' cursor-not-allowed bg-gray-100 dark:bg-gray-800'
  485. : ' bg-gray-50 hover:bg-gray-100 dark:bg-gray-700 dark:hover:bg-gray-800'} flex"
  486. type="submit"
  487. disabled={loading}
  488. >
  489. <div class=" self-center font-medium">{$i18n.t('Save & Create')}</div>
  490. {#if loading}
  491. <div class="ml-1.5 self-center">
  492. <svg
  493. class=" w-4 h-4"
  494. viewBox="0 0 24 24"
  495. fill="currentColor"
  496. xmlns="http://www.w3.org/2000/svg"
  497. ><style>
  498. .spinner_ajPY {
  499. transform-origin: center;
  500. animation: spinner_AtaB 0.75s infinite linear;
  501. }
  502. @keyframes spinner_AtaB {
  503. 100% {
  504. transform: rotate(360deg);
  505. }
  506. }
  507. </style><path
  508. 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"
  509. opacity=".25"
  510. /><path
  511. 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"
  512. class="spinner_ajPY"
  513. /></svg
  514. >
  515. </div>
  516. {/if}
  517. </button>
  518. </div>
  519. </form>
  520. </div>