try fix these errors that shows in “codepen.io” console.: ““chatbot response:” //…
- User
try fix these errors that shows in “codepen.io” console.: ““chatbot response:” // [object Object] { “detail”: [ { “loc”: [ “body”, “data” ], “msg”: “field required”, “type”: “value_error.missing” } ]
function initChat() { const chatContainer = document.createElement(“div”); chatContainer.setAttribute(“id”, “chat”); document.body.appendChild(chatContainer);
const messageForm = document.createElement(“form”); messageForm.addEventListener(“submit”, handleFormSubmit); chatContainer.appendChild(messageForm);
const messageInput = document.createElement(“input”); messageInput.setAttribute(“type”, “text”); messageInput.setAttribute(“id”, “message-input”); messageInput.setAttribute(“placeholder”, “Enter your message”); messageForm.appendChild(messageInput);
const sendMessageButton = document.createElement(“button”); sendMessageButton.setAttribute(“type”, “submit”); sendMessageButton.textContent = “Send”; messageForm.appendChild(sendMessageButton); }
async function sendChatMessage(messageText) { const body = JSON.stringify({ prompt: messageText });
const response = await fetch(“https://yuntian-deng-chatgpt.hf.space/run/predict”, { method: “POST”, headers: { “Content-Type”: “application/json”, }, body, });
const data = await response.json(); return data ? data.text.trim() : “”; }
async function joinChat() { const socket = new WebSocket(‘wss://yuntian-deng-chatgpt.hf.space/queue/join’); socket.onopen = function(event) { console.log(‘WebSocket is connected.’); }; socket.onmessage = async function(event) { const messageData = JSON.parse(event.data); const messageText = messageData.text.trim(); await addMessage(messageText, “bot”); }; }
async function addMessage(messageText, sender) { const messageContainer = document.createElement(“div”); messageContainer.classList.add(“message”); messageContainer.classList.add(sender);
const messageContent = document.createElement(“div”); messageContent.classList.add(“message-content”);
const messageTextEl = document.createElement(“p”); messageTextEl.textContent = messageText;
messageContent.appendChild(messageTextEl); messageContainer.appendChild(messageContent); document.querySelector(”#chat").appendChild(messageContainer);
document.querySelector(“#chat”).scrollTop = document.querySelector(“#chat”).scrollHeight; }
async function handleFormSubmit(event) { event.preventDefault(); const messageInput = document.querySelector(“#message-input”); const messageText = messageInput.value.trim();
if (messageText !== “”) { await addMessage(messageText, “user”);
const botResponse = await sendChatMessage(messageText); if (botResponse !== “”) { await addMessage(botResponse, “bot”); }
messageInput.value = “”; } }
- Assistant
There are no syntax errors, but there is an issue with the response from the chatbot. It is returning an object instead of a string.
To fix this, you can update the
sendChatMessagefunction to return thedata.textproperty from the API response instead of the wholedataobject:async function sendChatMessage(messageText) { const body = JSON.stringify({ prompt: messageText }); const response = await fetch("https://yuntian-deng-chatgpt.hf.space/run/predict", { method: "POST", headers: { "Content-Type": "application/json" }, body }); const data = await response.json(); return data.text ? data.text.trim() : ""; }This will return only the
textproperty of the API response, which should be a string.Additionally, make sure that the chatbot API is available and working properly.