1
0

simplechat.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  1. // @ts-check
  2. // A simple completions and chat/completions test related web front end logic
  3. // by Humans for All
  4. import * as du from "./datautils.mjs";
  5. import * as ui from "./ui.mjs"
  6. class Roles {
  7. static System = "system";
  8. static User = "user";
  9. static Assistant = "assistant";
  10. }
  11. class ApiEP {
  12. static Type = {
  13. Chat: "chat",
  14. Completion: "completion",
  15. }
  16. static UrlSuffix = {
  17. 'chat': `/chat/completions`,
  18. 'completion': `/completions`,
  19. }
  20. /**
  21. * Build the url from given baseUrl and apiEp id.
  22. * @param {string} baseUrl
  23. * @param {string} apiEP
  24. */
  25. static Url(baseUrl, apiEP) {
  26. if (baseUrl.endsWith("/")) {
  27. baseUrl = baseUrl.substring(0, baseUrl.length-1);
  28. }
  29. return `${baseUrl}${this.UrlSuffix[apiEP]}`;
  30. }
  31. }
  32. let gUsageMsg = `
  33. <p class="role-system">Usage</p>
  34. <ul class="ul1">
  35. <li> System prompt above, to try control ai response characteristics.</li>
  36. <ul class="ul2">
  37. <li> Completion mode - no system prompt normally.</li>
  38. </ul>
  39. <li> Use shift+enter for inserting enter/newline.</li>
  40. <li> Enter your query to ai assistant below.</li>
  41. <li> Default ContextWindow = [System, Last Query+Resp, Cur Query].</li>
  42. <ul class="ul2">
  43. <li> ChatHistInCtxt, MaxTokens, ModelCtxt window to expand</li>
  44. </ul>
  45. </ul>
  46. `;
  47. /** @typedef {{role: string, content: string}[]} ChatMessages */
  48. /** @typedef {{iLastSys: number, xchat: ChatMessages}} SimpleChatODS */
  49. class SimpleChat {
  50. /**
  51. * @param {string} chatId
  52. */
  53. constructor(chatId) {
  54. this.chatId = chatId;
  55. /**
  56. * Maintain in a form suitable for common LLM web service chat/completions' messages entry
  57. * @type {ChatMessages}
  58. */
  59. this.xchat = [];
  60. this.iLastSys = -1;
  61. this.latestResponse = "";
  62. }
  63. clear() {
  64. this.xchat = [];
  65. this.iLastSys = -1;
  66. }
  67. ods_key() {
  68. return `SimpleChat-${this.chatId}`
  69. }
  70. save() {
  71. /** @type {SimpleChatODS} */
  72. let ods = {iLastSys: this.iLastSys, xchat: this.xchat};
  73. localStorage.setItem(this.ods_key(), JSON.stringify(ods));
  74. }
  75. load() {
  76. let sods = localStorage.getItem(this.ods_key());
  77. if (sods == null) {
  78. return;
  79. }
  80. /** @type {SimpleChatODS} */
  81. let ods = JSON.parse(sods);
  82. this.iLastSys = ods.iLastSys;
  83. this.xchat = ods.xchat;
  84. }
  85. /**
  86. * Recent chat messages.
  87. * If iRecentUserMsgCnt < 0
  88. * Then return the full chat history
  89. * Else
  90. * Return chat messages from latest going back till the last/latest system prompt.
  91. * While keeping track that the number of user queries/messages doesnt exceed iRecentUserMsgCnt.
  92. * @param {number} iRecentUserMsgCnt
  93. */
  94. recent_chat(iRecentUserMsgCnt) {
  95. if (iRecentUserMsgCnt < 0) {
  96. return this.xchat;
  97. }
  98. if (iRecentUserMsgCnt == 0) {
  99. console.warn("WARN:SimpleChat:SC:RecentChat:iRecentUsermsgCnt of 0 means no user message/query sent");
  100. }
  101. /** @type{ChatMessages} */
  102. let rchat = [];
  103. let sysMsg = this.get_system_latest();
  104. if (sysMsg.length != 0) {
  105. rchat.push({role: Roles.System, content: sysMsg});
  106. }
  107. let iUserCnt = 0;
  108. let iStart = this.xchat.length;
  109. for(let i=this.xchat.length-1; i > this.iLastSys; i--) {
  110. if (iUserCnt >= iRecentUserMsgCnt) {
  111. break;
  112. }
  113. let msg = this.xchat[i];
  114. if (msg.role == Roles.User) {
  115. iStart = i;
  116. iUserCnt += 1;
  117. }
  118. }
  119. for(let i = iStart; i < this.xchat.length; i++) {
  120. let msg = this.xchat[i];
  121. if (msg.role == Roles.System) {
  122. continue;
  123. }
  124. rchat.push({role: msg.role, content: msg.content});
  125. }
  126. return rchat;
  127. }
  128. /**
  129. * Collate the latest response from the server/ai-model, as it is becoming available.
  130. * This is mainly useful for the stream mode.
  131. * @param {string} content
  132. */
  133. append_response(content) {
  134. this.latestResponse += content;
  135. }
  136. /**
  137. * Add an entry into xchat
  138. * @param {string} role
  139. * @param {string|undefined|null} content
  140. */
  141. add(role, content) {
  142. if ((content == undefined) || (content == null) || (content == "")) {
  143. return false;
  144. }
  145. this.xchat.push( {role: role, content: content} );
  146. if (role == Roles.System) {
  147. this.iLastSys = this.xchat.length - 1;
  148. }
  149. this.save();
  150. return true;
  151. }
  152. /**
  153. * Show the contents in the specified div
  154. * @param {HTMLDivElement} div
  155. * @param {boolean} bClear
  156. */
  157. show(div, bClear=true) {
  158. if (bClear) {
  159. div.replaceChildren();
  160. }
  161. let last = undefined;
  162. for(const x of this.recent_chat(gMe.iRecentUserMsgCnt)) {
  163. let entry = ui.el_create_append_p(`${x.role}: ${x.content}`, div);
  164. entry.className = `role-${x.role}`;
  165. last = entry;
  166. }
  167. if (last !== undefined) {
  168. last.scrollIntoView(false);
  169. } else {
  170. if (bClear) {
  171. div.innerHTML = gUsageMsg;
  172. gMe.setup_load(div, this);
  173. gMe.show_info(div);
  174. }
  175. }
  176. return last;
  177. }
  178. /**
  179. * Setup the fetch headers.
  180. * It picks the headers from gMe.headers.
  181. * It inserts Authorization only if its non-empty.
  182. * @param {string} apiEP
  183. */
  184. fetch_headers(apiEP) {
  185. let headers = new Headers();
  186. for(let k in gMe.headers) {
  187. let v = gMe.headers[k];
  188. if ((k == "Authorization") && (v.trim() == "")) {
  189. continue;
  190. }
  191. headers.append(k, v);
  192. }
  193. return headers;
  194. }
  195. /**
  196. * Add needed fields wrt json object to be sent wrt LLM web services completions endpoint.
  197. * The needed fields/options are picked from a global object.
  198. * Add optional stream flag, if required.
  199. * Convert the json into string.
  200. * @param {Object} obj
  201. */
  202. request_jsonstr_extend(obj) {
  203. for(let k in gMe.apiRequestOptions) {
  204. obj[k] = gMe.apiRequestOptions[k];
  205. }
  206. if (gMe.bStream) {
  207. obj["stream"] = true;
  208. }
  209. return JSON.stringify(obj);
  210. }
  211. /**
  212. * Return a string form of json object suitable for chat/completions
  213. */
  214. request_messages_jsonstr() {
  215. let req = {
  216. messages: this.recent_chat(gMe.iRecentUserMsgCnt),
  217. }
  218. return this.request_jsonstr_extend(req);
  219. }
  220. /**
  221. * Return a string form of json object suitable for /completions
  222. * @param {boolean} bInsertStandardRolePrefix Insert "<THE_ROLE>: " as prefix wrt each role's message
  223. */
  224. request_prompt_jsonstr(bInsertStandardRolePrefix) {
  225. let prompt = "";
  226. let iCnt = 0;
  227. for(const chat of this.recent_chat(gMe.iRecentUserMsgCnt)) {
  228. iCnt += 1;
  229. if (iCnt > 1) {
  230. prompt += "\n";
  231. }
  232. if (bInsertStandardRolePrefix) {
  233. prompt += `${chat.role}: `;
  234. }
  235. prompt += `${chat.content}`;
  236. }
  237. let req = {
  238. prompt: prompt,
  239. }
  240. return this.request_jsonstr_extend(req);
  241. }
  242. /**
  243. * Return a string form of json object suitable for specified api endpoint.
  244. * @param {string} apiEP
  245. */
  246. request_jsonstr(apiEP) {
  247. if (apiEP == ApiEP.Type.Chat) {
  248. return this.request_messages_jsonstr();
  249. } else {
  250. return this.request_prompt_jsonstr(gMe.bCompletionInsertStandardRolePrefix);
  251. }
  252. }
  253. /**
  254. * Extract the ai-model/assistant's response from the http response got.
  255. * Optionally trim the message wrt any garbage at the end.
  256. * @param {any} respBody
  257. * @param {string} apiEP
  258. */
  259. response_extract(respBody, apiEP) {
  260. let assistant = "";
  261. if (apiEP == ApiEP.Type.Chat) {
  262. assistant = respBody["choices"][0]["message"]["content"];
  263. } else {
  264. try {
  265. assistant = respBody["choices"][0]["text"];
  266. } catch {
  267. assistant = respBody["content"];
  268. }
  269. }
  270. return assistant;
  271. }
  272. /**
  273. * Extract the ai-model/assistant's response from the http response got in streaming mode.
  274. * @param {any} respBody
  275. * @param {string} apiEP
  276. */
  277. response_extract_stream(respBody, apiEP) {
  278. let assistant = "";
  279. if (apiEP == ApiEP.Type.Chat) {
  280. if (respBody["choices"][0]["finish_reason"] !== "stop") {
  281. assistant = respBody["choices"][0]["delta"]["content"];
  282. }
  283. } else {
  284. try {
  285. assistant = respBody["choices"][0]["text"];
  286. } catch {
  287. assistant = respBody["content"];
  288. }
  289. }
  290. return assistant;
  291. }
  292. /**
  293. * Allow setting of system prompt, but only at begining.
  294. * @param {string} sysPrompt
  295. * @param {string} msgTag
  296. */
  297. add_system_begin(sysPrompt, msgTag) {
  298. if (this.xchat.length == 0) {
  299. if (sysPrompt.length > 0) {
  300. return this.add(Roles.System, sysPrompt);
  301. }
  302. } else {
  303. if (sysPrompt.length > 0) {
  304. if (this.xchat[0].role !== Roles.System) {
  305. console.error(`ERRR:SimpleChat:SC:${msgTag}:You need to specify system prompt before any user query, ignoring...`);
  306. } else {
  307. if (this.xchat[0].content !== sysPrompt) {
  308. console.error(`ERRR:SimpleChat:SC:${msgTag}:You cant change system prompt, mid way through, ignoring...`);
  309. }
  310. }
  311. }
  312. }
  313. return false;
  314. }
  315. /**
  316. * Allow setting of system prompt, at any time.
  317. * @param {string} sysPrompt
  318. * @param {string} msgTag
  319. */
  320. add_system_anytime(sysPrompt, msgTag) {
  321. if (sysPrompt.length <= 0) {
  322. return false;
  323. }
  324. if (this.iLastSys < 0) {
  325. return this.add(Roles.System, sysPrompt);
  326. }
  327. let lastSys = this.xchat[this.iLastSys].content;
  328. if (lastSys !== sysPrompt) {
  329. return this.add(Roles.System, sysPrompt);
  330. }
  331. return false;
  332. }
  333. /**
  334. * Retrieve the latest system prompt.
  335. */
  336. get_system_latest() {
  337. if (this.iLastSys == -1) {
  338. return "";
  339. }
  340. let sysPrompt = this.xchat[this.iLastSys].content;
  341. return sysPrompt;
  342. }
  343. /**
  344. * Handle the multipart response from server/ai-model
  345. * @param {Response} resp
  346. * @param {string} apiEP
  347. * @param {HTMLDivElement} elDiv
  348. */
  349. async handle_response_multipart(resp, apiEP, elDiv) {
  350. let elP = ui.el_create_append_p("", elDiv);
  351. if (!resp.body) {
  352. throw Error("ERRR:SimpleChat:SC:HandleResponseMultiPart:No body...");
  353. }
  354. let tdUtf8 = new TextDecoder("utf-8");
  355. let rr = resp.body.getReader();
  356. this.latestResponse = "";
  357. let xLines = new du.NewLines();
  358. while(true) {
  359. let { value: cur, done: done } = await rr.read();
  360. if (cur) {
  361. let curBody = tdUtf8.decode(cur, {stream: true});
  362. console.debug("DBUG:SC:PART:Str:", curBody);
  363. xLines.add_append(curBody);
  364. }
  365. while(true) {
  366. let curLine = xLines.shift(!done);
  367. if (curLine == undefined) {
  368. break;
  369. }
  370. if (curLine.trim() == "") {
  371. continue;
  372. }
  373. if (curLine.startsWith("data:")) {
  374. curLine = curLine.substring(5);
  375. }
  376. if (curLine.trim() === "[DONE]") {
  377. break;
  378. }
  379. let curJson = JSON.parse(curLine);
  380. console.debug("DBUG:SC:PART:Json:", curJson);
  381. this.append_response(this.response_extract_stream(curJson, apiEP));
  382. }
  383. elP.innerText = this.latestResponse;
  384. elP.scrollIntoView(false);
  385. if (done) {
  386. break;
  387. }
  388. }
  389. console.debug("DBUG:SC:PART:Full:", this.latestResponse);
  390. return this.latestResponse;
  391. }
  392. /**
  393. * Handle the oneshot response from server/ai-model
  394. * @param {Response} resp
  395. * @param {string} apiEP
  396. */
  397. async handle_response_oneshot(resp, apiEP) {
  398. let respBody = await resp.json();
  399. console.debug(`DBUG:SimpleChat:SC:${this.chatId}:HandleUserSubmit:RespBody:${JSON.stringify(respBody)}`);
  400. return this.response_extract(respBody, apiEP);
  401. }
  402. /**
  403. * Handle the response from the server be it in oneshot or multipart/stream mode.
  404. * Also take care of the optional garbage trimming.
  405. * @param {Response} resp
  406. * @param {string} apiEP
  407. * @param {HTMLDivElement} elDiv
  408. */
  409. async handle_response(resp, apiEP, elDiv) {
  410. let theResp = {
  411. assistant: "",
  412. trimmed: "",
  413. }
  414. if (gMe.bStream) {
  415. try {
  416. theResp.assistant = await this.handle_response_multipart(resp, apiEP, elDiv);
  417. this.latestResponse = "";
  418. } catch (error) {
  419. theResp.assistant = this.latestResponse;
  420. this.add(Roles.Assistant, theResp.assistant);
  421. this.latestResponse = "";
  422. throw error;
  423. }
  424. } else {
  425. theResp.assistant = await this.handle_response_oneshot(resp, apiEP);
  426. }
  427. if (gMe.bTrimGarbage) {
  428. let origMsg = theResp.assistant;
  429. theResp.assistant = du.trim_garbage_at_end(origMsg);
  430. theResp.trimmed = origMsg.substring(theResp.assistant.length);
  431. }
  432. this.add(Roles.Assistant, theResp.assistant);
  433. return theResp;
  434. }
  435. }
  436. class MultiChatUI {
  437. constructor() {
  438. /** @type {Object<string, SimpleChat>} */
  439. this.simpleChats = {};
  440. /** @type {string} */
  441. this.curChatId = "";
  442. // the ui elements
  443. this.elInSystem = /** @type{HTMLInputElement} */(document.getElementById("system-in"));
  444. this.elDivChat = /** @type{HTMLDivElement} */(document.getElementById("chat-div"));
  445. this.elBtnUser = /** @type{HTMLButtonElement} */(document.getElementById("user-btn"));
  446. this.elInUser = /** @type{HTMLInputElement} */(document.getElementById("user-in"));
  447. this.elDivHeading = /** @type{HTMLSelectElement} */(document.getElementById("heading"));
  448. this.elDivSessions = /** @type{HTMLDivElement} */(document.getElementById("sessions-div"));
  449. this.elBtnSettings = /** @type{HTMLButtonElement} */(document.getElementById("settings"));
  450. this.validate_element(this.elInSystem, "system-in");
  451. this.validate_element(this.elDivChat, "chat-div");
  452. this.validate_element(this.elInUser, "user-in");
  453. this.validate_element(this.elDivHeading, "heading");
  454. this.validate_element(this.elDivChat, "sessions-div");
  455. this.validate_element(this.elBtnSettings, "settings");
  456. }
  457. /**
  458. * Check if the element got
  459. * @param {HTMLElement | null} el
  460. * @param {string} msgTag
  461. */
  462. validate_element(el, msgTag) {
  463. if (el == null) {
  464. throw Error(`ERRR:SimpleChat:MCUI:${msgTag} element missing in html...`);
  465. } else {
  466. console.debug(`INFO:SimpleChat:MCUI:${msgTag} Id[${el.id}] Name[${el["name"]}]`);
  467. }
  468. }
  469. /**
  470. * Reset user input ui.
  471. * * clear user input
  472. * * enable user input
  473. * * set focus to user input
  474. */
  475. ui_reset_userinput() {
  476. this.elInUser.value = "";
  477. this.elInUser.disabled = false;
  478. this.elInUser.focus();
  479. }
  480. /**
  481. * Setup the needed callbacks wrt UI, curChatId to defaultChatId and
  482. * optionally switch to specified defaultChatId.
  483. * @param {string} defaultChatId
  484. * @param {boolean} bSwitchSession
  485. */
  486. setup_ui(defaultChatId, bSwitchSession=false) {
  487. this.curChatId = defaultChatId;
  488. if (bSwitchSession) {
  489. this.handle_session_switch(this.curChatId);
  490. }
  491. this.elBtnSettings.addEventListener("click", (ev)=>{
  492. this.elDivChat.replaceChildren();
  493. gMe.show_settings(this.elDivChat);
  494. });
  495. this.elBtnUser.addEventListener("click", (ev)=>{
  496. if (this.elInUser.disabled) {
  497. return;
  498. }
  499. this.handle_user_submit(this.curChatId, gMe.apiEP).catch((/** @type{Error} */reason)=>{
  500. let msg = `ERRR:SimpleChat\nMCUI:HandleUserSubmit:${this.curChatId}\n${reason.name}:${reason.message}`;
  501. console.error(msg.replace("\n", ":"));
  502. alert(msg);
  503. this.ui_reset_userinput();
  504. });
  505. });
  506. this.elInUser.addEventListener("keyup", (ev)=> {
  507. // allow user to insert enter into their message using shift+enter.
  508. // while just pressing enter key will lead to submitting.
  509. if ((ev.key === "Enter") && (!ev.shiftKey)) {
  510. let value = this.elInUser.value;
  511. this.elInUser.value = value.substring(0,value.length-1);
  512. this.elBtnUser.click();
  513. ev.preventDefault();
  514. }
  515. });
  516. this.elInSystem.addEventListener("keyup", (ev)=> {
  517. // allow user to insert enter into the system prompt using shift+enter.
  518. // while just pressing enter key will lead to setting the system prompt.
  519. if ((ev.key === "Enter") && (!ev.shiftKey)) {
  520. let value = this.elInSystem.value;
  521. this.elInSystem.value = value.substring(0,value.length-1);
  522. let chat = this.simpleChats[this.curChatId];
  523. chat.add_system_anytime(this.elInSystem.value, this.curChatId);
  524. chat.show(this.elDivChat);
  525. ev.preventDefault();
  526. }
  527. });
  528. }
  529. /**
  530. * Setup a new chat session and optionally switch to it.
  531. * @param {string} chatId
  532. * @param {boolean} bSwitchSession
  533. */
  534. new_chat_session(chatId, bSwitchSession=false) {
  535. this.simpleChats[chatId] = new SimpleChat(chatId);
  536. if (bSwitchSession) {
  537. this.handle_session_switch(chatId);
  538. }
  539. }
  540. /**
  541. * Handle user query submit request, wrt specified chat session.
  542. * @param {string} chatId
  543. * @param {string} apiEP
  544. */
  545. async handle_user_submit(chatId, apiEP) {
  546. let chat = this.simpleChats[chatId];
  547. // In completion mode, if configured, clear any previous chat history.
  548. // So if user wants to simulate a multi-chat based completion query,
  549. // they will have to enter the full thing, as a suitable multiline
  550. // user input/query.
  551. if ((apiEP == ApiEP.Type.Completion) && (gMe.bCompletionFreshChatAlways)) {
  552. chat.clear();
  553. }
  554. chat.add_system_anytime(this.elInSystem.value, chatId);
  555. let content = this.elInUser.value;
  556. if (!chat.add(Roles.User, content)) {
  557. console.debug(`WARN:SimpleChat:MCUI:${chatId}:HandleUserSubmit:Ignoring empty user input...`);
  558. return;
  559. }
  560. chat.show(this.elDivChat);
  561. let theUrl = ApiEP.Url(gMe.baseURL, apiEP);
  562. let theBody = chat.request_jsonstr(apiEP);
  563. this.elInUser.value = "working...";
  564. this.elInUser.disabled = true;
  565. console.debug(`DBUG:SimpleChat:MCUI:${chatId}:HandleUserSubmit:${theUrl}:ReqBody:${theBody}`);
  566. let theHeaders = chat.fetch_headers(apiEP);
  567. let resp = await fetch(theUrl, {
  568. method: "POST",
  569. headers: theHeaders,
  570. body: theBody,
  571. });
  572. let theResp = await chat.handle_response(resp, apiEP, this.elDivChat);
  573. if (chatId == this.curChatId) {
  574. chat.show(this.elDivChat);
  575. if (theResp.trimmed.length > 0) {
  576. let p = ui.el_create_append_p(`TRIMMED:${theResp.trimmed}`, this.elDivChat);
  577. p.className="role-trim";
  578. }
  579. } else {
  580. console.debug(`DBUG:SimpleChat:MCUI:HandleUserSubmit:ChatId has changed:[${chatId}] [${this.curChatId}]`);
  581. }
  582. this.ui_reset_userinput();
  583. }
  584. /**
  585. * Show buttons for NewChat and available chat sessions, in the passed elDiv.
  586. * If elDiv is undefined/null, then use this.elDivSessions.
  587. * Take care of highlighting the selected chat-session's btn.
  588. * @param {HTMLDivElement | undefined} elDiv
  589. */
  590. show_sessions(elDiv=undefined) {
  591. if (!elDiv) {
  592. elDiv = this.elDivSessions;
  593. }
  594. elDiv.replaceChildren();
  595. // Btn for creating new chat session
  596. let btnNew = ui.el_create_button("New CHAT", (ev)=> {
  597. if (this.elInUser.disabled) {
  598. console.error(`ERRR:SimpleChat:MCUI:NewChat:Current session [${this.curChatId}] awaiting response, ignoring request...`);
  599. alert("ERRR:SimpleChat\nMCUI:NewChat\nWait for response to pending query, before starting new chat session");
  600. return;
  601. }
  602. let chatId = `Chat${Object.keys(this.simpleChats).length}`;
  603. let chatIdGot = prompt("INFO:SimpleChat\nMCUI:NewChat\nEnter id for new chat session", chatId);
  604. if (!chatIdGot) {
  605. console.error("ERRR:SimpleChat:MCUI:NewChat:Skipping based on user request...");
  606. return;
  607. }
  608. this.new_chat_session(chatIdGot, true);
  609. this.create_session_btn(elDiv, chatIdGot);
  610. ui.el_children_config_class(elDiv, chatIdGot, "session-selected", "");
  611. });
  612. elDiv.appendChild(btnNew);
  613. // Btns for existing chat sessions
  614. let chatIds = Object.keys(this.simpleChats);
  615. for(let cid of chatIds) {
  616. let btn = this.create_session_btn(elDiv, cid);
  617. if (cid == this.curChatId) {
  618. btn.className = "session-selected";
  619. }
  620. }
  621. }
  622. create_session_btn(elDiv, cid) {
  623. let btn = ui.el_create_button(cid, (ev)=>{
  624. let target = /** @type{HTMLButtonElement} */(ev.target);
  625. console.debug(`DBUG:SimpleChat:MCUI:SessionClick:${target.id}`);
  626. if (this.elInUser.disabled) {
  627. console.error(`ERRR:SimpleChat:MCUI:SessionClick:${target.id}:Current session [${this.curChatId}] awaiting response, ignoring switch...`);
  628. alert("ERRR:SimpleChat\nMCUI:SessionClick\nWait for response to pending query, before switching");
  629. return;
  630. }
  631. this.handle_session_switch(target.id);
  632. ui.el_children_config_class(elDiv, target.id, "session-selected", "");
  633. });
  634. elDiv.appendChild(btn);
  635. return btn;
  636. }
  637. /**
  638. * Switch ui to the specified chatId and set curChatId to same.
  639. * @param {string} chatId
  640. */
  641. async handle_session_switch(chatId) {
  642. let chat = this.simpleChats[chatId];
  643. if (chat == undefined) {
  644. console.error(`ERRR:SimpleChat:MCUI:HandleSessionSwitch:${chatId} missing...`);
  645. return;
  646. }
  647. this.elInSystem.value = chat.get_system_latest();
  648. this.elInUser.value = "";
  649. chat.show(this.elDivChat);
  650. this.elInUser.focus();
  651. this.curChatId = chatId;
  652. console.log(`INFO:SimpleChat:MCUI:HandleSessionSwitch:${chatId} entered...`);
  653. }
  654. }
  655. class Me {
  656. constructor() {
  657. this.baseURL = "http://127.0.0.1:8080";
  658. this.defaultChatIds = [ "Default", "Other" ];
  659. this.multiChat = new MultiChatUI();
  660. this.bStream = true;
  661. this.bCompletionFreshChatAlways = true;
  662. this.bCompletionInsertStandardRolePrefix = false;
  663. this.bTrimGarbage = true;
  664. this.iRecentUserMsgCnt = 2;
  665. this.sRecentUserMsgCnt = {
  666. "Full": -1,
  667. "Last0": 1,
  668. "Last1": 2,
  669. "Last2": 3,
  670. "Last4": 5,
  671. };
  672. this.apiEP = ApiEP.Type.Chat;
  673. this.headers = {
  674. "Content-Type": "application/json",
  675. "Authorization": "", // Authorization: Bearer OPENAI_API_KEY
  676. }
  677. // Add needed fields wrt json object to be sent wrt LLM web services completions endpoint.
  678. this.apiRequestOptions = {
  679. "model": "gpt-3.5-turbo",
  680. "temperature": 0.7,
  681. "max_tokens": 1024,
  682. "n_predict": 1024,
  683. "cache_prompt": false,
  684. //"frequency_penalty": 1.2,
  685. //"presence_penalty": 1.2,
  686. };
  687. }
  688. /**
  689. * Disable console.debug by mapping it to a empty function.
  690. */
  691. debug_disable() {
  692. this.console_debug = console.debug;
  693. console.debug = () => {
  694. };
  695. }
  696. /**
  697. * Setup the load saved chat ui.
  698. * @param {HTMLDivElement} div
  699. * @param {SimpleChat} chat
  700. */
  701. setup_load(div, chat) {
  702. if (!(chat.ods_key() in localStorage)) {
  703. return;
  704. }
  705. div.innerHTML += `<p class="role-system">Restore</p>
  706. <p>Load previously saved chat session, if available</p>`;
  707. let btn = ui.el_create_button(chat.ods_key(), (ev)=>{
  708. console.log("DBUG:SimpleChat:SC:Load", chat);
  709. chat.load();
  710. queueMicrotask(()=>{
  711. chat.show(div);
  712. this.multiChat.elInSystem.value = chat.get_system_latest();
  713. });
  714. });
  715. div.appendChild(btn);
  716. }
  717. /**
  718. * Show the configurable parameters info in the passed Div element.
  719. * @param {HTMLDivElement} elDiv
  720. * @param {boolean} bAll
  721. */
  722. show_info(elDiv, bAll=false) {
  723. let p = ui.el_create_append_p("Settings (devel-tools-console document[gMe])", elDiv);
  724. p.className = "role-system";
  725. if (bAll) {
  726. ui.el_create_append_p(`baseURL:${this.baseURL}`, elDiv);
  727. ui.el_create_append_p(`Authorization:${this.headers["Authorization"]}`, elDiv);
  728. ui.el_create_append_p(`bStream:${this.bStream}`, elDiv);
  729. ui.el_create_append_p(`bTrimGarbage:${this.bTrimGarbage}`, elDiv);
  730. ui.el_create_append_p(`ApiEndPoint:${this.apiEP}`, elDiv);
  731. ui.el_create_append_p(`iRecentUserMsgCnt:${this.iRecentUserMsgCnt}`, elDiv);
  732. ui.el_create_append_p(`bCompletionFreshChatAlways:${this.bCompletionFreshChatAlways}`, elDiv);
  733. ui.el_create_append_p(`bCompletionInsertStandardRolePrefix:${this.bCompletionInsertStandardRolePrefix}`, elDiv);
  734. }
  735. ui.el_create_append_p(`apiRequestOptions:${JSON.stringify(this.apiRequestOptions, null, " - ")}`, elDiv);
  736. ui.el_create_append_p(`headers:${JSON.stringify(this.headers, null, " - ")}`, elDiv);
  737. }
  738. /**
  739. * Auto create ui input elements for fields in apiRequestOptions
  740. * Currently supports text and number field types.
  741. * @param {HTMLDivElement} elDiv
  742. */
  743. show_settings_apirequestoptions(elDiv) {
  744. let typeDict = {
  745. "string": "text",
  746. "number": "number",
  747. };
  748. let fs = document.createElement("fieldset");
  749. let legend = document.createElement("legend");
  750. legend.innerText = "ApiRequestOptions";
  751. fs.appendChild(legend);
  752. elDiv.appendChild(fs);
  753. for(const k in this.apiRequestOptions) {
  754. let val = this.apiRequestOptions[k];
  755. let type = typeof(val);
  756. if (((type == "string") || (type == "number"))) {
  757. let inp = ui.el_creatediv_input(`Set${k}`, k, typeDict[type], this.apiRequestOptions[k], (val)=>{
  758. if (type == "number") {
  759. val = Number(val);
  760. }
  761. this.apiRequestOptions[k] = val;
  762. });
  763. fs.appendChild(inp.div);
  764. } else if (type == "boolean") {
  765. let bbtn = ui.el_creatediv_boolbutton(`Set{k}`, k, {true: "true", false: "false"}, val, (userVal)=>{
  766. this.apiRequestOptions[k] = userVal;
  767. });
  768. fs.appendChild(bbtn.div);
  769. }
  770. }
  771. }
  772. /**
  773. * Show settings ui for configurable parameters, in the passed Div element.
  774. * @param {HTMLDivElement} elDiv
  775. */
  776. show_settings(elDiv) {
  777. let inp = ui.el_creatediv_input("SetBaseURL", "BaseURL", "text", this.baseURL, (val)=>{
  778. this.baseURL = val;
  779. });
  780. elDiv.appendChild(inp.div);
  781. inp = ui.el_creatediv_input("SetAuthorization", "Authorization", "text", this.headers["Authorization"], (val)=>{
  782. this.headers["Authorization"] = val;
  783. });
  784. inp.el.placeholder = "Bearer OPENAI_API_KEY";
  785. elDiv.appendChild(inp.div);
  786. let bb = ui.el_creatediv_boolbutton("SetStream", "Stream", {true: "[+] yes stream", false: "[-] do oneshot"}, this.bStream, (val)=>{
  787. this.bStream = val;
  788. });
  789. elDiv.appendChild(bb.div);
  790. bb = ui.el_creatediv_boolbutton("SetTrimGarbage", "TrimGarbage", {true: "[+] yes trim", false: "[-] dont trim"}, this.bTrimGarbage, (val)=>{
  791. this.bTrimGarbage = val;
  792. });
  793. elDiv.appendChild(bb.div);
  794. this.show_settings_apirequestoptions(elDiv);
  795. let sel = ui.el_creatediv_select("SetApiEP", "ApiEndPoint", ApiEP.Type, this.apiEP, (val)=>{
  796. this.apiEP = ApiEP.Type[val];
  797. });
  798. elDiv.appendChild(sel.div);
  799. sel = ui.el_creatediv_select("SetChatHistoryInCtxt", "ChatHistoryInCtxt", this.sRecentUserMsgCnt, this.iRecentUserMsgCnt, (val)=>{
  800. this.iRecentUserMsgCnt = this.sRecentUserMsgCnt[val];
  801. });
  802. elDiv.appendChild(sel.div);
  803. bb = ui.el_creatediv_boolbutton("SetCompletionFreshChatAlways", "CompletionFreshChatAlways", {true: "[+] yes fresh", false: "[-] no, with history"}, this.bCompletionFreshChatAlways, (val)=>{
  804. this.bCompletionFreshChatAlways = val;
  805. });
  806. elDiv.appendChild(bb.div);
  807. bb = ui.el_creatediv_boolbutton("SetCompletionInsertStandardRolePrefix", "CompletionInsertStandardRolePrefix", {true: "[+] yes insert", false: "[-] dont insert"}, this.bCompletionInsertStandardRolePrefix, (val)=>{
  808. this.bCompletionInsertStandardRolePrefix = val;
  809. });
  810. elDiv.appendChild(bb.div);
  811. }
  812. }
  813. /** @type {Me} */
  814. let gMe;
  815. function startme() {
  816. console.log("INFO:SimpleChat:StartMe:Starting...");
  817. gMe = new Me();
  818. gMe.debug_disable();
  819. document["gMe"] = gMe;
  820. document["du"] = du;
  821. for (let cid of gMe.defaultChatIds) {
  822. gMe.multiChat.new_chat_session(cid);
  823. }
  824. gMe.multiChat.setup_ui(gMe.defaultChatIds[0], true);
  825. gMe.multiChat.show_sessions();
  826. }
  827. document.addEventListener("DOMContentLoaded", startme);