index.html.hpp 33 KB

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