index.html 34 KB

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