Interface.svelte 15 KB

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