mirror of
https://github.com/mii443/openai-api-rs.git
synced 2025-08-22 23:25:39 +00:00
* use reqwest
* support multipart form * fix audio apits
This commit is contained in:
17
Cargo.toml
17
Cargo.toml
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "openai-api-rs"
|
name = "openai-api-rs"
|
||||||
version = "4.1.1"
|
version = "5.0.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
authors = ["Dongri Jin <dongrium@gmail.com>"]
|
authors = ["Dongri Jin <dongrium@gmail.com>"]
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
@ -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
|
# 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]
|
[dependencies.serde]
|
||||||
version = "1"
|
version = "1"
|
||||||
features = ["derive"]
|
features = ["derive"]
|
||||||
default-features = false
|
|
||||||
|
|
||||||
[dependencies.serde_json]
|
[dependencies.serde_json]
|
||||||
version = "1"
|
version = "1"
|
||||||
default-features = false
|
|
||||||
|
|
||||||
[dependencies.minreq]
|
|
||||||
version = "2"
|
|
||||||
default-features = false
|
|
||||||
features = ["https-rustls", "json-using-serde", "proxy"]
|
|
||||||
|
11
README.md
11
README.md
@ -7,7 +7,7 @@ Check out the [docs.rs](https://docs.rs/openai-api-rs/).
|
|||||||
Cargo.toml
|
Cargo.toml
|
||||||
```toml
|
```toml
|
||||||
[dependencies]
|
[dependencies]
|
||||||
openai-api-rs = "4.1.1"
|
openai-api-rs = "5.0.0"
|
||||||
```
|
```
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
@ -48,13 +48,14 @@ $ export OPENAI_API_BASE=https://api.openai.com/v1
|
|||||||
|
|
||||||
## Example of chat completion
|
## Example of chat completion
|
||||||
```rust
|
```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::chat_completion::{self, ChatCompletionRequest};
|
||||||
use openai_api_rs::v1::common::GPT4_O;
|
use openai_api_rs::v1::common::GPT4_O;
|
||||||
use std::env;
|
use std::env;
|
||||||
|
|
||||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
#[tokio::main]
|
||||||
let client = Client::new(env::var("OPENAI_API_KEY").unwrap().to_string());
|
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(
|
let req = ChatCompletionRequest::new(
|
||||||
GPT4_O.to_string(),
|
GPT4_O.to_string(),
|
||||||
@ -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!("Content: {:?}", result.choices[0].message.content);
|
||||||
println!("Response Headers: {:?}", result.headers);
|
println!("Response Headers: {:?}", result.headers);
|
||||||
|
|
||||||
|
@ -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::assistant::AssistantRequest;
|
||||||
use openai_api_rs::v1::common::GPT4_O;
|
use openai_api_rs::v1::common::GPT4_O;
|
||||||
use openai_api_rs::v1::message::{CreateMessageRequest, MessageRole};
|
use openai_api_rs::v1::message::{CreateMessageRequest, MessageRole};
|
||||||
@ -7,8 +7,9 @@ use openai_api_rs::v1::thread::CreateThreadRequest;
|
|||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::env;
|
use std::env;
|
||||||
|
|
||||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
#[tokio::main]
|
||||||
let client = Client::new(env::var("OPENAI_API_KEY").unwrap().to_string());
|
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();
|
let mut tools = HashMap::new();
|
||||||
tools.insert("type".to_string(), "code_interpreter".to_string());
|
tools.insert("type".to_string(), "code_interpreter".to_string());
|
||||||
@ -21,11 +22,11 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
let req = req.clone().tools(vec![tools]);
|
let req = req.clone().tools(vec![tools]);
|
||||||
println!("AssistantRequest: {:?}", req);
|
println!("AssistantRequest: {:?}", req);
|
||||||
|
|
||||||
let result = client.create_assistant(req)?;
|
let result = client.create_assistant(req).await?;
|
||||||
println!("Create Assistant Result ID: {:?}", result.id);
|
println!("Create Assistant Result ID: {:?}", result.id);
|
||||||
|
|
||||||
let thread_req = CreateThreadRequest::new();
|
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());
|
println!("Create Thread Result ID: {:?}", thread_result.id.clone());
|
||||||
|
|
||||||
let message_req = CreateMessageRequest::new(
|
let message_req = CreateMessageRequest::new(
|
||||||
@ -33,16 +34,19 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
"`I need to solve the equation 3x + 11 = 14. Can you help me?".to_string(),
|
"`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());
|
println!("Create Message Result ID: {:?}", message_result.id.clone());
|
||||||
|
|
||||||
let run_req = CreateRunRequest::new(result.id);
|
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());
|
println!("Create Run Result ID: {:?}", run_result.id.clone());
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let run_result = client
|
let run_result = client
|
||||||
.retrieve_run(thread_result.id.clone(), run_result.id.clone())
|
.retrieve_run(thread_result.id.clone(), run_result.id.clone())
|
||||||
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
if run_result.status == "completed" {
|
if run_result.status == "completed" {
|
||||||
break;
|
break;
|
||||||
@ -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 data in list_message_result.data {
|
||||||
for content in data.content {
|
for content in data.content {
|
||||||
println!(
|
println!(
|
||||||
|
22
examples/audio_speech.rs
Normal file
22
examples/audio_speech.rs
Normal file
@ -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
examples/audio_transcriptions.rs
Normal file
20
examples/audio_transcriptions.rs
Normal file
@ -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
examples/audio_translations.rs
Normal file
20
examples/audio_translations.rs
Normal file
@ -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
|
@ -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::chat_completion::{self, ChatCompletionRequest};
|
||||||
use openai_api_rs::v1::common::GPT4_O;
|
use openai_api_rs::v1::common::GPT4_O;
|
||||||
use std::env;
|
use std::env;
|
||||||
|
|
||||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
#[tokio::main]
|
||||||
let client = Client::new(env::var("OPENAI_API_KEY").unwrap().to_string());
|
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(
|
let req = ChatCompletionRequest::new(
|
||||||
GPT4_O.to_string(),
|
GPT4_O.to_string(),
|
||||||
@ -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!("Content: {:?}", result.choices[0].message.content);
|
||||||
println!("Response Headers: {:?}", result.headers);
|
println!("Response Headers: {:?}", result.headers);
|
||||||
|
|
||||||
|
@ -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 openai_api_rs::v1::completion::{self, CompletionRequest};
|
||||||
use std::env;
|
use std::env;
|
||||||
|
|
||||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
#[tokio::main]
|
||||||
let client = Client::new(env::var("OPENAI_API_KEY").unwrap().to_string());
|
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(
|
let req = CompletionRequest::new(
|
||||||
completion::GPT3_TEXT_DAVINCI_003.to_string(),
|
completion::GPT3_TEXT_DAVINCI_003.to_string(),
|
||||||
@ -16,7 +17,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
.presence_penalty(0.6)
|
.presence_penalty(0.6)
|
||||||
.frequency_penalty(0.0);
|
.frequency_penalty(0.0);
|
||||||
|
|
||||||
let result = client.completion(req)?;
|
let result = client.completion(req).await?;
|
||||||
println!("{:}", result.choices[0].text);
|
println!("{:}", result.choices[0].text);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
BIN
examples/data/problem.mp3
Normal file
BIN
examples/data/problem.mp3
Normal file
Binary file not shown.
BIN
examples/data/problem_cn.mp3
Normal file
BIN
examples/data/problem_cn.mp3
Normal file
Binary file not shown.
@ -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::common::TEXT_EMBEDDING_3_SMALL;
|
||||||
use openai_api_rs::v1::embedding::EmbeddingRequest;
|
use openai_api_rs::v1::embedding::EmbeddingRequest;
|
||||||
use std::env;
|
use std::env;
|
||||||
|
|
||||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
#[tokio::main]
|
||||||
let client = Client::new(env::var("OPENAI_API_KEY").unwrap().to_string());
|
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
let client = OpenAIClient::new(env::var("OPENAI_API_KEY").unwrap().to_string());
|
||||||
|
|
||||||
let mut req =
|
let mut req =
|
||||||
EmbeddingRequest::new(TEXT_EMBEDDING_3_SMALL.to_string(), "story time".to_string());
|
EmbeddingRequest::new(TEXT_EMBEDDING_3_SMALL.to_string(), "story time".to_string());
|
||||||
req.dimensions = Some(10);
|
req.dimensions = Some(10);
|
||||||
|
|
||||||
let result = client.embedding(req)?;
|
let result = client.embedding(req).await?;
|
||||||
println!("{:?}", result.data);
|
println!("{:?}", result.data);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -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::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 serde::{Deserialize, Serialize};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::{env, vec};
|
use std::{env, vec};
|
||||||
@ -14,8 +14,9 @@ fn get_coin_price(coin: &str) -> f64 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
#[tokio::main]
|
||||||
let client = Client::new(env::var("OPENAI_API_KEY").unwrap().to_string());
|
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();
|
let mut properties = HashMap::new();
|
||||||
properties.insert(
|
properties.insert(
|
||||||
@ -28,7 +29,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
);
|
);
|
||||||
|
|
||||||
let req = ChatCompletionRequest::new(
|
let req = ChatCompletionRequest::new(
|
||||||
GPT3_5_TURBO_0613.to_string(),
|
GPT4_O.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 the price of Ethereum?")),
|
content: chat_completion::Content::Text(String::from("What is the price of Ethereum?")),
|
||||||
@ -53,7 +54,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
// let serialized = serde_json::to_string(&req).unwrap();
|
// let serialized = serde_json::to_string(&req).unwrap();
|
||||||
// println!("{}", serialized);
|
// println!("{}", serialized);
|
||||||
|
|
||||||
let result = client.chat_completion(req)?;
|
let result = client.chat_completion(req).await?;
|
||||||
|
|
||||||
match result.choices[0].finish_reason {
|
match result.choices[0].finish_reason {
|
||||||
None => {
|
None => {
|
||||||
|
@ -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::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 serde::{Deserialize, Serialize};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::{env, vec};
|
use std::{env, vec};
|
||||||
@ -14,8 +14,9 @@ fn get_coin_price(coin: &str) -> f64 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
#[tokio::main]
|
||||||
let client = Client::new(env::var("OPENAI_API_KEY").unwrap().to_string());
|
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();
|
let mut properties = HashMap::new();
|
||||||
properties.insert(
|
properties.insert(
|
||||||
@ -28,7 +29,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
);
|
);
|
||||||
|
|
||||||
let req = ChatCompletionRequest::new(
|
let req = ChatCompletionRequest::new(
|
||||||
GPT3_5_TURBO_0613.to_string(),
|
GPT4_O.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 the price of Ethereum?")),
|
content: chat_completion::Content::Text(String::from("What is the price of Ethereum?")),
|
||||||
@ -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 {
|
match result.choices[0].finish_reason {
|
||||||
None => {
|
None => {
|
||||||
@ -79,7 +80,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
println!("price: {}", price);
|
println!("price: {}", price);
|
||||||
|
|
||||||
let req = ChatCompletionRequest::new(
|
let req = ChatCompletionRequest::new(
|
||||||
GPT3_5_TURBO_0613.to_string(),
|
GPT4_O.to_string(),
|
||||||
vec![
|
vec![
|
||||||
chat_completion::ChatCompletionMessage {
|
chat_completion::ChatCompletionMessage {
|
||||||
role: chat_completion::MessageRole::user,
|
role: chat_completion::MessageRole::user,
|
||||||
@ -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);
|
println!("{:?}", result.choices[0].message.content);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,21 +0,0 @@
|
|||||||
use openai_api_rs::v1::api::Client;
|
|
||||||
use openai_api_rs::v1::audio::{self, AudioSpeechRequest, TTS_1};
|
|
||||||
use std::env;
|
|
||||||
|
|
||||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
||||||
let client = Client::new(env::var("OPENAI_API_KEY").unwrap().to_string());
|
|
||||||
|
|
||||||
let req = AudioSpeechRequest::new(
|
|
||||||
TTS_1.to_string(),
|
|
||||||
String::from("Money is not problem, Problem is no money"),
|
|
||||||
audio::VOICE_ALLOY.to_string(),
|
|
||||||
String::from("problem.mp3"),
|
|
||||||
);
|
|
||||||
|
|
||||||
let result = client.audio_speech(req)?;
|
|
||||||
println!("{:?}", result);
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
// OPENAI_API_KEY=xxxx cargo run --package openai-api-rs --example text_to_speech
|
|
@ -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::chat_completion::{self, ChatCompletionRequest};
|
||||||
use openai_api_rs::v1::common::GPT4_VISION_PREVIEW;
|
use openai_api_rs::v1::common::GPT4_VISION_PREVIEW;
|
||||||
use std::env;
|
use std::env;
|
||||||
|
|
||||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
#[tokio::main]
|
||||||
let client = Client::new(env::var("OPENAI_API_KEY").unwrap().to_string());
|
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(
|
let req = ChatCompletionRequest::new(
|
||||||
GPT4_VISION_PREVIEW.to_string(),
|
GPT4_VISION_PREVIEW.to_string(),
|
||||||
@ -30,7 +31,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);
|
println!("{:?}", result.choices[0].message.content);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
939
src/v1/api.rs
939
src/v1/api.rs
File diff suppressed because it is too large
Load Diff
@ -1,6 +1,6 @@
|
|||||||
use std::collections::HashMap;
|
use reqwest::header::HeaderMap;
|
||||||
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use crate::impl_builder_methods;
|
use crate::impl_builder_methods;
|
||||||
|
|
||||||
@ -115,8 +115,8 @@ impl AudioSpeechRequest {
|
|||||||
|
|
||||||
impl_builder_methods!(AudioSpeechRequest,);
|
impl_builder_methods!(AudioSpeechRequest,);
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, Serialize)]
|
#[derive(Debug)]
|
||||||
pub struct AudioSpeechResponse {
|
pub struct AudioSpeechResponse {
|
||||||
pub result: bool,
|
pub result: bool,
|
||||||
pub headers: Option<HashMap<String, String>>,
|
pub headers: Option<HeaderMap>,
|
||||||
}
|
}
|
||||||
|
@ -1,15 +1,26 @@
|
|||||||
|
use reqwest::{self};
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct APIError {
|
pub enum APIError {
|
||||||
pub message: String,
|
ReqwestError(reqwest::Error),
|
||||||
|
CustomError { message: String },
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for APIError {
|
impl fmt::Display for APIError {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
write!(f, "APIError: {}", self.message)
|
match self {
|
||||||
|
APIError::ReqwestError(err) => write!(f, "ReqwestError: {}", err),
|
||||||
|
APIError::CustomError { message } => write!(f, "APIError: {}", message),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Error for APIError {}
|
impl Error for APIError {}
|
||||||
|
|
||||||
|
impl From<reqwest::Error> for APIError {
|
||||||
|
fn from(err: reqwest::Error) -> APIError {
|
||||||
|
APIError::ReqwestError(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user