Interface.svelte 15 KB

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