index.html 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145
  1. <html>
  2. <head>
  3. <meta charset="UTF-8">
  4. <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  5. <meta name="color-scheme" content="light dark">
  6. <title>llama.cpp - chat</title>
  7. <style>
  8. body {
  9. font-family: system-ui;
  10. font-size: 90%;
  11. }
  12. .grid-container {
  13. display: grid;
  14. grid-template-columns: auto auto auto;
  15. padding: 10px;
  16. }
  17. .grid-item {
  18. padding: 5px;
  19. /* font-size: 30px; */
  20. text-align: center;
  21. }
  22. #container {
  23. margin: 0em auto;
  24. display: flex;
  25. flex-direction: column;
  26. justify-content: space-between;
  27. height: 100%;
  28. }
  29. main {
  30. margin: 3px;
  31. display: flex;
  32. flex-direction: column;
  33. justify-content: space-between;
  34. gap: 1em;
  35. flex-grow: 1;
  36. overflow-y: auto;
  37. border: 1px solid #ccc;
  38. border-radius: 5px;
  39. padding: 0.5em;
  40. }
  41. h1 {
  42. text-align: center;
  43. }
  44. .customlink:link {
  45. color: white;
  46. background-color: #007aff;
  47. font-weight: 600;
  48. text-decoration: none;
  49. float: right;
  50. margin-top: 30px;
  51. display: flex;
  52. flex-direction: row;
  53. gap: 0.5em;
  54. justify-content: flex-end;
  55. border-radius: 4px;
  56. padding: 8px;
  57. }
  58. .customlink:visited {
  59. color: white;
  60. background-color: #007aff;
  61. font-weight: 600;
  62. text-decoration: none;
  63. float: right;
  64. margin-top: 30px;
  65. display: flex;
  66. flex-direction: row;
  67. gap: 0.5em;
  68. justify-content: flex-end;
  69. padding: 8px;
  70. }
  71. .customlink:hover {
  72. color: white;
  73. background-color: #0070ee;
  74. font-weight: 600;
  75. text-decoration: none;
  76. float: right;
  77. margin-top: 30px;
  78. display: flex;
  79. flex-direction: row;
  80. gap: 0.5em;
  81. justify-content: flex-end;
  82. padding: 8px;
  83. }
  84. .customlink:active {
  85. color: #0070ee;
  86. background-color: #80b3ef;
  87. font-weight: 600;
  88. text-decoration: none;
  89. float: right;
  90. margin-top: 30px;
  91. display: flex;
  92. flex-direction: row;
  93. gap: 0.5em;
  94. justify-content: flex-end;
  95. padding: 8px;
  96. }
  97. body {
  98. max-width: 600px;
  99. min-width: 300px;
  100. line-height: 1.2;
  101. margin: 0 auto;
  102. padding: 0 0.5em;
  103. }
  104. p {
  105. overflow-wrap: break-word;
  106. word-wrap: break-word;
  107. hyphens: auto;
  108. margin-top: 0.5em;
  109. margin-bottom: 0.5em;
  110. }
  111. #write form {
  112. margin: 1em 0 0 0;
  113. display: flex;
  114. flex-direction: column;
  115. gap: 0.5em;
  116. align-items: stretch;
  117. }
  118. .right {
  119. display: flex;
  120. flex-direction: row;
  121. gap: 0.5em;
  122. justify-content: flex-end;
  123. }
  124. fieldset {
  125. border: none;
  126. padding: 0;
  127. margin: 0;
  128. }
  129. fieldset.two {
  130. display: grid;
  131. grid-template: "a a";
  132. gap: 1em;
  133. }
  134. fieldset.three {
  135. display: grid;
  136. grid-template: "a a a";
  137. gap: 1em;
  138. }
  139. details {
  140. border: 1px solid #aaa;
  141. border-radius: 4px;
  142. padding: 0.5em 0.5em 0;
  143. margin-top: 0.5em;
  144. }
  145. summary {
  146. font-weight: bold;
  147. margin: -0.5em -0.5em 0;
  148. padding: 0.5em;
  149. cursor: pointer;
  150. }
  151. details[open] {
  152. padding: 0.5em;
  153. }
  154. .prob-set {
  155. padding: 0.3em;
  156. border-bottom: 1px solid #ccc;
  157. }
  158. .popover-content {
  159. position: absolute;
  160. background-color: white;
  161. padding: 0.2em;
  162. box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  163. }
  164. textarea {
  165. padding: 5px;
  166. flex-grow: 1;
  167. width: 100%;
  168. }
  169. pre code {
  170. display: block;
  171. background-color: #222;
  172. color: #ddd;
  173. }
  174. code {
  175. font-family: monospace;
  176. padding: 0.1em 0.3em;
  177. border-radius: 3px;
  178. }
  179. fieldset label {
  180. margin: 0.5em 0;
  181. display: block;
  182. }
  183. fieldset label.slim {
  184. margin: 0 0.5em;
  185. display: inline;
  186. }
  187. header,
  188. footer {
  189. text-align: center;
  190. }
  191. footer {
  192. font-size: 80%;
  193. color: #888;
  194. }
  195. .mode-chat textarea[name=prompt] {
  196. height: 4.5em;
  197. }
  198. .mode-completion textarea[name=prompt] {
  199. height: 10em;
  200. }
  201. [contenteditable] {
  202. display: inline-block;
  203. white-space: pre-wrap;
  204. outline: 0px solid transparent;
  205. }
  206. @keyframes loading-bg-wipe {
  207. 0% {
  208. background-position: 0%;
  209. }
  210. 100% {
  211. background-position: 100%;
  212. }
  213. }
  214. .loading {
  215. --loading-color-1: #eeeeee00;
  216. --loading-color-2: #eeeeeeff;
  217. background-size: 50% 100%;
  218. background-image: linear-gradient(90deg, var(--loading-color-1), var(--loading-color-2), var(--loading-color-1));
  219. animation: loading-bg-wipe 2s linear infinite;
  220. }
  221. @media (prefers-color-scheme: dark) {
  222. .loading {
  223. --loading-color-1: #22222200;
  224. --loading-color-2: #222222ff;
  225. }
  226. .popover-content {
  227. background-color: black;
  228. }
  229. }
  230. </style>
  231. <script type="module">
  232. import {
  233. html, h, signal, effect, computed, render, useSignal, useEffect, useRef, Component
  234. } from './index.js';
  235. import { llama } from './completion.js';
  236. import { SchemaConverter } from './json-schema-to-grammar.mjs';
  237. let selected_image = false;
  238. var slot_id = -1;
  239. const session = signal({
  240. prompt: "This is a conversation between User and Llama, a friendly chatbot. Llama is helpful, kind, honest, good at writing, and never fails to answer any requests immediately and with precision.",
  241. template: "{{prompt}}\n\n{{history}}\n{{char}}:",
  242. historyTemplate: "{{name}}: {{message}}",
  243. transcript: [],
  244. type: "chat", // "chat" | "completion"
  245. char: "Llama",
  246. user: "User",
  247. image_selected: ''
  248. })
  249. const params = signal({
  250. n_predict: 400,
  251. temperature: 0.7,
  252. repeat_last_n: 256, // 0 = disable penalty, -1 = context size
  253. repeat_penalty: 1.18, // 1.0 = disabled
  254. penalize_nl: false,
  255. top_k: 40, // <= 0 to use vocab size
  256. top_p: 0.95, // 1.0 = disabled
  257. min_p: 0.05, // 0 = disabled
  258. tfs_z: 1.0, // 1.0 = disabled
  259. typical_p: 1.0, // 1.0 = disabled
  260. presence_penalty: 0.0, // 0.0 = disabled
  261. frequency_penalty: 0.0, // 0.0 = disabled
  262. mirostat: 0, // 0/1/2
  263. mirostat_tau: 5, // target entropy
  264. mirostat_eta: 0.1, // learning rate
  265. grammar: '',
  266. n_probs: 0, // no completion_probabilities,
  267. min_keep: 0, // min probs from each sampler,
  268. image_data: [],
  269. cache_prompt: true,
  270. api_key: ''
  271. })
  272. /* START: Support for storing prompt templates and parameters in browsers LocalStorage */
  273. const local_storage_storageKey = "llamacpp_server_local_storage";
  274. function local_storage_setDataFromObject(tag, content) {
  275. localStorage.setItem(local_storage_storageKey + '/' + tag, JSON.stringify(content));
  276. }
  277. function local_storage_setDataFromRawText(tag, content) {
  278. localStorage.setItem(local_storage_storageKey + '/' + tag, content);
  279. }
  280. function local_storage_getDataAsObject(tag) {
  281. const item = localStorage.getItem(local_storage_storageKey + '/' + tag);
  282. if (!item) {
  283. return null;
  284. } else {
  285. return JSON.parse(item);
  286. }
  287. }
  288. function local_storage_getDataAsRawText(tag) {
  289. const item = localStorage.getItem(local_storage_storageKey + '/' + tag);
  290. if (!item) {
  291. return null;
  292. } else {
  293. return item;
  294. }
  295. }
  296. // create a container for user templates and settings
  297. const savedUserTemplates = signal({})
  298. const selectedUserTemplate = signal({ name: '', template: { session: {}, params: {} } })
  299. // let's import locally saved templates and settings if there are any
  300. // user templates and settings are stored in one object
  301. // in form of { "templatename": "templatedata" } and { "settingstemplatename":"settingsdata" }
  302. console.log('Importing saved templates')
  303. let importedTemplates = local_storage_getDataAsObject('user_templates')
  304. if (importedTemplates) {
  305. // saved templates were successfully imported.
  306. console.log('Processing saved templates and updating default template')
  307. params.value = { ...params.value, image_data: [] };
  308. //console.log(importedTemplates);
  309. savedUserTemplates.value = importedTemplates;
  310. //override default template
  311. savedUserTemplates.value.default = { session: session.value, params: params.value }
  312. local_storage_setDataFromObject('user_templates', savedUserTemplates.value)
  313. } else {
  314. // no saved templates detected.
  315. console.log('Initializing LocalStorage and saving default template')
  316. savedUserTemplates.value = { "default": { session: session.value, params: params.value } }
  317. local_storage_setDataFromObject('user_templates', savedUserTemplates.value)
  318. }
  319. function userTemplateResetToDefault() {
  320. console.log('Resetting template to default')
  321. selectedUserTemplate.value.name = 'default';
  322. selectedUserTemplate.value.data = savedUserTemplates.value['default'];
  323. }
  324. function userTemplateApply(t) {
  325. session.value = t.data.session;
  326. session.value = { ...session.value, image_selected: '' };
  327. params.value = t.data.params;
  328. params.value = { ...params.value, image_data: [] };
  329. }
  330. function userTemplateResetToDefaultAndApply() {
  331. userTemplateResetToDefault()
  332. userTemplateApply(selectedUserTemplate.value)
  333. }
  334. function userTemplateLoadAndApplyAutosaved() {
  335. // get autosaved last used template
  336. let lastUsedTemplate = local_storage_getDataAsObject('user_templates_last')
  337. if (lastUsedTemplate) {
  338. console.log('Autosaved template found, restoring')
  339. selectedUserTemplate.value = lastUsedTemplate
  340. }
  341. else {
  342. console.log('No autosaved template found, using default template')
  343. // no autosaved last used template was found, so load from default.
  344. userTemplateResetToDefault()
  345. }
  346. console.log('Applying template')
  347. // and update internal data from templates
  348. userTemplateApply(selectedUserTemplate.value)
  349. }
  350. //console.log(savedUserTemplates.value)
  351. //console.log(selectedUserTemplate.value)
  352. function userTemplateAutosave() {
  353. console.log('Template Autosave...')
  354. if (selectedUserTemplate.value.name == 'default') {
  355. // we don't want to save over default template, so let's create a new one
  356. let newTemplateName = 'UserTemplate-' + Date.now().toString()
  357. let newTemplate = { 'name': newTemplateName, 'data': { 'session': session.value, 'params': params.value } }
  358. console.log('Saving as ' + newTemplateName)
  359. // save in the autosave slot
  360. local_storage_setDataFromObject('user_templates_last', newTemplate)
  361. // and load it back and apply
  362. userTemplateLoadAndApplyAutosaved()
  363. } else {
  364. local_storage_setDataFromObject('user_templates_last', { 'name': selectedUserTemplate.value.name, 'data': { 'session': session.value, 'params': params.value } })
  365. }
  366. }
  367. console.log('Checking for autosaved last used template')
  368. userTemplateLoadAndApplyAutosaved()
  369. /* END: Support for storing prompt templates and parameters in browsers LocalStorage */
  370. const llamaStats = signal(null)
  371. const controller = signal(null)
  372. // currently generating a completion?
  373. const generating = computed(() => controller.value != null)
  374. // has the user started a chat?
  375. const chatStarted = computed(() => session.value.transcript.length > 0)
  376. const transcriptUpdate = (transcript) => {
  377. session.value = {
  378. ...session.value,
  379. transcript
  380. }
  381. }
  382. // simple template replace
  383. const template = (str, extraSettings) => {
  384. let settings = session.value;
  385. if (extraSettings) {
  386. settings = { ...settings, ...extraSettings };
  387. }
  388. return String(str).replaceAll(/\{\{(.*?)\}\}/g, (_, key) => template(settings[key]));
  389. }
  390. async function runLlama(prompt, llamaParams, char) {
  391. const currentMessages = [];
  392. const history = session.value.transcript;
  393. if (controller.value) {
  394. throw new Error("already running");
  395. }
  396. controller.value = new AbortController();
  397. for await (const chunk of llama(prompt, llamaParams, { controller: controller.value, api_url: location.pathname.replace(/\/+$/, '') })) {
  398. const data = chunk.data;
  399. if (data.stop) {
  400. while (
  401. currentMessages.length > 0 &&
  402. currentMessages[currentMessages.length - 1].content.match(/\n$/) != null
  403. ) {
  404. currentMessages.pop();
  405. }
  406. transcriptUpdate([...history, [char, currentMessages]])
  407. console.log("Completion finished: '", currentMessages.map(msg => msg.content).join(''), "', summary: ", data);
  408. } else {
  409. currentMessages.push(data);
  410. slot_id = data.slot_id;
  411. if (selected_image && !data.multimodal) {
  412. alert("The server was not compiled for multimodal or the model projector can't be loaded.");
  413. return;
  414. }
  415. transcriptUpdate([...history, [char, currentMessages]])
  416. }
  417. if (data.timings) {
  418. llamaStats.value = data;
  419. }
  420. }
  421. controller.value = null;
  422. }
  423. // send message to server
  424. const chat = async (msg) => {
  425. if (controller.value) {
  426. console.log('already running...');
  427. return;
  428. }
  429. transcriptUpdate([...session.value.transcript, ["{{user}}", msg]])
  430. let prompt = template(session.value.template, {
  431. message: msg,
  432. history: session.value.transcript.flatMap(
  433. ([name, data]) =>
  434. template(
  435. session.value.historyTemplate,
  436. {
  437. name,
  438. message: Array.isArray(data) ?
  439. data.map(msg => msg.content).join('').replace(/^\s/, '') :
  440. data,
  441. }
  442. )
  443. ).join("\n"),
  444. });
  445. if (selected_image) {
  446. prompt = `A chat between a curious human and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the human's questions.\nUSER:[img-10]${msg}\nASSISTANT:`;
  447. }
  448. await runLlama(prompt, {
  449. ...params.value,
  450. slot_id: slot_id,
  451. stop: ["</s>", template("{{char}}:"), template("{{user}}:")],
  452. }, "{{char}}");
  453. }
  454. const runCompletion = () => {
  455. if (controller.value) {
  456. console.log('already running...');
  457. return;
  458. }
  459. const { prompt } = session.value;
  460. transcriptUpdate([...session.value.transcript, ["", prompt]]);
  461. runLlama(prompt, {
  462. ...params.value,
  463. slot_id: slot_id,
  464. stop: [],
  465. }, "").finally(() => {
  466. session.value.prompt = session.value.transcript.map(([_, data]) =>
  467. Array.isArray(data) ? data.map(msg => msg.content).join('') : data
  468. ).join('');
  469. session.value.transcript = [];
  470. })
  471. }
  472. const stop = (e) => {
  473. e.preventDefault();
  474. if (controller.value) {
  475. controller.value.abort();
  476. controller.value = null;
  477. }
  478. }
  479. const reset = (e) => {
  480. stop(e);
  481. transcriptUpdate([]);
  482. }
  483. const uploadImage = (e) => {
  484. e.preventDefault();
  485. document.getElementById("fileInput").click();
  486. document.getElementById("fileInput").addEventListener("change", function (event) {
  487. const selectedFile = event.target.files[0];
  488. if (selectedFile) {
  489. const reader = new FileReader();
  490. reader.onload = function () {
  491. const image_data = reader.result;
  492. session.value = { ...session.value, image_selected: image_data };
  493. params.value = {
  494. ...params.value, image_data: [
  495. { data: image_data.replace(/data:image\/[^;]+;base64,/, ''), id: 10 }]
  496. }
  497. };
  498. selected_image = true;
  499. reader.readAsDataURL(selectedFile);
  500. }
  501. });
  502. }
  503. function MessageInput() {
  504. const message = useSignal("")
  505. const submit = (e) => {
  506. stop(e);
  507. chat(message.value);
  508. message.value = "";
  509. }
  510. const enterSubmits = (event) => {
  511. if (event.which === 13 && !event.shiftKey) {
  512. submit(event);
  513. }
  514. }
  515. return html`
  516. <form onsubmit=${submit}>
  517. <div>
  518. <textarea
  519. className=${generating.value ? "loading" : null}
  520. oninput=${(e) => message.value = e.target.value}
  521. onkeypress=${enterSubmits}
  522. placeholder="Say something..."
  523. rows=2
  524. type="text"
  525. value="${message}"
  526. />
  527. </div>
  528. <div class="right">
  529. <button type="submit" disabled=${generating.value}>Send</button>
  530. <button onclick=${uploadImage}>Upload Image</button>
  531. <button onclick=${stop} disabled=${!generating.value}>Stop</button>
  532. <button onclick=${reset}>Reset</button>
  533. </div>
  534. </form>
  535. `
  536. }
  537. function CompletionControls() {
  538. const submit = (e) => {
  539. stop(e);
  540. runCompletion();
  541. }
  542. return html`
  543. <div>
  544. <button onclick=${submit} type="button" disabled=${generating.value}>Start</button>
  545. <button onclick=${stop} disabled=${!generating.value}>Stop</button>
  546. <button onclick=${reset}>Reset</button>
  547. </div>`;
  548. }
  549. const ChatLog = (props) => {
  550. const messages = session.value.transcript;
  551. const container = useRef(null)
  552. useEffect(() => {
  553. // scroll to bottom (if needed)
  554. const parent = container.current.parentElement;
  555. if (parent && parent.scrollHeight <= parent.scrollTop + parent.offsetHeight + 300) {
  556. parent.scrollTo(0, parent.scrollHeight)
  557. }
  558. }, [messages])
  559. const isCompletionMode = session.value.type === 'completion'
  560. const chatLine = ([user, data], index) => {
  561. let message
  562. const isArrayMessage = Array.isArray(data)
  563. if (params.value.n_probs > 0 && isArrayMessage) {
  564. message = html`<${Probabilities} data=${data} />`
  565. } else {
  566. const text = isArrayMessage ?
  567. data.map(msg => msg.content).join('') :
  568. data;
  569. message = isCompletionMode ?
  570. text :
  571. html`<${Markdownish} text=${template(text)} />`
  572. }
  573. if (user) {
  574. return html`<p key=${index}><strong>${template(user)}:</strong> ${message}</p>`
  575. } else {
  576. return isCompletionMode ?
  577. html`<span key=${index}>${message}</span>` :
  578. html`<p key=${index}>${message}</p>`
  579. }
  580. };
  581. const handleCompletionEdit = (e) => {
  582. session.value.prompt = e.target.innerText;
  583. session.value.transcript = [];
  584. }
  585. return html`
  586. <div id="chat" ref=${container} key=${messages.length}>
  587. <img style="width: 60%;${!session.value.image_selected ? `display: none;` : ``}" src="${session.value.image_selected}"/>
  588. <span contenteditable=${isCompletionMode} ref=${container} oninput=${handleCompletionEdit}>
  589. ${messages.flatMap(chatLine)}
  590. </span>
  591. </div>`;
  592. };
  593. const ConfigForm = (props) => {
  594. const updateSession = (el) => session.value = { ...session.value, [el.target.name]: el.target.value }
  595. const updateParams = (el) => params.value = { ...params.value, [el.target.name]: el.target.value }
  596. const updateParamsFloat = (el) => params.value = { ...params.value, [el.target.name]: parseFloat(el.target.value) }
  597. const updateParamsInt = (el) => params.value = { ...params.value, [el.target.name]: Math.floor(parseFloat(el.target.value)) }
  598. const updateParamsBool = (el) => params.value = { ...params.value, [el.target.name]: el.target.checked }
  599. const grammarJsonSchemaPropOrder = signal('')
  600. const updateGrammarJsonSchemaPropOrder = (el) => grammarJsonSchemaPropOrder.value = el.target.value
  601. const convertJSONSchemaGrammar = async () => {
  602. try {
  603. let schema = JSON.parse(params.value.grammar)
  604. const converter = new SchemaConverter({
  605. prop_order: grammarJsonSchemaPropOrder.value
  606. .split(',')
  607. .reduce((acc, cur, i) => ({ ...acc, [cur.trim()]: i }), {}),
  608. allow_fetch: true,
  609. })
  610. schema = await converter.resolveRefs(schema, 'input')
  611. converter.visit(schema, '')
  612. params.value = {
  613. ...params.value,
  614. grammar: converter.formatGrammar(),
  615. }
  616. } catch (e) {
  617. alert(`Convert failed: ${e.message}`)
  618. }
  619. }
  620. const FloatField = ({ label, max, min, name, step, value }) => {
  621. return html`
  622. <div>
  623. <label for="${name}">${label}</label>
  624. <input type="range" id="${name}" min="${min}" max="${max}" step="${step}" name="${name}" value="${value}" oninput=${updateParamsFloat} />
  625. <span>${value}</span>
  626. </div>
  627. `
  628. };
  629. const IntField = ({ label, max, min, name, value }) => {
  630. return html`
  631. <div>
  632. <label for="${name}">${label}</label>
  633. <input type="range" id="${name}" min="${min}" max="${max}" name="${name}" value="${value}" oninput=${updateParamsInt} />
  634. <span>${value}</span>
  635. </div>
  636. `
  637. };
  638. const BoolField = ({ label, name, value }) => {
  639. return html`
  640. <div>
  641. <label for="${name}">${label}</label>
  642. <input type="checkbox" id="${name}" name="${name}" checked="${value}" onclick=${updateParamsBool} />
  643. </div>
  644. `
  645. };
  646. const userTemplateReset = (e) => {
  647. e.preventDefault();
  648. userTemplateResetToDefaultAndApply()
  649. }
  650. const UserTemplateResetButton = () => {
  651. if (selectedUserTemplate.value.name == 'default') {
  652. return html`
  653. <button disabled>Using default template</button>
  654. `
  655. }
  656. return html`
  657. <button onclick=${userTemplateReset}>Reset all to default</button>
  658. `
  659. };
  660. useEffect(() => {
  661. // autosave template on every change
  662. userTemplateAutosave()
  663. }, [session.value, params.value])
  664. const GrammarControl = () => (
  665. html`
  666. <div>
  667. <label for="template">Grammar</label>
  668. <textarea id="grammar" name="grammar" placeholder="Use gbnf or JSON Schema+convert" value="${params.value.grammar}" rows=4 oninput=${updateParams}/>
  669. <input type="text" name="prop-order" placeholder="order: prop1,prop2,prop3" oninput=${updateGrammarJsonSchemaPropOrder} />
  670. <button type="button" onclick=${convertJSONSchemaGrammar}>Convert JSON Schema</button>
  671. </div>
  672. `
  673. );
  674. const PromptControlFieldSet = () => (
  675. html`
  676. <fieldset>
  677. <div>
  678. <label htmlFor="prompt">Prompt</label>
  679. <textarea type="text" name="prompt" value="${session.value.prompt}" oninput=${updateSession}/>
  680. </div>
  681. </fieldset>
  682. `
  683. );
  684. const ChatConfigForm = () => (
  685. html`
  686. ${PromptControlFieldSet()}
  687. <fieldset class="two">
  688. <div>
  689. <label for="user">User name</label>
  690. <input type="text" name="user" value="${session.value.user}" oninput=${updateSession} />
  691. </div>
  692. <div>
  693. <label for="bot">Bot name</label>
  694. <input type="text" name="char" value="${session.value.char}" oninput=${updateSession} />
  695. </div>
  696. </fieldset>
  697. <fieldset>
  698. <div>
  699. <label for="template">Prompt template</label>
  700. <textarea id="template" name="template" value="${session.value.template}" rows=4 oninput=${updateSession}/>
  701. </div>
  702. <div>
  703. <label for="template">Chat history template</label>
  704. <textarea id="template" name="historyTemplate" value="${session.value.historyTemplate}" rows=1 oninput=${updateSession}/>
  705. </div>
  706. ${GrammarControl()}
  707. </fieldset>
  708. `
  709. );
  710. const CompletionConfigForm = () => (
  711. html`
  712. ${PromptControlFieldSet()}
  713. <fieldset>${GrammarControl()}</fieldset>
  714. `
  715. );
  716. return html`
  717. <form>
  718. <fieldset class="two">
  719. <${UserTemplateResetButton}/>
  720. <div>
  721. <label class="slim"><input type="radio" name="type" value="chat" checked=${session.value.type === "chat"} oninput=${updateSession} /> Chat</label>
  722. <label class="slim"><input type="radio" name="type" value="completion" checked=${session.value.type === "completion"} oninput=${updateSession} /> Completion</label>
  723. </div>
  724. </fieldset>
  725. ${session.value.type === 'chat' ? ChatConfigForm() : CompletionConfigForm()}
  726. <fieldset class="two">
  727. ${IntField({ label: "Predictions", max: 2048, min: -1, name: "n_predict", value: params.value.n_predict })}
  728. ${FloatField({ label: "Temperature", max: 2.0, min: 0.0, name: "temperature", step: 0.01, value: params.value.temperature })}
  729. ${FloatField({ label: "Penalize repeat sequence", max: 2.0, min: 0.0, name: "repeat_penalty", step: 0.01, value: params.value.repeat_penalty })}
  730. ${IntField({ label: "Consider N tokens for penalize", max: 2048, min: 0, name: "repeat_last_n", value: params.value.repeat_last_n })}
  731. ${BoolField({ label: "Penalize repetition of newlines", name: "penalize_nl", value: params.value.penalize_nl })}
  732. ${IntField({ label: "Top-K sampling", max: 100, min: -1, name: "top_k", value: params.value.top_k })}
  733. ${FloatField({ label: "Top-P sampling", max: 1.0, min: 0.0, name: "top_p", step: 0.01, value: params.value.top_p })}
  734. ${FloatField({ label: "Min-P sampling", max: 1.0, min: 0.0, name: "min_p", step: 0.01, value: params.value.min_p })}
  735. </fieldset>
  736. <details>
  737. <summary>More options</summary>
  738. <fieldset class="two">
  739. ${FloatField({ label: "TFS-Z", max: 1.0, min: 0.0, name: "tfs_z", step: 0.01, value: params.value.tfs_z })}
  740. ${FloatField({ label: "Typical P", max: 1.0, min: 0.0, name: "typical_p", step: 0.01, value: params.value.typical_p })}
  741. ${FloatField({ label: "Presence penalty", max: 1.0, min: 0.0, name: "presence_penalty", step: 0.01, value: params.value.presence_penalty })}
  742. ${FloatField({ label: "Frequency penalty", max: 1.0, min: 0.0, name: "frequency_penalty", step: 0.01, value: params.value.frequency_penalty })}
  743. </fieldset>
  744. <hr />
  745. <fieldset class="three">
  746. <div>
  747. <label><input type="radio" name="mirostat" value="0" checked=${params.value.mirostat == 0} oninput=${updateParamsInt} /> no Mirostat</label>
  748. <label><input type="radio" name="mirostat" value="1" checked=${params.value.mirostat == 1} oninput=${updateParamsInt} /> Mirostat v1</label>
  749. <label><input type="radio" name="mirostat" value="2" checked=${params.value.mirostat == 2} oninput=${updateParamsInt} /> Mirostat v2</label>
  750. </div>
  751. ${FloatField({ label: "Mirostat tau", max: 10.0, min: 0.0, name: "mirostat_tau", step: 0.01, value: params.value.mirostat_tau })}
  752. ${FloatField({ label: "Mirostat eta", max: 1.0, min: 0.0, name: "mirostat_eta", step: 0.01, value: params.value.mirostat_eta })}
  753. </fieldset>
  754. <fieldset>
  755. ${IntField({ label: "Show Probabilities", max: 10, min: 0, name: "n_probs", value: params.value.n_probs })}
  756. </fieldset>
  757. <fieldset>
  758. ${IntField({ label: "Min Probabilities from each Sampler", max: 10, min: 0, name: "min_keep", value: params.value.min_keep })}
  759. </fieldset>
  760. <fieldset>
  761. <label for="api_key">API Key</label>
  762. <input type="text" name="api_key" value="${params.value.api_key}" placeholder="Enter API key" oninput=${updateParams} />
  763. </fieldset>
  764. </details>
  765. </form>
  766. `
  767. }
  768. const probColor = (p) => {
  769. const r = Math.floor(192 * (1 - p));
  770. const g = Math.floor(192 * p);
  771. return `rgba(${r},${g},0,0.3)`;
  772. }
  773. const Probabilities = (params) => {
  774. return params.data.map(msg => {
  775. const { completion_probabilities } = msg;
  776. if (
  777. !completion_probabilities ||
  778. completion_probabilities.length === 0
  779. ) return msg.content
  780. if (completion_probabilities.length > 1) {
  781. // Not for byte pair
  782. if (completion_probabilities[0].content.startsWith('byte: \\')) return msg.content
  783. const splitData = completion_probabilities.map(prob => ({
  784. content: prob.content,
  785. completion_probabilities: [prob]
  786. }))
  787. return html`<${Probabilities} data=${splitData} />`
  788. }
  789. const { probs, content } = completion_probabilities[0]
  790. const found = probs.find(p => p.tok_str === msg.content)
  791. const pColor = found ? probColor(found.prob) : 'transparent'
  792. const popoverChildren = html`
  793. <div class="prob-set">
  794. ${probs.map((p, index) => {
  795. return html`
  796. <div
  797. key=${index}
  798. title=${`prob: ${p.prob}`}
  799. style=${{
  800. padding: '0.3em',
  801. backgroundColor: p.tok_str === content ? probColor(p.prob) : 'transparent'
  802. }}
  803. >
  804. <span>${p.tok_str}: </span>
  805. <span>${Math.floor(p.prob * 100)}%</span>
  806. </div>
  807. `
  808. })}
  809. </div>
  810. `
  811. return html`
  812. <${Popover} style=${{ backgroundColor: pColor }} popoverChildren=${popoverChildren}>
  813. ${msg.content.match(/\n/gim) ? html`<br />` : msg.content}
  814. </>
  815. `
  816. });
  817. }
  818. // poor mans markdown replacement
  819. const Markdownish = (params) => {
  820. const chunks = params.text.split('```');
  821. for (let i = 0; i < chunks.length; i++) {
  822. if (i % 2 === 0) { // outside code block
  823. chunks[i] = chunks[i]
  824. .replace(/&/g, '&amp;')
  825. .replace(/</g, '&lt;')
  826. .replace(/>/g, '&gt;')
  827. .replace(/(^|\n)#{1,6} ([^\n]*)(?=([^`]*`[^`]*`)*[^`]*$)/g, '$1<h3>$2</h3>')
  828. .replace(/\*\*(.*?)\*\*(?=([^`]*`[^`]*`)*[^`]*$)/g, '<strong>$1</strong>')
  829. .replace(/__(.*?)__(?=([^`]*`[^`]*`)*[^`]*$)/g, '<strong>$1</strong>')
  830. .replace(/\*(.*?)\*(?=([^`]*`[^`]*`)*[^`]*$)/g, '<em>$1</em>')
  831. .replace(/_(.*?)_(?=([^`]*`[^`]*`)*[^`]*$)/g, '<em>$1</em>')
  832. .replace(/```.*?\n([\s\S]*?)```/g, '<pre><code>$1</code></pre>')
  833. .replace(/`(.*?)`/g, '<code>$1</code>')
  834. .replace(/\n/gim, '<br />');
  835. } else { // inside code block
  836. chunks[i] = `<pre><code>${chunks[i]}</code></pre>`;
  837. }
  838. }
  839. const restoredText = chunks.join('');
  840. return html`<span dangerouslySetInnerHTML=${{ __html: restoredText }} />`;
  841. };
  842. const ModelGenerationInfo = (params) => {
  843. if (!llamaStats.value) {
  844. return html`<span/>`
  845. }
  846. return html`
  847. <span>
  848. ${llamaStats.value.tokens_predicted} predicted, ${llamaStats.value.tokens_cached} cached, ${llamaStats.value.timings.predicted_per_token_ms.toFixed()}ms per token, ${llamaStats.value.timings.predicted_per_second.toFixed(2)} tokens per second
  849. </span>
  850. `
  851. }
  852. // simple popover impl
  853. const Popover = (props) => {
  854. const isOpen = useSignal(false);
  855. const position = useSignal({ top: '0px', left: '0px' });
  856. const buttonRef = useRef(null);
  857. const popoverRef = useRef(null);
  858. const togglePopover = () => {
  859. if (buttonRef.current) {
  860. const rect = buttonRef.current.getBoundingClientRect();
  861. position.value = {
  862. top: `${rect.bottom + window.scrollY}px`,
  863. left: `${rect.left + window.scrollX}px`,
  864. };
  865. }
  866. isOpen.value = !isOpen.value;
  867. };
  868. const handleClickOutside = (event) => {
  869. if (popoverRef.current && !popoverRef.current.contains(event.target) && !buttonRef.current.contains(event.target)) {
  870. isOpen.value = false;
  871. }
  872. };
  873. useEffect(() => {
  874. document.addEventListener('mousedown', handleClickOutside);
  875. return () => {
  876. document.removeEventListener('mousedown', handleClickOutside);
  877. };
  878. }, []);
  879. return html`
  880. <span style=${props.style} ref=${buttonRef} onClick=${togglePopover}>${props.children}</span>
  881. ${isOpen.value && html`
  882. <${Portal} into="#portal">
  883. <div
  884. ref=${popoverRef}
  885. class="popover-content"
  886. style=${{
  887. top: position.value.top,
  888. left: position.value.left,
  889. }}
  890. >
  891. ${props.popoverChildren}
  892. </div>
  893. </${Portal}>
  894. `}
  895. `;
  896. };
  897. // Source: preact-portal (https://github.com/developit/preact-portal/blob/master/src/preact-portal.js)
  898. /** Redirect rendering of descendants into the given CSS selector */
  899. class Portal extends Component {
  900. componentDidUpdate(props) {
  901. for (let i in props) {
  902. if (props[i] !== this.props[i]) {
  903. return setTimeout(this.renderLayer);
  904. }
  905. }
  906. }
  907. componentDidMount() {
  908. this.isMounted = true;
  909. this.renderLayer = this.renderLayer.bind(this);
  910. this.renderLayer();
  911. }
  912. componentWillUnmount() {
  913. this.renderLayer(false);
  914. this.isMounted = false;
  915. if (this.remote && this.remote.parentNode) this.remote.parentNode.removeChild(this.remote);
  916. }
  917. findNode(node) {
  918. return typeof node === 'string' ? document.querySelector(node) : node;
  919. }
  920. renderLayer(show = true) {
  921. if (!this.isMounted) return;
  922. // clean up old node if moving bases:
  923. if (this.props.into !== this.intoPointer) {
  924. this.intoPointer = this.props.into;
  925. if (this.into && this.remote) {
  926. this.remote = render(html`<${PortalProxy} />`, this.into, this.remote);
  927. }
  928. this.into = this.findNode(this.props.into);
  929. }
  930. this.remote = render(html`
  931. <${PortalProxy} context=${this.context}>
  932. ${show && this.props.children || null}
  933. </${PortalProxy}>
  934. `, this.into, this.remote);
  935. }
  936. render() {
  937. return null;
  938. }
  939. }
  940. // high-order component that renders its first child if it exists.
  941. // used as a conditional rendering proxy.
  942. class PortalProxy extends Component {
  943. getChildContext() {
  944. return this.props.context;
  945. }
  946. render({ children }) {
  947. return children || null;
  948. }
  949. }
  950. function App(props) {
  951. useEffect(() => {
  952. const query = new URLSearchParams(location.search).get("q");
  953. if (query) chat(query);
  954. }, []);
  955. return html`
  956. <div class="mode-${session.value.type}">
  957. <header>
  958. <div class="grid-container">
  959. <div class="grid-item"></div>
  960. <div class="grid-item"><h1>llama.cpp</h1></div>
  961. <div class="grid-item"><a class="customlink" href="index-new.html">New UI</a></div>
  962. </div>
  963. </header>
  964. <main id="content">
  965. <${chatStarted.value ? ChatLog : ConfigForm} />
  966. </main>
  967. <section id="write">
  968. <${session.value.type === 'chat' ? MessageInput : CompletionControls} />
  969. </section>
  970. <footer>
  971. <p><${ModelGenerationInfo} /></p>
  972. <p>Powered by <a href="https://github.com/ggerganov/llama.cpp">llama.cpp</a> and <a href="https://ggml.ai">ggml.ai</a>.</p>
  973. </footer>
  974. </div>
  975. `;
  976. }
  977. render(h(App), document.querySelector('#container'));
  978. </script>
  979. </head>
  980. <body>
  981. <div id="container">
  982. <input type="file" id="fileInput" accept="image/*" style="display: none;">
  983. </div>
  984. <div id="portal"></div>
  985. </body>
  986. </html>