Getting 401 error when trying to create node.js local chat bot

I cant figure it out. I’ve been trying to figure it out all day but I keep getting an error. It says something wrong with my API or something.

I’ve created an env file
Im pretty sure everything in the proper directories.

Below is my server.js file. Please help!

Please post your code as text and not as a screenshot so we can more easily read the code and help you @chris5 Post using Markdown with triple backticks, like this:

```
# your formatted coded here
```

Thanks so much.

:slight_smile:

@ruby_coder sorry I’m new to this stuff!
‘’’
const express = require(‘express’);
const { json } = require(‘express’);
require(‘dotenv’).config();

const app = express();
const port = 3000;

app.use(express.static(__dirname + ‘/public’));
app.use(json());

app.post(‘/generate_response’, async (req, res) => {
const conversation_history = req.body.conversation_history;
console.log(‘Received conversation history:’, conversation_history);
const response = await generate_response(conversation_history);
res.send(response);
});

app.listen(port, () => {
console.log(Modpod app listening at http://localhost:${port});
});

async function generate_response(conversation_history) {
try {
const got = await import(‘got’);
const url = ‘https://api.openai.com/v1/engines/gpt-3.5-turbo/completions’;
const messages = conversation_history.map((msg) => ({ role: msg.role, content: msg.content }));
const params = {
model: ‘gpt-3.5-turbo’,
messages: messages,
max_tokens: 150,
temperature: 0.7,
};
const headers = {
‘Content-Type’: ‘application/json’,
‘Authorization’: Bearer ${process.env.OPENAI_API_KEY},
};

const response = await got.default.post(url, { json: params, headers: headers }).json();
return response.choices[0].message.content;

} catch (error) {
console.error(‘Error while generating response:’, error);
return ‘An error occurred while generating a response. Please try again.’;
}
}
‘’’

OK, I understand @chris5

Please edit your code posted above and use Markdown to format your code, as follows (with triple backticks):

```
# your code goes here
```

When you post code without an markdown formatting tags, it’s nearly impossible to read and evaluate without getting a headache.

Thanks

:slight_smile:

Not trying to be funny, but have you put the error and your code into ChatGPT or Bing Chat to debug? It might actually solve it.

That said, the error you posted is a JSON parsing error, which happens when one of your functions expects a JSON type object (e.g. {myData:‘data’}) and instead gets another type, like a String, Number or another type.

Yeah I’ve tried to run it through so many times. Nothing is working…

I wonder why it’s not getting JSON

Did you use ChatGPT to write your code in the first place?

:slight_smile:

Yep. I’m guessing that’s why? Didn’t know it wouldn’t be able to take me all the way.

Yes.

ChatGPT writes code and it’s useful for developers who know the code and syntax, but it’s generally buggy and not really reliable; so it can be hard for people who are not software engineers to debug.

You are better off to start with code from the OpenAI API docs or the cookbook, written and tested by domain experts and not by a hallucinating, text auto-completing engine.

Note, I could tell you code was ChatGPT generated blah blah, because of this line:

That API end point does not exist because it was deprecated. ChatGPT is full of deprecated code which is old and does not work, especially related to the OpenAI API. People who advise you to use ChatGPT to write OpenAI API based code simply do not understand that this is not possible because of the cut-off date for the pre-trained models.

It’s a mistake to use ChatGPT to write OpenAI API-based code because the cut-off date for ChatGPT models were mid to late 2021 and much of the API code has been deprecated and changed.

You need to refrain from using ChatGPT and go to the OpenAI API docs and cookbook to write and develop code using the OpenAI API.

:slight_smile: