Connections.svelte 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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 placeholder={$i18n.t('API Key')} value={OPENAI_API_KEYS[idx]} />
  196. <div class="self-center flex items-center">
  197. {#if idx === 0}
  198. <button
  199. class="px-1"
  200. on:click={() => {
  201. OPENAI_API_BASE_URLS = [...OPENAI_API_BASE_URLS, ''];
  202. OPENAI_API_KEYS = [...OPENAI_API_KEYS, ''];
  203. }}
  204. type="button"
  205. >
  206. <svg
  207. xmlns="http://www.w3.org/2000/svg"
  208. viewBox="0 0 16 16"
  209. fill="currentColor"
  210. class="w-4 h-4"
  211. >
  212. <path
  213. 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"
  214. />
  215. </svg>
  216. </button>
  217. {:else}
  218. <button
  219. class="px-1"
  220. on:click={() => {
  221. OPENAI_API_BASE_URLS = OPENAI_API_BASE_URLS.filter(
  222. (url, urlIdx) => idx !== urlIdx
  223. );
  224. OPENAI_API_KEYS = OPENAI_API_KEYS.filter((key, keyIdx) => idx !== keyIdx);
  225. }}
  226. type="button"
  227. >
  228. <svg
  229. xmlns="http://www.w3.org/2000/svg"
  230. viewBox="0 0 16 16"
  231. fill="currentColor"
  232. class="w-4 h-4"
  233. >
  234. <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" />
  235. </svg>
  236. </button>
  237. {/if}
  238. </div>
  239. <div class="flex">
  240. <Tooltip content="Verify connection" className="self-start mt-0.5">
  241. <button
  242. class="self-center p-2 bg-gray-200 hover:bg-gray-300 dark:bg-gray-900 dark:hover:bg-gray-850 rounded-lg transition"
  243. on:click={() => {
  244. verifyOpenAIHandler(idx);
  245. }}
  246. type="button"
  247. >
  248. <svg
  249. xmlns="http://www.w3.org/2000/svg"
  250. viewBox="0 0 20 20"
  251. fill="currentColor"
  252. class="w-4 h-4"
  253. >
  254. <path
  255. fill-rule="evenodd"
  256. 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"
  257. clip-rule="evenodd"
  258. />
  259. </svg>
  260. </button>
  261. </Tooltip>
  262. </div>
  263. </div>
  264. <div class=" mb-1 text-xs text-gray-400 dark:text-gray-500">
  265. {$i18n.t('WebUI will make requests to')}
  266. <span class=" text-gray-200">'{url}/models'</span>
  267. </div>
  268. {/each}
  269. </div>
  270. {/if}
  271. </div>
  272. </div>
  273. <hr class=" dark:border-gray-850" />
  274. <div class="pr-1.5 space-y-2">
  275. <div class="flex justify-between items-center text-sm">
  276. <div class=" font-medium">{$i18n.t('Ollama API')}</div>
  277. <div class="mt-1">
  278. <Switch
  279. bind:state={ENABLE_OLLAMA_API}
  280. on:change={async () => {
  281. updateOllamaConfig(localStorage.token, ENABLE_OLLAMA_API);
  282. if (OLLAMA_BASE_URLS.length === 0) {
  283. OLLAMA_BASE_URLS = [''];
  284. }
  285. }}
  286. />
  287. </div>
  288. </div>
  289. {#if ENABLE_OLLAMA_API}
  290. <div class="flex w-full gap-1.5">
  291. <div class="flex-1 flex flex-col gap-2">
  292. {#each OLLAMA_BASE_URLS as url, idx}
  293. <div class="flex gap-1.5">
  294. <input
  295. class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
  296. placeholder={$i18n.t('Enter URL (e.g. http://localhost:11434)')}
  297. bind:value={url}
  298. />
  299. <div class="self-center flex items-center">
  300. {#if idx === 0}
  301. <button
  302. class="px-1"
  303. on:click={() => {
  304. OLLAMA_BASE_URLS = [...OLLAMA_BASE_URLS, ''];
  305. }}
  306. type="button"
  307. >
  308. <svg
  309. xmlns="http://www.w3.org/2000/svg"
  310. viewBox="0 0 16 16"
  311. fill="currentColor"
  312. class="w-4 h-4"
  313. >
  314. <path
  315. 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"
  316. />
  317. </svg>
  318. </button>
  319. {:else}
  320. <button
  321. class="px-1"
  322. on:click={() => {
  323. OLLAMA_BASE_URLS = OLLAMA_BASE_URLS.filter(
  324. (url, urlIdx) => idx !== urlIdx
  325. );
  326. }}
  327. type="button"
  328. >
  329. <svg
  330. xmlns="http://www.w3.org/2000/svg"
  331. viewBox="0 0 16 16"
  332. fill="currentColor"
  333. class="w-4 h-4"
  334. >
  335. <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" />
  336. </svg>
  337. </button>
  338. {/if}
  339. </div>
  340. <div class="flex">
  341. <Tooltip content="Verify connection" className="self-start mt-0.5">
  342. <button
  343. class="self-center p-2 bg-gray-200 hover:bg-gray-300 dark:bg-gray-900 dark:hover:bg-gray-850 rounded-lg transition"
  344. on:click={() => {
  345. verifyOllamaHandler(idx);
  346. }}
  347. type="button"
  348. >
  349. <svg
  350. xmlns="http://www.w3.org/2000/svg"
  351. viewBox="0 0 20 20"
  352. fill="currentColor"
  353. class="w-4 h-4"
  354. >
  355. <path
  356. fill-rule="evenodd"
  357. 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"
  358. clip-rule="evenodd"
  359. />
  360. </svg>
  361. </button>
  362. </Tooltip>
  363. </div>
  364. </div>
  365. {/each}
  366. </div>
  367. </div>
  368. <div class="mt-2 text-xs text-gray-400 dark:text-gray-500">
  369. {$i18n.t('Trouble accessing Ollama?')}
  370. <a
  371. class=" text-gray-300 font-medium underline"
  372. href="https://github.com/open-webui/open-webui#troubleshooting"
  373. target="_blank"
  374. >
  375. {$i18n.t('Click here for help.')}
  376. </a>
  377. </div>
  378. {/if}
  379. </div>
  380. {:else}
  381. <div class="flex h-full justify-center">
  382. <div class="my-auto">
  383. <Spinner className="size-6" />
  384. </div>
  385. </div>
  386. {/if}
  387. </div>
  388. <div class="flex justify-end pt-3 text-sm font-medium">
  389. <button
  390. class=" px-4 py-2 bg-emerald-700 hover:bg-emerald-800 text-gray-100 transition rounded-lg"
  391. type="submit"
  392. >
  393. {$i18n.t('Save')}
  394. </button>
  395. </div>
  396. </form>