Refactoring new client

This commit is contained in:
Dongri Jin
2024-11-10 15:03:34 +09:00
parent b02db53bf3
commit 78c8fee839
13 changed files with 104 additions and 63 deletions

View File

@ -9,7 +9,8 @@ use std::env;
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> { async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = OpenAIClient::new(env::var("OPENAI_API_KEY").unwrap().to_string()); let api_key = env::var("OPENAI_API_KEY").unwrap().to_string();
let client = OpenAIClient::builder().with_api_key(api_key).build()?;
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());

View File

@ -4,7 +4,8 @@ use std::env;
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> { async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = OpenAIClient::new(env::var("OPENAI_API_KEY").unwrap().to_string()); let api_key = env::var("OPENAI_API_KEY").unwrap().to_string();
let client = OpenAIClient::builder().with_api_key(api_key).build()?;
let req = AudioSpeechRequest::new( let req = AudioSpeechRequest::new(
TTS_1.to_string(), TTS_1.to_string(),

View File

@ -4,7 +4,8 @@ use std::env;
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> { async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = OpenAIClient::new(env::var("OPENAI_API_KEY").unwrap().to_string()); let api_key = env::var("OPENAI_API_KEY").unwrap().to_string();
let client = OpenAIClient::builder().with_api_key(api_key).build()?;
let req = AudioTranscriptionRequest::new( let req = AudioTranscriptionRequest::new(
"examples/data/problem.mp3".to_string(), "examples/data/problem.mp3".to_string(),

View File

@ -4,7 +4,8 @@ use std::env;
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> { async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = OpenAIClient::new(env::var("OPENAI_API_KEY").unwrap().to_string()); let api_key = env::var("OPENAI_API_KEY").unwrap().to_string();
let client = OpenAIClient::builder().with_api_key(api_key).build()?;
let req = AudioTranslationRequest::new( let req = AudioTranslationRequest::new(
"examples/data/problem_cn.mp3".to_string(), "examples/data/problem_cn.mp3".to_string(),

View File

@ -9,7 +9,8 @@ use std::str;
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> { async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = OpenAIClient::new(env::var("OPENAI_API_KEY").unwrap().to_string()); let api_key = env::var("OPENAI_API_KEY").unwrap().to_string();
let client = OpenAIClient::builder().with_api_key(api_key).build()?;
let req = FileUploadRequest::new( let req = FileUploadRequest::new(
"examples/data/batch_request.json".to_string(), "examples/data/batch_request.json".to_string(),

View File

@ -5,7 +5,8 @@ use std::env;
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> { async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = OpenAIClient::new(env::var("OPENAI_API_KEY").unwrap().to_string()); let api_key = env::var("OPENAI_API_KEY").unwrap().to_string();
let client = OpenAIClient::builder().with_api_key(api_key).build()?;
let req = ChatCompletionRequest::new( let req = ChatCompletionRequest::new(
GPT4_O_MINI.to_string(), GPT4_O_MINI.to_string(),

View File

@ -4,7 +4,8 @@ use std::env;
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> { async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = OpenAIClient::new(env::var("OPENAI_API_KEY").unwrap().to_string()); let api_key = env::var("OPENAI_API_KEY").unwrap().to_string();
let client = OpenAIClient::builder().with_api_key(api_key).build()?;
let req = CompletionRequest::new( let req = CompletionRequest::new(
completion::GPT3_TEXT_DAVINCI_003.to_string(), completion::GPT3_TEXT_DAVINCI_003.to_string(),

Binary file not shown.

View File

@ -5,7 +5,8 @@ use std::env;
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> { async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = OpenAIClient::new(env::var("OPENAI_API_KEY").unwrap().to_string()); let api_key = env::var("OPENAI_API_KEY").unwrap().to_string();
let client = OpenAIClient::builder().with_api_key(api_key).build()?;
let mut req = EmbeddingRequest::new( let mut req = EmbeddingRequest::new(
TEXT_EMBEDDING_3_SMALL.to_string(), TEXT_EMBEDDING_3_SMALL.to_string(),

View File

@ -17,7 +17,8 @@ fn get_coin_price(coin: &str) -> f64 {
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> { async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = OpenAIClient::new(env::var("OPENAI_API_KEY").unwrap().to_string()); let api_key = env::var("OPENAI_API_KEY").unwrap().to_string();
let client = OpenAIClient::builder().with_api_key(api_key).build()?;
let mut properties = HashMap::new(); let mut properties = HashMap::new();
properties.insert( properties.insert(

View File

@ -17,7 +17,8 @@ fn get_coin_price(coin: &str) -> f64 {
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> { async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = OpenAIClient::new(env::var("OPENAI_API_KEY").unwrap().to_string()); let api_key = env::var("OPENAI_API_KEY").unwrap().to_string();
let client = OpenAIClient::builder().with_api_key(api_key).build()?;
let mut properties = HashMap::new(); let mut properties = HashMap::new();
properties.insert( properties.insert(

View File

@ -5,7 +5,8 @@ use std::env;
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> { async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = OpenAIClient::new(env::var("OPENAI_API_KEY").unwrap().to_string()); let api_key = env::var("OPENAI_API_KEY").unwrap().to_string();
let client = OpenAIClient::builder().with_api_key(api_key).build()?;
let req = ChatCompletionRequest::new( let req = ChatCompletionRequest::new(
GPT4_O.to_string(), GPT4_O.to_string(),

View File

@ -38,11 +38,13 @@ use crate::v1::run::{
use crate::v1::thread::{CreateThreadRequest, ModifyThreadRequest, ThreadObject}; use crate::v1::thread::{CreateThreadRequest, ModifyThreadRequest, ThreadObject};
use bytes::Bytes; use bytes::Bytes;
use reqwest::header::{HeaderMap, HeaderName, HeaderValue};
use reqwest::multipart::{Form, Part}; use reqwest::multipart::{Form, Part};
use reqwest::{Client, Method, Response}; use reqwest::{Client, Method, Response};
use serde::Serialize; use serde::Serialize;
use serde_json::Value; use serde_json::Value;
use std::error::Error;
use std::fs::{create_dir_all, File}; use std::fs::{create_dir_all, File};
use std::io::Read; use std::io::Read;
use std::io::Write; use std::io::Write;
@ -50,61 +52,84 @@ use std::path::Path;
const API_URL_V1: &str = "https://api.openai.com/v1"; const API_URL_V1: &str = "https://api.openai.com/v1";
#[derive(Default)]
pub struct OpenAIClientBuilder {
api_endpoint: Option<String>,
api_key: Option<String>,
organization: Option<String>,
proxy: Option<String>,
timeout: Option<u64>,
headers: Option<HeaderMap>,
}
pub struct OpenAIClient { pub struct OpenAIClient {
pub api_endpoint: String, api_endpoint: String,
pub api_key: String, api_key: String,
pub organization: Option<String>, organization: Option<String>,
pub proxy: Option<String>, proxy: Option<String>,
pub timeout: Option<u64>, timeout: Option<u64>,
headers: Option<HeaderMap>,
}
impl OpenAIClientBuilder {
pub fn new() -> Self {
Self::default()
}
pub fn with_api_key(mut self, api_key: impl Into<String>) -> Self {
self.api_key = Some(api_key.into());
self
}
pub fn with_endpoint(mut self, endpoint: impl Into<String>) -> Self {
self.api_endpoint = Some(endpoint.into());
self
}
pub fn with_organization(mut self, organization: impl Into<String>) -> Self {
self.organization = Some(organization.into());
self
}
pub fn with_proxy(mut self, proxy: impl Into<String>) -> Self {
self.proxy = Some(proxy.into());
self
}
pub fn with_timeout(mut self, timeout: u64) -> Self {
self.timeout = Some(timeout);
self
}
pub fn with_header(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
let headers = self.headers.get_or_insert_with(HeaderMap::new);
headers.insert(
HeaderName::from_bytes(key.into().as_bytes()).expect("Invalid header name"),
HeaderValue::from_str(&value.into()).expect("Invalid header value"),
);
self
}
pub fn build(self) -> Result<OpenAIClient, Box<dyn Error>> {
let api_key = self.api_key.ok_or("API key is required")?;
let api_endpoint = self.api_endpoint.unwrap_or_else(|| {
std::env::var("OPENAI_API_BASE").unwrap_or_else(|_| API_URL_V1.to_owned())
});
Ok(OpenAIClient {
api_endpoint,
api_key,
organization: self.organization,
proxy: self.proxy,
timeout: self.timeout,
headers: self.headers,
})
}
} }
impl OpenAIClient { impl OpenAIClient {
pub fn new(api_key: String) -> Self { pub fn builder() -> OpenAIClientBuilder {
let endpoint = std::env::var("OPENAI_API_BASE").unwrap_or_else(|_| API_URL_V1.to_owned()); OpenAIClientBuilder::new()
Self::new_with_endpoint(endpoint, api_key)
}
pub fn new_with_endpoint(api_endpoint: String, api_key: String) -> Self {
Self {
api_endpoint,
api_key,
organization: None,
proxy: None,
timeout: None,
}
}
pub fn new_with_organization(api_key: String, organization: String) -> Self {
let endpoint = std::env::var("OPENAI_API_BASE").unwrap_or_else(|_| API_URL_V1.to_owned());
Self {
api_endpoint: endpoint,
api_key,
organization: Some(organization),
proxy: None,
timeout: None,
}
}
pub fn new_with_proxy(api_key: String, proxy: String) -> Self {
let endpoint = std::env::var("OPENAI_API_BASE").unwrap_or_else(|_| API_URL_V1.to_owned());
Self {
api_endpoint: endpoint,
api_key,
organization: None,
proxy: Some(proxy),
timeout: None,
}
}
pub fn new_with_timeout(api_key: String, timeout: u64) -> Self {
let endpoint = std::env::var("OPENAI_API_BASE").unwrap_or_else(|_| API_URL_V1.to_owned());
Self {
api_endpoint: endpoint,
api_key,
organization: None,
proxy: None,
timeout: Some(timeout),
}
} }
async fn build_request(&self, method: Method, path: &str) -> reqwest::RequestBuilder { async fn build_request(&self, method: Method, path: &str) -> reqwest::RequestBuilder {
@ -127,13 +152,18 @@ impl OpenAIClient {
let mut request = client let mut request = client
.request(method, url) .request(method, url)
// .header("Content-Type", "application/json")
.header("Authorization", format!("Bearer {}", self.api_key)); .header("Authorization", format!("Bearer {}", self.api_key));
if let Some(organization) = &self.organization { if let Some(organization) = &self.organization {
request = request.header("openai-organization", organization); request = request.header("openai-organization", organization);
} }
if let Some(headers) = &self.headers {
for (key, value) in headers {
request = request.header(key, value);
}
}
if Self::is_beta(path) { if Self::is_beta(path) {
request = request.header("OpenAI-Beta", "assistants=v2"); request = request.header("OpenAI-Beta", "assistants=v2");
} }