To use the OpenAI ChatGPT API with JavaScript, you can use the fetch API, axios, or any other HTTP client. Here is a simple example using Node.js and axios:
First, install axios:
```
npm install axios
```
```
const axios = require(‘axios’);
const OPENAI_API_KEY = ‘YOUR_OPENAI_API_KEY’;
const data = {
model: ‘gpt-3.5-turbo’,
messages: [
{
role: ‘system’,
content: ‘You are a helpful assistant.‘
},
{
role: ‘user’,
content: ‘Who won the world series in 2020?‘
}
]
};
axios.post(‘https://api.openai.com/v1/engines/davinci/chat/completions’, data, {
headers: {
‘Authorization’: `Bearer ${OPENAI_API_KEY}`,
‘Content-Type’: ‘application/json‘
}
})
.then(response => {
console.log(response.data);
})
.catch(error => {
console.error(error);
});
```
You can replace `‘Your_OPENAI_API_KEY’` with your OpenAI API key and make sure to follow the message structure as described in the OpenAI API documentation.
Please note, while OpenAI allows you to generate content with this API call, the content it generates back should always be reviewed and used with care as OpenAI takes no responsibility for the content created by the AI model.