simplechat.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  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. let curJson = JSON.parse(curLine);
  377. console.debug("DBUG:SC:PART:Json:", curJson);
  378. this.append_response(this.response_extract_stream(curJson, apiEP));
  379. }
  380. elP.innerText = this.latestResponse;
  381. elP.scrollIntoView(false);
  382. if (done) {
  383. break;
  384. }
  385. }
  386. console.debug("DBUG:SC:PART:Full:", this.latestResponse);
  387. return this.latestResponse;
  388. }
  389. /**
  390. * Handle the oneshot response from server/ai-model
  391. * @param {Response} resp
  392. * @param {string} apiEP
  393. */
  394. async handle_response_oneshot(resp, apiEP) {
  395. let respBody = await resp.json();
  396. console.debug(`DBUG:SimpleChat:SC:${this.chatId}:HandleUserSubmit:RespBody:${JSON.stringify(respBody)}`);
  397. return this.response_extract(respBody, apiEP);
  398. }
  399. /**
  400. * Handle the response from the server be it in oneshot or multipart/stream mode.
  401. * Also take care of the optional garbage trimming.
  402. * @param {Response} resp
  403. * @param {string} apiEP
  404. * @param {HTMLDivElement} elDiv
  405. */
  406. async handle_response(resp, apiEP, elDiv) {
  407. let theResp = {
  408. assistant: "",
  409. trimmed: "",
  410. }
  411. if (gMe.bStream) {
  412. try {
  413. theResp.assistant = await this.handle_response_multipart(resp, apiEP, elDiv);
  414. this.latestResponse = "";
  415. } catch (error) {
  416. theResp.assistant = this.latestResponse;
  417. this.add(Roles.Assistant, theResp.assistant);
  418. this.latestResponse = "";
  419. throw error;
  420. }
  421. } else {
  422. theResp.assistant = await this.handle_response_oneshot(resp, apiEP);
  423. }
  424. if (gMe.bTrimGarbage) {
  425. let origMsg = theResp.assistant;
  426. theResp.assistant = du.trim_garbage_at_end(origMsg);
  427. theResp.trimmed = origMsg.substring(theResp.assistant.length);
  428. }
  429. this.add(Roles.Assistant, theResp.assistant);
  430. return theResp;
  431. }
  432. }
  433. class MultiChatUI {
  434. constructor() {
  435. /** @type {Object<string, SimpleChat>} */
  436. this.simpleChats = {};
  437. /** @type {string} */
  438. this.curChatId = "";
  439. // the ui elements
  440. this.elInSystem = /** @type{HTMLInputElement} */(document.getElementById("system-in"));
  441. this.elDivChat = /** @type{HTMLDivElement} */(document.getElementById("chat-div"));
  442. this.elBtnUser = /** @type{HTMLButtonElement} */(document.getElementById("user-btn"));
  443. this.elInUser = /** @type{HTMLInputElement} */(document.getElementById("user-in"));
  444. this.elDivHeading = /** @type{HTMLSelectElement} */(document.getElementById("heading"));
  445. this.elDivSessions = /** @type{HTMLDivElement} */(document.getElementById("sessions-div"));
  446. this.elBtnSettings = /** @type{HTMLButtonElement} */(document.getElementById("settings"));
  447. this.validate_element(this.elInSystem, "system-in");
  448. this.validate_element(this.elDivChat, "chat-div");
  449. this.validate_element(this.elInUser, "user-in");
  450. this.validate_element(this.elDivHeading, "heading");
  451. this.validate_element(this.elDivChat, "sessions-div");
  452. this.validate_element(this.elBtnSettings, "settings");
  453. }
  454. /**
  455. * Check if the element got
  456. * @param {HTMLElement | null} el
  457. * @param {string} msgTag
  458. */
  459. validate_element(el, msgTag) {
  460. if (el == null) {
  461. throw Error(`ERRR:SimpleChat:MCUI:${msgTag} element missing in html...`);
  462. } else {
  463. console.debug(`INFO:SimpleChat:MCUI:${msgTag} Id[${el.id}] Name[${el["name"]}]`);
  464. }
  465. }
  466. /**
  467. * Reset user input ui.
  468. * * clear user input
  469. * * enable user input
  470. * * set focus to user input
  471. */
  472. ui_reset_userinput() {
  473. this.elInUser.value = "";
  474. this.elInUser.disabled = false;
  475. this.elInUser.focus();
  476. }
  477. /**
  478. * Setup the needed callbacks wrt UI, curChatId to defaultChatId and
  479. * optionally switch to specified defaultChatId.
  480. * @param {string} defaultChatId
  481. * @param {boolean} bSwitchSession
  482. */
  483. setup_ui(defaultChatId, bSwitchSession=false) {
  484. this.curChatId = defaultChatId;
  485. if (bSwitchSession) {
  486. this.handle_session_switch(this.curChatId);
  487. }
  488. this.elBtnSettings.addEventListener("click", (ev)=>{
  489. this.elDivChat.replaceChildren();
  490. gMe.show_settings(this.elDivChat);
  491. });
  492. this.elBtnUser.addEventListener("click", (ev)=>{
  493. if (this.elInUser.disabled) {
  494. return;
  495. }
  496. this.handle_user_submit(this.curChatId, gMe.apiEP).catch((/** @type{Error} */reason)=>{
  497. let msg = `ERRR:SimpleChat\nMCUI:HandleUserSubmit:${this.curChatId}\n${reason.name}:${reason.message}`;
  498. console.error(msg.replace("\n", ":"));
  499. alert(msg);
  500. this.ui_reset_userinput();
  501. });
  502. });
  503. this.elInUser.addEventListener("keyup", (ev)=> {
  504. // allow user to insert enter into their message using shift+enter.
  505. // while just pressing enter key will lead to submitting.
  506. if ((ev.key === "Enter") && (!ev.shiftKey)) {
  507. let value = this.elInUser.value;
  508. this.elInUser.value = value.substring(0,value.length-1);
  509. this.elBtnUser.click();
  510. ev.preventDefault();
  511. }
  512. });
  513. this.elInSystem.addEventListener("keyup", (ev)=> {
  514. // allow user to insert enter into the system prompt using shift+enter.
  515. // while just pressing enter key will lead to setting the system prompt.
  516. if ((ev.key === "Enter") && (!ev.shiftKey)) {
  517. let value = this.elInSystem.value;
  518. this.elInSystem.value = value.substring(0,value.length-1);
  519. let chat = this.simpleChats[this.curChatId];
  520. chat.add_system_anytime(this.elInSystem.value, this.curChatId);
  521. chat.show(this.elDivChat);
  522. ev.preventDefault();
  523. }
  524. });
  525. }
  526. /**
  527. * Setup a new chat session and optionally switch to it.
  528. * @param {string} chatId
  529. * @param {boolean} bSwitchSession
  530. */
  531. new_chat_session(chatId, bSwitchSession=false) {
  532. this.simpleChats[chatId] = new SimpleChat(chatId);
  533. if (bSwitchSession) {
  534. this.handle_session_switch(chatId);
  535. }
  536. }
  537. /**
  538. * Handle user query submit request, wrt specified chat session.
  539. * @param {string} chatId
  540. * @param {string} apiEP
  541. */
  542. async handle_user_submit(chatId, apiEP) {
  543. let chat = this.simpleChats[chatId];
  544. // In completion mode, if configured, clear any previous chat history.
  545. // So if user wants to simulate a multi-chat based completion query,
  546. // they will have to enter the full thing, as a suitable multiline
  547. // user input/query.
  548. if ((apiEP == ApiEP.Type.Completion) && (gMe.bCompletionFreshChatAlways)) {
  549. chat.clear();
  550. }
  551. chat.add_system_anytime(this.elInSystem.value, chatId);
  552. let content = this.elInUser.value;
  553. if (!chat.add(Roles.User, content)) {
  554. console.debug(`WARN:SimpleChat:MCUI:${chatId}:HandleUserSubmit:Ignoring empty user input...`);
  555. return;
  556. }
  557. chat.show(this.elDivChat);
  558. let theUrl = ApiEP.Url(gMe.baseURL, apiEP);
  559. let theBody = chat.request_jsonstr(apiEP);
  560. this.elInUser.value = "working...";
  561. this.elInUser.disabled = true;
  562. console.debug(`DBUG:SimpleChat:MCUI:${chatId}:HandleUserSubmit:${theUrl}:ReqBody:${theBody}`);
  563. let theHeaders = chat.fetch_headers(apiEP);
  564. let resp = await fetch(theUrl, {
  565. method: "POST",
  566. headers: theHeaders,
  567. body: theBody,
  568. });
  569. let theResp = await chat.handle_response(resp, apiEP, this.elDivChat);
  570. if (chatId == this.curChatId) {
  571. chat.show(this.elDivChat);
  572. if (theResp.trimmed.length > 0) {
  573. let p = ui.el_create_append_p(`TRIMMED:${theResp.trimmed}`, this.elDivChat);
  574. p.className="role-trim";
  575. }
  576. } else {
  577. console.debug(`DBUG:SimpleChat:MCUI:HandleUserSubmit:ChatId has changed:[${chatId}] [${this.curChatId}]`);
  578. }
  579. this.ui_reset_userinput();
  580. }
  581. /**
  582. * Show buttons for NewChat and available chat sessions, in the passed elDiv.
  583. * If elDiv is undefined/null, then use this.elDivSessions.
  584. * Take care of highlighting the selected chat-session's btn.
  585. * @param {HTMLDivElement | undefined} elDiv
  586. */
  587. show_sessions(elDiv=undefined) {
  588. if (!elDiv) {
  589. elDiv = this.elDivSessions;
  590. }
  591. elDiv.replaceChildren();
  592. // Btn for creating new chat session
  593. let btnNew = ui.el_create_button("New CHAT", (ev)=> {
  594. if (this.elInUser.disabled) {
  595. console.error(`ERRR:SimpleChat:MCUI:NewChat:Current session [${this.curChatId}] awaiting response, ignoring request...`);
  596. alert("ERRR:SimpleChat\nMCUI:NewChat\nWait for response to pending query, before starting new chat session");
  597. return;
  598. }
  599. let chatId = `Chat${Object.keys(this.simpleChats).length}`;
  600. let chatIdGot = prompt("INFO:SimpleChat\nMCUI:NewChat\nEnter id for new chat session", chatId);
  601. if (!chatIdGot) {
  602. console.error("ERRR:SimpleChat:MCUI:NewChat:Skipping based on user request...");
  603. return;
  604. }
  605. this.new_chat_session(chatIdGot, true);
  606. this.create_session_btn(elDiv, chatIdGot);
  607. ui.el_children_config_class(elDiv, chatIdGot, "session-selected", "");
  608. });
  609. elDiv.appendChild(btnNew);
  610. // Btns for existing chat sessions
  611. let chatIds = Object.keys(this.simpleChats);
  612. for(let cid of chatIds) {
  613. let btn = this.create_session_btn(elDiv, cid);
  614. if (cid == this.curChatId) {
  615. btn.className = "session-selected";
  616. }
  617. }
  618. }
  619. create_session_btn(elDiv, cid) {
  620. let btn = ui.el_create_button(cid, (ev)=>{
  621. let target = /** @type{HTMLButtonElement} */(ev.target);
  622. console.debug(`DBUG:SimpleChat:MCUI:SessionClick:${target.id}`);
  623. if (this.elInUser.disabled) {
  624. console.error(`ERRR:SimpleChat:MCUI:SessionClick:${target.id}:Current session [${this.curChatId}] awaiting response, ignoring switch...`);
  625. alert("ERRR:SimpleChat\nMCUI:SessionClick\nWait for response to pending query, before switching");
  626. return;
  627. }
  628. this.handle_session_switch(target.id);
  629. ui.el_children_config_class(elDiv, target.id, "session-selected", "");
  630. });
  631. elDiv.appendChild(btn);
  632. return btn;
  633. }
  634. /**
  635. * Switch ui to the specified chatId and set curChatId to same.
  636. * @param {string} chatId
  637. */
  638. async handle_session_switch(chatId) {
  639. let chat = this.simpleChats[chatId];
  640. if (chat == undefined) {
  641. console.error(`ERRR:SimpleChat:MCUI:HandleSessionSwitch:${chatId} missing...`);
  642. return;
  643. }
  644. this.elInSystem.value = chat.get_system_latest();
  645. this.elInUser.value = "";
  646. chat.show(this.elDivChat);
  647. this.elInUser.focus();
  648. this.curChatId = chatId;
  649. console.log(`INFO:SimpleChat:MCUI:HandleSessionSwitch:${chatId} entered...`);
  650. }
  651. }
  652. class Me {
  653. constructor() {
  654. this.baseURL = "http://127.0.0.1:8080";
  655. this.defaultChatIds = [ "Default", "Other" ];
  656. this.multiChat = new MultiChatUI();
  657. this.bStream = true;
  658. this.bCompletionFreshChatAlways = true;
  659. this.bCompletionInsertStandardRolePrefix = false;
  660. this.bTrimGarbage = true;
  661. this.iRecentUserMsgCnt = 2;
  662. this.sRecentUserMsgCnt = {
  663. "Full": -1,
  664. "Last0": 1,
  665. "Last1": 2,
  666. "Last2": 3,
  667. "Last4": 5,
  668. };
  669. this.apiEP = ApiEP.Type.Chat;
  670. this.headers = {
  671. "Content-Type": "application/json",
  672. "Authorization": "", // Authorization: Bearer OPENAI_API_KEY
  673. }
  674. // Add needed fields wrt json object to be sent wrt LLM web services completions endpoint.
  675. this.apiRequestOptions = {
  676. "model": "gpt-3.5-turbo",
  677. "temperature": 0.7,
  678. "max_tokens": 1024,
  679. "n_predict": 1024,
  680. "cache_prompt": false,
  681. //"frequency_penalty": 1.2,
  682. //"presence_penalty": 1.2,
  683. };
  684. }
  685. /**
  686. * Disable console.debug by mapping it to a empty function.
  687. */
  688. debug_disable() {
  689. this.console_debug = console.debug;
  690. console.debug = () => {
  691. };
  692. }
  693. /**
  694. * Setup the load saved chat ui.
  695. * @param {HTMLDivElement} div
  696. * @param {SimpleChat} chat
  697. */
  698. setup_load(div, chat) {
  699. if (!(chat.ods_key() in localStorage)) {
  700. return;
  701. }
  702. div.innerHTML += `<p class="role-system">Restore</p>
  703. <p>Load previously saved chat session, if available</p>`;
  704. let btn = ui.el_create_button(chat.ods_key(), (ev)=>{
  705. console.log("DBUG:SimpleChat:SC:Load", chat);
  706. chat.load();
  707. queueMicrotask(()=>{
  708. chat.show(div);
  709. this.multiChat.elInSystem.value = chat.get_system_latest();
  710. });
  711. });
  712. div.appendChild(btn);
  713. }
  714. /**
  715. * Show the configurable parameters info in the passed Div element.
  716. * @param {HTMLDivElement} elDiv
  717. * @param {boolean} bAll
  718. */
  719. show_info(elDiv, bAll=false) {
  720. let p = ui.el_create_append_p("Settings (devel-tools-console document[gMe])", elDiv);
  721. p.className = "role-system";
  722. if (bAll) {
  723. ui.el_create_append_p(`baseURL:${this.baseURL}`, elDiv);
  724. ui.el_create_append_p(`Authorization:${this.headers["Authorization"]}`, elDiv);
  725. ui.el_create_append_p(`bStream:${this.bStream}`, elDiv);
  726. ui.el_create_append_p(`bTrimGarbage:${this.bTrimGarbage}`, elDiv);
  727. ui.el_create_append_p(`ApiEndPoint:${this.apiEP}`, elDiv);
  728. ui.el_create_append_p(`iRecentUserMsgCnt:${this.iRecentUserMsgCnt}`, elDiv);
  729. ui.el_create_append_p(`bCompletionFreshChatAlways:${this.bCompletionFreshChatAlways}`, elDiv);
  730. ui.el_create_append_p(`bCompletionInsertStandardRolePrefix:${this.bCompletionInsertStandardRolePrefix}`, elDiv);
  731. }
  732. ui.el_create_append_p(`apiRequestOptions:${JSON.stringify(this.apiRequestOptions, null, " - ")}`, elDiv);
  733. ui.el_create_append_p(`headers:${JSON.stringify(this.headers, null, " - ")}`, elDiv);
  734. }
  735. /**
  736. * Auto create ui input elements for fields in apiRequestOptions
  737. * Currently supports text and number field types.
  738. * @param {HTMLDivElement} elDiv
  739. */
  740. show_settings_apirequestoptions(elDiv) {
  741. let typeDict = {
  742. "string": "text",
  743. "number": "number",
  744. };
  745. let fs = document.createElement("fieldset");
  746. let legend = document.createElement("legend");
  747. legend.innerText = "ApiRequestOptions";
  748. fs.appendChild(legend);
  749. elDiv.appendChild(fs);
  750. for(const k in this.apiRequestOptions) {
  751. let val = this.apiRequestOptions[k];
  752. let type = typeof(val);
  753. if (((type == "string") || (type == "number"))) {
  754. let inp = ui.el_creatediv_input(`Set${k}`, k, typeDict[type], this.apiRequestOptions[k], (val)=>{
  755. if (type == "number") {
  756. val = Number(val);
  757. }
  758. this.apiRequestOptions[k] = val;
  759. });
  760. fs.appendChild(inp.div);
  761. } else if (type == "boolean") {
  762. let bbtn = ui.el_creatediv_boolbutton(`Set{k}`, k, {true: "true", false: "false"}, val, (userVal)=>{
  763. this.apiRequestOptions[k] = userVal;
  764. });
  765. fs.appendChild(bbtn.div);
  766. }
  767. }
  768. }
  769. /**
  770. * Show settings ui for configurable parameters, in the passed Div element.
  771. * @param {HTMLDivElement} elDiv
  772. */
  773. show_settings(elDiv) {
  774. let inp = ui.el_creatediv_input("SetBaseURL", "BaseURL", "text", this.baseURL, (val)=>{
  775. this.baseURL = val;
  776. });
  777. elDiv.appendChild(inp.div);
  778. inp = ui.el_creatediv_input("SetAuthorization", "Authorization", "text", this.headers["Authorization"], (val)=>{
  779. this.headers["Authorization"] = val;
  780. });
  781. inp.el.placeholder = "Bearer OPENAI_API_KEY";
  782. elDiv.appendChild(inp.div);
  783. let bb = ui.el_creatediv_boolbutton("SetStream", "Stream", {true: "[+] yes stream", false: "[-] do oneshot"}, this.bStream, (val)=>{
  784. this.bStream = val;
  785. });
  786. elDiv.appendChild(bb.div);
  787. bb = ui.el_creatediv_boolbutton("SetTrimGarbage", "TrimGarbage", {true: "[+] yes trim", false: "[-] dont trim"}, this.bTrimGarbage, (val)=>{
  788. this.bTrimGarbage = val;
  789. });
  790. elDiv.appendChild(bb.div);
  791. this.show_settings_apirequestoptions(elDiv);
  792. let sel = ui.el_creatediv_select("SetApiEP", "ApiEndPoint", ApiEP.Type, this.apiEP, (val)=>{
  793. this.apiEP = ApiEP.Type[val];
  794. });
  795. elDiv.appendChild(sel.div);
  796. sel = ui.el_creatediv_select("SetChatHistoryInCtxt", "ChatHistoryInCtxt", this.sRecentUserMsgCnt, this.iRecentUserMsgCnt, (val)=>{
  797. this.iRecentUserMsgCnt = this.sRecentUserMsgCnt[val];
  798. });
  799. elDiv.appendChild(sel.div);
  800. bb = ui.el_creatediv_boolbutton("SetCompletionFreshChatAlways", "CompletionFreshChatAlways", {true: "[+] yes fresh", false: "[-] no, with history"}, this.bCompletionFreshChatAlways, (val)=>{
  801. this.bCompletionFreshChatAlways = val;
  802. });
  803. elDiv.appendChild(bb.div);
  804. bb = ui.el_creatediv_boolbutton("SetCompletionInsertStandardRolePrefix", "CompletionInsertStandardRolePrefix", {true: "[+] yes insert", false: "[-] dont insert"}, this.bCompletionInsertStandardRolePrefix, (val)=>{
  805. this.bCompletionInsertStandardRolePrefix = val;
  806. });
  807. elDiv.appendChild(bb.div);
  808. }
  809. }
  810. /** @type {Me} */
  811. let gMe;
  812. function startme() {
  813. console.log("INFO:SimpleChat:StartMe:Starting...");
  814. gMe = new Me();
  815. gMe.debug_disable();
  816. document["gMe"] = gMe;
  817. document["du"] = du;
  818. for (let cid of gMe.defaultChatIds) {
  819. gMe.multiChat.new_chat_session(cid);
  820. }
  821. gMe.multiChat.setup_ui(gMe.defaultChatIds[0], true);
  822. gMe.multiChat.show_sessions();
  823. }
  824. document.addEventListener("DOMContentLoaded", startme);