#401 response even though i have valid authentication

22 messages · Page 1 of 1 (latest)

lavish hinge
#

I am trying to use the text-davinci-003 model for a web dev project of mine, but despite having a 100% working API key, no rate limits and enough funds to cover the token usage i am getting a 401 response. This does not happen when using the same API key in a python script using the openai library, it only happens with the NPM package.

Error Message:
xhr.js:210 - POST https://api.openai.com/v1/completions 401

hallow glen
#

Seems like it could be a pretty straightforward authentication error. The authentication/configuration between Python and the openai NPM package are slightly different, so it's hard to tell exactly what's going wrong without a view into your code that's calling the package.

lavish hinge
#

let me prepare relevant code snippets

#
import { OpenAIApi } from 'openai';

const openai = new OpenAIApi({
  apiKey: 'my api key :)'
});

const App = () => {
  ...

  const getGPTResponse = async (prompt) => {
    const gptResponse = await openai.createCompletion({
      engine: 'text-davinci-003',
      prompt,
      maxTokens: 2000
    });
    return gptResponse.data.choices[0].text;
  };

  const sendInput = async (event) => {
    ...

    const promptPrimer = `(my prompt)`;

    try {
      const response = await getGPTResponse(promptPrimer);
      ...

    } catch (error) {
      console.log('Error:', error);
    }
  };
#

as i said, i have no problems with the python library using the exact same API key, and I tried a manual request using cURL and that worked as well, I only get Authentication errors when using the openai npm package

#

and i also tried declaring the openai const using this (same result):

const openai = new OpenAIApi('my api key :)');
#

if necessary i can use axios requests to get my response but i'd love to figure out why this occurs first, and if i can fix this. because so far this kinda looks like a bug in the NPM package (as long as i am not making an obvious mistake that i am overlooking of course)

lavish hinge
hallow glen
#

Yeah, it's kind of what I thought. With Python you just have to set the API key value for your openai object, but with Node you have to import both the openaiapi object and the configuration object, and set the api key value within the configuration object. i.e.


const configuration = new Configuration({
  apiKey: 'MY-API-KEY',
});
const openai = new OpenAIApi(configuration);


app.post('/message', async (req, res) => {
    const userInput = req.body.text;

    const response = await openai.createChatCompletion({
        model: "gpt-3.5-turbo",
        messages: [{"role": "system", "content": "You are a helpful assistant."}, {role: "user", content: userInput }],
      });

      console.log(response.data.choices[0]);
    const aiResponse = response.data.choices[0].message.content;

    res.json({ text: aiResponse });
});```
#

It looks like you've got some react in there as well, but I assume that's the front end that's calling your node server

lavish hinge
#

actually my entire page is a ReactJS one-pager

#

just meant to be a quick, simple tool

hallow glen
#

Gotcha! My React knowledge is definitely not up to snuff, so if it's those parts that are broken I won't be much help, but it looks like it's just the config object missing

lavish hinge
#

now i am getting a 400 🤔

#

ah, user agent stuff appareantly

#

but already thank you so much for the help!

hallow glen
#

Glad I could help. AI has pulled me slightly back into web development after I've been buried in data engineering for 5+ years

lavish hinge
#

mood, but for me it was software engineering

#

also appareantly it's a bad idea alltogether to query the API in your front end so now i'll have to learn how to set up a backend server

hallow glen
#

It's pretty easy. I ran into CORS issues locally, but got those solved, and now looking to move my whole project onto Azure

#

(and also need to learn more React, so my front-end doesn't look like a garbage fire, lol)

lavish hinge
#

Would fire base suffice? It’s literally only for sending prompts to openai and sending back the response, for like 5 users a month