WebSearch.svelte 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <script lang="ts">
  2. import { getRAGConfig, updateRAGConfig } from '$lib/apis/rag';
  3. import Switch from '$lib/components/common/Switch.svelte';
  4. import { documents, models } from '$lib/stores';
  5. import { onMount, getContext } from 'svelte';
  6. import { toast } from 'svelte-sonner';
  7. const i18n = getContext('i18n');
  8. export let saveHandler: Function;
  9. let webConfig = null;
  10. let webSearchEngines = ['searxng', 'google_pse', 'brave', 'serpstack', 'serper', 'serply'];
  11. let youtubeLanguage = 'en';
  12. let youtubeTranslation = null;
  13. const submitHandler = async () => {
  14. const res = await updateRAGConfig(localStorage.token, {
  15. web: webConfig,
  16. youtube: {
  17. language: youtubeLanguage.split(',').map((lang) => lang.trim()),
  18. translation: youtubeTranslation
  19. }
  20. });
  21. };
  22. onMount(async () => {
  23. const res = await getRAGConfig(localStorage.token);
  24. if (res) {
  25. webConfig = res.web;
  26. youtubeLanguage = res.youtube.language.join(',');
  27. youtubeTranslation = res.youtube.translation;
  28. }
  29. });
  30. </script>
  31. <form
  32. class="flex flex-col h-full justify-between space-y-3 text-sm"
  33. on:submit|preventDefault={async () => {
  34. await submitHandler();
  35. saveHandler();
  36. }}
  37. >
  38. <div class=" space-y-3 overflow-y-scroll scrollbar-hidden h-full">
  39. {#if webConfig}
  40. <div>
  41. <div class=" mb-1 text-sm font-medium">
  42. {$i18n.t('Web Search')}
  43. </div>
  44. <div>
  45. <div class=" py-0.5 flex w-full justify-between">
  46. <div class=" self-center text-xs font-medium">
  47. {$i18n.t('Enable Web Search')}
  48. </div>
  49. <Switch bind:state={webConfig.search.enabled} />
  50. </div>
  51. </div>
  52. <div class=" py-0.5 flex w-full justify-between">
  53. <div class=" self-center text-xs font-medium">{$i18n.t('Web Search Engine')}</div>
  54. <div class="flex items-center relative">
  55. <select
  56. class="dark:bg-gray-900 w-fit pr-8 rounded px-2 p-1 text-xs bg-transparent outline-none text-right"
  57. bind:value={webConfig.search.engine}
  58. placeholder={$i18n.t('Select a engine')}
  59. required
  60. >
  61. <option disabled selected value="">{$i18n.t('Select a engine')}</option>
  62. {#each webSearchEngines as engine}
  63. <option value={engine}>{engine}</option>
  64. {/each}
  65. </select>
  66. </div>
  67. </div>
  68. {#if webConfig.search.engine !== ''}
  69. <div class="mt-1.5">
  70. {#if webConfig.search.engine === 'searxng'}
  71. <div>
  72. <div class=" self-center text-xs font-medium mb-1">
  73. {$i18n.t('Searxng Query URL')}
  74. </div>
  75. <div class="flex w-full">
  76. <div class="flex-1">
  77. <input
  78. class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
  79. type="text"
  80. placeholder={$i18n.t('Enter Searxng Query URL')}
  81. bind:value={webConfig.search.searxng_query_url}
  82. autocomplete="off"
  83. />
  84. </div>
  85. </div>
  86. </div>
  87. {:else if webConfig.search.engine === 'google_pse'}
  88. <div>
  89. <div class=" self-center text-xs font-medium mb-1">
  90. {$i18n.t('Google PSE API Key')}
  91. </div>
  92. <div class="flex w-full">
  93. <div class="flex-1">
  94. <input
  95. class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
  96. type="text"
  97. placeholder={$i18n.t('Enter Google PSE API Key')}
  98. bind:value={webConfig.search.google_pse_api_key}
  99. autocomplete="off"
  100. />
  101. </div>
  102. </div>
  103. </div>
  104. <div class="mt-1.5">
  105. <div class=" self-center text-xs font-medium mb-1">
  106. {$i18n.t('Google PSE Engine Id')}
  107. </div>
  108. <div class="flex w-full">
  109. <div class="flex-1">
  110. <input
  111. class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
  112. type="text"
  113. placeholder={$i18n.t('Enter Google PSE Engine Id')}
  114. bind:value={webConfig.search.google_pse_engine_id}
  115. autocomplete="off"
  116. />
  117. </div>
  118. </div>
  119. </div>
  120. {:else if webConfig.search.engine === 'brave'}
  121. <div>
  122. <div class=" self-center text-xs font-medium mb-1">
  123. {$i18n.t('Brave Search API Key')}
  124. </div>
  125. <div class="flex w-full">
  126. <div class="flex-1">
  127. <input
  128. class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
  129. type="text"
  130. placeholder={$i18n.t('Enter Brave Search API Key')}
  131. bind:value={webConfig.search.brave_search_api_key}
  132. autocomplete="off"
  133. />
  134. </div>
  135. </div>
  136. </div>
  137. {:else if webConfig.search.engine === 'serpstack'}
  138. <div>
  139. <div class=" self-center text-xs font-medium mb-1">
  140. {$i18n.t('Serpstack API Key')}
  141. </div>
  142. <div class="flex w-full">
  143. <div class="flex-1">
  144. <input
  145. class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
  146. type="text"
  147. placeholder={$i18n.t('Enter Serpstack API Key')}
  148. bind:value={webConfig.search.serpstack_api_key}
  149. autocomplete="off"
  150. />
  151. </div>
  152. </div>
  153. </div>
  154. {:else if webConfig.search.engine === 'serper'}
  155. <div>
  156. <div class=" self-center text-xs font-medium mb-1">
  157. {$i18n.t('Serper API Key')}
  158. </div>
  159. <div class="flex w-full">
  160. <div class="flex-1">
  161. <input
  162. class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
  163. type="text"
  164. placeholder={$i18n.t('Enter Serper API Key')}
  165. bind:value={webConfig.search.serper_api_key}
  166. autocomplete="off"
  167. />
  168. </div>
  169. </div>
  170. </div>
  171. {:else if webConfig.search.engine === 'serply'}
  172. <div>
  173. <div class=" self-center text-xs font-medium mb-1">
  174. {$i18n.t('Serply API Key')}
  175. </div>
  176. <div class="flex w-full">
  177. <div class="flex-1">
  178. <input
  179. class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
  180. type="text"
  181. placeholder={$i18n.t('Enter Serply API Key')}
  182. bind:value={webConfig.search.serply_api_key}
  183. autocomplete="off"
  184. />
  185. </div>
  186. </div>
  187. </div>
  188. {/if}
  189. </div>
  190. {/if}
  191. {#if webConfig.search.enabled}
  192. <div class="mt-2 flex gap-2 mb-1">
  193. <div class="w-full">
  194. <div class=" self-center text-xs font-medium mb-1">
  195. {$i18n.t('Search Result Count')}
  196. </div>
  197. <input
  198. class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
  199. placeholder={$i18n.t('Search Result Count')}
  200. bind:value={webConfig.search.result_count}
  201. required
  202. />
  203. </div>
  204. <div class="w-full">
  205. <div class=" self-center text-xs font-medium mb-1">
  206. {$i18n.t('Concurrent Requests')}
  207. </div>
  208. <input
  209. class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
  210. placeholder={$i18n.t('Concurrent Requests')}
  211. bind:value={webConfig.search.concurrent_requests}
  212. required
  213. />
  214. </div>
  215. </div>
  216. {/if}
  217. </div>
  218. <hr class=" dark:border-gray-850 my-2" />
  219. <div>
  220. <div class=" mb-1 text-sm font-medium">
  221. {$i18n.t('Web Loader Settings')}
  222. </div>
  223. <div>
  224. <div class=" py-0.5 flex w-full justify-between">
  225. <div class=" self-center text-xs font-medium">
  226. {$i18n.t('Bypass SSL verification for Websites')}
  227. </div>
  228. <button
  229. class="p-1 px-3 text-xs flex rounded transition"
  230. on:click={() => {
  231. webConfig.ssl_verification = !webConfig.ssl_verification;
  232. submitHandler();
  233. }}
  234. type="button"
  235. >
  236. {#if webConfig.ssl_verification === true}
  237. <span class="ml-2 self-center">{$i18n.t('On')}</span>
  238. {:else}
  239. <span class="ml-2 self-center">{$i18n.t('Off')}</span>
  240. {/if}
  241. </button>
  242. </div>
  243. </div>
  244. <div class=" mt-2 mb-1 text-sm font-medium">
  245. {$i18n.t('Youtube Loader Settings')}
  246. </div>
  247. <div>
  248. <div class=" py-0.5 flex w-full justify-between">
  249. <div class=" w-20 text-xs font-medium self-center">{$i18n.t('Language')}</div>
  250. <div class=" flex-1 self-center">
  251. <input
  252. class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
  253. type="text"
  254. placeholder={$i18n.t('Enter language codes')}
  255. bind:value={youtubeLanguage}
  256. autocomplete="off"
  257. />
  258. </div>
  259. </div>
  260. </div>
  261. </div>
  262. {/if}
  263. </div>
  264. <div class="flex justify-end pt-3 text-sm font-medium">
  265. <button
  266. class=" px-4 py-2 bg-emerald-700 hover:bg-emerald-800 text-gray-100 transition rounded-lg"
  267. type="submit"
  268. >
  269. {$i18n.t('Save')}
  270. </button>
  271. </div>
  272. </form>