index.html 31 KB

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