Dino Geek, try to help you

How to connect to ChatGPT API with JavaScript?


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.


Simply generate articles to optimize your SEO
Simply generate articles to optimize your SEO





DinoGeek offers simple articles on complex technologies

Would you like to be quoted in this article? It's very simple, contact us at dino@eiki.fr

CSS | NodeJS | DNS | DMARC | MAPI | NNTP | htaccess | PHP | HTTPS | Drupal | WEB3 | LLM | Wordpress | TLD | Domain name | IMAP | TCP | NFT | MariaDB | FTP | Zigbee | NMAP | SNMP | SEO | E-Mail | LXC | HTTP | MangoDB | SFTP | RAG | SSH | HTML | ChatGPT API | OSPF | JavaScript | Docker | OpenVZ | ChatGPT | VPS | ZIMBRA | SPF | UDP | Joomla | IPV6 | BGP | Django | Reactjs | DKIM | VMWare | RSYNC | Python | TFTP | Webdav | FAAS | Apache | IPV4 | LDAP | POP3 | SMTP

| Whispers of love (API) | Déclaration d'Amour |






Legal Notice / General Conditions of Use