Skip to content

Commit

Permalink
Merge pull request #92 from dongri/use-reqwest
Browse files Browse the repository at this point in the history
change http client
  • Loading branch information
dongri authored Jul 9, 2024
2 parents be66044 + 3e5286d commit 5090b42
Show file tree
Hide file tree
Showing 18 changed files with 485 additions and 671 deletions.
17 changes: 9 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "openai-api-rs"
version = "4.1.1"
version = "5.0.0"
edition = "2021"
authors = ["Dongri Jin <[email protected]>"]
license = "MIT"
Expand All @@ -9,16 +9,17 @@ 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.12"
features = ["json", "multipart"]

[dependencies.tokio]
version = "1"
features = ["full"]

[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", "proxy"]
11 changes: 6 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 = "4.1.1"
openai-api-rs = "5.0.0"
```

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

## Example of chat completion
```rust
use openai_api_rs::v1::api::Client;
use openai_api_rs::v1::api::OpenAIClient;
use openai_api_rs::v1::chat_completion::{self, ChatCompletionRequest};
use openai_api_rs::v1::common::GPT4_O;
use std::env;

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

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

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

Expand Down
23 changes: 15 additions & 8 deletions examples/assistant.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use openai_api_rs::v1::api::Client;
use openai_api_rs::v1::api::OpenAIClient;
use openai_api_rs::v1::assistant::AssistantRequest;
use openai_api_rs::v1::common::GPT4_O;
use openai_api_rs::v1::message::{CreateMessageRequest, MessageRole};
Expand All @@ -7,8 +7,9 @@ use openai_api_rs::v1::thread::CreateThreadRequest;
use std::collections::HashMap;
use std::env;

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

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

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

let thread_req = CreateThreadRequest::new();
let thread_result = client.create_thread(thread_req)?;
let thread_result = client.create_thread(thread_req).await?;
println!("Create Thread Result ID: {:?}", thread_result.id.clone());

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

let message_result = client.create_message(thread_result.id.clone(), message_req)?;
let message_result = client
.create_message(thread_result.id.clone(), message_req)
.await?;
println!("Create Message Result ID: {:?}", message_result.id.clone());

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

loop {
let run_result = client
.retrieve_run(thread_result.id.clone(), run_result.id.clone())
.await
.unwrap();
if run_result.status == "completed" {
break;
Expand All @@ -52,7 +56,10 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
}
}

let list_message_result = client.list_messages(thread_result.id.clone()).unwrap();
let list_message_result = client
.list_messages(thread_result.id.clone())
.await
.unwrap();
for data in list_message_result.data {
for content in data.content {
println!(
Expand Down
22 changes: 22 additions & 0 deletions examples/audio_speech.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use openai_api_rs::v1::api::OpenAIClient;
use openai_api_rs::v1::audio::{self, AudioSpeechRequest, TTS_1};
use std::env;

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

let req = AudioSpeechRequest::new(
TTS_1.to_string(),
String::from("Money is not the problem, the problem is no money."),
audio::VOICE_ALLOY.to_string(),
String::from("examples/data/problem.mp3"),
);

let result = client.audio_speech(req).await?;
println!("{:?}", result);

Ok(())
}

// OPENAI_API_KEY=xxxx cargo run --package openai-api-rs --example audio_speech
20 changes: 20 additions & 0 deletions examples/audio_transcriptions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use openai_api_rs::v1::api::OpenAIClient;
use openai_api_rs::v1::audio::{AudioTranscriptionRequest, WHISPER_1};
use std::env;

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

let req = AudioTranscriptionRequest::new(
"examples/data/problem.mp3".to_string(),
WHISPER_1.to_string(),
);

let result = client.audio_transcription(req).await?;
println!("{:?}", result);

Ok(())
}

// OPENAI_API_KEY=xxxx cargo run --package openai-api-rs --example audio_translations
20 changes: 20 additions & 0 deletions examples/audio_translations.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use openai_api_rs::v1::api::OpenAIClient;
use openai_api_rs::v1::audio::{AudioTranslationRequest, WHISPER_1};
use std::env;

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

let req = AudioTranslationRequest::new(
"examples/data/problem_cn.mp3".to_string(),
WHISPER_1.to_string(),
);

let result = client.audio_translation(req).await?;
println!("{:?}", result);

Ok(())
}

// OPENAI_API_KEY=xxxx cargo run --package openai-api-rs --example audio_transcriptions
9 changes: 5 additions & 4 deletions examples/chat_completion.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use openai_api_rs::v1::api::Client;
use openai_api_rs::v1::api::OpenAIClient;
use openai_api_rs::v1::chat_completion::{self, ChatCompletionRequest};
use openai_api_rs::v1::common::GPT4_O;
use std::env;

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

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

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

Expand Down
9 changes: 5 additions & 4 deletions examples/completion.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use openai_api_rs::v1::api::Client;
use openai_api_rs::v1::api::OpenAIClient;
use openai_api_rs::v1::completion::{self, CompletionRequest};
use std::env;

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

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

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

Ok(())
Expand Down
Binary file added examples/data/problem.mp3
Binary file not shown.
Binary file added examples/data/problem_cn.mp3
Binary file not shown.
9 changes: 5 additions & 4 deletions examples/embedding.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
use openai_api_rs::v1::api::Client;
use openai_api_rs::v1::api::OpenAIClient;
use openai_api_rs::v1::common::TEXT_EMBEDDING_3_SMALL;
use openai_api_rs::v1::embedding::EmbeddingRequest;
use std::env;

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

let mut req =
EmbeddingRequest::new(TEXT_EMBEDDING_3_SMALL.to_string(), "story time".to_string());
req.dimensions = Some(10);

let result = client.embedding(req)?;
let result = client.embedding(req).await?;
println!("{:?}", result.data);

Ok(())
Expand Down
13 changes: 7 additions & 6 deletions examples/function_call.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use openai_api_rs::v1::api::Client;
use openai_api_rs::v1::api::OpenAIClient;
use openai_api_rs::v1::chat_completion::{self, ChatCompletionRequest};
use openai_api_rs::v1::common::GPT3_5_TURBO_0613;
use openai_api_rs::v1::common::GPT4_O;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::{env, vec};
Expand All @@ -14,8 +14,9 @@ fn get_coin_price(coin: &str) -> f64 {
}
}

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

