+page.svelte 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846
  1. <script lang="ts">
  2. import { openDB, deleteDB } from 'idb';
  3. import { v4 as uuidv4 } from 'uuid';
  4. import { marked } from 'marked';
  5. import fileSaver from 'file-saver';
  6. const { saveAs } = fileSaver;
  7. import hljs from 'highlight.js';
  8. import 'highlight.js/styles/github-dark.min.css';
  9. import auto_render from 'katex/dist/contrib/auto-render.mjs';
  10. import 'katex/dist/katex.min.css';
  11. import toast from 'svelte-french-toast';
  12. import { API_BASE_URL as BUILD_TIME_API_BASE_URL } from '$lib/constants';
  13. import { onMount, tick } from 'svelte';
  14. import Navbar from '$lib/components/layout/Navbar.svelte';
  15. import SettingsModal from '$lib/components/chat/SettingsModal.svelte';
  16. import Suggestions from '$lib/components/chat/Suggestions.svelte';
  17. let API_BASE_URL = BUILD_TIME_API_BASE_URL;
  18. let db;
  19. // let selectedModel = '';
  20. let selectedModels = [''];
  21. let settings = {
  22. system: null,
  23. temperature: null
  24. };
  25. let fileUploadEnabled = false;
  26. let speechRecognition;
  27. let speechRecognitionEnabled = true;
  28. let speechRecognitionListening = false;
  29. let models = [];
  30. let chats = [];
  31. let chatId = uuidv4();
  32. let title = '';
  33. let prompt = '';
  34. let messages = [];
  35. let history = {
  36. messages: {},
  37. currentId: null
  38. };
  39. $: if (history.currentId !== null) {
  40. let _messages = [];
  41. let currentMessage = history.messages[history.currentId];
  42. while (currentMessage !== null) {
  43. _messages.unshift({ ...currentMessage });
  44. currentMessage =
  45. currentMessage.parentId !== null ? history.messages[currentMessage.parentId] : null;
  46. }
  47. messages = _messages;
  48. }
  49. let showSettings = false;
  50. let stopResponseFlag = false;
  51. let autoScroll = true;
  52. let suggestions = ''; // $page.url.searchParams.get('suggestions');
  53. onMount(async () => {
  54. await Promise.all([await createNewChat(true), await setDBandLoadChats()]);
  55. });
  56. //////////////////////////
  57. // Helper functions
  58. //////////////////////////
  59. const splitStream = (splitOn) => {
  60. let buffer = '';
  61. return new TransformStream({
  62. transform(chunk, controller) {
  63. buffer += chunk;
  64. const parts = buffer.split(splitOn);
  65. parts.slice(0, -1).forEach((part) => controller.enqueue(part));
  66. buffer = parts[parts.length - 1];
  67. },
  68. flush(controller) {
  69. if (buffer) controller.enqueue(buffer);
  70. }
  71. });
  72. };
  73. const copyToClipboard = (text) => {
  74. if (!navigator.clipboard) {
  75. var textArea = document.createElement('textarea');
  76. textArea.value = text;
  77. // Avoid scrolling to bottom
  78. textArea.style.top = '0';
  79. textArea.style.left = '0';
  80. textArea.style.position = 'fixed';
  81. document.body.appendChild(textArea);
  82. textArea.focus();
  83. textArea.select();
  84. try {
  85. var successful = document.execCommand('copy');
  86. var msg = successful ? 'successful' : 'unsuccessful';
  87. console.log('Fallback: Copying text command was ' + msg);
  88. } catch (err) {
  89. console.error('Fallback: Oops, unable to copy', err);
  90. }
  91. document.body.removeChild(textArea);
  92. return;
  93. }
  94. navigator.clipboard.writeText(text).then(
  95. function () {
  96. console.log('Async: Copying to clipboard was successful!');
  97. toast.success('Copying to clipboard was successful!');
  98. },
  99. function (err) {
  100. console.error('Async: Could not copy text: ', err);
  101. }
  102. );
  103. };
  104. const createCopyCodeBlockButton = () => {
  105. // use a class selector if available
  106. let blocks = document.querySelectorAll('pre');
  107. console.log(blocks);
  108. blocks.forEach((block) => {
  109. // only add button if browser supports Clipboard API
  110. if (navigator.clipboard && block.childNodes.length < 2) {
  111. let code = block.querySelector('code');
  112. code.style.borderTopRightRadius = 0;
  113. code.style.borderTopLeftRadius = 0;
  114. let topBarDiv = document.createElement('div');
  115. topBarDiv.style.backgroundColor = '#202123';
  116. topBarDiv.style.overflowX = 'auto';
  117. topBarDiv.style.display = 'flex';
  118. topBarDiv.style.justifyContent = 'space-between';
  119. topBarDiv.style.padding = '0 1rem';
  120. topBarDiv.style.paddingTop = '4px';
  121. topBarDiv.style.borderTopRightRadius = '8px';
  122. topBarDiv.style.borderTopLeftRadius = '8px';
  123. let langDiv = document.createElement('div');
  124. let codeClassNames = code?.className.split(' ');
  125. langDiv.textContent =
  126. codeClassNames[0] === 'hljs' ? codeClassNames[1].slice(9) : codeClassNames[0].slice(9);
  127. langDiv.style.color = 'white';
  128. langDiv.style.margin = '4px';
  129. langDiv.style.fontSize = '0.75rem';
  130. let button = document.createElement('button');
  131. button.textContent = 'Copy Code';
  132. button.style.background = 'none';
  133. button.style.fontSize = '0.75rem';
  134. button.style.border = 'none';
  135. button.style.margin = '4px';
  136. button.style.cursor = 'pointer';
  137. button.style.color = '#ddd';
  138. button.addEventListener('click', () => copyCode(block, button));
  139. topBarDiv.appendChild(langDiv);
  140. topBarDiv.appendChild(button);
  141. block.prepend(topBarDiv);
  142. // button.addEventListener('click', async () => {
  143. // await copyCode(block, button);
  144. // });
  145. }
  146. });
  147. async function copyCode(block, button) {
  148. let code = block.querySelector('code');
  149. let text = code.innerText;
  150. await navigator.clipboard.writeText(text);
  151. // visual feedback that task is completed
  152. button.innerText = 'Copied!';
  153. setTimeout(() => {
  154. button.innerText = 'Copy Code';
  155. }, 1000);
  156. }
  157. };
  158. const renderLatex = () => {
  159. let chatMessageElements = document.getElementsByClassName('chat-assistant');
  160. // let lastChatMessageElement = chatMessageElements[chatMessageElements.length - 1];
  161. for (const element of chatMessageElements) {
  162. auto_render(element, {
  163. // customised options
  164. // • auto-render specific keys, e.g.:
  165. delimiters: [
  166. { left: '$$', right: '$$', display: true },
  167. { left: '$', right: '$', display: true },
  168. { left: '\\(', right: '\\)', display: true },
  169. { left: '\\[', right: '\\]', display: true }
  170. ],
  171. // • rendering keys, e.g.:
  172. throwOnError: false
  173. });
  174. }
  175. };
  176. const speechRecognitionHandler = () => {
  177. // Check if SpeechRecognition is supported
  178. if (speechRecognitionListening) {
  179. speechRecognition.stop();
  180. } else {
  181. if ('SpeechRecognition' in window || 'webkitSpeechRecognition' in window) {
  182. // Create a SpeechRecognition object
  183. speechRecognition = new (window.SpeechRecognition || window.webkitSpeechRecognition)();
  184. // Set continuous to true for continuous recognition
  185. speechRecognition.continuous = true;
  186. // Set the timeout for turning off the recognition after inactivity (in milliseconds)
  187. const inactivityTimeout = 3000; // 3 seconds
  188. let timeoutId;
  189. // Start recognition
  190. speechRecognition.start();
  191. speechRecognitionListening = true;
  192. // Event triggered when speech is recognized
  193. speechRecognition.onresult = function (event) {
  194. // Clear the inactivity timeout
  195. clearTimeout(timeoutId);
  196. // Handle recognized speech
  197. console.log(event);
  198. const transcript = event.results[Object.keys(event.results).length - 1][0].transcript;
  199. prompt = `${prompt}${transcript}`;
  200. // Restart the inactivity timeout
  201. timeoutId = setTimeout(() => {
  202. console.log('Speech recognition turned off due to inactivity.');
  203. speechRecognition.stop();
  204. }, inactivityTimeout);
  205. };
  206. // Event triggered when recognition is ended
  207. speechRecognition.onend = function () {
  208. // Restart recognition after it ends
  209. console.log('recognition ended');
  210. speechRecognitionListening = false;
  211. if (prompt !== '' && settings?.speechAutoSend === true) {
  212. submitPrompt(prompt);
  213. }
  214. };
  215. // Event triggered when an error occurs
  216. speechRecognition.onerror = function (event) {
  217. console.log(event);
  218. toast.error(`Speech recognition error: ${event.error}`);
  219. speechRecognitionListening = false;
  220. };
  221. } else {
  222. toast.error('SpeechRecognition API is not supported in this browser.');
  223. }
  224. }
  225. };
  226. //////////////////////////
  227. // Web functions
  228. //////////////////////////
  229. const createNewChat = async (init = false) => {
  230. if (init || messages.length > 0) {
  231. chatId = uuidv4();
  232. autoScroll = true;
  233. title = '';
  234. messages = [];
  235. history = {
  236. messages: {},
  237. currentId: null
  238. };
  239. settings = JSON.parse(localStorage.getItem('settings') ?? JSON.stringify(settings));
  240. API_BASE_URL = settings?.API_BASE_URL ?? BUILD_TIME_API_BASE_URL;
  241. console.log(API_BASE_URL);
  242. if (models.length === 0) {
  243. await getModelTags();
  244. }
  245. // selectedModel =
  246. // settings.model && models.map((model) => model.name).includes(settings.model)
  247. // ? settings.model
  248. // : '';
  249. selectedModels = settings.models ?? [''];
  250. console.log(chatId);
  251. }
  252. };
  253. const setDBandLoadChats = async () => {
  254. db = await openDB('Chats', 1, {
  255. upgrade(db) {
  256. const store = db.createObjectStore('chats', {
  257. keyPath: 'id',
  258. autoIncrement: true
  259. });
  260. store.createIndex('timestamp', 'timestamp');
  261. }
  262. });
  263. chats = await db.getAllFromIndex('chats', 'timestamp');
  264. };
  265. const saveDefaultModel = () => {
  266. settings.models = selectedModels;
  267. localStorage.setItem('settings', JSON.stringify(settings));
  268. toast.success('Default model updated');
  269. };
  270. const saveSettings = async (updated) => {
  271. console.log(updated);
  272. settings = { ...settings, ...updated };
  273. localStorage.setItem('settings', JSON.stringify(settings));
  274. API_BASE_URL = updated?.API_BASE_URL ?? API_BASE_URL;
  275. await getModelTags();
  276. };
  277. const loadChat = async (id) => {
  278. const chat = await db.get('chats', id);
  279. console.log(chat);
  280. if (chatId !== chat.id) {
  281. if ('history' in chat && chat.history !== undefined) {
  282. history = chat.history;
  283. } else {
  284. let _history = {
  285. messages: {},
  286. currentId: null
  287. };
  288. let parentMessageId = null;
  289. let messageId = null;
  290. for (const message of chat.messages) {
  291. messageId = uuidv4();
  292. if (parentMessageId !== null) {
  293. _history.messages[parentMessageId].childrenIds = [
  294. ..._history.messages[parentMessageId].childrenIds,
  295. messageId
  296. ];
  297. }
  298. _history.messages[messageId] = {
  299. ...message,
  300. id: messageId,
  301. parentId: parentMessageId,
  302. childrenIds: []
  303. };
  304. parentMessageId = messageId;
  305. }
  306. _history.currentId = messageId;
  307. history = _history;
  308. }
  309. if ('models' in chat && chat.models !== undefined) {
  310. selectedModels = chat.models ?? selectedModels;
  311. } else {
  312. selectedModels = [chat.model ?? ''];
  313. }
  314. console.log(history);
  315. title = chat.title;
  316. chatId = chat.id;
  317. settings.system = chat.system ?? settings.system;
  318. settings.temperature = chat.temperature ?? settings.temperature;
  319. autoScroll = true;
  320. await tick();
  321. if (messages.length > 0) {
  322. history.messages[messages.at(-1).id].done = true;
  323. }
  324. renderLatex();
  325. hljs.highlightAll();
  326. createCopyCodeBlockButton();
  327. }
  328. };
  329. const editChatTitle = async (id, _title) => {
  330. const chat = await db.get('chats', id);
  331. console.log(chat);
  332. await db.put('chats', {
  333. ...chat,
  334. title: _title
  335. });
  336. title = _title;
  337. chats = await db.getAllFromIndex('chats', 'timestamp');
  338. };
  339. const deleteChat = async (id) => {
  340. createNewChat();
  341. const chat = await db.delete('chats', id);
  342. console.log(chat);
  343. chats = await db.getAllFromIndex('chats', 'timestamp');
  344. };
  345. const deleteChatHistory = async () => {
  346. const tx = db.transaction('chats', 'readwrite');
  347. await Promise.all([tx.store.clear(), tx.done]);
  348. chats = await db.getAllFromIndex('chats', 'timestamp');
  349. };
  350. const importChatHistory = async (chatHistory) => {
  351. for (const chat of chatHistory) {
  352. console.log(chat);
  353. await db.put('chats', {
  354. id: chat.id,
  355. model: chat.model,
  356. models: chat.models,
  357. system: chat.system,
  358. options: chat.options,
  359. title: chat.title,
  360. timestamp: chat.timestamp,
  361. messages: chat.messages,
  362. history: chat.history
  363. });
  364. }
  365. chats = await db.getAllFromIndex('chats', 'timestamp');
  366. console.log(chats);
  367. };
  368. const exportChatHistory = async () => {
  369. chats = await db.getAllFromIndex('chats', 'timestamp');
  370. let blob = new Blob([JSON.stringify(chats)], { type: 'application/json' });
  371. saveAs(blob, `chat-export-${Date.now()}.json`);
  372. };
  373. const openSettings = async () => {
  374. showSettings = true;
  375. };
  376. const editMessageHandler = async (messageId) => {
  377. // let editMessage = history.messages[messageId];
  378. history.messages[messageId].edit = true;
  379. history.messages[messageId].editedContent = history.messages[messageId].content;
  380. };
  381. const confirmEditMessage = async (messageId) => {
  382. history.messages[messageId].edit = false;
  383. let userPrompt = history.messages[messageId].editedContent;
  384. let userMessageId = uuidv4();
  385. let userMessage = {
  386. id: userMessageId,
  387. parentId: history.messages[messageId].parentId,
  388. childrenIds: [],
  389. role: 'user',
  390. content: userPrompt
  391. };
  392. let messageParentId = history.messages[messageId].parentId;
  393. if (messageParentId !== null) {
  394. history.messages[messageParentId].childrenIds = [
  395. ...history.messages[messageParentId].childrenIds,
  396. userMessageId
  397. ];
  398. }
  399. history.messages[userMessageId] = userMessage;
  400. history.currentId = userMessageId;
  401. await tick();
  402. await sendPrompt(userPrompt, userMessageId);
  403. };
  404. const cancelEditMessage = (messageId) => {
  405. history.messages[messageId].edit = false;
  406. history.messages[messageId].editedContent = undefined;
  407. };
  408. const rateMessage = async (messageIdx, rating) => {
  409. messages = messages.map((message, idx) => {
  410. if (messageIdx === idx) {
  411. message.rating = rating;
  412. }
  413. return message;
  414. });
  415. await db.put('chats', {
  416. id: chatId,
  417. title: title === '' ? 'New Chat' : title,
  418. models: selectedModels,
  419. system: settings.system,
  420. options: {
  421. temperature: settings.temperature
  422. },
  423. timestamp: Date.now(),
  424. messages: messages,
  425. history: history
  426. });
  427. console.log(messages);
  428. };
  429. const showPreviousMessage = async (message) => {
  430. if (message.parentId !== null) {
  431. let messageId =
  432. history.messages[message.parentId].childrenIds[
  433. Math.max(history.messages[message.parentId].childrenIds.indexOf(message.id) - 1, 0)
  434. ];
  435. if (message.id !== messageId) {
  436. let messageChildrenIds = history.messages[messageId].childrenIds;
  437. while (messageChildrenIds.length !== 0) {
  438. messageId = messageChildrenIds.at(-1);
  439. messageChildrenIds = history.messages[messageId].childrenIds;
  440. }
  441. history.currentId = messageId;
  442. }
  443. } else {
  444. let childrenIds = Object.values(history.messages)
  445. .filter((message) => message.parentId === null)
  446. .map((message) => message.id);
  447. let messageId = childrenIds[Math.max(childrenIds.indexOf(message.id) - 1, 0)];
  448. if (message.id !== messageId) {
  449. let messageChildrenIds = history.messages[messageId].childrenIds;
  450. while (messageChildrenIds.length !== 0) {
  451. messageId = messageChildrenIds.at(-1);
  452. messageChildrenIds = history.messages[messageId].childrenIds;
  453. }
  454. history.currentId = messageId;
  455. }
  456. }
  457. await tick();
  458. renderLatex();
  459. hljs.highlightAll();
  460. createCopyCodeBlockButton();
  461. };
  462. const showNextMessage = async (message) => {
  463. if (message.parentId !== null) {
  464. let messageId =
  465. history.messages[message.parentId].childrenIds[
  466. Math.min(
  467. history.messages[message.parentId].childrenIds.indexOf(message.id) + 1,
  468. history.messages[message.parentId].childrenIds.length - 1
  469. )
  470. ];
  471. if (message.id !== messageId) {
  472. let messageChildrenIds = history.messages[messageId].childrenIds;
  473. while (messageChildrenIds.length !== 0) {
  474. messageId = messageChildrenIds.at(-1);
  475. messageChildrenIds = history.messages[messageId].childrenIds;
  476. }
  477. history.currentId = messageId;
  478. }
  479. } else {
  480. let childrenIds = Object.values(history.messages)
  481. .filter((message) => message.parentId === null)
  482. .map((message) => message.id);
  483. let messageId =
  484. childrenIds[Math.min(childrenIds.indexOf(message.id) + 1, childrenIds.length - 1)];
  485. if (message.id !== messageId) {
  486. let messageChildrenIds = history.messages[messageId].childrenIds;
  487. while (messageChildrenIds.length !== 0) {
  488. messageId = messageChildrenIds.at(-1);
  489. messageChildrenIds = history.messages[messageId].childrenIds;
  490. }
  491. history.currentId = messageId;
  492. }
  493. }
  494. await tick();
  495. renderLatex();
  496. hljs.highlightAll();
  497. createCopyCodeBlockButton();
  498. };
  499. //////////////////////////
  500. // Ollama functions
  501. //////////////////////////
  502. const getModelTags = async (url = null, type = 'all') => {
  503. const res = await fetch(`${url === null ? API_BASE_URL : url}/tags`, {
  504. method: 'GET',
  505. headers: {
  506. Accept: 'application/json',
  507. 'Content-Type': 'application/json',
  508. ...(settings.authHeader && { Authorization: settings.authHeader })
  509. }
  510. })
  511. .then(async (res) => {
  512. if (!res.ok) throw await res.json();
  513. return res.json();
  514. })
  515. .catch((error) => {
  516. console.log(error);
  517. toast.error('Server connection failed');
  518. return null;
  519. });
  520. console.log(res);
  521. if (type === 'all') {
  522. if (settings.OPENAI_API_KEY) {
  523. // Validate OPENAI_API_KEY
  524. const openaiModelRes = await fetch(`https://api.openai.com/v1/models`, {
  525. method: 'GET',
  526. headers: {
  527. 'Content-Type': 'application/json',
  528. Authorization: `Bearer ${settings.OPENAI_API_KEY}`
  529. }
  530. })
  531. .then(async (res) => {
  532. if (!res.ok) throw await res.json();
  533. return res.json();
  534. })
  535. .catch((error) => {
  536. console.log(error);
  537. toast.error(`OpenAI: ${error?.error?.message ?? 'Network Problem'}`);
  538. return null;
  539. });
  540. const openaiModels = openaiModelRes?.data ?? null;
  541. if (openaiModels) {
  542. models = [
  543. ...(res?.models ?? []),
  544. { name: 'hr' },
  545. ...openaiModels
  546. .map((model) => ({ name: model.id, label: 'OpenAI' }))
  547. .filter((model) => model.name.includes('gpt'))
  548. ];
  549. } else {
  550. models = res?.models ?? [];
  551. }
  552. } else {
  553. models = res?.models ?? [];
  554. }
  555. return models;
  556. } else {
  557. return res?.models ?? null;
  558. }
  559. };
  560. const sendPrompt = async (userPrompt, parentId) => {
  561. await Promise.all(
  562. selectedModels.map(async (model) => {
  563. if (model.includes('gpt-')) {
  564. await sendPromptOpenAI(model, userPrompt, parentId);
  565. } else {
  566. await sendPromptOllama(model, userPrompt, parentId);
  567. }
  568. })
  569. );
  570. // if (selectedModel.includes('gpt-')) {
  571. // await sendPromptOpenAI(userPrompt, parentId);
  572. // } else {
  573. // await sendPromptOllama(userPrompt, parentId);
  574. // }
  575. console.log(history);
  576. };
  577. const sendPromptOllama = async (model, userPrompt, parentId) => {
  578. let responseMessageId = uuidv4();
  579. let responseMessage = {
  580. parentId: parentId,
  581. id: responseMessageId,
  582. childrenIds: [],
  583. role: 'assistant',
  584. content: '',
  585. model: model
  586. };
  587. history.messages[responseMessageId] = responseMessage;
  588. history.currentId = responseMessageId;
  589. if (parentId !== null) {
  590. history.messages[parentId].childrenIds = [
  591. ...history.messages[parentId].childrenIds,
  592. responseMessageId
  593. ];
  594. }
  595. window.scrollTo({ top: document.body.scrollHeight });
  596. const res = await fetch(`${API_BASE_URL}/generate`, {
  597. method: 'POST',
  598. headers: {
  599. 'Content-Type': 'text/event-stream',
  600. ...(settings.authHeader && { Authorization: settings.authHeader })
  601. },
  602. body: JSON.stringify({
  603. model: model,
  604. prompt: userPrompt,
  605. system: settings.system ?? undefined,
  606. options: {
  607. seed: settings.seed ?? undefined,
  608. temperature: settings.temperature ?? undefined,
  609. repeat_penalty: settings.repeat_penalty ?? undefined,
  610. top_k: settings.top_k ?? undefined,
  611. top_p: settings.top_p ?? undefined
  612. },
  613. format: settings.requestFormat ?? undefined,
  614. context:
  615. history.messages[parentId] !== null &&
  616. history.messages[parentId].parentId in history.messages
  617. ? history.messages[history.messages[parentId].parentId]?.context ?? undefined
  618. : undefined
  619. })
  620. });
  621. const reader = res.body
  622. .pipeThrough(new TextDecoderStream())
  623. .pipeThrough(splitStream('\n'))
  624. .getReader();
  625. while (true) {
  626. const { value, done } = await reader.read();
  627. if (done || stopResponseFlag) {
  628. if (stopResponseFlag) {
  629. responseMessage.done = true;
  630. messages = messages;
  631. hljs.highlightAll();
  632. createCopyCodeBlockButton();
  633. renderLatex();
  634. }
  635. break;
  636. }
  637. try {
  638. let lines = value.split('\n');
  639. for (const line of lines) {
  640. if (line !== '') {
  641. console.log(line);
  642. let data = JSON.parse(line);
  643. if (data.done == false) {
  644. if (responseMessage.content == '' && data.response == '\n') {
  645. continue;
  646. } else {
  647. responseMessage.content += data.response;
  648. messages = messages;
  649. }
  650. } else {
  651. responseMessage.done = true;
  652. responseMessage.context = data.context;
  653. messages = messages;
  654. hljs.highlightAll();
  655. createCopyCodeBlockButton();
  656. renderLatex();
  657. }
  658. }
  659. }
  660. } catch (error) {
  661. console.log(error);
  662. }
  663. if (autoScroll) {
  664. window.scrollTo({ top: document.body.scrollHeight });
  665. }
  666. await db.put('chats', {
  667. id: chatId,
  668. title: title === '' ? 'New Chat' : title,
  669. models: selectedModels,
  670. system: settings.system,
  671. options: {
  672. temperature: settings.temperature
  673. },
  674. timestamp: Date.now(),
  675. messages: messages,
  676. history: history
  677. });
  678. }
  679. stopResponseFlag = false;
  680. await tick();
  681. if (autoScroll) {
  682. window.scrollTo({ top: document.body.scrollHeight });
  683. }
  684. if (messages.length == 2) {
  685. await generateChatTitle(chatId, userPrompt);
  686. }
  687. };
  688. const sendPromptOpenAI = async (model, userPrompt, parentId) => {
  689. if (settings.OPENAI_API_KEY) {
  690. if (models) {
  691. let responseMessageId = uuidv4();
  692. let responseMessage = {
  693. parentId: parentId,
  694. id: responseMessageId,
  695. childrenIds: [],
  696. role: 'assistant',
  697. content: '',
  698. model: model
  699. };
  700. history.messages[responseMessageId] = responseMessage;
  701. history.currentId = responseMessageId;
  702. if (parentId !== null) {
  703. history.messages[parentId].childrenIds = [
  704. ...history.messages[parentId].childrenIds,
  705. responseMessageId
  706. ];
  707. }
  708. window.scrollTo({ top: document.body.scrollHeight });
  709. const res = await fetch(`https://api.openai.com/v1/chat/completions`, {
  710. method: 'POST',
  711. headers: {
  712. 'Content-Type': 'application/json',
  713. Authorization: `Bearer ${settings.OPENAI_API_KEY}`
  714. },
  715. body: JSON.stringify({
  716. model: model,
  717. stream: true,
  718. messages: [
  719. settings.system
  720. ? {
  721. role: 'system',
  722. content: settings.system
  723. }
  724. : undefined,
  725. ...messages
  726. ]
  727. .filter((message) => message)
  728. .map((message) => ({ role: message.role, content: message.content })),
  729. temperature: settings.temperature ?? undefined,
  730. top_p: settings.top_p ?? undefined,
  731. frequency_penalty: settings.repeat_penalty ?? undefined
  732. })
  733. });
  734. const reader = res.body
  735. .pipeThrough(new TextDecoderStream())
  736. .pipeThrough(splitStream('\n'))
  737. .getReader();
  738. while (true) {
  739. const { value, done } = await reader.read();
  740. if (done || stopResponseFlag) {
  741. if (stopResponseFlag) {
  742. responseMessage.done = true;
  743. messages = messages;
  744. }
  745. break;
  746. }
  747. try {
  748. let lines = value.split('\n');
  749. for (const line of lines) {
  750. if (line !== '') {
  751. console.log(line);
  752. if (line === 'data: [DONE]') {
  753. responseMessage.done = true;
  754. messages = messages;
  755. } else {
  756. let data = JSON.parse(line.replace(/^data: /, ''));
  757. console.log(data);
  758. if (responseMessage.content == '' && data.choices[0].delta.content == '\n') {
  759. continue;
  760. } else {
  761. responseMessage.content += data.choices[0].delta.content ?? '';
  762. messages = messages;
  763. }
  764. }
  765. }
  766. }
  767. } catch (error) {
  768. console.log(error);
  769. }
  770. if (autoScroll) {
  771. window.scrollTo({ top: document.body.scrollHeight });
  772. }
  773. await db.put('chats', {
  774. id: chatId,
  775. title: title === '' ? 'New Chat' : title,
  776. models: selectedModels,
  777. system: settings.system,
  778. options: {
  779. temperature: settings.temperature
  780. },
  781. timestamp: Date.now(),
  782. messages: messages,
  783. history: history
  784. });
  785. }
  786. stopResponseFlag = false;
  787. hljs.highlightAll();
  788. createCopyCodeBlockButton();
  789. renderLatex();
  790. await tick();
  791. if (autoScroll) {
  792. window.scrollTo({ top: document.body.scrollHeight });
  793. }
  794. if (messages.length == 2) {
  795. await setChatTitle(chatId, userPrompt);
  796. }
  797. }
  798. }
  799. };
  800. const submitPrompt = async (userPrompt) => {
  801. console.log('submitPrompt');
  802. if (selectedModels.includes('')) {
  803. toast.error('Model not selected');
  804. } else if (messages.length != 0 && messages.at(-1).done != true) {
  805. console.log('wait');
  806. } else {
  807. document.getElementById('chat-textarea').style.height = '';
  808. let userMessageId = uuidv4();
  809. let userMessage = {
  810. id: userMessageId,
  811. parentId: messages.length !== 0 ? messages.at(-1).id : null,
  812. childrenIds: [],
  813. role: 'user',
  814. content: userPrompt
  815. };
  816. if (messages.length !== 0) {
  817. history.messages[messages.at(-1).id].childrenIds.push(userMessageId);
  818. }
  819. history.messages[userMessageId] = userMessage;
  820. history.currentId = userMessageId;
  821. prompt = '';
  822. if (messages.length == 0) {
  823. await db.put('chats', {
  824. id: chatId,
  825. models: selectedModels,
  826. system: settings.system,
  827. options: {
  828. temperature: settings.temperature
  829. },
  830. title: 'New Chat',
  831. timestamp: Date.now(),
  832. messages: messages,
  833. history: history
  834. });
  835. chats = await db.getAllFromIndex('chats', 'timestamp');
  836. }
  837. setTimeout(() => {
  838. window.scrollTo({ top: document.body.scrollHeight, behavior: 'smooth' });
  839. }, 50);
  840. await sendPrompt(userPrompt, userMessageId);
  841. chats = await db.getAllFromIndex('chats', 'timestamp');
  842. }
  843. };
  844. const regenerateResponse = async () => {
  845. console.log('regenerateResponse');
  846. if (messages.length != 0 && messages.at(-1).done == true) {
  847. messages.splice(messages.length - 1, 1);
  848. messages = messages;
  849. let userMessage = messages.at(-1);
  850. let userPrompt = userMessage.content;
  851. await sendPrompt(userPrompt, userMessage.id);
  852. chats = await db.getAllFromIndex('chats', 'timestamp');
  853. }
  854. };
  855. const stopResponse = () => {
  856. stopResponseFlag = true;
  857. console.log('stopResponse');
  858. };
  859. const generateChatTitle = async (_chatId, userPrompt) => {
  860. console.log('generateChatTitle');
  861. const res = await fetch(`${API_BASE_URL}/generate`, {
  862. method: 'POST',
  863. headers: {
  864. 'Content-Type': 'text/event-stream',
  865. ...(settings.authHeader && { Authorization: settings.authHeader })
  866. },
  867. body: JSON.stringify({
  868. model: selectedModels[0],
  869. prompt: `Generate a brief 3-5 word title for this question, excluding the term 'title.' Then, please reply with only the title: ${userPrompt}`,
  870. stream: false
  871. })
  872. })
  873. .then(async (res) => {
  874. if (!res.ok) throw await res.json();
  875. return res.json();
  876. })
  877. .catch((error) => {
  878. console.log(error);
  879. return null;
  880. });
  881. if (res) {
  882. await setChatTitle(_chatId, res.response === '' ? 'New Chat' : res.response);
  883. }
  884. };
  885. const setChatTitle = async (_chatId, _title) => {
  886. const chat = await db.get('chats', _chatId);
  887. await db.put('chats', { ...chat, title: _title });
  888. if (chat.id === chatId) {
  889. title = _title;
  890. }
  891. };
  892. </script>
  893. <svelte:window
  894. on:scroll={(e) => {
  895. autoScroll = window.innerHeight + window.scrollY >= document.body.offsetHeight - 40;
  896. }}
  897. />
  898. <div class="app">
  899. <div
  900. class=" text-gray-700 dark:text-gray-100 bg-white dark:bg-gray-800 min-h-screen overflow-auto flex flex-row"
  901. >
  902. <Navbar
  903. selectedChatId={chatId}
  904. {chats}
  905. {title}
  906. {loadChat}
  907. {editChatTitle}
  908. {deleteChat}
  909. {createNewChat}
  910. {importChatHistory}
  911. {exportChatHistory}
  912. {deleteChatHistory}
  913. {openSettings}
  914. />
  915. <SettingsModal bind:show={showSettings} {saveSettings} {getModelTags} />
  916. <div class="min-h-screen w-full flex justify-center">
  917. <div class=" py-2.5 flex flex-col justify-between w-full">
  918. <div class="max-w-2xl mx-auto w-full px-3 md:px-0 mt-10">
  919. <div class="flex flex-col my-2">
  920. {#each selectedModels as selectedModel, selectedModelIdx}
  921. <div class="flex">
  922. <select
  923. id="models"
  924. class="outline-none bg-transparent text-lg font-semibold rounded-lg block w-full placeholder-gray-400"
  925. bind:value={selectedModel}
  926. disabled={messages.length != 0}
  927. >
  928. <option class=" text-gray-700" value="" selected>Select a model</option>
  929. {#each models as model}
  930. {#if model.name === 'hr'}
  931. <hr />
  932. {:else}
  933. <option value={model.name} class="text-gray-700 text-lg">{model.name}</option>
  934. {/if}
  935. {/each}
  936. </select>
  937. {#if selectedModelIdx === 0}
  938. <button
  939. class=" self-center {selectedModelIdx === 0
  940. ? 'mr-3'
  941. : 'mr-7'} disabled:text-gray-600 disabled:hover:text-gray-600"
  942. disabled={selectedModels.length === 3 || messages.length != 0}
  943. on:click={() => {
  944. if (selectedModels.length < 3) {
  945. selectedModels = [...selectedModels, ''];
  946. }
  947. }}
  948. >
  949. <svg
  950. xmlns="http://www.w3.org/2000/svg"
  951. fill="none"
  952. viewBox="0 0 24 24"
  953. stroke-width="1.5"
  954. stroke="currentColor"
  955. class="w-4 h-4"
  956. >
  957. <path stroke-linecap="round" stroke-linejoin="round" d="M12 6v12m6-6H6" />
  958. </svg>
  959. </button>
  960. {:else}
  961. <button
  962. class=" self-center disabled:text-gray-600 disabled:hover:text-gray-600 {selectedModelIdx ===
  963. 0
  964. ? 'mr-3'
  965. : 'mr-7'}"
  966. disabled={messages.length != 0}
  967. on:click={() => {
  968. selectedModels.splice(selectedModelIdx, 1);
  969. selectedModels = selectedModels;
  970. }}
  971. >
  972. <svg
  973. xmlns="http://www.w3.org/2000/svg"
  974. fill="none"
  975. viewBox="0 0 24 24"
  976. stroke-width="1.5"
  977. stroke="currentColor"
  978. class="w-4 h-4"
  979. >
  980. <path stroke-linecap="round" stroke-linejoin="round" d="M19.5 12h-15" />
  981. </svg>
  982. </button>
  983. {/if}
  984. {#if selectedModelIdx === 0}
  985. <button
  986. class=" self-center dark:hover:text-gray-300"
  987. on:click={() => {
  988. openSettings();
  989. }}
  990. >
  991. <svg
  992. xmlns="http://www.w3.org/2000/svg"
  993. fill="none"
  994. viewBox="0 0 24 24"
  995. stroke-width="1.5"
  996. stroke="currentColor"
  997. class="w-4 h-4"
  998. >
  999. <path
  1000. stroke-linecap="round"
  1001. stroke-linejoin="round"
  1002. d="M10.343 3.94c.09-.542.56-.94 1.11-.94h1.093c.55 0 1.02.398 1.11.94l.149.894c.07.424.384.764.78.93.398.164.855.142 1.205-.108l.737-.527a1.125 1.125 0 011.45.12l.773.774c.39.389.44 1.002.12 1.45l-.527.737c-.25.35-.272.806-.107 1.204.165.397.505.71.93.78l.893.15c.543.09.94.56.94 1.109v1.094c0 .55-.397 1.02-.94 1.11l-.893.149c-.425.07-.765.383-.93.78-.165.398-.143.854.107 1.204l.527.738c.32.447.269 1.06-.12 1.45l-.774.773a1.125 1.125 0 01-1.449.12l-.738-.527c-.35-.25-.806-.272-1.203-.107-.397.165-.71.505-.781.929l-.149.894c-.09.542-.56.94-1.11.94h-1.094c-.55 0-1.019-.398-1.11-.94l-.148-.894c-.071-.424-.384-.764-.781-.93-.398-.164-.854-.142-1.204.108l-.738.527c-.447.32-1.06.269-1.45-.12l-.773-.774a1.125 1.125 0 01-.12-1.45l.527-.737c.25-.35.273-.806.108-1.204-.165-.397-.505-.71-.93-.78l-.894-.15c-.542-.09-.94-.56-.94-1.109v-1.094c0-.55.398-1.02.94-1.11l.894-.149c.424-.07.765-.383.93-.78.165-.398.143-.854-.107-1.204l-.527-.738a1.125 1.125 0 01.12-1.45l.773-.773a1.125 1.125 0 011.45-.12l.737.527c.35.25.807.272 1.204.107.397-.165.71-.505.78-.929l.15-.894z"
  1003. />
  1004. <path
  1005. stroke-linecap="round"
  1006. stroke-linejoin="round"
  1007. d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"
  1008. />
  1009. </svg>
  1010. </button>
  1011. {/if}
  1012. </div>
  1013. {/each}
  1014. </div>
  1015. <div class="text-left mt-1.5 text-xs text-gray-500">
  1016. <button on:click={saveDefaultModel}> Set as default</button>
  1017. </div>
  1018. </div>
  1019. <div class=" h-full mt-10 mb-32 w-full flex flex-col">
  1020. {#if messages.length == 0}
  1021. <div class="m-auto text-center max-w-md pb-56 px-2">
  1022. <div class="flex justify-center mt-8">
  1023. <img src="/ollama.png" class=" w-16 invert-[10%] dark:invert-[100%] rounded-full" />
  1024. </div>
  1025. <div class=" mt-1 text-2xl text-gray-800 dark:text-gray-100 font-semibold">
  1026. How can I help you today?
  1027. </div>
  1028. </div>
  1029. {:else}
  1030. {#each messages as message, messageIdx}
  1031. <div class=" w-full">
  1032. <div class="flex justify-between px-5 mb-3 max-w-3xl mx-auto rounded-lg group">
  1033. <div class=" flex w-full">
  1034. <div class=" mr-4">
  1035. <img
  1036. src="{message.role == 'user'
  1037. ? settings.gravatarUrl
  1038. ? settings.gravatarUrl
  1039. : '/user'
  1040. : '/favicon'}.png"
  1041. class=" max-w-[28px] object-cover rounded-full"
  1042. />
  1043. </div>
  1044. <div class="w-full">
  1045. <div class=" self-center font-bold mb-0.5">
  1046. {#if message.role === 'user'}
  1047. You
  1048. {:else}
  1049. Ollama <span class=" text-gray-500 text-sm font-medium"
  1050. >{message.model ? ` ${message.model}` : ''}</span
  1051. >
  1052. {/if}
  1053. </div>
  1054. {#if message.role !== 'user' && message.content === ''}
  1055. <div class="w-full mt-3">
  1056. <div class="animate-pulse flex w-full">
  1057. <div class="space-y-2 w-full">
  1058. <div class="h-2 bg-gray-200 dark:bg-gray-600 rounded mr-14" />
  1059. <div class="grid grid-cols-3 gap-4">
  1060. <div class="h-2 bg-gray-200 dark:bg-gray-600 rounded col-span-2" />
  1061. <div class="h-2 bg-gray-200 dark:bg-gray-600 rounded col-span-1" />
  1062. </div>
  1063. <div class="grid grid-cols-4 gap-4">
  1064. <div class="h-2 bg-gray-200 dark:bg-gray-600 rounded col-span-1" />
  1065. <div class="h-2 bg-gray-200 dark:bg-gray-600 rounded col-span-2" />
  1066. <div
  1067. class="h-2 bg-gray-200 dark:bg-gray-600 rounded col-span-1 mr-4"
  1068. />
  1069. </div>
  1070. <div class="h-2 bg-gray-200 dark:bg-gray-600 rounded" />
  1071. </div>
  1072. </div>
  1073. </div>
  1074. {:else}
  1075. <div
  1076. class="prose chat-{message.role} w-full max-w-full dark:prose-invert prose-headings:my-0 prose-p:my-0 prose-p:-mb-4 prose-pre:my-0 prose-table:my-0 prose-blockquote:my-0 prose-img:my-0 prose-ul:-my-4 prose-ol:-my-4 prose-li:-my-3 prose-ul:-mb-6 prose-ol:-mb-6 prose-li:-mb-4 whitespace-pre-line"
  1077. >
  1078. {#if message.role == 'user'}
  1079. {#if message?.edit === true}
  1080. <div class=" w-full">
  1081. <textarea
  1082. class=" bg-transparent outline-none w-full resize-none"
  1083. bind:value={history.messages[message.id].editedContent}
  1084. on:input={(e) => {
  1085. e.target.style.height = '';
  1086. e.target.style.height = `${e.target.scrollHeight}px`;
  1087. }}
  1088. on:focus={(e) => {
  1089. e.target.style.height = '';
  1090. e.target.style.height = `${e.target.scrollHeight}px`;
  1091. }}
  1092. />
  1093. <div class=" flex justify-end space-x-2 text-sm font-medium">
  1094. <button
  1095. class="px-4 py-2.5 bg-emerald-600 hover:bg-emerald-700 text-gray-100 transition rounded-lg"
  1096. on:click={() => {
  1097. confirmEditMessage(message.id);
  1098. }}
  1099. >
  1100. Save & Submit
  1101. </button>
  1102. <button
  1103. class=" px-4 py-2.5 hover:bg-gray-100 dark:bg-gray-800 dark:hover:bg-gray-700 text-gray-700 dark:text-gray-100 transition outline outline-1 outline-gray-200 dark:outline-gray-600 rounded-lg"
  1104. on:click={() => {
  1105. cancelEditMessage(message.id);
  1106. }}
  1107. >
  1108. Cancel
  1109. </button>
  1110. </div>
  1111. </div>
  1112. {:else}
  1113. <div class="w-full">
  1114. {message.content}
  1115. <div class=" flex justify-start space-x-1">
  1116. {#if message.parentId !== null && message.parentId in history.messages && (history.messages[message.parentId]?.childrenIds.length ?? 0) > 1}
  1117. <div class="flex self-center">
  1118. <button
  1119. class="self-center"
  1120. on:click={() => {
  1121. showPreviousMessage(message);
  1122. }}
  1123. >
  1124. <svg
  1125. xmlns="http://www.w3.org/2000/svg"
  1126. viewBox="0 0 20 20"
  1127. fill="currentColor"
  1128. class="w-4 h-4"
  1129. >
  1130. <path
  1131. fill-rule="evenodd"
  1132. d="M12.79 5.23a.75.75 0 01-.02 1.06L8.832 10l3.938 3.71a.75.75 0 11-1.04 1.08l-4.5-4.25a.75.75 0 010-1.08l4.5-4.25a.75.75 0 011.06.02z"
  1133. clip-rule="evenodd"
  1134. />
  1135. </svg>
  1136. </button>
  1137. <div class="text-xs font-bold self-center">
  1138. {history.messages[message.parentId].childrenIds.indexOf(
  1139. message.id
  1140. ) + 1} / {history.messages[message.parentId].childrenIds
  1141. .length}
  1142. </div>
  1143. <button
  1144. class="self-center"
  1145. on:click={() => {
  1146. showNextMessage(message);
  1147. }}
  1148. >
  1149. <svg
  1150. xmlns="http://www.w3.org/2000/svg"
  1151. viewBox="0 0 20 20"
  1152. fill="currentColor"
  1153. class="w-4 h-4"
  1154. >
  1155. <path
  1156. fill-rule="evenodd"
  1157. d="M7.21 14.77a.75.75 0 01.02-1.06L11.168 10 7.23 6.29a.75.75 0 111.04-1.08l4.5 4.25a.75.75 0 010 1.08l-4.5 4.25a.75.75 0 01-1.06-.02z"
  1158. clip-rule="evenodd"
  1159. />
  1160. </svg>
  1161. </button>
  1162. </div>
  1163. {:else if message.parentId === null && Object.values(history.messages).filter((message) => message.parentId === null).length > 1}
  1164. <div class="flex self-center">
  1165. <button
  1166. class="self-center"
  1167. on:click={() => {
  1168. showPreviousMessage(message);
  1169. }}
  1170. >
  1171. <svg
  1172. xmlns="http://www.w3.org/2000/svg"
  1173. viewBox="0 0 20 20"
  1174. fill="currentColor"
  1175. class="w-4 h-4"
  1176. >
  1177. <path
  1178. fill-rule="evenodd"
  1179. d="M12.79 5.23a.75.75 0 01-.02 1.06L8.832 10l3.938 3.71a.75.75 0 11-1.04 1.08l-4.5-4.25a.75.75 0 010-1.08l4.5-4.25a.75.75 0 011.06.02z"
  1180. clip-rule="evenodd"
  1181. />
  1182. </svg>
  1183. </button>
  1184. <div class="text-xs font-bold self-center">
  1185. {Object.values(history.messages)
  1186. .filter((message) => message.parentId === null)
  1187. .map((message) => message.id)
  1188. .indexOf(message.id) + 1} / {Object.values(
  1189. history.messages
  1190. ).filter((message) => message.parentId === null).length}
  1191. </div>
  1192. <button
  1193. class="self-center"
  1194. on:click={() => {
  1195. showNextMessage(message);
  1196. }}
  1197. >
  1198. <svg
  1199. xmlns="http://www.w3.org/2000/svg"
  1200. viewBox="0 0 20 20"
  1201. fill="currentColor"
  1202. class="w-4 h-4"
  1203. >
  1204. <path
  1205. fill-rule="evenodd"
  1206. d="M7.21 14.77a.75.75 0 01.02-1.06L11.168 10 7.23 6.29a.75.75 0 111.04-1.08l4.5 4.25a.75.75 0 010 1.08l-4.5 4.25a.75.75 0 01-1.06-.02z"
  1207. clip-rule="evenodd"
  1208. />
  1209. </svg>
  1210. </button>
  1211. </div>
  1212. {/if}
  1213. <button
  1214. class="invisible group-hover:visible p-1 rounded dark:hover:bg-gray-800 transition"
  1215. on:click={() => {
  1216. editMessageHandler(message.id);
  1217. }}
  1218. >
  1219. <svg
  1220. xmlns="http://www.w3.org/2000/svg"
  1221. fill="none"
  1222. viewBox="0 0 24 24"
  1223. stroke-width="1.5"
  1224. stroke="currentColor"
  1225. class="w-4 h-4"
  1226. >
  1227. <path
  1228. stroke-linecap="round"
  1229. stroke-linejoin="round"
  1230. d="M16.862 4.487l1.687-1.688a1.875 1.875 0 112.652 2.652L6.832 19.82a4.5 4.5 0 01-1.897 1.13l-2.685.8.8-2.685a4.5 4.5 0 011.13-1.897L16.863 4.487zm0 0L19.5 7.125"
  1231. />
  1232. </svg>
  1233. </button>
  1234. </div>
  1235. </div>
  1236. {/if}
  1237. {:else}
  1238. <div class="w-full">
  1239. {@html marked(message.content.replace('\\\\', '\\\\\\'))}
  1240. {#if message.done}
  1241. <div class=" flex justify-start space-x-1 -mt-2">
  1242. {#if message.parentId !== null && message.parentId in history.messages && (history.messages[message.parentId]?.childrenIds.length ?? 0) > 1}
  1243. <div class="flex self-center">
  1244. <button
  1245. class="self-center"
  1246. on:click={() => {
  1247. showPreviousMessage(message);
  1248. }}
  1249. >
  1250. <svg
  1251. xmlns="http://www.w3.org/2000/svg"
  1252. viewBox="0 0 20 20"
  1253. fill="currentColor"
  1254. class="w-4 h-4"
  1255. >
  1256. <path
  1257. fill-rule="evenodd"
  1258. d="M12.79 5.23a.75.75 0 01-.02 1.06L8.832 10l3.938 3.71a.75.75 0 11-1.04 1.08l-4.5-4.25a.75.75 0 010-1.08l4.5-4.25a.75.75 0 011.06.02z"
  1259. clip-rule="evenodd"
  1260. />
  1261. </svg>
  1262. </button>
  1263. <div class="text-xs font-bold self-center">
  1264. {history.messages[message.parentId].childrenIds.indexOf(
  1265. message.id
  1266. ) + 1} / {history.messages[message.parentId].childrenIds
  1267. .length}
  1268. </div>
  1269. <button
  1270. class="self-center"
  1271. on:click={() => {
  1272. showNextMessage(message);
  1273. }}
  1274. >
  1275. <svg
  1276. xmlns="http://www.w3.org/2000/svg"
  1277. viewBox="0 0 20 20"
  1278. fill="currentColor"
  1279. class="w-4 h-4"
  1280. >
  1281. <path
  1282. fill-rule="evenodd"
  1283. d="M7.21 14.77a.75.75 0 01.02-1.06L11.168 10 7.23 6.29a.75.75 0 111.04-1.08l4.5 4.25a.75.75 0 010 1.08l-4.5 4.25a.75.75 0 01-1.06-.02z"
  1284. clip-rule="evenodd"
  1285. />
  1286. </svg>
  1287. </button>
  1288. </div>
  1289. {/if}
  1290. <button
  1291. class="{messageIdx + 1 === messages.length
  1292. ? 'visible'
  1293. : 'invisible group-hover:visible'} p-1 rounded dark:hover:bg-gray-800 transition"
  1294. on:click={() => {
  1295. copyToClipboard(message.content);
  1296. }}
  1297. >
  1298. <svg
  1299. xmlns="http://www.w3.org/2000/svg"
  1300. fill="none"
  1301. viewBox="0 0 24 24"
  1302. stroke-width="1.5"
  1303. stroke="currentColor"
  1304. class="w-4 h-4"
  1305. >
  1306. <path
  1307. stroke-linecap="round"
  1308. stroke-linejoin="round"
  1309. d="M15.666 3.888A2.25 2.25 0 0013.5 2.25h-3c-1.03 0-1.9.693-2.166 1.638m7.332 0c.055.194.084.4.084.612v0a.75.75 0 01-.75.75H9a.75.75 0 01-.75-.75v0c0-.212.03-.418.084-.612m7.332 0c.646.049 1.288.11 1.927.184 1.1.128 1.907 1.077 1.907 2.185V19.5a2.25 2.25 0 01-2.25 2.25H6.75A2.25 2.25 0 014.5 19.5V6.257c0-1.108.806-2.057 1.907-2.185a48.208 48.208 0 011.927-.184"
  1310. />
  1311. </svg>
  1312. </button>
  1313. <button
  1314. class="{messageIdx + 1 === messages.length
  1315. ? 'visible'
  1316. : 'invisible group-hover:visible'} p-1 rounded dark:hover:bg-gray-800 transition"
  1317. on:click={() => {
  1318. rateMessage(messageIdx, 1);
  1319. }}
  1320. >
  1321. <svg
  1322. stroke="currentColor"
  1323. fill="none"
  1324. stroke-width="2"
  1325. viewBox="0 0 24 24"
  1326. stroke-linecap="round"
  1327. stroke-linejoin="round"
  1328. class="w-4 h-4"
  1329. xmlns="http://www.w3.org/2000/svg"
  1330. ><path
  1331. d="M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3"
  1332. /></svg
  1333. >
  1334. </button>
  1335. <button
  1336. class="{messageIdx + 1 === messages.length
  1337. ? 'visible'
  1338. : 'invisible group-hover:visible'} p-1 rounded dark:hover:bg-gray-800 transition"
  1339. on:click={() => {
  1340. rateMessage(messageIdx, -1);
  1341. }}
  1342. >
  1343. <svg
  1344. stroke="currentColor"
  1345. fill="none"
  1346. stroke-width="2"
  1347. viewBox="0 0 24 24"
  1348. stroke-linecap="round"
  1349. stroke-linejoin="round"
  1350. class="w-4 h-4"
  1351. xmlns="http://www.w3.org/2000/svg"
  1352. ><path
  1353. d="M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17"
  1354. /></svg
  1355. >
  1356. </button>
  1357. {#if messageIdx + 1 === messages.length}
  1358. <button
  1359. class="{messageIdx + 1 === messages.length
  1360. ? 'visible'
  1361. : 'invisible group-hover:visible'} p-1 rounded dark:hover:bg-gray-800 transition"
  1362. on:click={regenerateResponse}
  1363. >
  1364. <svg
  1365. xmlns="http://www.w3.org/2000/svg"
  1366. fill="none"
  1367. viewBox="0 0 24 24"
  1368. stroke-width="1.5"
  1369. stroke="currentColor"
  1370. class="w-4 h-4"
  1371. >
  1372. <path
  1373. stroke-linecap="round"
  1374. stroke-linejoin="round"
  1375. d="M16.023 9.348h4.992v-.001M2.985 19.644v-4.992m0 0h4.992m-4.993 0l3.181 3.183a8.25 8.25 0 0013.803-3.7M4.031 9.865a8.25 8.25 0 0113.803-3.7l3.181 3.182m0-4.991v4.99"
  1376. />
  1377. </svg>
  1378. </button>
  1379. {/if}
  1380. </div>
  1381. {/if}
  1382. </div>
  1383. {/if}
  1384. </div>
  1385. {/if}
  1386. </div>
  1387. <!-- {} -->
  1388. </div>
  1389. </div>
  1390. </div>
  1391. {/each}
  1392. {/if}
  1393. </div>
  1394. </div>
  1395. <div class="fixed bottom-0 w-full">
  1396. <div class=" pt-5">
  1397. <div class="max-w-3xl px-2.5 pt-2.5 -mb-0.5 mx-auto inset-x-0">
  1398. {#if messages.length == 0 && suggestions !== 'false'}
  1399. <Suggestions {submitPrompt} />
  1400. {/if}
  1401. {#if autoScroll === false && messages.length > 0}
  1402. <div class=" flex justify-center mb-4">
  1403. <button
  1404. class=" bg-white/20 p-1.5 rounded-full"
  1405. on:click={() => {
  1406. window.scrollTo({ top: document.body.scrollHeight, behavior: 'smooth' });
  1407. }}
  1408. >
  1409. <svg
  1410. xmlns="http://www.w3.org/2000/svg"
  1411. viewBox="0 0 20 20"
  1412. fill="currentColor"
  1413. class="w-5 h-5"
  1414. >
  1415. <path
  1416. fill-rule="evenodd"
  1417. d="M10 3a.75.75 0 01.75.75v10.638l3.96-4.158a.75.75 0 111.08 1.04l-5.25 5.5a.75.75 0 01-1.08 0l-5.25-5.5a.75.75 0 111.08-1.04l3.96 4.158V3.75A.75.75 0 0110 3z"
  1418. clip-rule="evenodd"
  1419. />
  1420. </svg>
  1421. </button>
  1422. </div>
  1423. {/if}
  1424. <div class="bg-gradient-to-t from-white dark:from-gray-800 from-40% pb-2">
  1425. <form
  1426. class=" flex relative w-full"
  1427. on:submit|preventDefault={() => {
  1428. submitPrompt(prompt);
  1429. }}
  1430. >
  1431. <textarea
  1432. id="chat-textarea"
  1433. class="rounded-xl dark:bg-gray-800 dark:text-gray-100 outline-none border dark:border-gray-600 w-full py-3
  1434. {fileUploadEnabled ? 'pl-12' : 'pl-5'} {speechRecognitionEnabled ? 'pr-20' : 'pr-12'} resize-none"
  1435. placeholder={speechRecognitionListening ? 'Listening...' : 'Send a message'}
  1436. bind:value={prompt}
  1437. on:keypress={(e) => {
  1438. if (e.keyCode == 13 && !e.shiftKey) {
  1439. e.preventDefault();
  1440. }
  1441. if (prompt !== '' && e.keyCode == 13 && !e.shiftKey) {
  1442. submitPrompt(prompt);
  1443. }
  1444. }}
  1445. rows="1"
  1446. on:input={(e) => {
  1447. e.target.style.height = '';
  1448. e.target.style.height = Math.min(e.target.scrollHeight, 200) + 2 + 'px';
  1449. }}
  1450. />
  1451. {#if fileUploadEnabled}
  1452. <div class=" absolute left-0 bottom-0">
  1453. <div class="pl-2.5 pb-[9px]">
  1454. <button
  1455. class=" text-gray-600 dark:text-gray-200 transition rounded-lg p-1.5"
  1456. type="button"
  1457. on:click={() => {
  1458. console.log('file');
  1459. }}
  1460. >
  1461. <svg
  1462. xmlns="http://www.w3.org/2000/svg"
  1463. viewBox="0 0 20 20"
  1464. fill="currentColor"
  1465. class="w-5 h-5"
  1466. >
  1467. <path
  1468. fill-rule="evenodd"
  1469. d="M15.621 4.379a3 3 0 00-4.242 0l-7 7a3 3 0 004.241 4.243h.001l.497-.5a.75.75 0 011.064 1.057l-.498.501-.002.002a4.5 4.5 0 01-6.364-6.364l7-7a4.5 4.5 0 016.368 6.36l-3.455 3.553A2.625 2.625 0 119.52 9.52l3.45-3.451a.75.75 0 111.061 1.06l-3.45 3.451a1.125 1.125 0 001.587 1.595l3.454-3.553a3 3 0 000-4.242z"
  1470. clip-rule="evenodd"
  1471. />
  1472. </svg>
  1473. </button>
  1474. </div>
  1475. </div>
  1476. {/if}
  1477. <div class=" absolute right-0 bottom-0">
  1478. <div class="pr-2.5 pb-[9px]">
  1479. {#if messages.length == 0 || messages.at(-1).done == true}
  1480. {#if speechRecognitionEnabled}
  1481. <button
  1482. class=" text-gray-600 dark:text-gray-300 transition rounded-lg p-1 mr-0.5"
  1483. type="button"
  1484. on:click={() => {
  1485. speechRecognitionHandler();
  1486. }}
  1487. >
  1488. {#if speechRecognitionListening}
  1489. <svg
  1490. class=" w-5 h-5 translate-y-[0.5px]"
  1491. fill="currentColor"
  1492. viewBox="0 0 24 24"
  1493. xmlns="http://www.w3.org/2000/svg"
  1494. ><style>
  1495. .spinner_qM83 {
  1496. animation: spinner_8HQG 1.05s infinite;
  1497. }
  1498. .spinner_oXPr {
  1499. animation-delay: 0.1s;
  1500. }
  1501. .spinner_ZTLf {
  1502. animation-delay: 0.2s;
  1503. }
  1504. @keyframes spinner_8HQG {
  1505. 0%,
  1506. 57.14% {
  1507. animation-timing-function: cubic-bezier(0.33, 0.66, 0.66, 1);
  1508. transform: translate(0);
  1509. }
  1510. 28.57% {
  1511. animation-timing-function: cubic-bezier(0.33, 0, 0.66, 0.33);
  1512. transform: translateY(-6px);
  1513. }
  1514. 100% {
  1515. transform: translate(0);
  1516. }
  1517. }
  1518. </style><circle class="spinner_qM83" cx="4" cy="12" r="2.5" /><circle
  1519. class="spinner_qM83 spinner_oXPr"
  1520. cx="12"
  1521. cy="12"
  1522. r="2.5"
  1523. /><circle
  1524. class="spinner_qM83 spinner_ZTLf"
  1525. cx="20"
  1526. cy="12"
  1527. r="2.5"
  1528. /></svg
  1529. >
  1530. {:else}
  1531. <svg
  1532. xmlns="http://www.w3.org/2000/svg"
  1533. viewBox="0 0 20 20"
  1534. fill="currentColor"
  1535. class="w-5 h-5 translate-y-[0.5px]"
  1536. >
  1537. <path d="M7 4a3 3 0 016 0v6a3 3 0 11-6 0V4z" />
  1538. <path
  1539. d="M5.5 9.643a.75.75 0 00-1.5 0V10c0 3.06 2.29 5.585 5.25 5.954V17.5h-1.5a.75.75 0 000 1.5h4.5a.75.75 0 000-1.5h-1.5v-1.546A6.001 6.001 0 0016 10v-.357a.75.75 0 00-1.5 0V10a4.5 4.5 0 01-9 0v-.357z"
  1540. />
  1541. </svg>
  1542. {/if}
  1543. </button>
  1544. {/if}
  1545. <button
  1546. class="{prompt !== ''
  1547. ? 'bg-black text-white hover:bg-gray-900 dark:bg-white dark:text-black dark:hover:bg-gray-100 '
  1548. : 'text-white bg-gray-100 dark:text-gray-800 dark:bg-gray-600 disabled'} transition rounded-lg p-1"
  1549. type="submit"
  1550. disabled={prompt === ''}
  1551. >
  1552. <svg
  1553. xmlns="http://www.w3.org/2000/svg"
  1554. viewBox="0 0 20 20"
  1555. fill="currentColor"
  1556. class="w-5 h-5"
  1557. >
  1558. <path
  1559. fill-rule="evenodd"
  1560. d="M10 17a.75.75 0 01-.75-.75V5.612L5.29 9.77a.75.75 0 01-1.08-1.04l5.25-5.5a.75.75 0 011.08 0l5.25 5.5a.75.75 0 11-1.08 1.04l-3.96-4.158V16.25A.75.75 0 0110 17z"
  1561. clip-rule="evenodd"
  1562. />
  1563. </svg>
  1564. </button>
  1565. {:else}
  1566. <button
  1567. class="bg-white hover:bg-gray-100 text-gray-800 dark:bg-gray-700 dark:text-white dark:hover:bg-gray-800 transition rounded-lg p-1.5"
  1568. on:click={stopResponse}
  1569. >
  1570. <svg
  1571. xmlns="http://www.w3.org/2000/svg"
  1572. viewBox="0 0 24 24"
  1573. fill="currentColor"
  1574. class="w-5 h-5"
  1575. >
  1576. <path
  1577. fill-rule="evenodd"
  1578. d="M2.25 12c0-5.385 4.365-9.75 9.75-9.75s9.75 4.365 9.75 9.75-4.365 9.75-9.75 9.75S2.25 17.385 2.25 12zm6-2.438c0-.724.588-1.312 1.313-1.312h4.874c.725 0 1.313.588 1.313 1.313v4.874c0 .725-.588 1.313-1.313 1.313H9.564a1.312 1.312 0 01-1.313-1.313V9.564z"
  1579. clip-rule="evenodd"
  1580. />
  1581. </svg>
  1582. </button>
  1583. {/if}
  1584. </div>
  1585. </div>
  1586. </form>
  1587. <div class="mt-1.5 text-xs text-gray-500 text-center">
  1588. LLMs can make mistakes. Verify important information.
  1589. </div>
  1590. </div>
  1591. </div>
  1592. </div>
  1593. </div>
  1594. </div>
  1595. </div>
  1596. </div>
  1597. <style>
  1598. .loading {
  1599. display: inline-block;
  1600. clip-path: inset(0 1ch 0 0);
  1601. animation: l 1s steps(3) infinite;
  1602. letter-spacing: -0.5px;
  1603. }
  1604. @keyframes l {
  1605. to {
  1606. clip-path: inset(0 -1ch 0 0);
  1607. }
  1608. }
  1609. pre[class*='language-'] {
  1610. position: relative;
  1611. overflow: auto;
  1612. /* make space */
  1613. margin: 5px 0;
  1614. padding: 1.75rem 0 1.75rem 1rem;
  1615. border-radius: 10px;
  1616. }
  1617. pre[class*='language-'] button {
  1618. position: absolute;
  1619. top: 5px;
  1620. right: 5px;
  1621. font-size: 0.9rem;
  1622. padding: 0.15rem;
  1623. background-color: #828282;
  1624. border: ridge 1px #7b7b7c;
  1625. border-radius: 5px;
  1626. text-shadow: #c4c4c4 0 0 2px;
  1627. }
  1628. pre[class*='language-'] button:hover {
  1629. cursor: pointer;
  1630. background-color: #bcbabb;
  1631. }
  1632. </style>