SettingsModal.svelte 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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 API_BASE_URL = BUILD_TIME_API_BASE_URL;
  9. let system = '';
  10. let temperature = 0.8;
  11. let selectedMenu = 'general';
  12. let modelTag = '';
  13. let deleteModelTag = '';
  14. let digest = '';
  15. let pullProgress = '';
  16. const splitStream = (splitOn) => {
  17. let buffer = '';
  18. return new TransformStream({
  19. transform(chunk, controller) {
  20. buffer += chunk;
  21. const parts = buffer.split(splitOn);
  22. parts.slice(0, -1).forEach((part) => controller.enqueue(part));
  23. buffer = parts[parts.length - 1];
  24. },
  25. flush(controller) {
  26. if (buffer) controller.enqueue(buffer);
  27. }
  28. });
  29. };
  30. const checkOllamaConnection = async () => {
  31. if (API_BASE_URL === '') {
  32. API_BASE_URL = BUILD_TIME_API_BASE_URL;
  33. }
  34. const res = await getModelTags(API_BASE_URL);
  35. if (res) {
  36. toast.success('Server connection verified');
  37. saveSettings(
  38. API_BASE_URL,
  39. system != '' ? system : null,
  40. temperature != 0.8 ? temperature : null
  41. );
  42. }
  43. };
  44. const pullModelHandler = async () => {
  45. const res = await fetch(`${API_BASE_URL}/pull`, {
  46. method: 'POST',
  47. headers: {
  48. 'Content-Type': 'text/event-stream'
  49. },
  50. body: JSON.stringify({
  51. name: modelTag
  52. })
  53. });
  54. const reader = res.body
  55. .pipeThrough(new TextDecoderStream())
  56. .pipeThrough(splitStream('\n'))
  57. .getReader();
  58. while (true) {
  59. const { value, done } = await reader.read();
  60. if (done) break;
  61. try {
  62. let lines = value.split('\n');
  63. for (const line of lines) {
  64. if (line !== '') {
  65. console.log(line);
  66. let data = JSON.parse(line);
  67. console.log(data);
  68. if (data.error) {
  69. throw data.error;
  70. }
  71. if (data.status) {
  72. if (!data.status.includes('downloading')) {
  73. toast.success(data.status);
  74. } else {
  75. digest = data.digest;
  76. if (data.completed) {
  77. pullProgress = Math.round((data.completed / data.total) * 1000) / 10;
  78. } else {
  79. pullProgress = 100;
  80. }
  81. }
  82. }
  83. }
  84. }
  85. } catch (error) {
  86. console.log(error);
  87. toast.error(error);
  88. }
  89. }
  90. modelTag = '';
  91. await getModelTags();
  92. };
  93. const deleteModelHandler = async () => {
  94. const res = await fetch(`${API_BASE_URL}/delete`, {
  95. method: 'DELETE',
  96. headers: {
  97. 'Content-Type': 'text/event-stream'
  98. },
  99. body: JSON.stringify({
  100. name: deleteModelTag
  101. })
  102. });
  103. const reader = res.body
  104. .pipeThrough(new TextDecoderStream())
  105. .pipeThrough(splitStream('\n'))
  106. .getReader();
  107. while (true) {
  108. const { value, done } = await reader.read();
  109. if (done) break;
  110. try {
  111. let lines = value.split('\n');
  112. for (const line of lines) {
  113. if (line !== '' && line !== 'null') {
  114. console.log(line);
  115. let data = JSON.parse(line);
  116. console.log(data);
  117. if (data.error) {
  118. throw data.error;
  119. }
  120. if (data.status) {
  121. }
  122. } else {
  123. toast.success(`Deleted ${deleteModelTag}`);
  124. }
  125. }
  126. } catch (error) {
  127. console.log(error);
  128. toast.error(error);
  129. }
  130. }
  131. deleteModelTag = '';
  132. await getModelTags();
  133. };
  134. $: if (show) {
  135. let settings = JSON.parse(localStorage.getItem('settings') ?? '{}');
  136. API_BASE_URL = settings.API_BASE_URL ?? BUILD_TIME_API_BASE_URL;
  137. system = settings.system ?? '';
  138. temperature = settings.temperature ?? 0.8;
  139. }
  140. </script>
  141. <Modal bind:show>
  142. <div class="rounded-lg bg-gray-900">
  143. <div class=" flex justify-between text-gray-300 px-5 py-4">
  144. <div class=" text-lg font-medium self-center">Settings</div>
  145. <button
  146. class="self-center"
  147. on:click={() => {
  148. show = false;
  149. }}
  150. >
  151. <svg
  152. xmlns="http://www.w3.org/2000/svg"
  153. viewBox="0 0 20 20"
  154. fill="currentColor"
  155. class="w-5 h-5"
  156. >
  157. <path
  158. 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"
  159. />
  160. </svg>
  161. </button>
  162. </div>
  163. <hr class=" border-gray-800" />
  164. <div class="flex flex-col md:flex-row w-full p-4 md:space-x-4">
  165. <div
  166. 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"
  167. >
  168. <button
  169. class="px-2 py-2 rounded flex-1 md:flex-none flex text-right transition {selectedMenu ===
  170. 'general'
  171. ? 'bg-gray-700'
  172. : 'hover:bg-gray-800'}"
  173. on:click={() => {
  174. selectedMenu = 'general';
  175. }}
  176. >
  177. <div class=" self-center mr-2">
  178. <svg
  179. xmlns="http://www.w3.org/2000/svg"
  180. viewBox="0 0 20 20"
  181. fill="currentColor"
  182. class="w-4 h-4"
  183. >
  184. <path
  185. fill-rule="evenodd"
  186. 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"
  187. clip-rule="evenodd"
  188. />
  189. </svg>
  190. </div>
  191. <div class=" self-center">General</div>
  192. </button>
  193. <button
  194. class="px-2 py-2 rounded flex-1 md:flex-none flex text-right transition {selectedMenu ===
  195. 'models'
  196. ? 'bg-gray-700'
  197. : 'hover:bg-gray-800'}"
  198. on:click={() => {
  199. selectedMenu = 'models';
  200. }}
  201. >
  202. <div class=" self-center mr-2">
  203. <svg
  204. xmlns="http://www.w3.org/2000/svg"
  205. viewBox="0 0 20 20"
  206. fill="currentColor"
  207. class="w-4 h-4"
  208. >
  209. <path
  210. fill-rule="evenodd"
  211. 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"
  212. clip-rule="evenodd"
  213. />
  214. </svg>
  215. </div>
  216. <div class=" self-center">Models</div>
  217. </button>
  218. </div>
  219. <div class="flex-1 md:min-h-[300px]">
  220. {#if selectedMenu === 'general'}
  221. <div class="flex flex-col space-y-3">
  222. <div>
  223. <div class=" mb-2.5 text-sm font-medium">Ollama Server URL</div>
  224. <div class="flex w-full">
  225. <div class="flex-1 mr-2">
  226. <input
  227. class="w-full rounded py-2 px-4 text-sm text-gray-300 bg-gray-800 outline-none"
  228. placeholder="Enter URL (e.g. http://localhost:11434/api)"
  229. bind:value={API_BASE_URL}
  230. />
  231. </div>
  232. <button
  233. class="px-3 bg-gray-600 hover:bg-gray-700 rounded transition"
  234. on:click={() => {
  235. checkOllamaConnection();
  236. }}
  237. >
  238. <svg
  239. xmlns="http://www.w3.org/2000/svg"
  240. viewBox="0 0 20 20"
  241. fill="currentColor"
  242. class="w-4 h-4"
  243. >
  244. <path
  245. fill-rule="evenodd"
  246. 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"
  247. clip-rule="evenodd"
  248. />
  249. </svg>
  250. </button>
  251. </div>
  252. <div class="mt-2 text-xs text-gray-500">
  253. Trouble accessing Ollama? <a
  254. class=" text-gray-300 font-medium"
  255. href="https://github.com/ollama-webui/ollama-webui#troubleshooting"
  256. target="_blank"
  257. >
  258. Click here for help.
  259. </a>
  260. </div>
  261. </div>
  262. <hr class=" border-gray-700" />
  263. <div>
  264. <div class=" mb-2.5 text-sm font-medium">System Prompt</div>
  265. <textarea
  266. bind:value={system}
  267. class="w-full rounded p-4 text-sm text-gray-300 bg-gray-800 outline-none"
  268. rows="4"
  269. />
  270. </div>
  271. <hr class=" border-gray-700" />
  272. <div>
  273. <label for="steps-range" class=" mb-2 text-sm font-medium flex justify-between">
  274. <div>Temperature</div>
  275. <div>
  276. {temperature}
  277. </div></label
  278. >
  279. <input
  280. id="steps-range"
  281. type="range"
  282. min="0"
  283. max="1"
  284. bind:value={temperature}
  285. step="0.05"
  286. class="w-full h-2 rounded-lg appearance-none cursor-pointer bg-gray-700"
  287. />
  288. </div>
  289. <div class="flex justify-end pt-3 text-sm font-medium">
  290. <button
  291. class=" px-4 py-2 bg-emerald-600 hover:bg-emerald-700 transition rounded"
  292. on:click={() => {
  293. saveSettings(
  294. API_BASE_URL === '' ? BUILD_TIME_API_BASE_URL : API_BASE_URL,
  295. system != '' ? system : null,
  296. temperature != 0.8 ? temperature : null
  297. );
  298. show = false;
  299. }}
  300. >
  301. Save
  302. </button>
  303. </div>
  304. </div>
  305. {:else if selectedMenu === 'models'}
  306. <div class="flex flex-col space-y-3 text-sm">
  307. <div>
  308. <div class=" mb-2.5 text-sm font-medium">Pull a model</div>
  309. <div class="flex w-full">
  310. <div class="flex-1 mr-2">
  311. <input
  312. class="w-full rounded py-2 px-4 text-sm text-gray-300 bg-gray-800 outline-none"
  313. placeholder="Enter model tag (e.g. mistral:7b)"
  314. bind:value={modelTag}
  315. />
  316. </div>
  317. <button
  318. class="px-3 bg-emerald-600 hover:bg-emerald-700 rounded transition"
  319. on:click={() => {
  320. pullModelHandler();
  321. }}
  322. >
  323. <svg
  324. xmlns="http://www.w3.org/2000/svg"
  325. viewBox="0 0 20 20"
  326. fill="currentColor"
  327. class="w-4 h-4"
  328. >
  329. <path
  330. 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"
  331. />
  332. <path
  333. 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"
  334. />
  335. </svg>
  336. </button>
  337. </div>
  338. <div class="mt-2 text-xs text-gray-500">
  339. To access the available model names for downloading, <a
  340. class=" text-gray-300 font-medium"
  341. href="https://ollama.ai/library"
  342. target="_blank">click here.</a
  343. >
  344. </div>
  345. {#if pullProgress !== ''}
  346. <div class="mt-2">
  347. <div class=" mb-2 text-xs">Pull Progress</div>
  348. <div class="w-full rounded-full bg-gray-800">
  349. <div
  350. class="bg-gray-600 text-xs font-medium text-blue-100 text-center p-0.5 leading-none rounded-full"
  351. style="width: {Math.max(15, pullProgress)}%"
  352. >
  353. {pullProgress}%
  354. </div>
  355. </div>
  356. <div class="mt-1 text-xs text-gray-700" style="font-size: 0.5rem;">
  357. {digest}
  358. </div>
  359. </div>
  360. {/if}
  361. </div>
  362. <hr class=" border-gray-700" />
  363. <div>
  364. <div class=" mb-2.5 text-sm font-medium">Delete a model</div>
  365. <div class="flex w-full">
  366. <div class="flex-1 mr-2">
  367. <input
  368. class="w-full rounded py-2 px-4 text-sm text-gray-300 bg-gray-800 outline-none"
  369. placeholder="Enter model tag (e.g. mistral:7b)"
  370. bind:value={deleteModelTag}
  371. />
  372. </div>
  373. <button
  374. class="px-3 bg-red-700 hover:bg-red-800 rounded transition"
  375. on:click={() => {
  376. deleteModelHandler();
  377. }}
  378. >
  379. <svg
  380. xmlns="http://www.w3.org/2000/svg"
  381. viewBox="0 0 20 20"
  382. fill="currentColor"
  383. class="w-4 h-4"
  384. >
  385. <path
  386. fill-rule="evenodd"
  387. 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"
  388. clip-rule="evenodd"
  389. />
  390. </svg>
  391. </button>
  392. </div>
  393. </div>
  394. </div>
  395. {/if}
  396. </div>
  397. </div>
  398. </div>
  399. </Modal>