Dino Geek, try to help you

How to connect to ChatGPT API with Ruby?


You can connect to the OpenAI GPT-3 API including the chat models using the OpenAI API. Here is an example using Ruby:

Firstly, you need to install the required gem by running:

```
gem install openai
```

Then you can use below sample code to use Chat GPT:

```
require ‘openai’

Openai.api_key = ‘your-openai-key’

response = Openai::ChatCompletion.create( model: “gpt-3.5-turbo”, messages: [ { role: “system”, content: “You are a helpful assistant.” }, { role: “user”, content: “Who won the world series in 2020?” } ]
)

puts response.choices0.message[‘content’]
```

First, the ‘openai’ gem is required. Next, the ‘openai’ gem uses your API key to connect to the OpenAI API.

Then, we create a ChatCompletion using the create method.

However, note that the chat versions of the models take a slightly different set of parameters. In this case, the API needs to be fed an array of message objects, which can be of role ‘system’, ‘user’, or ‘assistant’. You can include as many messages as you would like, but one session of chat should be done under one API call, as the state of the conversation will not be saved across different calls also it won’t remember past requests.

Finally, the API response is printed to the console.

Don’t forget to replace ‘your-openai-key’ with your actual OpenAI 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