Skip to content

Commit 5090b42

Browse files
authored
Merge pull request #92 from dongri/use-reqwest
change http client
2 parents be66044 + 3e5286d commit 5090b42

18 files changed

+485
-671
lines changed

Cargo.toml

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "openai-api-rs"
3-
version = "4.1.1"
3+
version = "5.0.0"
44
edition = "2021"
55
authors = ["Dongri Jin <[email protected]>"]
66
license = "MIT"
@@ -9,16 +9,17 @@ repository = "https://github.com/dongri/openai-api-rs"
99

1010
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1111

12+
[dependencies.reqwest]
13+
version = "0.12"
14+
features = ["json", "multipart"]
15+
16+
[dependencies.tokio]
17+
version = "1"
18+
features = ["full"]
19+
1220
[dependencies.serde]
1321
version = "1"
1422
features = ["derive"]
15-
default-features = false
1623

1724
[dependencies.serde_json]
1825
version = "1"
19-
default-features = false
20-
21-
[dependencies.minreq]
22-
version = "2"
23-
default-features = false
24-
features = ["https-rustls", "json-using-serde", "proxy"]

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Check out the [docs.rs](https://docs.rs/openai-api-rs/).
77
Cargo.toml
88
```toml
99
[dependencies]
10-
openai-api-rs = "4.1.1"
10+
openai-api-rs = "5.0.0"
1111
```
1212

1313
## Usage
@@ -48,13 +48,14 @@ $ export OPENAI_API_BASE=https://api.openai.com/v1
4848

4949
## Example of chat completion
5050
```rust
51-
use openai_api_rs::v1::api::Client;
51+
use openai_api_rs::v1::api::OpenAIClient;
5252
use openai_api_rs::v1::chat_completion::{self, ChatCompletionRequest};
5353
use openai_api_rs::v1::common::GPT4_O;
5454
use std::env;
5555

56-
fn main() -> Result<(), Box<dyn std::error::Error>> {
57-
let client = Client::new(env::var("OPENAI_API_KEY").unwrap().to_string());
56+
#[tokio::main]
57+
async fn main() -> Result<(), Box<dyn std::error::Error>> {
58+
let client = OpenAIClient::new(env::var("OPENAI_API_KEY").unwrap().to_string());
5859

5960
let req = ChatCompletionRequest::new(
6061
GPT4_O.to_string(),
@@ -65,7 +66,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
6566
}],
6667
);
6768

68-
let result = client.chat_completion(req)?;
69+
let result = client.chat_completion(req).await?;
6970
println!("Content: {:?}", result.choices[0].message.content);
7071
println!("Response Headers: {:?}", result.headers);
7172

examples/assistant.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use openai_api_rs::v1::api::Client;
1+
use openai_api_rs::v1::api::OpenAIClient;
22
use openai_api_rs::v1::assistant::AssistantRequest;
33
use openai_api_rs::v1::common::GPT4_O;
44
use openai_api_rs::v1::message::{CreateMessageRequest, MessageRole};
@@ -7,8 +7,9 @@ use openai_api_rs::v1::thread::CreateThreadRequest;
77
use std::collections::HashMap;
88
use std::env;
99

10-
fn main() -> Result<(), Box<dyn std::error::Error>> {
11-
let client = Client::new(env::var("OPENAI_API_KEY").unwrap().to_string());
10+
#[tokio::main]
11+
async fn main() -> Result<(), Box<dyn std::error::Error>> {
12+
let client = OpenAIClient::new(env::var("OPENAI_API_KEY").unwrap().to_string());
1213

1314
let mut tools = HashMap::new();
1415
tools.insert("type".to_string(), "code_interpreter".to_string());
@@ -21,28 +22,31 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
2122
let req = req.clone().tools(vec![tools]);
2223
println!("AssistantRequest: {:?}", req);
2324

24-
let result = client.create_assistant(req)?;
25+
let result = client.create_assistant(req).await?;
2526
println!("Create Assistant Result ID: {:?}", result.id);
2627

2728
let thread_req = CreateThreadRequest::new();
28-
let thread_result = client.create_thread(thread_req)?;
29+
let thread_result = client.create_thread(thread_req).await?;
2930
println!("Create Thread Result ID: {:?}", thread_result.id.clone());
3031

3132
let message_req = CreateMessageRequest::new(
3233
MessageRole::user,
3334
"`I need to solve the equation 3x + 11 = 14. Can you help me?".to_string(),
3435
);
3536

36-
let message_result = client.create_message(thread_result.id.clone(), message_req)?;
37+
let message_result = client
38+
.create_message(thread_result.id.clone(), message_req)
39+
.await?;
3740
println!("Create Message Result ID: {:?}", message_result.id.clone());
3841

3942
let run_req = CreateRunRequest::new(result.id);
40-
let run_result = client.create_run(thread_result.id.clone(), run_req)?;
43+
let run_result = client.create_run(thread_result.id.clone(), run_req).await?;
4144
println!("Create Run Result ID: {:?}", run_result.id.clone());
4245

4346
loop {
4447
let run_result = client
4548
.retrieve_run(thread_result.id.clone(), run_result.id.clone())
49+
.await
4650
.unwrap();
4751
if run_result.status == "completed" {
4852
break;
@@ -52,7 +56,10 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
5256
}
5357
}
5458