let mut properties = HashMap::new();
properties.insert(
Expand All @@ -28,7 +29,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
);

let req = ChatCompletionRequest::new(
GPT3_5_TURBO_0613.to_string(),
GPT4_O.to_string(),
vec![chat_completion::ChatCompletionMessage {
role: chat_completion::MessageRole::user,
content: chat_completion::Content::Text(String::from("What is the price of Ethereum?")),
Expand All @@ -53,7 +54,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// let serialized = serde_json::to_string(&req).unwrap();
// println!("{}", serialized);

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

match result.choices[0].finish_reason {
None => {
Expand Down
17 changes: 9 additions & 8 deletions examples/function_call_role.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use openai_api_rs::v1::api::Client;
use openai_api_rs::v1::api::OpenAIClient;
use openai_api_rs::v1::chat_completion::{self, ChatCompletionRequest};
use openai_api_rs::v1::common::GPT3_5_TURBO_0613;
use openai_api_rs::v1::common::GPT4_O;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::{env, vec};
Expand All @@ -14,8 +14,9 @@ fn get_coin_price(coin: &str) -> f64 {
}
}

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

let mut properties = HashMap::new();
properties.insert(
Expand All @@ -28,7 +29,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
);

let req = ChatCompletionRequest::new(
GPT3_5_TURBO_0613.to_string(),
GPT4_O.to_string(),
vec![chat_completion::ChatCompletionMessage {
role: chat_completion::MessageRole::user,
content: chat_completion::Content::Text(String::from("What is the price of Ethereum?")),
Expand All @@ -48,7 +49,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
},
}]);

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

match result.choices[0].finish_reason {
None => {
Expand Down Expand Up @@ -79,7 +80,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("price: {}", price);

let req = ChatCompletionRequest::new(
GPT3_5_TURBO_0613.to_string(),
GPT4_O.to_string(),
vec![
chat_completion::ChatCompletionMessage {
role: chat_completion::MessageRole::user,
Expand All @@ -99,7 +100,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
],
);

let result = client.chat_completion(req)?;
let result = client.chat_completion(req).await?;
println!("{:?}", result.choices[0].message.content);
}
}
Expand Down
21 changes: 0 additions & 21 deletions examples/text_to_speech.rs

This file was deleted.

Loading

0 comments on commit 5090b42

Please sign in to comment.