Connections.svelte 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. <script lang="ts">
  2. import { models, user } from '$lib/stores';
  3. import { createEventDispatcher, onMount, getContext, tick } from 'svelte';
  4. const dispatch = createEventDispatcher();
  5. import {
  6. getOllamaConfig,
  7. getOllamaUrls,
  8. getOllamaVersion,
  9. updateOllamaConfig,
  10. updateOllamaUrls
  11. } from '$lib/apis/ollama';
  12. import {
  13. getOpenAIConfig,
  14. getOpenAIKeys,
  15. getOpenAIModels,
  16. getOpenAIUrls,
  17. updateOpenAIConfig,
  18. updateOpenAIKeys,
  19. updateOpenAIUrls
  20. } from '$lib/apis/openai';
  21. import { toast } from 'svelte-sonner';
  22. import Switch from '$lib/components/common/Switch.svelte';
  23. import Spinner from '$lib/components/common/Spinner.svelte';
  24. import Tooltip from '$lib/components/common/Tooltip.svelte';
  25. import { getModels as _getModels } from '$lib/apis';
  26. import SensitiveInput from '$lib/components/common/SensitiveInput.svelte';
  27. const i18n = getContext('i18n');
  28. const getModels = async () => {
  29. const models = await _getModels(localStorage.token);
  30. return models;
  31. };
  32. // External
  33. let OLLAMA_BASE_URLS = [''];
  34. let OPENAI_API_KEYS = [''];
  35. let OPENAI_API_BASE_URLS = [''];
  36. let pipelineUrls = {};
  37. let ENABLE_OPENAI_API = null;
  38. let ENABLE_OLLAMA_API = null;
  39. const verifyOpenAIHandler = async (idx) => {
  40. OPENAI_API_BASE_URLS = OPENAI_API_BASE_URLS.map((url) => url.replace(/\/$/, ''));
  41. OPENAI_API_BASE_URLS = await updateOpenAIUrls(localStorage.token, OPENAI_API_BASE_URLS);
  42. OPENAI_API_KEYS = await updateOpenAIKeys(localStorage.token, OPENAI_API_KEYS);
  43. const res = await getOpenAIModels(localStorage.token, idx).catch((error) => {
  44. toast.error(error);
  45. return null;
  46. });
  47. if (res) {
  48. toast.success($i18n.t('Server connection verified'));
  49. if (res.pipelines) {
  50. pipelineUrls[OPENAI_API_BASE_URLS[idx]] = true;
  51. }
  52. }
  53. await models.set(await getModels());
  54. };
  55. const verifyOllamaHandler = async (idx) => {
  56. OLLAMA_BASE_URLS = OLLAMA_BASE_URLS.filter((url) => url !== '').map((url) =>
  57. url.replace(/\/$/, '')
  58. );
  59. OLLAMA_BASE_URLS = await updateOllamaUrls(localStorage.token, OLLAMA_BASE_URLS);
  60. const res = await getOllamaVersion(localStorage.token, idx).catch((error) => {
  61. toast.error(error);
  62. return null;
  63. });
  64. if (res) {
  65. toast.success($i18n.t('Server connection verified'));
  66. }
  67. await models.set(await getModels());
  68. };
  69. const updateOpenAIHandler = async () => {
  70. OPENAI_API_BASE_URLS = OPENAI_API_BASE_URLS.map((url) => url.replace(/\/$/, ''));
  71. // Check if API KEYS length is same than API URLS length
  72. if (OPENAI_API_KEYS.length !== OPENAI_API_BASE_URLS.length) {
  73. // if there are more keys than urls, remove the extra keys
  74. if (OPENAI_API_KEYS.length > OPENAI_API_BASE_URLS.length) {
  75. OPENAI_API_KEYS = OPENAI_API_KEYS.slice(0, OPENAI_API_BASE_URLS.length);
  76. }
  77. // if there are more urls than keys, add empty keys
  78. if (OPENAI_API_KEYS.length < OPENAI_API_BASE_URLS.length) {
  79. const diff = OPENAI_API_BASE_URLS.length - OPENAI_API_KEYS.length;
  80. for (let i = 0; i < diff; i++) {
  81. OPENAI_API_KEYS.push('');
  82. }
  83. }
  84. }
  85. OPENAI_API_BASE_URLS = await updateOpenAIUrls(localStorage.token, OPENAI_API_BASE_URLS);
  86. OPENAI_API_KEYS = await updateOpenAIKeys(localStorage.token, OPENAI_API_KEYS);
  87. await models.set(await getModels());
  88. };
  89. const updateOllamaUrlsHandler = async () => {
  90. OLLAMA_BASE_URLS = OLLAMA_BASE_URLS.filter((url) => url !== '').map((url) =>
  91. url.replace(/\/$/, '')
  92. );
  93. console.log(OLLAMA_BASE_URLS);
  94. if (OLLAMA_BASE_URLS.length === 0) {
  95. ENABLE_OLLAMA_API = false;
  96. await updateOllamaConfig(localStorage.token, ENABLE_OLLAMA_API);
  97. toast.info($i18n.t('Ollama API disabled'));
  98. } else {
  99. OLLAMA_BASE_URLS = await updateOllamaUrls(localStorage.token, OLLAMA_BASE_URLS);
  100. const ollamaVersion = await getOllamaVersion(localStorage.token).catch((error) => {
  101. toast.error(error);
  102. return null;
  103. });
  104. if (ollamaVersion) {
  105. toast.success($i18n.t('Server connection verified'));
  106. await models.set(await getModels());
  107. }
  108. }
  109. };
  110. onMount(async () => {
  111. if ($user.role === 'admin') {
  112. await Promise.all([
  113. (async () => {
  114. OLLAMA_BASE_URLS = await getOllamaUrls(localStorage.token);
  115. })(),
  116. (async () => {
  117. OPENAI_API_BASE_URLS = await getOpenAIUrls(localStorage.token);
  118. })(),
  119. (async () => {
  120. OPENAI_API_KEYS = await getOpenAIKeys(localStorage.token);
  121. })()
  122. ]);
  123. OPENAI_API_BASE_URLS.forEach(async (url, idx) => {
  124. const res = await getOpenAIModels(localStorage.token, idx);
  125. if (res.pipelines) {
  126. pipelineUrls[url] = true;
  127. }
  128. });
  129. const ollamaConfig = await getOllamaConfig(localStorage.token);
  130. const openaiConfig = await getOpenAIConfig(localStorage.token);
  131. ENABLE_OPENAI_API = openaiConfig.ENABLE_OPENAI_API;
  132. ENABLE_OLLAMA_API = ollamaConfig.ENABLE_OLLAMA_API;
  133. }
  134. });
  135. </script>
  136. <form
  137. class="flex flex-col h-full justify-between text-sm"
  138. on:submit|preventDefault={() => {
  139. updateOpenAIHandler();
  140. updateOllamaUrlsHandler();
  141. dispatch('save');
  142. }}
  143. >
  144. <div class="space-y-3 overflow-y-scroll scrollbar-hidden h-full">
  145. {#if ENABLE_OPENAI_API !== null && ENABLE_OLLAMA_API !== null}
  146. <div class=" space-y-3">
  147. <div class="mt-2 space-y-2 pr-1.5">
  148. <div class="flex justify-between items-center text-sm">
  149. <div class=" font-medium">{$i18n.t('OpenAI API')}</div>
  150. <div class="mt-1">
  151. <Switch
  152. bind:state={ENABLE_OPENAI_API}
  153. on:change={async () => {
  154. updateOpenAIConfig(localStorage.token, ENABLE_OPENAI_API);
  155. }}
  156. />
  157. </div>
  158. </div>
  159. {#if ENABLE_OPENAI_API}
  160. <div class="flex flex-col gap-1">
  161. {#each OPENAI_API_BASE_URLS as url, idx}
  162. <div class="flex w-full gap-2">
  163. <div class="flex-1 relative">
  164. <input
  165. class="w-full rounded-lg py-2 px-4 {pipelineUrls[url]
  166. ? 'pr-8'
  167. : ''} text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
  168. placeholder={$i18n.t('API Base URL')}
  169. bind:value={url}
  170. autocomplete="off"
  171. />
  172. {#if pipelineUrls[url]}
  173. <div class=" absolute top-2.5 right-2.5">
  174. <Tooltip content="Pipelines">
  175. <svg
  176. xmlns="http://www.w3.org/2000/svg"
  177. viewBox="0 0 24 24"
  178. fill="currentColor"
  179. class="size-4"
  180. >
  181. <path
  182. d="M11.644 1.59a.75.75 0 0 1 .712 0l9.75 5.25a.75.75 0 0 1 0 1.32l-9.75 5.25a.75.75 0 0 1-.712 0l-9.75-5.25a.75.75 0 0 1 0-1.32l9.75-5.25Z"
  183. />
  184. <path
  185. d="m3.265 10.602 7.668 4.129a2.25 2.25 0 0 0 2.134 0l7.668-4.13 1.37.739a.75.75 0 0 1 0 1.32l-9.75 5.25a.75.75 0 0 1-.71 0l-9.75-5.25a.75.75 0 0 1 0-1.32l1.37-.738Z"
  186. />
  187. <path
  188. d="m10.933 19.231-7.668-4.13-1.37.739a.75.75 0 0 0 0 1.32l9.75 5.25c.221.12.489.12.71 0l9.75-5.25a.75.75 0 0 0 0-1.32l-1.37-.738-7.668 4.13a2.25 2.25 0 0 1-2.134-.001Z"
  189. />
  190. </svg>
  191. </Tooltip>
  192. </div>
  193. {/if}
  194. </div>
  195. <SensitiveInput
  196. placeholder={$i18n.t('API Key')}
  197. bind:value={OPENAI_API_KEYS[idx]}
  198. />
  199. <div class="self-center flex items-center">
  200. {#if idx === 0}
  201. <button
  202. class="px-1"
  203. on:click={() => {
  204. OPENAI_API_BASE_URLS = [...OPENAI_API_BASE_URLS, ''];
  205. OPENAI_API_KEYS = [...OPENAI_API_KEYS, ''];
  206. }}
  207. type="button"
  208. >
  209. <svg
  210. xmlns="http://www.w3.org/2000/svg"
  211. viewBox="0 0 16 16"
  212. fill="currentColor"
  213. class="w-4 h-4"
  214. >
  215. <path
  216. d="M8.75 3.75a.75.75 0 0 0-1.5 0v3.5h-3.5a.75.75 0 0 0 0 1.5h3.5v3.5a.75.75 0 0 0 1.5 0v-3.5h3.5a.75.75 0 0 0 0-1.5h-3.5v-3.5Z"
  217. />
  218. </svg>
  219. </button>
  220. {:else}
  221. <button
  222. class="px-1"
  223. on:click={() => {
  224. OPENAI_API_BASE_URLS = OPENAI_API_BASE_URLS.filter(
  225. (url, urlIdx) => idx !== urlIdx
  226. );
  227. OPENAI_API_KEYS = OPENAI_API_KEYS.filter((key, keyIdx) => idx !== keyIdx);
  228. }}
  229. type="button"
  230. >
  231. <svg
  232. xmlns="http://www.w3.org/2000/svg"
  233. viewBox="0 0 16 16"
  234. fill="currentColor"
  235. class="w-4 h-4"
  236. >
  237. <path d="M3.75 7.25a.75.75 0 0 0 0 1.5h8.5a.75.75 0 0 0 0-1.5h-8.5Z" />
  238. </svg>
  239. </button>
  240. {/if}
  241. </div>
  242. <div class="flex">
  243. <Tooltip content="Verify connection" className="self-start mt-0.5">
  244. <button
  245. class="self-center p-2 bg-gray-200 hover:bg-gray-300 dark:bg-gray-900 dark:hover:bg-gray-850 rounded-lg transition"
  246. on:click={() => {
  247. verifyOpenAIHandler(idx);
  248. }}
  249. type="button"
  250. >
  251. <svg
  252. xmlns="http://www.w3.org/2000/svg"
  253. viewBox="0 0 20 20"
  254. fill="currentColor"
  255. class="w-4 h-4"
  256. >
  257. <path
  258. fill-rule="evenodd"
  259. d="M15.312 11.424a5.5 5.5 0 01-9.201 2.466l-.312-.311h2.433a.75.75 0 000-1.5H3.989a.75.75 0 00-.75.75v4.242a.75.75 0 001.5 0v-2.43l.31.31a7 7 0 0011.712-3.138.75.75 0 00-1.449-.39zm1.23-3.723a.75.75 0 00.219-.53V2.929a.75.75 0 00-1.5 0V5.36l-.31-.31A7 7 0 003.239 8.188a.75.75 0 101.448.389A5.5 5.5 0 0113.89 6.11l.311.31h-2.432a.75.75 0 000 1.5h4.243a.75.75 0 00.53-.219z"
  260. clip-rule="evenodd"
  261. />
  262. </svg>
  263. </button>
  264. </Tooltip>
  265. </div>
  266. </div>
  267. <div class=" mb-1 text-xs text-gray-400 dark:text-gray-500">
  268. {$i18n.t('WebUI will make requests to')}
  269. <span class=" text-gray-200">'{url}/models'</span>
  270. </div>
  271. {/each}
  272. </div>
  273. {/if}
  274. </div>
  275. </div>
  276. <hr class=" dark:border-gray-850" />
  277. <div class="pr-1.5 space-y-2">
  278. <div class="flex justify-between items-center text-sm">
  279. <div class=" font-medium">{$i18n.t('Ollama API')}</div>
  280. <div class="mt-1">
  281. <Switch
  282. bind:state={ENABLE_OLLAMA_API}
  283. on:change={async () => {
  284. updateOllamaConfig(localStorage.token, ENABLE_OLLAMA_API);
  285. if (OLLAMA_BASE_URLS.length === 0) {
  286. OLLAMA_BASE_URLS = [''];
  287. }
  288. }}
  289. />
  290. </div>
  291. </div>
  292. {#if ENABLE_OLLAMA_API}
  293. <div class="flex w-full gap-1.5">
  294. <div class="flex-1 flex flex-col gap-2">
  295. {#each OLLAMA_BASE_URLS as url, idx}
  296. <div class="flex gap-1.5">
  297. <input
  298. class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
  299. placeholder={$i18n.t('Enter URL (e.g. http://localhost:11434)')}
  300. bind:value={url}
  301. />
  302. <div class="self-center flex items-center">
  303. {#if idx === 0}
  304. <button
  305. class="px-1"
  306. on:click={() => {
  307. OLLAMA_BASE_URLS = [...OLLAMA_BASE_URLS, ''];
  308. }}
  309. type="button"
  310. >
  311. <svg
  312. xmlns="http://www.w3.org/2000/svg"
  313. viewBox="0 0 16 16"
  314. fill="currentColor"
  315. class="w-4 h-4"
  316. >
  317. <path
  318. d="M8.75 3.75a.75.75 0 0 0-1.5 0v3.5h-3.5a.75.75 0 0 0 0 1.5h3.5v3.5a.75.75 0 0 0 1.5 0v-3.5h3.5a.75.75 0 0 0 0-1.5h-3.5v-3.5Z"
  319. />
  320. </svg>
  321. </button>
  322. {:else}
  323. <button
  324. class="px-1"
  325. on:click={() => {
  326. OLLAMA_BASE_URLS = OLLAMA_BASE_URLS.filter(
  327. (url, urlIdx) => idx !== urlIdx
  328. );
  329. }}
  330. type="button"
  331. >
  332. <svg
  333. xmlns="http://www.w3.org/2000/svg"
  334. viewBox="0 0 16 16"
  335. fill="currentColor"
  336. class="w-4 h-4"
  337. >
  338. <path d="M3.75 7.25a.75.75 0 0 0 0 1.5h8.5a.75.75 0 0 0 0-1.5h-8.5Z" />
  339. </svg>
  340. </button>
  341. {/if}
  342. </div>
  343. <div class="flex">
  344. <Tooltip content="Verify connection" className="self-start mt-0.5">
  345. <button
  346. class="self-center p-2 bg-gray-200 hover:bg-gray-300 dark:bg-gray-900 dark:hover:bg-gray-850 rounded-lg transition"
  347. on:click={() => {
  348. verifyOllamaHandler(idx);
  349. }}
  350. type="button"
  351. >
  352. <svg
  353. xmlns="http://www.w3.org/2000/svg"
  354. viewBox="0 0 20 20"
  355. fill="currentColor"
  356. class="w-4 h-4"
  357. >
  358. <path
  359. fill-rule="evenodd"
  360. d="M15.312 11.424a5.5 5.5 0 01-9.201 2.466l-.312-.311h2.433a.75.75 0 000-1.5H3.989a.75.75 0 00-.75.75v4.242a.75.75 0 001.5 0v-2.43l.31.31a7 7 0 0011.712-3.138.75.75 0 00-1.449-.39zm1.23-3.723a.75.75 0 00.219-.53V2.929a.75.75 0 00-1.5 0V5.36l-.31-.31A7 7 0 003.239 8.188a.75.75 0 101.448.389A5.5 5.5 0 0113.89 6.11l.311.31h-2.432a.75.75 0 000 1.5h4.243a.75.75 0 00.53-.219z"
  361. clip-rule="evenodd"
  362. />
  363. </svg>
  364. </button>
  365. </Tooltip>
  366. </div>
  367. </div>
  368. {/each}
  369. </div>
  370. </div>
  371. <div class="mt-2 text-xs text-gray-400 dark:text-gray-500">
  372. {$i18n.t('Trouble accessing Ollama?')}
  373. <a
  374. class=" text-gray-300 font-medium underline"
  375. href="https://github.com/open-webui/open-webui#troubleshooting"
  376. target="_blank"
  377. >
  378. {$i18n.t('Click here for help.')}
  379. </a>
  380. </div>
  381. {/if}
  382. </div>
  383. {:else}
  384. <div class="flex h-full justify-center">
  385. <div class="my-auto">
  386. <Spinner className="size-6" />
  387. </div>
  388. </div>
  389. {/if}
  390. </div>
  391. <div class="flex justify-end pt-3 text-sm font-medium">
  392. <button
  393. class=" px-4 py-2 bg-emerald-700 hover:bg-emerald-800 text-gray-100 transition rounded-lg"
  394. type="submit"
  395. >
  396. {$i18n.t('Save')}
  397. </button>
  398. </div>
  399. </form>