WebSearch.svelte 7.9 KB

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