Interface.svelte 13 KB

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