Using New ChatGPT API With Expressjs Error

Previously I developed this ChatGPT AI chatbot powered by GPT-3 using Sveltekit as frontend and Expressjs as Backend which works fine.
The source code is long so, you can check it at https://github.com/AiHiPUniversity/ai-chatbot-openai-sveltekit-boilerplate-1/blob/main/server/server.js

I tried updating it to use the new ChatGPT “gpt-3.5-turbo” then it breaks with errors like:

createChatComplete is not a function

Can anyone please help with the right modification for the section that makes the API call?

NOTE: “prompt” represents the variable value of user input sent from the Sveltkit frontend to the Expressjs backend to add up for the API call.

Previously working version with “text-davinci-003” extracted from the full Expressjs code repo above:

const response = await openai.createCompletion({
  model: "text-davinci-003", 
  prompt: `${prompt}`, 
  temperature: 1,
  max_tokens: 2000, 
  top_p: 1, g
  frequency_penalty: 0.5, 
  presence_penalty: 0, 
});

res.status(200).send({
  ai: response.data.choices[0].text
});

The current one not working with “gpt-3.5-turbo”:

const completion = await openai.createChatCompletion({
  model: "gpt-3.5-turbo",
  messages: [
    { role: "system", content: "You are a helpful assistant that helps developers with coding and programming tasks." },
    { role: "user", content: {prompt} }],
});

console.log(completion.data.choices[0].message);
// Send the response back to the frontend
res.status(200).send({
  ai: completion.data.choices[0].message,
});

Have you had a look at the new docs?

Hope this helps.

1 Like

Yes, have done that if you try “openai.” it gives a lot of available functions that you can call from the NPM Openai module but the createChatCompletion is not yet there even though it was used in the official documentation here https://platform.openai.com/docs/api-reference/chat/create?lang=node.js:

const { Configuration, OpenAIApi } = require(“openai”);

const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);

const completion = await openai.createChatCompletion({
model: “gpt-3.5-turbo”,
messages: [{role: “user”, content: “Hello world”}],
});
console.log(completion.data.choices[0].message);

Probably, there is a need for openai to update the Openai NPM package to accept that as a function

1 Like

Im having the same issue - openai.createChatCompletion is not a function. I’m using Node and just have tried updating the npm openai package but doesnt sem to work, Have you got any further?

1 Like

@Foskaay I have solved this by doing my own API call - first instal and require axios
npm install axios then you can configure your call to look something like this:

async function callChatGTP() { var data = JSON.stringify({
“model”: “gpt-3.5-turbo”,
“messages”: [
{ role: “system”, content: Your prompt here },
{ role: “assistant”, content: “Hi. How can I help you today?” },
{ role: “user”, content: your userinput here }],

    });

    var config = {
        method: 'post',
        maxBodyLength: Infinity,
        url: 'https://api.openai.com/v1/chat/completions',
        headers: {
            'Content-Type': 'application/json',
            'Authorization': `Bearer YOUR_API_KEY`
            ''
        },
        data: data
    };
    let completion = await axios(config)
        .then(function (response) {
            console.log(JSON.stringify(response.data));
            ));
            let data = response.data;
            return data
        })
        .catch(function (error) {
            console.log(error, 'error in calling chat completion');
        });

}

Good luck and Happy GTPChatting!

1 Like

@pete1 Glad, you got a workaround with Axios, I will find time to try your suggested method out.

For now, I also got a workaround am using already with a code wrapper package as posted in this thread https://community.openai.com/t/gpt-completion-chatgpt-chatml-format/81882

The JS version is working for me with the Expressjs configuration without axios.
If you want to see it in action you can check the repo out
https://github.com/AiHiPUniversity/ai-chatbot-openai-sveltekit-boilerplate-1/blob/main/server/server.js

Hopefully, Openai will update their NPM package soon to make this more straightforward to implement like GPT-3 API.

@pete1 I tried to use your code above but seems a lot of things is broken, can you send me like a repo link where you have a working sample for check or a screenshot of your expressjs application using the code implementation you sent.
Thanks

Hi Solomun - here is a repo I made for you in Nodejs. Hope it helps!

2 Likes

@pete1 Will check it out.
Thanks :+1:

1 Like