SettingsModal.svelte 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. <script lang="ts">
  2. import Modal from '../common/Modal.svelte';
  3. import { API_BASE_URL as BUILD_TIME_API_BASE_URL } from '$lib/constants';
  4. import toast from 'svelte-french-toast';
  5. export let show = false;
  6. export let saveSettings: Function;
  7. export let getModelTags: Function;
  8. let selectedTab = 'general';
  9. // General
  10. let API_BASE_URL = BUILD_TIME_API_BASE_URL;
  11. let OPENAI_API_KEY = '';
  12. let system = '';
  13. // Models
  14. let modelTag = '';
  15. let deleteModelTag = '';
  16. let digest = '';
  17. let pullProgress = null;
  18. // Advanced
  19. let temperature = 0.8;
  20. const splitStream = (splitOn) => {
  21. let buffer = '';
  22. return new TransformStream({
  23. transform(chunk, controller) {
  24. buffer += chunk;
  25. const parts = buffer.split(splitOn);
  26. parts.slice(0, -1).forEach((part) => controller.enqueue(part));
  27. buffer = parts[parts.length - 1];
  28. },
  29. flush(controller) {
  30. if (buffer) controller.enqueue(buffer);
  31. }
  32. });
  33. };
  34. const checkOllamaConnection = async () => {
  35. if (API_BASE_URL === '') {
  36. API_BASE_URL = BUILD_TIME_API_BASE_URL;
  37. }
  38. const res = await getModelTags(API_BASE_URL);
  39. if (res) {
  40. toast.success('Server connection verified');
  41. saveSettings({
  42. API_BASE_URL: API_BASE_URL
  43. });
  44. }
  45. };
  46. const pullModelHandler = async () => {
  47. const res = await fetch(`${API_BASE_URL}/pull`, {
  48. method: 'POST',
  49. headers: {
  50. 'Content-Type': 'text/event-stream'
  51. },
  52. body: JSON.stringify({
  53. name: modelTag
  54. })
  55. });
  56. const reader = res.body
  57. .pipeThrough(new TextDecoderStream())
  58. .pipeThrough(splitStream('\n'))
  59. .getReader();
  60. while (true) {
  61. const { value, done } = await reader.read();
  62. if (done) break;
  63. try {
  64. let lines = value.split('\n');
  65. for (const line of lines) {
  66. if (line !== '') {
  67. console.log(line);
  68. let data = JSON.parse(line);
  69. console.log(data);
  70. if (data.error) {
  71. throw data.error;
  72. }
  73. if (data.status) {
  74. if (!data.status.includes('downloading')) {
  75. toast.success(data.status);
  76. } else {
  77. digest = data.digest;
  78. if (data.completed) {
  79. pullProgress = Math.round((data.completed / data.total) * 1000) / 10;
  80. } else {
  81. pullProgress = 100;
  82. }
  83. }
  84. }
  85. }
  86. }
  87. } catch (error) {
  88. console.log(error);
  89. toast.error(error);
  90. }
  91. }
  92. modelTag = '';
  93. await getModelTags();
  94. };
  95. const deleteModelHandler = async () => {
  96. const res = await fetch(`${API_BASE_URL}/delete`, {
  97. method: 'DELETE',
  98. headers: {
  99. 'Content-Type': 'text/event-stream'
  100. },
  101. body: JSON.stringify({
  102. name: deleteModelTag
  103. })
  104. });
  105. const reader = res.body
  106. .pipeThrough(new TextDecoderStream())
  107. .pipeThrough(splitStream('\n'))
  108. .getReader();
  109. while (true) {
  110. const { value, done } = await reader.read();
  111. if (done) break;
  112. try {
  113. let lines = value.split('\n');
  114. for (const line of lines) {
  115. if (line !== '' && line !== 'null') {
  116. console.log(line);
  117. let data = JSON.parse(line);
  118. console.log(data);
  119. if (data.error) {
  120. throw data.error;
  121. }
  122. if (data.status) {
  123. }
  124. } else {
  125. toast.success(`Deleted ${deleteModelTag}`);
  126. }
  127. }
  128. } catch (error) {
  129. console.log(error);
  130. toast.error(error);
  131. }
  132. }
  133. deleteModelTag = '';
  134. await getModelTags();
  135. };
  136. $: if (show) {
  137. let settings = JSON.parse(localStorage.getItem('settings') ?? '{}');
  138. console.log(settings);
  139. API_BASE_URL = settings.API_BASE_URL ?? BUILD_TIME_API_BASE_URL;
  140. OPENAI_API_KEY = settings.OPENAI_API_KEY ?? '';
  141. system = settings.system ?? '';
  142. temperature = settings.temperature ?? 0.8;
  143. }
  144. </script>
  145. <Modal bind:show>
  146. <div class="rounded-lg bg-gray-900">
  147. <div class=" flex justify-between text-gray-300 px-5 py-4">
  148. <div class=" text-lg font-medium self-center">Settings</div>
  149. <button
  150. class="self-center"
  151. on:click={() => {
  152. show = false;
  153. }}
  154. >
  155. <svg
  156. xmlns="http://www.w3.org/2000/svg"
  157. viewBox="0 0 20 20"
  158. fill="currentColor"
  159. class="w-5 h-5"
  160. >
  161. <path
  162. 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"
  163. />
  164. </svg>
  165. </button>
  166. </div>
  167. <hr class=" border-gray-800" />
  168. <div class="flex flex-col md:flex-row w-full p-4 md:space-x-4">
  169. <div
  170. class="flex flex-row space-x-1 md:space-x-0 md:space-y-1 md:flex-col flex-1 md:flex-none md:w-40 text-gray-200 text-xs text-left mb-3 md:mb-0"
  171. >
  172. <button
  173. class="px-2.5 py-2.5 rounded-lg flex-1 md:flex-none flex text-right transition {selectedTab ===
  174. 'general'
  175. ? 'bg-gray-700'
  176. : 'hover:bg-gray-800'}"
  177. on:click={() => {
  178. selectedTab = 'general';
  179. }}
  180. >
  181. <div class=" self-center mr-2">
  182. <svg
  183. xmlns="http://www.w3.org/2000/svg"
  184. viewBox="0 0 20 20"
  185. fill="currentColor"
  186. class="w-4 h-4"
  187. >
  188. <path
  189. fill-rule="evenodd"
  190. d="M8.34 1.804A1 1 0 019.32 1h1.36a1 1 0 01.98.804l.295 1.473c.497.144.971.342 1.416.587l1.25-.834a1 1 0 011.262.125l.962.962a1 1 0 01.125 1.262l-.834 1.25c.245.445.443.919.587 1.416l1.473.294a1 1 0 01.804.98v1.361a1 1 0 01-.804.98l-1.473.295a6.95 6.95 0 01-.587 1.416l.834 1.25a1 1 0 01-.125 1.262l-.962.962a1 1 0 01-1.262.125l-1.25-.834a6.953 6.953 0 01-1.416.587l-.294 1.473a1 1 0 01-.98.804H9.32a1 1 0 01-.98-.804l-.295-1.473a6.957 6.957 0 01-1.416-.587l-1.25.834a1 1 0 01-1.262-.125l-.962-.962a1 1 0 01-.125-1.262l.834-1.25a6.957 6.957 0 01-.587-1.416l-1.473-.294A1 1 0 011 10.68V9.32a1 1 0 01.804-.98l1.473-.295c.144-.497.342-.971.587-1.416l-.834-1.25a1 1 0 01.125-1.262l.962-.962A1 1 0 015.38 3.03l1.25.834a6.957 6.957 0 011.416-.587l.294-1.473zM13 10a3 3 0 11-6 0 3 3 0 016 0z"
  191. clip-rule="evenodd"
  192. />
  193. </svg>
  194. </div>
  195. <div class=" self-center">General</div>
  196. </button>
  197. <button
  198. class="px-2.5 py-2.5 rounded-lg flex-1 md:flex-none flex text-right transition {selectedTab ===
  199. 'models'
  200. ? 'bg-gray-700'
  201. : 'hover:bg-gray-800'}"
  202. on:click={() => {
  203. selectedTab = 'models';
  204. }}
  205. >
  206. <div class=" self-center mr-2">
  207. <svg
  208. xmlns="http://www.w3.org/2000/svg"
  209. viewBox="0 0 20 20"
  210. fill="currentColor"
  211. class="w-4 h-4"
  212. >
  213. <path
  214. fill-rule="evenodd"
  215. d="M10 1c3.866 0 7 1.79 7 4s-3.134 4-7 4-7-1.79-7-4 3.134-4 7-4zm5.694 8.13c.464-.264.91-.583 1.306-.952V10c0 2.21-3.134 4-7 4s-7-1.79-7-4V8.178c.396.37.842.688 1.306.953C5.838 10.006 7.854 10.5 10 10.5s4.162-.494 5.694-1.37zM3 13.179V15c0 2.21 3.134 4 7 4s7-1.79 7-4v-1.822c-.396.37-.842.688-1.306.953-1.532.875-3.548 1.369-5.694 1.369s-4.162-.494-5.694-1.37A7.009 7.009 0 013 13.179z"
  216. clip-rule="evenodd"
  217. />
  218. </svg>
  219. </div>
  220. <div class=" self-center">Models</div>
  221. </button>
  222. <button
  223. class="px-2.5 py-2.5 rounded-lg flex-1 md:flex-none flex text-right transition {selectedTab ===
  224. 'advanced'
  225. ? 'bg-gray-700'
  226. : 'hover:bg-gray-800'}"
  227. on:click={() => {
  228. selectedTab = 'advanced';
  229. }}
  230. >
  231. <div class=" self-center mr-2">
  232. <svg
  233. xmlns="http://www.w3.org/2000/svg"
  234. viewBox="0 0 20 20"
  235. fill="currentColor"
  236. class="w-4 h-4"
  237. >
  238. <path
  239. d="M12 4.467c0-.405.262-.75.559-1.027.276-.257.441-.584.441-.94 0-.828-.895-1.5-2-1.5s-2 .672-2 1.5c0 .362.171.694.456.953.29.265.544.6.544.994a.968.968 0 01-1.024.974 39.655 39.655 0 01-3.014-.306.75.75 0 00-.847.847c.14.993.242 1.999.306 3.014A.968.968 0 014.447 10c-.393 0-.729-.253-.994-.544C3.194 9.17 2.862 9 2.5 9 1.672 9 1 9.895 1 11s.672 2 1.5 2c.356 0 .683-.165.94-.441.276-.297.622-.559 1.027-.559a.997.997 0 011.004 1.03 39.747 39.747 0 01-.319 3.734.75.75 0 00.64.842c1.05.146 2.111.252 3.184.318A.97.97 0 0010 16.948c0-.394-.254-.73-.545-.995C9.171 15.693 9 15.362 9 15c0-.828.895-1.5 2-1.5s2 .672 2 1.5c0 .356-.165.683-.441.94-.297.276-.559.622-.559 1.027a.998.998 0 001.03 1.005c1.337-.05 2.659-.162 3.961-.337a.75.75 0 00.644-.644c.175-1.302.288-2.624.337-3.961A.998.998 0 0016.967 12c-.405 0-.75.262-1.027.559-.257.276-.584.441-.94.441-.828 0-1.5-.895-1.5-2s.672-2 1.5-2c.362 0 .694.17.953.455.265.291.601.545.995.545a.97.97 0 00.976-1.024 41.159 41.159 0 00-.318-3.184.75.75 0 00-.842-.64c-1.228.164-2.473.271-3.734.319A.997.997 0 0112 4.467z"
  240. />
  241. </svg>
  242. </div>
  243. <div class=" self-center">Advanced</div>
  244. </button>
  245. </div>
  246. <div class="flex-1 md:min-h-[300px]">
  247. {#if selectedTab === 'general'}
  248. <div class="flex flex-col space-y-3">
  249. <div>
  250. <div class=" mb-2.5 text-sm font-medium">Ollama Server URL</div>
  251. <div class="flex w-full">
  252. <div class="flex-1 mr-2">
  253. <input
  254. class="w-full rounded py-2 px-4 text-sm text-gray-300 bg-gray-800 outline-none"
  255. placeholder="Enter URL (e.g. http://localhost:11434/api)"
  256. bind:value={API_BASE_URL}
  257. />
  258. </div>
  259. <button
  260. class="px-3 bg-gray-600 hover:bg-gray-700 rounded transition"
  261. on:click={() => {
  262. checkOllamaConnection();
  263. }}
  264. >
  265. <svg
  266. xmlns="http://www.w3.org/2000/svg"
  267. viewBox="0 0 20 20"
  268. fill="currentColor"
  269. class="w-4 h-4"
  270. >
  271. <path
  272. fill-rule="evenodd"
  273. 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"
  274. clip-rule="evenodd"
  275. />
  276. </svg>
  277. </button>
  278. </div>
  279. <div class="mt-2 text-xs text-gray-500">
  280. Trouble accessing Ollama? <a
  281. class=" text-gray-300 font-medium"
  282. href="https://github.com/ollama-webui/ollama-webui#troubleshooting"
  283. target="_blank"
  284. >
  285. Click here for help.
  286. </a>
  287. </div>
  288. </div>
  289. <hr class=" border-gray-700" />
  290. <div>
  291. <div class=" mb-2.5 text-sm font-medium">
  292. OpenAI API Key <span class=" text-gray-400 text-sm">(optional)</span>
  293. </div>
  294. <div class="flex w-full">
  295. <div class="flex-1 mr-2">
  296. <input
  297. class="w-full rounded py-2 px-4 text-sm text-gray-300 bg-gray-800 outline-none"
  298. placeholder="Enter OpenAI API Key"
  299. bind:value={OPENAI_API_KEY}
  300. autocomplete="off"
  301. />
  302. </div>
  303. </div>
  304. <div class="mt-2 text-xs text-gray-500">
  305. Adds optional support for 'gpt-*' models available.
  306. </div>
  307. </div>
  308. <hr class=" border-gray-700" />
  309. <div>
  310. <div class=" mb-2.5 text-sm font-medium">System Prompt</div>
  311. <textarea
  312. bind:value={system}
  313. class="w-full rounded p-4 text-sm text-gray-300 bg-gray-800 outline-none resize-none"
  314. rows="4"
  315. />
  316. </div>
  317. <div class="flex justify-end pt-3 text-sm font-medium">
  318. <button
  319. class=" px-4 py-2 bg-emerald-600 hover:bg-emerald-700 transition rounded"
  320. on:click={() => {
  321. saveSettings({
  322. API_BASE_URL: API_BASE_URL === '' ? BUILD_TIME_API_BASE_URL : API_BASE_URL,
  323. OPENAI_API_KEY: OPENAI_API_KEY !== '' ? OPENAI_API_KEY : undefined,
  324. system: system !== '' ? system : undefined
  325. });
  326. show = false;
  327. }}
  328. >
  329. Save
  330. </button>
  331. </div>
  332. </div>
  333. {:else if selectedTab === 'models'}
  334. <div class="flex flex-col space-y-3 text-sm mb-10">
  335. <div>
  336. <div class=" mb-2.5 text-sm font-medium">Pull a model</div>
  337. <div class="flex w-full">
  338. <div class="flex-1 mr-2">
  339. <input
  340. class="w-full rounded py-2 px-4 text-sm text-gray-300 bg-gray-800 outline-none"
  341. placeholder="Enter model tag (e.g. mistral:7b)"
  342. bind:value={modelTag}
  343. />
  344. </div>
  345. <button
  346. class="px-3 bg-emerald-600 hover:bg-emerald-700 rounded transition"
  347. on:click={() => {
  348. pullModelHandler();
  349. }}
  350. >
  351. <svg
  352. xmlns="http://www.w3.org/2000/svg"
  353. viewBox="0 0 20 20"
  354. fill="currentColor"
  355. class="w-4 h-4"
  356. >
  357. <path
  358. d="M10.75 2.75a.75.75 0 00-1.5 0v8.614L6.295 8.235a.75.75 0 10-1.09 1.03l4.25 4.5a.75.75 0 001.09 0l4.25-4.5a.75.75 0 00-1.09-1.03l-2.955 3.129V2.75z"
  359. />
  360. <path
  361. d="M3.5 12.75a.75.75 0 00-1.5 0v2.5A2.75 2.75 0 004.75 18h10.5A2.75 2.75 0 0018 15.25v-2.5a.75.75 0 00-1.5 0v2.5c0 .69-.56 1.25-1.25 1.25H4.75c-.69 0-1.25-.56-1.25-1.25v-2.5z"
  362. />
  363. </svg>
  364. </button>
  365. </div>
  366. <div class="mt-2 text-xs text-gray-500">
  367. To access the available model names for downloading, <a
  368. class=" text-gray-300 font-medium"
  369. href="https://ollama.ai/library"
  370. target="_blank">click here.</a
  371. >
  372. </div>
  373. {#if pullProgress !== null}
  374. <div class="mt-2">
  375. <div class=" mb-2 text-xs">Pull Progress</div>
  376. <div class="w-full rounded-full bg-gray-800">
  377. <div
  378. class="bg-gray-600 text-xs font-medium text-blue-100 text-center p-0.5 leading-none rounded-full"
  379. style="width: {Math.max(15, pullProgress ?? 0)}%"
  380. >
  381. {pullProgress ?? 0}%
  382. </div>
  383. </div>
  384. <div class="mt-1 text-xs text-gray-700" style="font-size: 0.5rem;">
  385. {digest}
  386. </div>
  387. </div>
  388. {/if}
  389. </div>
  390. <hr class=" border-gray-700" />
  391. <div>
  392. <div class=" mb-2.5 text-sm font-medium">Delete a model</div>
  393. <div class="flex w-full">
  394. <div class="flex-1 mr-2">
  395. <input
  396. class="w-full rounded py-2 px-4 text-sm text-gray-300 bg-gray-800 outline-none"
  397. placeholder="Enter model tag (e.g. mistral:7b)"
  398. bind:value={deleteModelTag}
  399. />
  400. </div>
  401. <button
  402. class="px-3 bg-red-700 hover:bg-red-800 rounded transition"
  403. on:click={() => {
  404. deleteModelHandler();
  405. }}
  406. >
  407. <svg
  408. xmlns="http://www.w3.org/2000/svg"
  409. viewBox="0 0 20 20"
  410. fill="currentColor"
  411. class="w-4 h-4"
  412. >
  413. <path
  414. fill-rule="evenodd"
  415. d="M8.75 1A2.75 2.75 0 006 3.75v.443c-.795.077-1.584.176-2.365.298a.75.75 0 10.23 1.482l.149-.022.841 10.518A2.75 2.75 0 007.596 19h4.807a2.75 2.75 0 002.742-2.53l.841-10.52.149.023a.75.75 0 00.23-1.482A41.03 41.03 0 0014 4.193V3.75A2.75 2.75 0 0011.25 1h-2.5zM10 4c.84 0 1.673.025 2.5.075V3.75c0-.69-.56-1.25-1.25-1.25h-2.5c-.69 0-1.25.56-1.25 1.25v.325C8.327 4.025 9.16 4 10 4zM8.58 7.72a.75.75 0 00-1.5.06l.3 7.5a.75.75 0 101.5-.06l-.3-7.5zm4.34.06a.75.75 0 10-1.5-.06l-.3 7.5a.75.75 0 101.5.06l.3-7.5z"
  416. clip-rule="evenodd"
  417. />
  418. </svg>
  419. </button>
  420. </div>
  421. </div>
  422. </div>
  423. {:else if selectedTab === 'advanced'}
  424. <div class="flex flex-col h-full justify-between space-y-3 text-sm">
  425. <div>
  426. <label for="steps-range" class=" mb-2 text-sm font-medium flex justify-between">
  427. <div>Temperature</div>
  428. <div>
  429. {temperature}
  430. </div></label
  431. >
  432. <input
  433. id="steps-range"
  434. type="range"
  435. min="0"
  436. max="1"
  437. bind:value={temperature}
  438. step="0.05"
  439. class="w-full h-2 rounded-lg appearance-none cursor-pointer bg-gray-700"
  440. />
  441. </div>
  442. <div class="flex justify-end pt-3 text-sm font-medium">
  443. <button
  444. class=" px-4 py-2 bg-emerald-600 hover:bg-emerald-700 transition rounded"
  445. on:click={() => {
  446. saveSettings({
  447. temperature: temperature !== 0.8 ? temperature : undefined
  448. });
  449. show = false;
  450. }}
  451. >
  452. Save
  453. </button>
  454. </div>
  455. </div>
  456. {/if}
  457. </div>
  458. </div>
  459. </div>
  460. </Modal>