Dino Geek, try to help you

How to connect to ChatGPT API with TypeScript?


First, hire the OpenAI npm package:

```
npm install @openai/api
```

Then, use the OpenAI SDK in your TypeScript program:

```
import { OpenAIApi } from ‘@openai/api’;

const openai = new OpenAIApi({ apiKey: ‘YOUR_OPENAI_API_KEY‘
});

const exampleUsage = async () => { const gptResponse = await openai.createCompletion({ engine: ‘text-davinci-002’, prompt: ‘Translate the following English text to French: “{text}”’, max_tokens: 60 }); console.log(gptResponse)
};

exampleUsage();
```

Don’t forget to replace `‘YOUR_OPENAI_API_KEY’` with your OpenAI API key.

Please note, for using the ChatGPT API use `openai.createChatCompletion(params)` function. Also methods of OpenAI are async, you should remember about it working with them.

For better result set chat model (`‘gpt-3.5-turbo’`) and pass array of messages. Each message in messages array should have role (‘system’, ‘user’, or ‘assistant’) and content (the text of the message from that role). Here is how you can modify above example to use Chat API:

```
import { OpenAIApi } from ‘@openai/api’;

const openai = new OpenAIApi({ apiKey: ‘YOUR_OPENAI_API_KEY‘
});

const exampleUsage = async () => { const gptResponse = await openai.createChatCompletion({ model: ‘gpt-3.5-turbo’, messages: [ {“role”: “system”, “content”: “You are a helpful assistant.”}, {“role”: “user”, “content”: “Who won the world series in 2020?”}, ] }); console.log(gptResponse)
};

exampleUsage();
```

Don’t forget to replace `‘YOUR_OPENAI_API_KEY’` with your OpenAI API key.


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