mirror of
https://github.com/mii443/openai-api-rs.git
synced 2025-08-27 09:29:34 +00:00
Add chat gpt api
This commit is contained in:
239
src/v1/api.rs
239
src/v1/api.rs
@ -1,32 +1,22 @@
|
||||
|
||||
use crate::v1::chat_completion::{ChatCompletionRequest, ChatCompletionResponse};
|
||||
use crate::v1::completion::{CompletionRequest, CompletionResponse};
|
||||
use crate::v1::edit::{EditRequest, EditResponse};
|
||||
use crate::v1::image::{
|
||||
ImageGenerationRequest,
|
||||
ImageGenerationResponse,
|
||||
ImageEditRequest,
|
||||
ImageEditResponse,
|
||||
ImageVariationRequest,
|
||||
ImageVariationResponse,
|
||||
};
|
||||
use crate::v1::embedding::{EmbeddingRequest, EmbeddingResponse};
|
||||
use crate::v1::file::{
|
||||
FileListResponse,
|
||||
FileUploadRequest,
|
||||
FileDeleteRequest, FileDeleteResponse, FileListResponse, FileRetrieveContentRequest,
|
||||
FileRetrieveContentResponse, FileRetrieveRequest, FileRetrieveResponse, FileUploadRequest,
|
||||
FileUploadResponse,
|
||||
FileDeleteRequest,
|
||||
FileDeleteResponse,
|
||||
FileRetrieveRequest,
|
||||
FileRetrieveResponse,
|
||||
FileRetrieveContentRequest,
|
||||
FileRetrieveContentResponse,
|
||||
};
|
||||
use crate::v1::image::{
|
||||
ImageEditRequest, ImageEditResponse, ImageGenerationRequest, ImageGenerationResponse,
|
||||
ImageVariationRequest, ImageVariationResponse,
|
||||
};
|
||||
use reqwest::Response;
|
||||
|
||||
const APU_URL_V1: &str = "https://api.openai.com/v1";
|
||||
const APU_URL_V1: &str = "https://api.openai.com/v1";
|
||||
|
||||
pub struct Client {
|
||||
pub api_key: String,
|
||||
pub api_key: String,
|
||||
}
|
||||
|
||||
impl Client {
|
||||
@ -34,25 +24,30 @@ impl Client {
|
||||
Self { api_key }
|
||||
}
|
||||
|
||||
pub async fn post<T:serde::ser::Serialize>(&self, path: &str, params: &T) -> Result<Response, Box<dyn std::error::Error>> {
|
||||
pub async fn post<T: serde::ser::Serialize>(
|
||||
&self,
|
||||
path: &str,
|
||||
params: &T,
|
||||
) -> Result<Response, Box<dyn std::error::Error>> {
|
||||
let client = reqwest::Client::new();
|
||||
let url = format!("{}{}", APU_URL_V1, path);
|
||||
let url = format!("{APU_URL_V1}{path}");
|
||||
let res = client
|
||||
.post(&url)
|
||||
.header(reqwest::header::CONTENT_TYPE, "application/json")
|
||||
.header(reqwest::header::AUTHORIZATION, "Bearer ".to_owned() + &self.api_key)
|
||||
.header(
|
||||
reqwest::header::AUTHORIZATION,
|
||||
"Bearer ".to_owned() + &self.api_key,
|
||||
)
|
||||
.json(¶ms)
|
||||
.send()
|
||||
.await;
|
||||
match res {
|
||||
Ok(res) => match res.status().is_success() {
|
||||
true => Ok(res),
|
||||
false => {
|
||||
Err(Box::new(std::io::Error::new(
|
||||
std::io::ErrorKind::Other,
|
||||
format!("{}: {}", res.status(), res.text().await.unwrap())
|
||||
)))
|
||||
},
|
||||
false => Err(Box::new(std::io::Error::new(
|
||||
std::io::ErrorKind::Other,
|
||||
format!("{}: {}", res.status(), res.text().await.unwrap()),
|
||||
))),
|
||||
},
|
||||
Err(e) => Err(Box::new(e)),
|
||||
}
|
||||
@ -60,22 +55,23 @@ impl Client {
|
||||
|
||||
pub async fn get(&self, path: &str) -> Result<Response, Box<dyn std::error::Error>> {
|
||||
let client = reqwest::Client::new();
|
||||
let url = format!("{}{}", APU_URL_V1, path);
|
||||
let url = format!("{APU_URL_V1}{path}");
|
||||
let res = client
|
||||
.get(&url)
|
||||
.header(reqwest::header::CONTENT_TYPE, "application/json")
|
||||
.header(reqwest::header::AUTHORIZATION, "Bearer ".to_owned() + &self.api_key)
|
||||
.header(
|
||||
reqwest::header::AUTHORIZATION,
|
||||
"Bearer ".to_owned() + &self.api_key,
|
||||
)
|
||||
.send()
|
||||
.await;
|
||||
match res {
|
||||
Ok(res) => match res.status().is_success() {
|
||||
true => Ok(res),
|
||||
false => {
|
||||
Err(Box::new(std::io::Error::new(
|
||||
std::io::ErrorKind::Other,
|
||||
format!("{}: {}", res.status(), res.text().await.unwrap())
|
||||
)))
|
||||
},
|
||||
false => Err(Box::new(std::io::Error::new(
|
||||
std::io::ErrorKind::Other,
|
||||
format!("{}: {}", res.status(), res.text().await.unwrap()),
|
||||
))),
|
||||
},
|
||||
Err(e) => Err(Box::new(e)),
|
||||
}
|
||||
@ -83,37 +79,39 @@ impl Client {
|
||||
|
||||
pub async fn delete(&self, path: &str) -> Result<Response, Box<dyn std::error::Error>> {
|
||||
let client = reqwest::Client::new();
|
||||
let url = format!("{}{}", APU_URL_V1, path);
|
||||
let url = format!("{APU_URL_V1}{path}");
|
||||
let res = client
|
||||
.delete(&url)
|
||||
.header(reqwest::header::CONTENT_TYPE, "application/json")
|
||||
.header(reqwest::header::AUTHORIZATION, "Bearer ".to_owned() + &self.api_key)
|
||||
.header(
|
||||
reqwest::header::AUTHORIZATION,
|
||||
"Bearer ".to_owned() + &self.api_key,
|
||||
)
|
||||
.send()
|
||||
.await;
|
||||
match res {
|
||||
Ok(res) => match res.status().is_success() {
|
||||
true => Ok(res),
|
||||
false => {
|
||||
Err(Box::new(std::io::Error::new(
|
||||
std::io::ErrorKind::Other,
|
||||
format!("{}: {}", res.status(), res.text().await.unwrap())
|
||||
)))
|
||||
},
|
||||
false => Err(Box::new(std::io::Error::new(
|
||||
std::io::ErrorKind::Other,
|
||||
format!("{}: {}", res.status(), res.text().await.unwrap()),
|
||||
))),
|
||||
},
|
||||
Err(e) => Err(Box::new(e)),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn completion(&self, req: CompletionRequest) -> Result<CompletionResponse, Box<dyn std::error::Error>> {
|
||||
pub async fn completion(
|
||||
&self,
|
||||
req: CompletionRequest,
|
||||
) -> Result<CompletionResponse, Box<dyn std::error::Error>> {
|
||||
let res = self.post("/completions", &req).await;
|
||||
match res {
|
||||
Ok(res) => {
|
||||
let r = res.json::<CompletionResponse>().await?;
|
||||
return Ok(r);
|
||||
},
|
||||
Err(e) => {
|
||||
return Err(e);
|
||||
},
|
||||
Ok(r)
|
||||
}
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
|
||||
@ -122,63 +120,65 @@ impl Client {
|
||||
match res {
|
||||
Ok(res) => {
|
||||
let r = res.json::<EditResponse>().await?;
|
||||
return Ok(r);
|
||||
},
|
||||
Err(e) => {
|
||||
return Err(e);
|
||||
},
|
||||
Ok(r)
|
||||
}
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn image_generation(&self, req: ImageGenerationRequest) -> Result<ImageGenerationResponse, Box<dyn std::error::Error>> {
|
||||
pub async fn image_generation(
|
||||
&self,
|
||||
req: ImageGenerationRequest,
|
||||
) -> Result<ImageGenerationResponse, Box<dyn std::error::Error>> {
|
||||
let res = self.post("/images/generations", &req).await;
|
||||
match res {
|
||||
Ok(res) => {
|
||||
let r = res.json::<ImageGenerationResponse>().await?;
|
||||
return Ok(r);
|
||||
},
|
||||
Err(e) => {
|
||||
return Err(e);
|
||||
},
|
||||
Ok(r)
|
||||
}
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn image_edit(&self, req: ImageEditRequest) -> Result<ImageEditResponse, Box<dyn std::error::Error>> {
|
||||
pub async fn image_edit(
|
||||
&self,
|
||||
req: ImageEditRequest,
|
||||
) -> Result<ImageEditResponse, Box<dyn std::error::Error>> {
|
||||
let res = self.post("/images/edits", &req).await;
|
||||
match res {
|
||||
Ok(res) => {
|
||||
let r = res.json::<ImageEditResponse>().await?;
|
||||
return Ok(r);
|
||||
},
|
||||
Err(e) => {
|
||||
return Err(e);
|
||||
},
|
||||
Ok(r)
|
||||
}
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn image_variation(&self, req: ImageVariationRequest) -> Result<ImageVariationResponse, Box<dyn std::error::Error>> {
|
||||
pub async fn image_variation(
|
||||
&self,
|
||||
req: ImageVariationRequest,
|
||||
) -> Result<ImageVariationResponse, Box<dyn std::error::Error>> {
|
||||
let res = self.post("/images/variations", &req).await;
|
||||
match res {
|
||||
Ok(res) => {
|
||||
let r = res.json::<ImageVariationResponse>().await?;
|
||||
return Ok(r);
|
||||
},
|
||||
Err(e) => {
|
||||
return Err(e);
|
||||
},
|
||||
Ok(r)
|
||||
}
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn embedding(&self, req: EmbeddingRequest) -> Result<EmbeddingResponse, Box<dyn std::error::Error>> {
|
||||
pub async fn embedding(
|
||||
&self,
|
||||
req: EmbeddingRequest,
|
||||
) -> Result<EmbeddingResponse, Box<dyn std::error::Error>> {
|
||||
let res = self.post("/embeddings", &req).await;
|
||||
match res {
|
||||
Ok(res) => {
|
||||
let r = res.json::<EmbeddingResponse>().await?;
|
||||
return Ok(r);
|
||||
},
|
||||
Err(e) => {
|
||||
return Err(e);
|
||||
},
|
||||
Ok(r)
|
||||
}
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
|
||||
@ -187,64 +187,81 @@ impl Client {
|
||||
match res {
|
||||
Ok(res) => {
|
||||
let r = res.json::<FileListResponse>().await?;
|
||||
return Ok(r);
|
||||
},
|
||||
Err(e) => {
|
||||
return Err(e);
|
||||
},
|
||||
Ok(r)
|
||||
}
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn file_upload(&self, req: FileUploadRequest) -> Result<FileUploadResponse, Box<dyn std::error::Error>> {
|
||||
|
||||
pub async fn file_upload(
|
||||
&self,
|
||||
req: FileUploadRequest,
|
||||
) -> Result<FileUploadResponse, Box<dyn std::error::Error>> {
|
||||
let res = self.post("/files", &req).await;
|
||||
match res {
|
||||
Ok(res) => {
|
||||
let r = res.json::<FileUploadResponse>().await?;
|
||||
return Ok(r);
|
||||
},
|
||||
Err(e) => {
|
||||
return Err(e);
|
||||
},
|
||||
Ok(r)
|
||||
}
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn file_delete(&self, req: FileDeleteRequest) -> Result<FileDeleteResponse, Box<dyn std::error::Error>> {
|
||||
|
||||
pub async fn file_delete(
|
||||
&self,
|
||||
req: FileDeleteRequest,
|
||||
) -> Result<FileDeleteResponse, Box<dyn std::error::Error>> {
|
||||
let res = self.delete(&format!("{}/{}", "/files", req.file_id)).await;
|
||||
match res {
|
||||
Ok(res) => {
|
||||
let r = res.json::<FileDeleteResponse>().await?;
|
||||
return Ok(r);
|
||||
},
|
||||
Err(e) => {
|
||||
return Err(e);
|
||||
},
|
||||
Ok(r)
|
||||
}
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn file_retrieve(&self, req: FileRetrieveRequest) -> Result<FileRetrieveResponse, Box<dyn std::error::Error>> {
|
||||
pub async fn file_retrieve(
|
||||
&self,
|
||||
req: FileRetrieveRequest,
|
||||
) -> Result<FileRetrieveResponse, Box<dyn std::error::Error>> {
|
||||
let res = self.get(&format!("{}/{}", "/files", req.file_id)).await;
|
||||
match res {
|
||||
Ok(res) => {
|
||||
let r = res.json::<FileRetrieveResponse>().await?;
|
||||
return Ok(r);
|
||||
},
|
||||
Err(e) => {
|
||||
return Err(e);
|
||||
},
|
||||
Ok(r)
|
||||
}
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn file_retrieve_content(&self, req: FileRetrieveContentRequest) -> Result<FileRetrieveContentResponse, Box<dyn std::error::Error>> {
|
||||
let res = self.get(&format!("{}/{}/content", "/files", req.file_id)).await;
|
||||
pub async fn file_retrieve_content(
|
||||
&self,
|
||||
req: FileRetrieveContentRequest,
|
||||
) -> Result<FileRetrieveContentResponse, Box<dyn std::error::Error>> {
|
||||
let res = self
|
||||
.get(&format!("{}/{}/content", "/files", req.file_id))
|
||||
.await;
|
||||
match res {
|
||||
Ok(res) => {
|
||||
let r = res.json::<FileRetrieveContentResponse>().await?;
|
||||
return Ok(r);
|
||||
},
|
||||
Err(e) => {
|
||||
return Err(e);
|
||||
},
|
||||
Ok(r)
|
||||
}
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn chat_completion(
|
||||
&self,
|
||||
req: ChatCompletionRequest,
|
||||
) -> Result<ChatCompletionResponse, Box<dyn std::error::Error>> {
|
||||
let res = self.post("/chat/completions", &req).await;
|
||||
match res {
|
||||
Ok(res) => {
|
||||
let r = res.json::<ChatCompletionResponse>().await?;
|
||||
Ok(r)
|
||||
}
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user