55-
let list_message_result = client.list_messages(thread_result.id.clone()).unwrap();
59+
let list_message_result = client
60+
.list_messages(thread_result.id.clone())
61+
.await
62+
.unwrap();
5663
for data in list_message_result.data {
5764
for content in data.content {
5865
println!(

examples/audio_speech.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use openai_api_rs::v1::api::OpenAIClient;
2+
use openai_api_rs::v1::audio::{self, AudioSpeechRequest, TTS_1};
3+
use std::env;
4+
5+
#[tokio::main]
6+
async fn main() -> Result<(), Box<dyn std::error::Error>> {
7+
let client = OpenAIClient::new(env::var("OPENAI_API_KEY").unwrap().to_string());
8+
9+
let req = AudioSpeechRequest::new(
10+
TTS_1.to_string(),
11+
String::from("Money is not the problem, the problem is no money."),
12+
audio::VOICE_ALLOY.to_string(),
13+
String::from("examples/data/problem.mp3"),
14+
);
15+
16+
let result = client.audio_speech(req).await?;
17+
println!("{:?}", result);
18+
19+
Ok(())
20+
}
21+
22+
// OPENAI_API_KEY=xxxx cargo run --package openai-api-rs --example audio_speech

examples/audio_transcriptions.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use openai_api_rs::v1::api::OpenAIClient;
2+
use openai_api_rs::v1::audio::{AudioTranscriptionRequest, WHISPER_1};
3+
use std::env;
4+
5+
#[tokio::main]
6+
async fn main() -> Result<(), Box<dyn std::error::Error>> {
7+
let client = OpenAIClient::new(env::var("OPENAI_API_KEY").unwrap().to_string());
8+
9+
let req = AudioTranscriptionRequest::new(
10+
"examples/data/problem.mp3".to_string(),
11+
WHISPER_1.to_string(),
12+
);
13+
14+
let result = client.audio_transcription(req).await?;
15+
println!("{:?}", result);
16+
17+
Ok(())
18+
}
19+
20+
// OPENAI_API_KEY=xxxx cargo run --package openai-api-rs --example audio_translations

examples/audio_translations.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use openai_api_rs::v1::api::OpenAIClient;
2+
use openai_api_rs::v1::audio::{AudioTranslationRequest, WHISPER_1};
3+
use std::env;
4+
5+
#[tokio::main]
6+
async fn main() -> Result<(), Box<dyn std::error::Error>> {
7+
let client = OpenAIClient::new(env::var("OPENAI_API_KEY").unwrap().to_string());
8+
9+
let req = AudioTranslationRequest::new(
10+
"examples/data/problem_cn.mp3".to_string(),
11+
WHISPER_1.to_string(),
12+
);
13+
14+
let result = client.audio_translation(req).await?;
15+
println!("{:?}", result);
16+
17+
Ok(())
18+
}
19+
20+
// OPENAI_API_KEY=xxxx cargo run --package openai-api-rs --example audio_transcriptions

examples/chat_completion.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
use openai_api_rs::v1::api::Client;
1+
use openai_api_rs::v1::api::OpenAIClient;
22
use openai_api_rs::v1::chat_completion::{self, ChatCompletionRequest};
33
use openai_api_rs::v1::common::GPT4_O;
44
use std::env;
55

6-
fn main() -> Result<(), Box<dyn std::error::Error>> {
7-
let client = Client::new(env::var("OPENAI_API_KEY").unwrap().to_string());
6+
#[tokio::main]
7+
async fn main() -> Result<(), Box<dyn std::error::Error>> {
8+
let client = OpenAIClient::new(env::var("OPENAI_API_KEY").unwrap().to_string());
89

910
let req = ChatCompletionRequest::new(
1011
GPT4_O.to_string(),
@@ -15,7 +16,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
1516
}],
1617
);
1718

18-
let result = client.chat_completion(req)?;
19+
let result = client.chat_completion(req).await?;
1920
println!("Content: {:?}", result.choices[0].message.content);
2021
println!("Response Headers: {:?}", result.headers);
2122

examples/completion.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
use openai_api_rs::v1::api::Client;
1+
use openai_api_rs::v1::api::OpenAIClient;
22
use openai_api_rs::v1::completion::{self, CompletionRequest};
33
use std::env;
44

5-
fn main() -> Result<(), Box<dyn std::error::Error>> {
6-
let client = Client::new(env::var("OPENAI_API_KEY").unwrap().to_string());
5+
#[tokio::main]
6+
async fn main() -> Result<(), Box<dyn std::error::Error>> {
7+
let client = OpenAIClient::new(env::var("OPENAI_API_KEY").unwrap().to_string());
78

89
let req = CompletionRequest::new(
910
completion::GPT3_TEXT_DAVINCI_003.to_string(),
@@ -16,7 +17,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
1617
.presence_penalty(0.6)
1718
.frequency_penalty(0.0);
1819

19-
let result = client.completion(req)?;
20+
let result = client.completion(req).await?;
2021
println!("{:}", result.choices[0].text);
2122

2223
Ok(())

examples/data/problem.mp3

60 KB
Binary file not shown.

examples/data/problem_cn.mp3

44.1 KB
Binary file not shown.

0 commit comments

Comments
 (0)