Skip to content

Commit

Permalink
Merge pull request #31 from Anush008/main
Browse files Browse the repository at this point in the history
refactor: Transition from Async to Sync Requests using minreq
  • Loading branch information
Dongri Jin authored Aug 27, 2023
2 parents bf7cc4b + cadcb7a commit e0e6a92
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 172 deletions.
20 changes: 14 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "openai-api-rs"
version = "0.1.15"
version = "1.0.0"
edition = "2021"
authors = ["Dongri Jin <[email protected]>"]
license = "MIT"
Expand All @@ -9,8 +9,16 @@ repository = "https://github.com/dongri/openai-api-rs"

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

[dependencies]
reqwest = { version = "0.11", features = ["json"] }
tokio = { version = "1", features = ["full"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1.0.97"
[dependencies.serde]
version = "1"
features = ["derive"]
default-features = false

[dependencies.serde_json]
version = "1"
default-features = false

[dependencies.minreq]
version = "2"
default-features = false
features = ["https-rustls", "json-using-serde"]
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Check out the [docs.rs](https://docs.rs/openai-api-rs/).
Cargo.toml
```toml
[dependencies]
openai-api-rs = "0.1"
openai-api-rs = "1.0"
```

## Usage
Expand Down Expand Up @@ -56,7 +56,7 @@ let req = ChatCompletionRequest {

### Send request
```rust
let result = client.completion(req).await?;
let result = client.completion(req)?;
println!("{:?}", result.choices[0].text);
```

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

#[tokio::main]
async 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 req = ChatCompletionRequest {
model: chat_completion::GPT4.to_string(),
Expand All @@ -90,7 +89,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
logit_bias: None,
user: None,
};
let result = client.chat_completion(req).await?;
let result = client.chat_completion(req)?;
println!("{:?}", result.choices[0].message.content);
Ok(())
}
Expand Down
5 changes: 2 additions & 3 deletions examples/chat_completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ use openai_api_rs::v1::api::Client;
use openai_api_rs::v1::chat_completion::{self, ChatCompletionRequest};
use std::env;

#[tokio::main]
async 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 req = ChatCompletionRequest {
model: chat_completion::GPT4.to_string(),
Expand All @@ -26,7 +25,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
logit_bias: None,
user: None,
};
let result = client.chat_completion(req).await?;
let result = client.chat_completion(req)?;
println!("{:?}", result.choices[0].message.content);
Ok(())
}
Expand Down
5 changes: 2 additions & 3 deletions examples/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ use openai_api_rs::v1::api::Client;
use openai_api_rs::v1::completion::{self, CompletionRequest};
use std::env;

#[tokio::main]
async 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 req = CompletionRequest {
model: completion::GPT3_TEXT_DAVINCI_003.to_string(),
Expand All @@ -23,7 +22,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
logit_bias: None,
user: None,
};
let result = client.completion(req).await?;
let result = client.completion(req)?;
println!("{:}", result.choices[0].text);

Ok(())
Expand Down
5 changes: 2 additions & 3 deletions examples/embedding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ use openai_api_rs::v1::api::Client;
use openai_api_rs::v1::embedding::EmbeddingRequest;
use std::env;

#[tokio::main]
async 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 req = EmbeddingRequest {
model: "text-embedding-ada-002".to_string(),
input: "story time".to_string(),
user: Option::None,
};
let result = client.embedding(req).await?;
let result = client.embedding(req)?;
println!("{:?}", result.data);

Ok(())
Expand Down
9 changes: 4 additions & 5 deletions examples/function_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::{env, vec};

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

#[tokio::main]
async 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 mut properties = HashMap::new();
Expand Down Expand Up @@ -60,7 +59,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
user: None,
};

let result = client.chat_completion(req).await?;
let result = client.chat_completion(req)?;

match result.choices[0].finish_reason {
chat_completion::FinishReason::stop => {
Expand All @@ -82,7 +81,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let c: Currency = serde_json::from_str(&arguments)?;
let coin = c.coin;
if name == "get_coin_price" {
let price = get_coin_price(&coin).await;
let price = get_coin_price(&coin);
println!("{} price: {}", coin, price);
}
}
Expand Down
11 changes: 5 additions & 6 deletions examples/function_call_role.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::{env, vec};

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

#[tokio::main]
async 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 mut properties = HashMap::new();
Expand Down Expand Up @@ -60,7 +59,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
user: None,
};

let result = client.chat_completion(req).await?;
let result = client.chat_completion(req)?;

match result.choices[0].finish_reason {
chat_completion::FinishReason::stop => {
Expand Down Expand Up @@ -93,7 +92,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
chat_completion::ChatCompletionMessage {
role: chat_completion::MessageRole::function,
content: {
let price = get_coin_price(&coin).await;
let price = get_coin_price(&coin);
format!("{{\"price\": {}}}", price)
},
name: Some(String::from("get_coin_price")),
Expand All @@ -113,7 +112,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
logit_bias: None,
user: None,
};
let result = client.chat_completion(req).await?;
let result = client.chat_completion(req)?;
println!("{:?}", result.choices[0].message.content);
}
chat_completion::FinishReason::content_filter => {
Expand Down
Loading

0 comments on commit e0e6a92

Please sign in to comment.