index.html 30 KB

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