I am trying to get the response from my gpt api, word by word like chatGPT generates and not all at once. I have all other things working, getting the response as expected just not in chunks .
I am able to print the partial response in console but unable to show it on UI, could anyone help here?
This is my backend code
import { ChatGPTAPI } from "chatgpt";
app.post("/", async (req, res) => {
const { message } = req.body;
const api = new ChatGPTAPI({
apiKey: OPENAI_API_KEY,
});
const resp = await api.sendMessage(
message, {
onProgress: (partialResponse) => {
console.log(partialResponse);
},
}
);
// Code for sending the response all at once
// if (resp.text) {
// res.json({
// message: resp.text,
// });
// }
});
const server = app.listen(5000, () => {
console.log("app listening");
});
server.headersTimeout = 610000;
This is how I am fetching it in frontend
const handleSubmit = (e) => {
e.preventDefault();
fetch("http://localhost:5000", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ message }),
})
.then((res) => res.json())
.then((data) => {
setResponse(data.message);
setMessage("");
});
};