Interface.svelte 12 KB

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