Interface.svelte 12 KB

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