Interface.svelte 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. <script lang="ts">
  2. import { v4 as uuidv4 } from 'uuid';
  3. import { toast } from 'svelte-sonner';
  4. import { getBackendConfig, getModels, getTaskConfig, updateTaskConfig } from '$lib/apis';
  5. import { setDefaultPromptSuggestions } from '$lib/apis/configs';
  6. import { config, settings, user } from '$lib/stores';
  7. import { createEventDispatcher, onMount, getContext } from 'svelte';
  8. import { banners as _banners } from '$lib/stores';
  9. import type { Banner } from '$lib/types';
  10. import { getBanners, setBanners } from '$lib/apis/configs';
  11. import Tooltip from '$lib/components/common/Tooltip.svelte';
  12. import Switch from '$lib/components/common/Switch.svelte';
  13. import Textarea from '$lib/components/common/Textarea.svelte';
  14. import Spinner from '$lib/components/common/Spinner.svelte';
  15. import { getBaseModels } from '$lib/apis/models';
  16. const dispatch = createEventDispatcher();
  17. const i18n = getContext('i18n');
  18. let taskConfig = {
  19. TASK_MODEL: '',
  20. TASK_MODEL_EXTERNAL: '',
  21. ENABLE_TITLE_GENERATION: true,
  22. TITLE_GENERATION_PROMPT_TEMPLATE: '',
  23. IMAGE_PROMPT_GENERATION_PROMPT_TEMPLATE: '',
  24. ENABLE_AUTOCOMPLETE_GENERATION: true,
  25. AUTOCOMPLETE_GENERATION_INPUT_MAX_LENGTH: -1,
  26. TAGS_GENERATION_PROMPT_TEMPLATE: '',
  27. ENABLE_TAGS_GENERATION: true,
  28. ENABLE_SEARCH_QUERY_GENERATION: true,
  29. ENABLE_RETRIEVAL_QUERY_GENERATION: true,
  30. QUERY_GENERATION_PROMPT_TEMPLATE: '',
  31. TOOLS_FUNCTION_CALLING_PROMPT_TEMPLATE: ''
  32. };
  33. let promptSuggestions = [];
  34. let banners: Banner[] = [];
  35. const updateInterfaceHandler = async () => {
  36. taskConfig = await updateTaskConfig(localStorage.token, taskConfig);
  37. promptSuggestions = await setDefaultPromptSuggestions(localStorage.token, promptSuggestions);
  38. await updateBanners();
  39. await config.set(await getBackendConfig());
  40. };
  41. onMount(async () => {
  42. await init();
  43. taskConfig = await getTaskConfig(localStorage.token);
  44. promptSuggestions = $config?.default_prompt_suggestions ?? [];
  45. banners = await getBanners(localStorage.token);
  46. });
  47. const updateBanners = async () => {
  48. _banners.set(await setBanners(localStorage.token, banners));
  49. };
  50. let workspaceModels = null;
  51. let baseModels = null;
  52. let models = null;
  53. const init = async () => {
  54. workspaceModels = await getBaseModels(localStorage.token);
  55. baseModels = await getModels(localStorage.token, null, true);
  56. models = baseModels.map((m) => {
  57. const workspaceModel = workspaceModels.find((wm) => wm.id === m.id);
  58. if (workspaceModel) {
  59. return {
  60. ...m,
  61. ...workspaceModel
  62. };
  63. } else {
  64. return {
  65. ...m,
  66. id: m.id,
  67. name: m.name,
  68. is_active: true
  69. };
  70. }
  71. });
  72. console.debug('models', models);
  73. };
  74. </script>
  75. {#if models !== null && taskConfig}
  76. <form
  77. class="flex flex-col h-full justify-between space-y-3 text-sm"
  78. on:submit|preventDefault={() => {
  79. updateInterfaceHandler();
  80. dispatch('save');
  81. }}
  82. >
  83. <div class=" overflow-y-scroll scrollbar-hidden h-full pr-1.5">
  84. <div class="mb-3.5">
  85. <div class=" mb-2.5 text-base font-medium">{$i18n.t('Tasks')}</div>
  86. <hr class=" border-gray-100 dark:border-gray-850 my-2" />
  87. <div class=" mb-1 font-medium flex items-center">
  88. <div class=" text-xs mr-1">{$i18n.t('Set Task Model')}</div>
  89. <Tooltip
  90. content={$i18n.t(
  91. 'A task model is used when performing tasks such as generating titles for chats and web search queries'
  92. )}
  93. >
  94. <svg
  95. xmlns="http://www.w3.org/2000/svg"
  96. fill="none"
  97. viewBox="0 0 24 24"
  98. stroke-width="1.5"
  99. stroke="currentColor"
  100. class="size-3.5"
  101. >
  102. <path
  103. stroke-linecap="round"
  104. stroke-linejoin="round"
  105. 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"
  106. />
  107. </svg>
  108. </Tooltip>
  109. </div>
  110. <div class=" mb-2.5 flex w-full gap-2">
  111. <div class="flex-1">
  112. <div class=" text-xs mb-1">{$i18n.t('Local Models')}</div>
  113. <select
  114. class="w-full rounded-lg py-2 px-4 text-sm bg-gray-50 dark:text-gray-300 dark:bg-gray-850 outline-hidden"
  115. bind:value={taskConfig.TASK_MODEL}
  116. placeholder={$i18n.t('Select a model')}
  117. on:change={() => {
  118. if (taskConfig.TASK_MODEL) {
  119. const model = models.find((m) => m.id === taskConfig.TASK_MODEL);
  120. if (model) {
  121. if (model?.access_control !== null) {
  122. toast.error(
  123. $i18n.t(
  124. 'This model is not publicly available. Please select another model.'
  125. )
  126. );
  127. }
  128. taskConfig.TASK_MODEL = model.id;
  129. } else {
  130. taskConfig.TASK_MODEL = '';
  131. }
  132. }
  133. }}
  134. >
  135. <option value="" selected>{$i18n.t('Current Model')}</option>
  136. {#each models.filter((m) => m.owned_by === 'ollama') as model}
  137. <option value={model.id} class="bg-gray-100 dark:bg-gray-700">
  138. {model.name}
  139. </option>
  140. {/each}
  141. </select>
  142. </div>
  143. <div class="flex-1">
  144. <div class=" text-xs mb-1">{$i18n.t('External Models')}</div>
  145. <select
  146. class="w-full rounded-lg py-2 px-4 text-sm bg-gray-50 dark:text-gray-300 dark:bg-gray-850 outline-hidden"
  147. bind:value={taskConfig.TASK_MODEL_EXTERNAL}
  148. placeholder={$i18n.t('Select a model')}
  149. on:change={() => {
  150. if (taskConfig.TASK_MODEL_EXTERNAL) {
  151. const model = models.find((m) => m.id === taskConfig.TASK_MODEL_EXTERNAL);
  152. if (model) {
  153. if (model?.access_control !== null) {
  154. toast.error(
  155. $i18n.t(
  156. 'This model is not publicly available. Please select another model.'
  157. )
  158. );
  159. }
  160. taskConfig.TASK_MODEL_EXTERNAL = model.id;
  161. } else {
  162. taskConfig.TASK_MODEL_EXTERNAL = '';
  163. }
  164. }
  165. }}
  166. >
  167. <option value="" selected>{$i18n.t('Current Model')}</option>
  168. {#each models as model}
  169. <option value={model.id} class="bg-gray-100 dark:bg-gray-700">
  170. {model.name}
  171. </option>
  172. {/each}
  173. </select>
  174. </div>
  175. </div>
  176. <div class="mb-2.5 flex w-full items-center justify-between">
  177. <div class=" self-center text-xs font-medium">
  178. {$i18n.t('Title Generation')}
  179. </div>
  180. <Switch bind:state={taskConfig.ENABLE_TITLE_GENERATION} />
  181. </div>
  182. {#if taskConfig.ENABLE_TITLE_GENERATION}
  183. <div class="mb-2.5">
  184. <div class=" mb-1 text-xs font-medium">{$i18n.t('Title Generation Prompt')}</div>
  185. <Tooltip
  186. content={$i18n.t('Leave empty to use the default prompt, or enter a custom prompt')}
  187. placement="top-start"
  188. >
  189. <Textarea
  190. bind:value={taskConfig.TITLE_GENERATION_PROMPT_TEMPLATE}
  191. placeholder={$i18n.t(
  192. 'Leave empty to use the default prompt, or enter a custom prompt'
  193. )}
  194. />
  195. </Tooltip>
  196. </div>
  197. {/if}
  198. <div class="mb-2.5 flex w-full items-center justify-between">
  199. <div class=" self-center text-xs font-medium">
  200. {$i18n.t('Tags Generation')}
  201. </div>
  202. <Switch bind:state={taskConfig.ENABLE_TAGS_GENERATION} />
  203. </div>
  204. {#if taskConfig.ENABLE_TAGS_GENERATION}
  205. <div class="mb-2.5">
  206. <div class=" mb-1 text-xs font-medium">{$i18n.t('Tags Generation Prompt')}</div>
  207. <Tooltip
  208. content={$i18n.t('Leave empty to use the default prompt, or enter a custom prompt')}
  209. placement="top-start"
  210. >
  211. <Textarea
  212. bind:value={taskConfig.TAGS_GENERATION_PROMPT_TEMPLATE}
  213. placeholder={$i18n.t(
  214. 'Leave empty to use the default prompt, or enter a custom prompt'
  215. )}
  216. />
  217. </Tooltip>
  218. </div>
  219. {/if}
  220. <div class="mb-2.5 flex w-full items-center justify-between">
  221. <div class=" self-center text-xs font-medium">
  222. {$i18n.t('Retrieval Query Generation')}
  223. </div>
  224. <Switch bind:state={taskConfig.ENABLE_RETRIEVAL_QUERY_GENERATION} />
  225. </div>
  226. <div class="mb-2.5 flex w-full items-center justify-between">
  227. <div class=" self-center text-xs font-medium">
  228. {$i18n.t('Web Search Query Generation')}
  229. </div>
  230. <Switch bind:state={taskConfig.ENABLE_SEARCH_QUERY_GENERATION} />
  231. </div>
  232. <div class="mb-2.5">
  233. <div class=" mb-1 text-xs font-medium">{$i18n.t('Query Generation Prompt')}</div>
  234. <Tooltip
  235. content={$i18n.t('Leave empty to use the default prompt, or enter a custom prompt')}
  236. placement="top-start"
  237. >
  238. <Textarea
  239. bind:value={taskConfig.QUERY_GENERATION_PROMPT_TEMPLATE}
  240. placeholder={$i18n.t(
  241. 'Leave empty to use the default prompt, or enter a custom prompt'
  242. )}
  243. />
  244. </Tooltip>
  245. </div>
  246. <div class="mb-2.5 flex w-full items-center justify-between">
  247. <div class=" self-center text-xs font-medium">
  248. {$i18n.t('Autocomplete Generation')}
  249. </div>
  250. <Tooltip content={$i18n.t('Enable autocomplete generation for chat messages')}>
  251. <Switch bind:state={taskConfig.ENABLE_AUTOCOMPLETE_GENERATION} />
  252. </Tooltip>
  253. </div>
  254. {#if taskConfig.ENABLE_AUTOCOMPLETE_GENERATION}
  255. <div class="mb-2.5">
  256. <div class=" mb-1 text-xs font-medium">
  257. {$i18n.t('Autocomplete Generation Input Max Length')}
  258. </div>
  259. <Tooltip
  260. content={$i18n.t('Character limit for autocomplete generation input')}
  261. placement="top-start"
  262. >
  263. <input
  264. class="w-full outline-hidden bg-transparent"
  265. bind:value={taskConfig.AUTOCOMPLETE_GENERATION_INPUT_MAX_LENGTH}
  266. placeholder={$i18n.t('-1 for no limit, or a positive integer for a specific limit')}
  267. />
  268. </Tooltip>
  269. </div>
  270. {/if}
  271. <div class="mb-2.5">
  272. <div class=" mb-1 text-xs font-medium">{$i18n.t('Image Prompt Generation Prompt')}</div>
  273. <Tooltip
  274. content={$i18n.t('Leave empty to use the default prompt, or enter a custom prompt')}
  275. placement="top-start"
  276. >
  277. <Textarea
  278. bind:value={taskConfig.IMAGE_PROMPT_GENERATION_PROMPT_TEMPLATE}
  279. placeholder={$i18n.t(
  280. 'Leave empty to use the default prompt, or enter a custom prompt'
  281. )}
  282. />
  283. </Tooltip>
  284. </div>
  285. <div class="mb-2.5">
  286. <div class=" mb-1 text-xs font-medium">{$i18n.t('Tools Function Calling Prompt')}</div>
  287. <Tooltip
  288. content={$i18n.t('Leave empty to use the default prompt, or enter a custom prompt')}
  289. placement="top-start"
  290. >
  291. <Textarea
  292. bind:value={taskConfig.TOOLS_FUNCTION_CALLING_PROMPT_TEMPLATE}
  293. placeholder={$i18n.t(
  294. 'Leave empty to use the default prompt, or enter a custom prompt'
  295. )}
  296. />
  297. </Tooltip>
  298. </div>
  299. </div>
  300. <div class="mb-3.5">
  301. <div class=" mb-2.5 text-base font-medium">{$i18n.t('UI')}</div>
  302. <hr class=" border-gray-100 dark:border-gray-850 my-2" />
  303. <div class=" {banners.length > 0 ? ' mb-3' : ''}">
  304. <div class="mb-2.5 flex w-full justify-between">
  305. <div class=" self-center text-sm font-semibold">
  306. {$i18n.t('Banners')}
  307. </div>
  308. <button
  309. class="p-1 px-3 text-xs flex rounded-sm transition"
  310. type="button"
  311. on:click={() => {
  312. if (banners.length === 0 || banners.at(-1).content !== '') {
  313. banners = [
  314. ...banners,
  315. {
  316. id: uuidv4(),
  317. type: '',
  318. title: '',
  319. content: '',
  320. dismissible: true,
  321. timestamp: Math.floor(Date.now() / 1000)
  322. }
  323. ];
  324. }
  325. }}
  326. >
  327. <svg
  328. xmlns="http://www.w3.org/2000/svg"
  329. viewBox="0 0 20 20"
  330. fill="currentColor"
  331. class="w-4 h-4"
  332. >
  333. <path
  334. 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"
  335. />
  336. </svg>
  337. </button>
  338. </div>
  339. <div class=" flex flex-col space-y-1">
  340. {#each banners as banner, bannerIdx}
  341. <div class=" flex justify-between">
  342. <div
  343. class="flex flex-row flex-1 border rounded-xl border-gray-100 dark:border-gray-850"
  344. >
  345. <select
  346. class="w-fit capitalize rounded-xl py-2 px-4 text-xs bg-transparent outline-hidden"
  347. bind:value={banner.type}
  348. required
  349. >
  350. {#if banner.type == ''}
  351. <option value="" selected disabled class="text-gray-900"
  352. >{$i18n.t('Type')}</option
  353. >
  354. {/if}
  355. <option value="info" class="text-gray-900">{$i18n.t('Info')}</option>
  356. <option value="warning" class="text-gray-900">{$i18n.t('Warning')}</option>
  357. <option value="error" class="text-gray-900">{$i18n.t('Error')}</option>
  358. <option value="success" class="text-gray-900">{$i18n.t('Success')}</option>
  359. </select>
  360. <input
  361. class="pr-5 py-1.5 text-xs w-full bg-transparent outline-hidden"
  362. placeholder={$i18n.t('Content')}
  363. bind:value={banner.content}
  364. />
  365. <div class="relative top-1.5 -left-2">
  366. <Tooltip content={$i18n.t('Dismissible')} className="flex h-fit items-center">
  367. <Switch bind:state={banner.dismissible} />
  368. </Tooltip>
  369. </div>
  370. </div>
  371. <button
  372. class="px-2"
  373. type="button"
  374. on:click={() => {
  375. banners.splice(bannerIdx, 1);
  376. banners = banners;
  377. }}
  378. >
  379. <svg
  380. xmlns="http://www.w3.org/2000/svg"
  381. viewBox="0 0 20 20"
  382. fill="currentColor"
  383. class="w-4 h-4"
  384. >
  385. <path
  386. 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"
  387. />
  388. </svg>
  389. </button>
  390. </div>
  391. {/each}
  392. </div>
  393. </div>
  394. {#if $user?.role === 'admin'}
  395. <div class=" space-y-3">
  396. <div class="flex w-full justify-between mb-2">
  397. <div class=" self-center text-sm font-semibold">
  398. {$i18n.t('Default Prompt Suggestions')}
  399. </div>
  400. <button
  401. class="p-1 px-3 text-xs flex rounded-sm transition"
  402. type="button"
  403. on:click={() => {
  404. if (promptSuggestions.length === 0 || promptSuggestions.at(-1).content !== '') {
  405. promptSuggestions = [...promptSuggestions, { content: '', title: ['', ''] }];
  406. }
  407. }}
  408. >
  409. <svg
  410. xmlns="http://www.w3.org/2000/svg"
  411. viewBox="0 0 20 20"
  412. fill="currentColor"
  413. class="w-4 h-4"
  414. >
  415. <path
  416. 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"
  417. />
  418. </svg>
  419. </button>
  420. </div>
  421. <div class="grid lg:grid-cols-2 flex-col gap-1.5">
  422. {#each promptSuggestions as prompt, promptIdx}
  423. <div
  424. class=" flex border border-gray-100 dark:border-none dark:bg-gray-850 rounded-xl py-1.5"
  425. >
  426. <div class="flex flex-col flex-1 pl-1">
  427. <div class="flex border-b border-gray-100 dark:border-gray-850 w-full">
  428. <input
  429. class="px-3 py-1.5 text-xs w-full bg-transparent outline-hidden border-r border-gray-100 dark:border-gray-850"
  430. placeholder={$i18n.t('Title (e.g. Tell me a fun fact)')}
  431. bind:value={prompt.title[0]}
  432. />
  433. <input
  434. class="px-3 py-1.5 text-xs w-full bg-transparent outline-hidden border-r border-gray-100 dark:border-gray-850"
  435. placeholder={$i18n.t('Subtitle (e.g. about the Roman Empire)')}
  436. bind:value={prompt.title[1]}
  437. />
  438. </div>
  439. <textarea
  440. class="px-3 py-1.5 text-xs w-full bg-transparent outline-hidden border-r border-gray-100 dark:border-gray-850 resize-none"
  441. placeholder={$i18n.t(
  442. 'Prompt (e.g. Tell me a fun fact about the Roman Empire)'
  443. )}
  444. rows="3"
  445. bind:value={prompt.content}
  446. />
  447. </div>
  448. <button
  449. class="px-3"
  450. type="button"
  451. on:click={() => {
  452. promptSuggestions.splice(promptIdx, 1);
  453. promptSuggestions = promptSuggestions;
  454. }}
  455. >
  456. <svg
  457. xmlns="http://www.w3.org/2000/svg"
  458. viewBox="0 0 20 20"
  459. fill="currentColor"
  460. class="w-4 h-4"
  461. >
  462. <path
  463. 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"
  464. />
  465. </svg>
  466. </button>
  467. </div>
  468. {/each}
  469. </div>
  470. {#if promptSuggestions.length > 0}
  471. <div class="text-xs text-left w-full mt-2">
  472. {$i18n.t('Adjusting these settings will apply changes universally to all users.')}
  473. </div>
  474. {/if}
  475. </div>
  476. {/if}
  477. </div>
  478. </div>
  479. <div class="flex justify-end text-sm font-medium">
  480. <button
  481. 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-full"
  482. type="submit"
  483. >
  484. {$i18n.t('Save')}
  485. </button>
  486. </div>
  487. </form>
  488. {:else}
  489. <div class=" h-full w-full flex justify-center items-center">
  490. <Spinner />
  491. </div>
  492. {/if}