Dino Geek, try to help you

How to connect to ChatGPT API with Rust?


Before you start using the ChatGPT API with Rust, make sure you have installed the reqwest and serde libraries which are required to make HTTP requests and parse JSON responses, respectively.

The following steps illustrate how to establish a connection to the ChatGPT API in Rust:

1. First, add dependencies to your `Cargo.toml` file.

```
[dependencies]
serde = { version = “1.0”, features = [“derive”] }
serde_json = “1.0“
reqwest = { version = “0.11”, features = [“json”] }
```

1. Here is a basic implementation using `reqwest` for API calls with the POST method and serde for JSON parsing:

```
use serde::{Serialize, Deserialize};
use reqwest::Error;

#[derive(Serialize)]
struct Message { role: String, content: String,
}

#[derive(Serialize)]
struct Prompt { messages: Vec,
}

#[derive(Deserialize)]
struct Choices { message: Message,
}

#[derive(Deserialize)]
struct Response { choices: Vec,
}

#[tokio::main]
async fn main() -> Result<(), Error> { let client = reqwest::Client::new();

let prompt = Prompt { messages: vec![ Message { role: “system”.to_string(), content: “You are a helpful assistant.”.to_string(), }, Message { role: “user”.to_string(), content: “Who won the world series in 2020?”.to_string(), }, ], }; let res: Response = client.post(“https://api.openai.com/v1/engines/davinci-codex/completions”) .header(“Authorization”, “Bearer YOUR_OPENAI_KEY”) .header(“Content-Type”, “application/json”) .json(&prompt) .send() .await? .json() .await?; println!(“{:?}”, res.choices0.message.content); Ok(()) } ```

In the function main, a struct Prompt is instantiated and later serialized into JSON body while sending a POST request.

Remember to replace “Bearer YOUR_OPENAI_KEY” to the actual API key that you received when you registered on the OpenAI website.

You’ll also need to target a runtime for Tokio as it’s using the async features, add this to your `Cargo.toml`.

```
[dependencies]
tokio = { version = “1”, features = [“full”] }
```

Please note, the provided code is only suitable for text-based API responses. If the OpenAI API provides binary data, you’ll need to handle it differently.


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