ok, look, next problem. when gpt sends messages to me, message fragments that contain…
- User
ok, look, next problem.
when gpt sends messages to me, message fragments that contain code are formatted in with class="hljs language-undefined" or class="language-css hljs" or whatever.
inside the original message it looks like "
.preformatted" or like "```css .gpt-message .content p { color: #FFFFFF; line-height: 1.6; /* Added this line to increase space between lines */ }I need to make fragments with one quote ` bold, and code fragments with three quotes ```, such as ``` css or ``` python or something else, are done as they are now here's my function in chat.js: const ask_gpt = async (message) => { try { message_input.value = ``; message_input.innerHTML = ``; message_input.innerText = ``; add_conversation(window.conversation_id, message.substr(0, 20)); window.scrollTo(0, 0); window.controller = new AbortController(); jailbreak = document.getElementById("jailbreak"); model = document.getElementById("model"); prompt_lock = true; window.text = ``; window.token = message_id(); stop_generating.classList.remove(`stop_generating-hidden`); // User message message_box.innerHTML += ` <div class="message user-message"> <div class="user"> ${user_image} </div> <div class="content" id="user_${token}"> <div class="preformatted">${format(message)}</div> </div> </div> `; /* .replace(/(?:\r\n|\r|\n)/g, '<br>') */ message_box.scrollTop = message_box.scrollHeight; window.scrollTo(0, 0); await new Promise((r) => setTimeout(r, 500)); window.scrollTo(0, 0); // GPT message message_box.innerHTML += ` <div class="message gpt-message"> <div class="user"> ${gpt_image} </div> <div class="content" id="gpt_${window.token}"> <div id="cursor"></div> </div> </div> `; message_box.scrollTop = message_box.scrollHeight; window.scrollTo(0, 0); await new Promise((r) => setTimeout(r, 1000)); window.scrollTo(0, 0); const response = await fetch(`/backend-api/v2/conversation`, { method: `POST`, signal: window.controller.signal, headers: { "content-type": `application/json`, accept: `text/event-stream`, }, body: JSON.stringify({ conversation_id: window.conversation_id, action: `_ask`, model: model.options[model.selectedIndex].value, jailbreak: jailbreak.options[jailbreak.selectedIndex].value, meta: { id: window.token, content: { conversation: await get_conversation(window.conversation_id), internet_access: document.getElementById("switch").checked, content_type: "text", parts: [ { content: message, role: "user", }, ], }, }, }), }); const reader = response.body.getReader(); while (true) { const { value, done } = await reader.read(); if (done) break; chunk = new TextDecoder().decode(value); if ( chunk.includes( `<form id="challenge-form" action="/backend-api/v2/conversation?` ) ) { chunk = `cloudflare token expired, please refresh the page.`; } text += chunk; // const objects = chunk.match(/({.+?})/g); // try { if (JSON.parse(objects[0]).success === false) throw new Error(JSON.parse(objects[0]).error) } catch (e) {} // objects.forEach((object) => { // console.log(object) // try { text += h2a(JSON.parse(object).content) } catch(t) { console.log(t); throw new Error(t)} // }); document.getElementById(`gpt_${window.token}`).innerHTML = markdown.render(text); document.querySelectorAll(`code`).forEach((el) => { hljs.highlightElement(el); }); window.scrollTo(0, 0); message_box.scrollTo({ top: message_box.scrollHeight, behavior: "auto" }); } // if text contains : if ( text.includes( `instead. Maintaining this website and API costs a lot of money` ) ) { document.getElementById(`gpt_${window.token}`).innerHTML = "An error occured, please reload / refresh cache and try again."; } add_message(window.conversation_id, "user", message); add_message(window.conversation_id, "assistant", text); message_box.scrollTop = message_box.scrollHeight; await remove_cancel_button(); prompt_lock = false; await load_conversations(20, 0); window.scrollTo(0, 0); } catch (e) { add_message(window.conversation_id, "user", message); message_box.scrollTop = message_box.scrollHeight; await remove_cancel_button(); prompt_lock = false; await load_conversations(20, 0); console.log(e); let cursorDiv = document.getElementById(`cursor`); if (cursorDiv) cursorDiv.parentNode.removeChild(cursorDiv); if (e.name != `AbortError`) { let error_message = `oops ! something went wrong, please try again / reload. [stacktrace in console]`; document.getElementById(`gpt_${window.token}`).innerHTML = error_message; add_message(window.conversation_id, "assistant", error_message); } else { document.getElementById(`gpt_${window.token}`).innerHTML += ` [aborted]`; add_message(window.conversation_id, "assistant", text + ` [aborted]`); } window.scrollTo(0, 0); } }; - Assistant
To achieve this, you can modify the
format()function to check for the type of code block (one backtick or three backticks) and apply the necessary formatting. I've added a newformatCodeBlock()function inside the existingformat()function to handle the processing of the code blocks.Here's an updated version of your
format()function to implement the desired behavior for one backtick and three backticks:function format(text) { const formatCodeBlock = (code, lang) => { if (lang) { return `<code class="${lang.trim()} hljs">${code}</code>`; } return `<code class="hljs language-undefined"><b>${code}</b></code>`; }; text = text.replace(/</g, '<').replace(/>/g, '>'); const regex = /(`{1,3})(.+?)(\1)/gs; let match; let formattedText = ''; let lastIndex = 0; while ((match = regex.exec(text)) !== null) { formattedText += text.slice(lastIndex, match.index); const code = match[2].trim(); const formatting = match[1].length; if (formatting === 3) { // For three backticks const lang = code.startsWith('css') ? 'language-css' : undefined; const codeText = code.startsWith('css') ? code.slice(3).trim() : code; formattedText += formatCodeBlock(codeText, lang); } else { // For one backtick formattedText += formatCodeBlock(code); } lastIndex = regex.lastIndex; } formattedText += text.slice(lastIndex); return formattedText; }This version of
format()checks if there are one or three backticks and formats the code block accordingly. If there are three backticks and a language identifier (e.g., 'css'), it will apply the corresponding class (e.g., 'language-css), otherwise, the class will be 'hljs language-undefined'. For one backtick, the text inside the code block will be made bold.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.