Interface.svelte 11 KB

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