index.html 34 KB

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