Question:
Hi all, following on from my previous post I have been playing with trying to set up streaming for my Openai api. I now can’t get the api to work and keep returning a 400 error. Just to recap - I am trying to create an ai chatbot for the homepage of my website.
Product:
I’m using Wix Studio and Openai
What have you already tried:
Here are the 2 codes I’m currently using:
Stream - main api call
import fetch from ‘wix-fetch’;
import axios from ‘axios’;
import sendStreamToFrontend from ‘backend/realtime’;
const EXAMPLE_MESSAGES = [{
role: “system”,
content: “You are a helpful occupational health and safety assistant”,
},
{
role: “user”,
content: “what is the health and safety at work act?”
}
]
export async function getChatStream(messages, resourceId) {
const apiKey = “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”;
const endpoint = “https://api.openai.com/v1/chat/completions”;
const body = {
messages,
model: “gpt-3.5-turbo-0125”,
stream: true,
}
try {
const client = axios.create({
headers: {
Authorization: `Bearer ${apiKey}`,
}
})
const response = await client.post(endpoint, body, { responseType: 'stream' });
let receivedChunks = [];
response.data.on('data', async (chunk) => {
const chunkString = chunk.toString();
const deltas = extractDeltasFromString(chunkString);
receivedChunks = [...receivedChunks, ...deltas];
sendStreamToFrontend(receivedChunks, resourceId)
// console.log(receivedChunks.map((chunk)=> chunk.content).join(""));
})
} catch (error) {
console.log("Error getting completion", error);
}
}
function extractDeltasFromString(stringData) {
const deltaMatches = stringData.match(/“delta”:\s*({[^{}]*})/g);
console.log(deltaMatches);
let deltas = ;
for (let i = 0; i < deltaMatches.length; i++) {
let delta = JSON.parse(deltaMatches[i].substring(8));
console.log(delta)
deltas.push(delta)
}
return deltas;
}
Realtime add on
import wixRealtimeBackend from ‘wix-realtime-backend’;
export async function sendStreamToFrontend(receivedChunks, resourceId) {
const channel = {
name: “chatbot”,
resourceId,
};
wixRealtimeBackend.publish(channel, receivedChunks)
.then(() => {
});
}
Additional information:
Currently receiving 400 errors when calling the api. Any help would be appreciated.