mirror of
https://github.com/mii443/openai-api-rs.git
synced 2025-08-23 15:48:07 +00:00
28
README.md
28
README.md
@ -18,27 +18,18 @@ The library needs to be configured with your account's secret key, which is avai
|
|||||||
$ export OPENAI_API_KEY=sk-xxxxxxx
|
$ export OPENAI_API_KEY=sk-xxxxxxx
|
||||||
```
|
```
|
||||||
|
|
||||||
### Set OPENAI_API_BASE to environment variable (optional)
|
|
||||||
```bash
|
|
||||||
$ export OPENAI_API_BASE=https://api.openai.com/v1
|
|
||||||
```
|
|
||||||
|
|
||||||
### Create client
|
### Create client
|
||||||
```rust
|
```rust
|
||||||
use openai_api_rs::v1::api::Client;
|
|
||||||
use std::env;
|
|
||||||
let client = Client::new(env::var("OPENAI_API_KEY").unwrap().to_string());
|
let client = Client::new(env::var("OPENAI_API_KEY").unwrap().to_string());
|
||||||
```
|
```
|
||||||
|
|
||||||
### Create request
|
### Create request
|
||||||
```rust
|
```rust
|
||||||
use openai_api_rs::v1::chat_completion::{self, ChatCompletionRequest};
|
|
||||||
use openai_api_rs::v1::common::GPT4;
|
|
||||||
let req = ChatCompletionRequest::new(
|
let req = ChatCompletionRequest::new(
|
||||||
GPT4.to_string(),
|
GPT4.to_string(),
|
||||||
vec![chat_completion::ChatCompletionMessage {
|
vec![chat_completion::ChatCompletionMessage {
|
||||||
role: chat_completion::MessageRole::user,
|
role: chat_completion::MessageRole::user,
|
||||||
content: chat_completion::Content::Text(String::from("Hello OpenAI!")),
|
content: chat_completion::Content::Text(String::from("What is bitcoin?")),
|
||||||
name: None,
|
name: None,
|
||||||
}],
|
}],
|
||||||
);
|
);
|
||||||
@ -46,8 +37,13 @@ let req = ChatCompletionRequest::new(
|
|||||||
|
|
||||||
### Send request
|
### Send request
|
||||||
```rust
|
```rust
|
||||||
let result = client.completion(req)?;
|
let result = client.chat_completion(req)?;
|
||||||
println!("{:?}", result.choices[0].text);
|
println!("Content: {:?}", result.choices[0].message.content);
|
||||||
|
```
|
||||||
|
|
||||||
|
### Set OPENAI_API_BASE to environment variable (optional)
|
||||||
|
```bash
|
||||||
|
$ export OPENAI_API_BASE=https://api.openai.com/v1
|
||||||
```
|
```
|
||||||
|
|
||||||
## Example of chat completion
|
## Example of chat completion
|
||||||
@ -59,16 +55,20 @@ use std::env;
|
|||||||
|
|
||||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
let client = Client::new(env::var("OPENAI_API_KEY").unwrap().to_string());
|
let client = Client::new(env::var("OPENAI_API_KEY").unwrap().to_string());
|
||||||
|
|
||||||
let req = ChatCompletionRequest::new(
|
let req = ChatCompletionRequest::new(
|
||||||
GPT4.to_string(),
|
GPT4.to_string(),
|
||||||
vec![chat_completion::ChatCompletionMessage {
|
vec![chat_completion::ChatCompletionMessage {
|
||||||
role: chat_completion::MessageRole::user,
|
role: chat_completion::MessageRole::user,
|
||||||
content: chat_completion::Content::Text(String::from("What is Bitcoin?")),
|
content: chat_completion::Content::Text(String::from("What is bitcoin?")),
|
||||||
name: None,
|
name: None,
|
||||||
}],
|
}],
|
||||||
);
|
);
|
||||||
|
|
||||||
let result = client.chat_completion(req)?;
|
let result = client.chat_completion(req)?;
|
||||||
println!("{:?}", result.choices[0].message.content);
|
println!("Content: {:?}", result.choices[0].message.content);
|
||||||
|
println!("Response Headers: {:?}", result.headers);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
Reference in New Issue
Block a user