index.html 43 KB

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