mirror of
https://github.com/mii443/openai-api-rs.git
synced 2025-08-22 23:25:39 +00:00
Add chat gpt api
This commit is contained in:
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "openai-api-rs"
|
name = "openai-api-rs"
|
||||||
version = "0.1.1"
|
version = "0.1.2"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
authors = ["Dongri Jin <dongrify@gmail.com>"]
|
authors = ["Dongri Jin <dongrify@gmail.com>"]
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
|
26
README.md
26
README.md
@ -4,7 +4,7 @@
|
|||||||
Cargo.toml
|
Cargo.toml
|
||||||
```toml
|
```toml
|
||||||
[dependencies]
|
[dependencies]
|
||||||
openai-api-rs = "0.1"
|
openai-api-rs = "0.1.2"
|
||||||
```
|
```
|
||||||
|
|
||||||
## Example:
|
## Example:
|
||||||
@ -12,6 +12,30 @@ openai-api-rs = "0.1"
|
|||||||
export OPENAI_API_KEY={YOUR_API}
|
export OPENAI_API_KEY={YOUR_API}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Chat
|
||||||
|
```rust
|
||||||
|
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>> {
|
||||||
|
let client = Client::new(env::var("OPENAI_API_KEY").unwrap().to_string());
|
||||||
|
let req = ChatCompletionRequest {
|
||||||
|
model: chat_completion::GPT3_5_TURBO.to_string(),
|
||||||
|
messages: vec![chat_completion::ChatCompletionMessage {
|
||||||
|
role: chat_completion::MessageRole::user,
|
||||||
|
content: String::from("NFTとは?"),
|
||||||
|
}],
|
||||||
|
};
|
||||||
|
let result = client.chat_completion(req).await?;
|
||||||
|
println!("{:?}", result.choices[0].message.content);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Completion
|
||||||
```rust
|
```rust
|
||||||
use openai_api_rs::v1::completion::{self, CompletionRequest};
|
use openai_api_rs::v1::completion::{self, CompletionRequest};
|
||||||
use openai_api_rs::v1::api::Client;
|
use openai_api_rs::v1::api::Client;
|
||||||
|
21
examples/chat_completion.rs
Normal file
21
examples/chat_completion.rs
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
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>> {
|
||||||
|
let client = Client::new(env::var("OPENAI_API_KEY").unwrap().to_string());
|
||||||
|
let req = ChatCompletionRequest {
|
||||||
|
model: chat_completion::GPT3_5_TURBO.to_string(),
|
||||||
|
messages: vec![chat_completion::ChatCompletionMessage {
|
||||||
|
role: chat_completion::MessageRole::user,
|
||||||
|
content: String::from("NFTとは?"),
|
||||||
|
}],
|
||||||
|
};
|
||||||
|
let result = client.chat_completion(req).await?;
|
||||||
|
println!("{:?}", result.choices[0].message.content);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
// OPENAI_API_KEY=xxxx cargo run --package openai-api-rs --example chat_completion
|
@ -1,5 +1,5 @@
|
|||||||
use openai_api_rs::v1::completion::{self, CompletionRequest};
|
|
||||||
use openai_api_rs::v1::api::Client;
|
use openai_api_rs::v1::api::Client;
|
||||||
|
use openai_api_rs::v1::completion::{self, CompletionRequest};
|
||||||
use std::env;
|
use std::env;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
@ -22,11 +22,11 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
best_of: None,
|
best_of: None,
|
||||||
logit_bias: None,
|
logit_bias: None,
|
||||||
user: None,
|
user: None,
|
||||||
};
|
};
|
||||||
let result = client.completion(req).await?;
|
let result = client.completion(req).await?;
|
||||||
println!("{:}", result.choices[0].text);
|
println!("{:}", result.choices[0].text);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
// cargo run --package openai-rs --example completion
|
// OPENAI_API_KEY=xxxx cargo run --package openai-api-rs --example completion
|
||||||
|
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::completion::{CompletionRequest, CompletionResponse};
|
||||||
use crate::v1::edit::{EditRequest, EditResponse};
|
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::embedding::{EmbeddingRequest, EmbeddingResponse};
|
||||||
use crate::v1::file::{
|
use crate::v1::file::{
|
||||||
FileListResponse,
|
FileDeleteRequest, FileDeleteResponse, FileListResponse, FileRetrieveContentRequest,
|
||||||
FileUploadRequest,
|
FileRetrieveContentResponse, FileRetrieveRequest, FileRetrieveResponse, FileUploadRequest,
|
||||||
FileUploadResponse,
|
FileUploadResponse,
|
||||||
FileDeleteRequest,
|
};
|
||||||
FileDeleteResponse,
|
use crate::v1::image::{
|
||||||
FileRetrieveRequest,
|
ImageEditRequest, ImageEditResponse, ImageGenerationRequest, ImageGenerationResponse,
|
||||||
FileRetrieveResponse,
|
ImageVariationRequest, ImageVariationResponse,
|
||||||
FileRetrieveContentRequest,
|
|
||||||
FileRetrieveContentResponse,
|
|
||||||
};
|
};
|
||||||
use reqwest::Response;
|
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 struct Client {
|
||||||
pub api_key: String,
|
pub api_key: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Client {
|
impl Client {
|
||||||
@ -34,25 +24,30 @@ impl Client {
|
|||||||
Self { api_key }
|
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 client = reqwest::Client::new();
|
||||||
let url = format!("{}{}", APU_URL_V1, path);
|
let url = format!("{APU_URL_V1}{path}");
|
||||||
let res = client
|
let res = client
|
||||||
.post(&url)
|
.post(&url)
|
||||||
.header(reqwest::header::CONTENT_TYPE, "application/json")
|
.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)
|
.json(¶ms)
|
||||||
.send()
|
.send()
|
||||||
.await;
|
.await;
|
||||||
match res {
|
match res {
|
||||||
Ok(res) => match res.status().is_success() {
|
Ok(res) => match res.status().is_success() {
|
||||||
true => Ok(res),
|
true => Ok(res),
|
||||||
false => {
|
false => Err(Box::new(std::io::Error::new(
|
||||||
Err(Box::new(std::io::Error::new(
|
std::io::ErrorKind::Other,
|
||||||
std::io::ErrorKind::Other,
|
format!("{}: {}", res.status(), res.text().await.unwrap()),
|
||||||
format!("{}: {}", res.status(), res.text().await.unwrap())
|
))),
|
||||||
)))
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
Err(e) => Err(Box::new(e)),
|
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>> {
|
pub async fn get(&self, path: &str) -> Result<Response, Box<dyn std::error::Error>> {
|
||||||
let client = reqwest::Client::new();
|
let client = reqwest::Client::new();
|
||||||
let url = format!("{}{}", APU_URL_V1, path);
|
let url = format!("{APU_URL_V1}{path}");
|
||||||
let res = client
|
let res = client
|
||||||
.get(&url)
|
.get(&url)
|
||||||
.header(reqwest::header::CONTENT_TYPE, "application/json")
|
.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()
|
.send()
|
||||||
.await;
|
.await;
|
||||||
match res {
|
match res {
|
||||||
Ok(res) => match res.status().is_success() {
|
Ok(res) => match res.status().is_success() {
|
||||||
true => Ok(res),
|
true => Ok(res),
|
||||||
false => {
|
false => Err(Box::new(std::io::Error::new(
|
||||||
Err(Box::new(std::io::Error::new(
|
std::io::ErrorKind::Other,
|
||||||
std::io::ErrorKind::Other,
|
format!("{}: {}", res.status(), res.text().await.unwrap()),
|
||||||
format!("{}: {}", res.status(), res.text().await.unwrap())
|
))),
|
||||||
)))
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
Err(e) => Err(Box::new(e)),
|
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>> {
|
pub async fn delete(&self, path: &str) -> Result<Response, Box<dyn std::error::Error>> {
|
||||||
let client = reqwest::Client::new();
|
let client = reqwest::Client::new();
|
||||||
let url = format!("{}{}", APU_URL_V1, path);
|
let url = format!("{APU_URL_V1}{path}");
|
||||||
let res = client
|
let res = client
|
||||||
.delete(&url)
|
.delete(&url)
|
||||||
.header(reqwest::header::CONTENT_TYPE, "application/json")
|
.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()
|
.send()
|
||||||
.await;
|
.await;
|
||||||
match res {
|
match res {
|
||||||
Ok(res) => match res.status().is_success() {
|
Ok(res) => match res.status().is_success() {
|
||||||
true => Ok(res),
|
true => Ok(res),
|
||||||
false => {
|
false => Err(Box::new(std::io::Error::new(
|
||||||
Err(Box::new(std::io::Error::new(
|
std::io::ErrorKind::Other,
|
||||||
std::io::ErrorKind::Other,
|
format!("{}: {}", res.status(), res.text().await.unwrap()),
|
||||||
format!("{}: {}", res.status(), res.text().await.unwrap())
|
))),
|
||||||
)))
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
Err(e) => Err(Box::new(e)),
|
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;
|
let res = self.post("/completions", &req).await;
|
||||||
match res {
|
match res {
|
||||||
Ok(res) => {
|
Ok(res) => {
|
||||||
let r = res.json::<CompletionResponse>().await?;
|
let r = res.json::<CompletionResponse>().await?;
|
||||||
return Ok(r);
|
Ok(r)
|
||||||
},
|
}
|
||||||
Err(e) => {
|
Err(e) => Err(e),
|
||||||
return Err(e);
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -122,63 +120,65 @@ impl Client {
|
|||||||
match res {
|
match res {
|
||||||
Ok(res) => {
|
Ok(res) => {
|
||||||
let r = res.json::<EditResponse>().await?;
|
let r = res.json::<EditResponse>().await?;
|
||||||
return Ok(r);
|
Ok(r)
|
||||||
},
|
}
|
||||||
Err(e) => {
|
Err(e) => Err(e),
|
||||||
return 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;
|
let res = self.post("/images/generations", &req).await;
|
||||||
match res {
|
match res {
|
||||||
Ok(res) => {
|
Ok(res) => {
|
||||||
let r = res.json::<ImageGenerationResponse>().await?;
|
let r = res.json::<ImageGenerationResponse>().await?;
|
||||||
return Ok(r);
|
Ok(r)
|
||||||
},
|
}
|
||||||
Err(e) => {
|
Err(e) => Err(e),
|
||||||
return 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;
|
let res = self.post("/images/edits", &req).await;
|
||||||
match res {
|
match res {
|
||||||
Ok(res) => {
|
Ok(res) => {
|
||||||
let r = res.json::<ImageEditResponse>().await?;
|
let r = res.json::<ImageEditResponse>().await?;
|
||||||
return Ok(r);
|
Ok(r)
|
||||||
},
|
}
|
||||||
Err(e) => {
|
Err(e) => Err(e),
|
||||||
return 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;
|
let res = self.post("/images/variations", &req).await;
|
||||||
match res {
|
match res {
|
||||||
Ok(res) => {
|
Ok(res) => {
|
||||||
let r = res.json::<ImageVariationResponse>().await?;
|
let r = res.json::<ImageVariationResponse>().await?;
|
||||||
return Ok(r);
|
Ok(r)
|
||||||
},
|
}
|
||||||
Err(e) => {
|
Err(e) => Err(e),
|
||||||
return 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;
|
let res = self.post("/embeddings", &req).await;
|
||||||
match res {
|
match res {
|
||||||
Ok(res) => {
|
Ok(res) => {
|
||||||
let r = res.json::<EmbeddingResponse>().await?;
|
let r = res.json::<EmbeddingResponse>().await?;
|
||||||
return Ok(r);
|
Ok(r)
|
||||||
},
|
}
|
||||||
Err(e) => {
|
Err(e) => Err(e),
|
||||||
return Err(e);
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -187,64 +187,81 @@ impl Client {
|
|||||||
match res {
|
match res {
|
||||||
Ok(res) => {
|
Ok(res) => {
|
||||||
let r = res.json::<FileListResponse>().await?;
|
let r = res.json::<FileListResponse>().await?;
|
||||||
return Ok(r);
|
Ok(r)
|
||||||
},
|
}
|
||||||
Err(e) => {
|
Err(e) => Err(e),
|
||||||
return 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;
|
let res = self.post("/files", &req).await;
|
||||||
match res {
|
match res {
|
||||||
Ok(res) => {
|
Ok(res) => {
|
||||||
let r = res.json::<FileUploadResponse>().await?;
|
let r = res.json::<FileUploadResponse>().await?;
|
||||||
return Ok(r);
|
Ok(r)
|
||||||
},
|
}
|
||||||
Err(e) => {
|
Err(e) => Err(e),
|
||||||
return 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;
|
let res = self.delete(&format!("{}/{}", "/files", req.file_id)).await;
|
||||||
match res {
|
match res {
|
||||||
Ok(res) => {
|
Ok(res) => {
|
||||||
let r = res.json::<FileDeleteResponse>().await?;
|
let r = res.json::<FileDeleteResponse>().await?;
|
||||||
return Ok(r);
|
Ok(r)
|
||||||
},
|
}
|
||||||
Err(e) => {
|
Err(e) => Err(e),
|
||||||
return 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;
|
let res = self.get(&format!("{}/{}", "/files", req.file_id)).await;
|
||||||
match res {
|
match res {
|
||||||
Ok(res) => {
|
Ok(res) => {
|
||||||
let r = res.json::<FileRetrieveResponse>().await?;
|
let r = res.json::<FileRetrieveResponse>().await?;
|
||||||
return Ok(r);
|
Ok(r)
|
||||||
},
|
}
|
||||||
Err(e) => {
|
Err(e) => Err(e),
|
||||||
return Err(e);
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn file_retrieve_content(&self, req: FileRetrieveContentRequest) -> Result<FileRetrieveContentResponse, Box<dyn std::error::Error>> {
|
pub async fn file_retrieve_content(
|
||||||
let res = self.get(&format!("{}/{}/content", "/files", req.file_id)).await;
|
&self,
|
||||||
|
req: FileRetrieveContentRequest,
|
||||||
|
) -> Result<FileRetrieveContentResponse, Box<dyn std::error::Error>> {
|
||||||
|
let res = self
|
||||||
|
.get(&format!("{}/{}/content", "/files", req.file_id))
|
||||||
|
.await;
|
||||||
match res {
|
match res {
|
||||||
Ok(res) => {
|
Ok(res) => {
|
||||||
let r = res.json::<FileRetrieveContentResponse>().await?;
|
let r = res.json::<FileRetrieveContentResponse>().await?;
|
||||||
return Ok(r);
|
Ok(r)
|
||||||
},
|
}
|
||||||
Err(e) => {
|
Err(e) => Err(e),
|
||||||
return 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),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
43
src/v1/chat_completion.rs
Normal file
43
src/v1/chat_completion.rs
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
use crate::v1::common;
|
||||||
|
|
||||||
|
pub const GPT3_5_TURBO: &str = "gpt-3.5-turbo";
|
||||||
|
pub const GPT3_5_TURBO_0301: &str = "gpt-3.5-turbo-0301";
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize)]
|
||||||
|
pub struct ChatCompletionRequest {
|
||||||
|
pub model: String,
|
||||||
|
pub messages: Vec<ChatCompletionMessage>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
|
#[allow(non_camel_case_types)]
|
||||||
|
pub enum MessageRole {
|
||||||
|
user,
|
||||||
|
system,
|
||||||
|
assistant,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
|
pub struct ChatCompletionMessage {
|
||||||
|
pub role: MessageRole,
|
||||||
|
pub content: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize)]
|
||||||
|
pub struct ChatCompletionChoice {
|
||||||
|
pub index: i64,
|
||||||
|
pub message: ChatCompletionMessage,
|
||||||
|
pub finish_reason: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize)]
|
||||||
|
pub struct ChatCompletionResponse {
|
||||||
|
pub id: String,
|
||||||
|
pub object: String,
|
||||||
|
pub created: i64,
|
||||||
|
pub model: String,
|
||||||
|
pub choices: Vec<ChatCompletionChoice>,
|
||||||
|
pub usage: common::Usage,
|
||||||
|
}
|
@ -1,10 +1,8 @@
|
|||||||
|
use serde::Deserialize;
|
||||||
use serde::{Deserialize};
|
|
||||||
|
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
pub struct Usage {
|
pub struct Usage {
|
||||||
pub prompt_tokens: i32,
|
pub prompt_tokens: i32,
|
||||||
pub completion_tokens: i32,
|
pub completion_tokens: i32,
|
||||||
pub total_tokens: i32,
|
pub total_tokens: i32,
|
||||||
}
|
}
|
||||||
|
@ -1,79 +1,79 @@
|
|||||||
use serde::{Serialize, Deserialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::option::Option;
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
use std::option::Option;
|
||||||
|
|
||||||
use crate::v1::common;
|
use crate::v1::common;
|
||||||
|
|
||||||
pub const GPT3_TEXT_DAVINCI_003: &str = "text-davinci-003";
|
pub const GPT3_TEXT_DAVINCI_003: &str = "text-davinci-003";
|
||||||
pub const GPT3_TEXT_DAVINCI_002: &str = "text-davinci-002";
|
pub const GPT3_TEXT_DAVINCI_002: &str = "text-davinci-002";
|
||||||
pub const GPT3_TEXT_CURIE_001: &str = "text-curie-001";
|
pub const GPT3_TEXT_CURIE_001: &str = "text-curie-001";
|
||||||
pub const GPT3_TEXT_BABBAGE_001: &str = "text-babbage-001";
|
pub const GPT3_TEXT_BABBAGE_001: &str = "text-babbage-001";
|
||||||
pub const GPT3_TEXT_ADA_001: &str = "text-ada-001";
|
pub const GPT3_TEXT_ADA_001: &str = "text-ada-001";
|
||||||
pub const GPT3_TEXT_DAVINCI_001: &str = "text-davinci-001";
|
pub const GPT3_TEXT_DAVINCI_001: &str = "text-davinci-001";
|
||||||
pub const GPT3_DAVINCI_INSTRUCT_BETA: &str = "davinci-instruct-beta";
|
pub const GPT3_DAVINCI_INSTRUCT_BETA: &str = "davinci-instruct-beta";
|
||||||
pub const GPT3_DAVINCI: &str = "davinci";
|
pub const GPT3_DAVINCI: &str = "davinci";
|
||||||
pub const GPT3_CURIE_INSTRUCT_BETA: &str = "curie-instruct-beta";
|
pub const GPT3_CURIE_INSTRUCT_BETA: &str = "curie-instruct-beta";
|
||||||
pub const GPT3_CURIE: &str = "curie";
|
pub const GPT3_CURIE: &str = "curie";
|
||||||
pub const GPT3_ADA: &str = "ada";
|
pub const GPT3_ADA: &str = "ada";
|
||||||
pub const GPT3_BABBAGE: &str = "babbage";
|
pub const GPT3_BABBAGE: &str = "babbage";
|
||||||
|
|
||||||
#[derive(Debug, Serialize)]
|
#[derive(Debug, Serialize)]
|
||||||
pub struct CompletionRequest {
|
pub struct CompletionRequest {
|
||||||
pub model: String,
|
pub model: String,
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub prompt: Option<String>,
|
pub prompt: Option<String>,
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub suffix: Option<String>,
|
pub suffix: Option<String>,
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub max_tokens: Option<i32>,
|
pub max_tokens: Option<i32>,
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub temperature: Option<f32>,
|
pub temperature: Option<f32>,
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub top_p: Option<f32>,
|
pub top_p: Option<f32>,
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub n: Option<i32>,
|
pub n: Option<i32>,
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub stream: Option<bool>,
|
pub stream: Option<bool>,
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub logprobs: Option<i32>,
|
pub logprobs: Option<i32>,
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub echo: Option<bool>,
|
pub echo: Option<bool>,
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub stop: Option<Vec<String>>,
|
pub stop: Option<Vec<String>>,
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub presence_penalty: Option<f32>,
|
pub presence_penalty: Option<f32>,
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub frequency_penalty: Option<f32>,
|
pub frequency_penalty: Option<f32>,
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub best_of: Option<i32>,
|
pub best_of: Option<i32>,
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub logit_bias: Option<HashMap<String, i32>>,
|
pub logit_bias: Option<HashMap<String, i32>>,
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub user: Option<String>,
|
pub user: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
pub struct CompletionChoice {
|
pub struct CompletionChoice {
|
||||||
pub text: String,
|
pub text: String,
|
||||||
pub index: i64,
|
pub index: i64,
|
||||||
pub finish_reason: String,
|
pub finish_reason: String,
|
||||||
pub logprobs: Option<LogprobResult>,
|
pub logprobs: Option<LogprobResult>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
pub struct LogprobResult {
|
pub struct LogprobResult {
|
||||||
pub tokens: Vec<String>,
|
pub tokens: Vec<String>,
|
||||||
pub token_logprobs: Vec<f32>,
|
pub token_logprobs: Vec<f32>,
|
||||||
pub top_logprobs: Vec<HashMap<String, f32>>,
|
pub top_logprobs: Vec<HashMap<String, f32>>,
|
||||||
pub text_offset: Vec<i32>,
|
pub text_offset: Vec<i32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
pub struct CompletionResponse {
|
pub struct CompletionResponse {
|
||||||
pub id: String,
|
pub id: String,
|
||||||
pub object: String,
|
pub object: String,
|
||||||
pub created: i64,
|
pub created: i64,
|
||||||
pub model: String,
|
pub model: String,
|
||||||
pub choices: Vec<CompletionChoice>,
|
pub choices: Vec<CompletionChoice>,
|
||||||
pub usage: common::Usage,
|
pub usage: common::Usage,
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use serde::{Serialize, Deserialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::option::Option;
|
use std::option::Option;
|
||||||
|
|
||||||
use crate::v1::common;
|
use crate::v1::common;
|
||||||
@ -18,15 +18,15 @@ pub struct EditRequest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
pub struct EditChoice{
|
pub struct EditChoice {
|
||||||
pub text: String,
|
pub text: String,
|
||||||
pub index: i32,
|
pub index: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
pub struct EditResponse {
|
pub struct EditResponse {
|
||||||
pub object: String,
|
pub object: String,
|
||||||
pub created: i64,
|
pub created: i64,
|
||||||
pub usage: common::Usage,
|
pub usage: common::Usage,
|
||||||
pub choices: Vec<EditChoice>,
|
pub choices: Vec<EditChoice>,
|
||||||
}
|
}
|
||||||
|
@ -1,14 +1,14 @@
|
|||||||
use serde::{Serialize, Deserialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::option::Option;
|
use std::option::Option;
|
||||||
|
|
||||||
use crate::v1::common;
|
use crate::v1::common;
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
pub struct EmbeddingData{
|
pub struct EmbeddingData {
|
||||||
pub object: String,
|
pub object: String,
|
||||||
pub embedding: Vec<f32>,
|
pub embedding: Vec<f32>,
|
||||||
pub index: i32,
|
pub index: i32,
|
||||||
pub usage: common::Usage,
|
pub usage: common::Usage,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize)]
|
#[derive(Debug, Serialize)]
|
||||||
@ -19,9 +19,8 @@ pub struct EmbeddingRequest {
|
|||||||
pub user: Option<String>,
|
pub user: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
pub struct EmbeddingResponse {
|
pub struct EmbeddingResponse {
|
||||||
pub object: String,
|
pub object: String,
|
||||||
pub data: Vec<EmbeddingData>,
|
pub data: Vec<EmbeddingData>,
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
use serde::{Serialize, Deserialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
pub struct FileData{
|
pub struct FileData {
|
||||||
pub id: String,
|
pub id: String,
|
||||||
pub oejct: String,
|
pub oejct: String,
|
||||||
pub bytes: i32,
|
pub bytes: i32,
|
||||||
pub created_at: i64,
|
pub created_at: i64,
|
||||||
@ -13,10 +13,9 @@ pub struct FileData{
|
|||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
pub struct FileListResponse {
|
pub struct FileListResponse {
|
||||||
pub object: String,
|
pub object: String,
|
||||||
pub data: Vec<FileData>,
|
pub data: Vec<FileData>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[derive(Debug, Serialize)]
|
#[derive(Debug, Serialize)]
|
||||||
pub struct FileUploadRequest {
|
pub struct FileUploadRequest {
|
||||||
pub file: String,
|
pub file: String,
|
||||||
@ -33,7 +32,6 @@ pub struct FileUploadResponse {
|
|||||||
pub purpose: String,
|
pub purpose: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[derive(Debug, Serialize)]
|
#[derive(Debug, Serialize)]
|
||||||
pub struct FileDeleteRequest {
|
pub struct FileDeleteRequest {
|
||||||
pub file_id: String,
|
pub file_id: String,
|
||||||
@ -61,7 +59,6 @@ pub struct FileRetrieveResponse {
|
|||||||
pub purpose: String,
|
pub purpose: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[derive(Debug, Serialize)]
|
#[derive(Debug, Serialize)]
|
||||||
pub struct FileRetrieveContentRequest {
|
pub struct FileRetrieveContentRequest {
|
||||||
pub file_id: String,
|
pub file_id: String,
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
use serde::{Serialize, Deserialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::option::Option;
|
use std::option::Option;
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
pub struct ImageData{
|
pub struct ImageData {
|
||||||
pub url: String,
|
pub url: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize)]
|
#[derive(Debug, Serialize)]
|
||||||
@ -19,11 +19,10 @@ pub struct ImageGenerationRequest {
|
|||||||
pub user: Option<String>,
|
pub user: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
pub struct ImageGenerationResponse {
|
pub struct ImageGenerationResponse {
|
||||||
pub created: i64,
|
pub created: i64,
|
||||||
pub data: Vec<ImageData>,
|
pub data: Vec<ImageData>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize)]
|
#[derive(Debug, Serialize)]
|
||||||
@ -45,7 +44,7 @@ pub struct ImageEditRequest {
|
|||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
pub struct ImageEditResponse {
|
pub struct ImageEditResponse {
|
||||||
pub created: i64,
|
pub created: i64,
|
||||||
pub data: Vec<ImageData>,
|
pub data: Vec<ImageData>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize)]
|
#[derive(Debug, Serialize)]
|
||||||
@ -64,5 +63,5 @@ pub struct ImageVariationRequest {
|
|||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
pub struct ImageVariationResponse {
|
pub struct ImageVariationResponse {
|
||||||
pub created: i64,
|
pub created: i64,
|
||||||
pub data: Vec<ImageData>,
|
pub data: Vec<ImageData>,
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
pub mod common;
|
pub mod common;
|
||||||
|
|
||||||
|
pub mod chat_completion;
|
||||||
pub mod completion;
|
pub mod completion;
|
||||||
pub mod edit;
|
pub mod edit;
|
||||||
pub mod image;
|
|
||||||
pub mod embedding;
|
pub mod embedding;
|
||||||
pub mod file;
|
pub mod file;
|
||||||
|
pub mod image;
|
||||||
|
|
||||||
pub mod api;
|
pub mod api;
|
||||||
|
Reference in New Issue
Block a user