Skip to content

Commit e0e6a92

Browse files
author
Dongri Jin
authored
Merge pull request #31 from Anush008/main
refactor: Transition from Async to Sync Requests using minreq
2 parents bf7cc4b + cadcb7a commit e0e6a92

File tree

8 files changed

+161
-172
lines changed

8 files changed

+161
-172
lines changed

Cargo.toml

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "openai-api-rs"
3-
version = "0.1.15"
3+
version = "1.0.0"
44
edition = "2021"
55
authors = ["Dongri Jin <[email protected]>"]
66
license = "MIT"
@@ -9,8 +9,16 @@ 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]
13-
reqwest = { version = "0.11", features = ["json"] }
14-
tokio = { version = "1", features = ["full"] }
15-
serde = { version = "1", features = ["derive"] }
16-
serde_json = "1.0.97"
12+
[dependencies.serde]
13+
version = "1"
14+
features = ["derive"]
15+
default-features = false
16+
17+
[dependencies.serde_json]
18+
version = "1"
19+
default-features = false
20+
21+
[dependencies.minreq]
22+
version = "2"
23+
default-features = false
24+
features = ["https-rustls", "json-using-serde"]

README.md

Lines changed: 4 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 = "0.1"
10+
openai-api-rs = "1.0"
1111
```
1212

1313
## Usage
@@ -56,7 +56,7 @@ let req = ChatCompletionRequest {
5656

5757
### Send request
5858
```rust
59-
let result = client.completion(req).await?;
59+
let result = client.completion(req)?;
6060
println!("{:?}", result.choices[0].text);
6161
```
6262

@@ -66,8 +66,7 @@ use openai_api_rs::v1::api::Client;
6666
use openai_api_rs::v1::chat_completion::{self, ChatCompletionRequest};
6767
use std::env;
6868

69-
#[tokio::main]
70-
async fn main() -> Result<(), Box<dyn std::error::Error>> {
69+
fn main() -> Result<(), Box<dyn std::error::Error>> {
7170
let client = Client::new(env::var("OPENAI_API_KEY").unwrap().to_string());
7271
let req = ChatCompletionRequest {
7372
model: chat_completion::GPT4.to_string(),
@@ -90,7 +89,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
9089
logit_bias: None,
9190
user: None,
9291
};
93-
let result = client.chat_completion(req).await?;
92+
let result = client.chat_completion(req)?;
9493
println!("{:?}", result.choices[0].message.content);
9594
Ok(())
9695
}

examples/chat_completion.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ use openai_api_rs::v1::api::Client;
22
use openai_api_rs::v1::chat_completion::{self, ChatCompletionRequest};
33
use std::env;
44

5-
#[tokio::main]
6-
async fn main() -> Result<(), Box<dyn std::error::Error>> {
5+
fn main() -> Result<(), Box<dyn std::error::Error>> {
76
let client = Client::new(env::var("OPENAI_API_KEY").unwrap().to_string());
87
let req = ChatCompletionRequest {
98
model: chat_completion::GPT4.to_string(),
@@ -26,7 +25,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
2625
logit_bias: None,
2726
user: None,
2827
};
29-
let result = client.chat_completion(req).await?;
28+
let result = client.chat_completion(req)?;
3029
println!("{:?}", result.choices[0].message.content);
3130
Ok(())
3231
}

examples/completion.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ use openai_api_rs::v1::api::Client;
22
use openai_api_rs::v1::completion::{self, CompletionRequest};
33
use std::env;
44

5-
#[tokio::main]
6-
async fn main() -> Result<(), Box<dyn std::error::Error>> {
5+
fn main() -> Result<(), Box<dyn std::error::Error>> {
76
let client = Client::new(env::var("OPENAI_API_KEY").unwrap().to_string());
87
let req = CompletionRequest {
98
model: completion::GPT3_TEXT_DAVINCI_003.to_string(),
@@ -23,7 +22,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
2322
logit_bias: None,
2423
user: None,
2524
};
26-
let result = client.completion(req).await?;
25+
let result = client.completion(req)?;
2726
println!("{:}", result.choices[0].text);
2827

2928
Ok(())

examples/embedding.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@ use openai_api_rs::v1::api::Client;
22
use openai_api_rs::v1::embedding::EmbeddingRequest;
33
use std::env;
44

5-
#[tokio::main]
6-
async fn main() -> Result<(), Box<dyn std::error::Error>> {
5+
fn main() -> Result<(), Box<dyn std::error::Error>> {
76
let client = Client::new(env::var("OPENAI_API_KEY").unwrap().to_string());
87
let req = EmbeddingRequest {
98
model: "text-embedding-ada-002".to_string(),
109
input: "story time".to_string(),
1110
user: Option::None,
1211
};
13-
let result = client.embedding(req).await?;
12+
let result = client.embedding(req)?;
1413
println!("{:?}", result.data);
1514

1615
Ok(())

examples/function_call.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize};
44
use std::collections::HashMap;
55
use std::{env, vec};
66

7-
async fn get_coin_price(coin: &str) -> f64 {
7+
fn get_coin_price(coin: &str) -> f64 {
88
let coin = coin.to_lowercase();
99
match coin.as_str() {
1010
"btc" | "bitcoin" => 10000.0,
@@ -13,8 +13,7 @@ async fn get_coin_price(coin: &str) -> f64 {
1313
}
1414
}
1515

16-
#[tokio::main]
17-
async fn main() -> Result<(), Box<dyn std::error::Error>> {
16+
fn main() -> Result<(), Box<dyn std::error::Error>> {
1817
let client = Client::new(env::var("OPENAI_API_KEY").unwrap().to_string());
1918

2019
let mut properties = HashMap::new();
@@ -60,7 +59,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
6059
user: None,
6160
};
6261

63-
let result = client.chat_completion(req).await?;
62+
let result = client.chat_completion(req)?;
6463

6564
match result.choices[0].finish_reason {
6665
chat_completion::FinishReason::stop => {
@@ -82,7 +81,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
8281
let c: Currency = serde_json::from_str(&arguments)?;
8382
let coin = c.coin;
8483
if name == "get_coin_price" {
85-
let price = get_coin_price(&coin).await;
84+
let price = get_coin_price(&coin);
8685
println!("{} price: {}", coin, price);
8786
}
8887
}

examples/function_call_role.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize};
44
use std::collections::HashMap;
55
use std::{env, vec};
66

7-
async fn get_coin_price(coin: &str) -> f64 {
7+
fn get_coin_price(coin: &str) -> f64 {
88
let coin = coin.to_lowercase();
99
match coin.as_str() {
1010
"btc" | "bitcoin" => 10000.0,
@@ -13,8 +13,7 @@ async fn get_coin_price(coin: &str) -> f64 {
1313
}
1414
}
1515

16-
#[tokio::main]
17-
async fn main() -> Result<(), Box<dyn std::error::Error>> {
16+
fn main() -> Result<(), Box<dyn std::error::Error>> {
1817
let client = Client::new(env::var("OPENAI_API_KEY").unwrap().to_string());
1918

2019
let mut properties = HashMap::new();
@@ -60,7 +59,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
6059
user: None,
6160
};
6261

63-
let result = client.chat_completion(req).await?;
62+
let result = client.chat_completion(req)?;
6463

6564
match result.choices[0].finish_reason {
6665
chat_completion::FinishReason::stop => {
@@ -93,7 +92,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
9392
chat_completion::ChatCompletionMessage {
9493
role: chat_completion::MessageRole::function,
9594
content: {
96-
let price = get_coin_price(&coin).await;
95+
let price = get_coin_price(&coin);
9796
format!("{{\"price\": {}}}", price)
9897
},
9998
name: Some(String::from("get_coin_price")),
@@ -113,7 +112,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
113112
logit_bias: None,
114113
user: None,
115114
};
116-
let result = client.chat_completion(req).await?;
115+
let result = client.chat_completion(req)?;
117116
println!("{:?}", result.choices[0].message.content);
118117
}
119118
chat_completion::FinishReason::content_filter => {

0 commit comments

Comments
 (0)