openai_chatgpt_api/
lib.rs1pub(crate) mod http;
2pub(crate) mod error;
3mod v1;
4
5use serde_json::{Value};
6use crate::error::ChatGptError;
7use crate::http::HttpClient;
8
9pub use crate::v1::models::*;
10pub use crate::v1::completions::*;
11pub use crate::v1::chat::*;
12pub use crate::v1::edits::*;
13pub use crate::v1::images::*;
14pub use crate::v1::embeddings::*;
15pub use crate::v1::audio::*;
16pub use crate::v1::{ChatGptResponse, ChatGptRequest};
17
18pub struct ChatGpt {
19 oepenai_api_key: String,
20 org_id: String,
21}
22
23
24impl ChatGpt {
25 pub fn new(oepenai_api_key: &str) -> Self {
26 Self {
27 oepenai_api_key: oepenai_api_key.to_string(),
28 org_id: "".to_string(),
29 }
30 }
31 pub fn new_org(oepenai_api_key: String, org_id: String) -> Self {
32 Self {
33 oepenai_api_key,
34 org_id,
35 }
36 }
37
38
39 pub async fn models_list(&self) -> Result<ChatGptResponseModelList, ChatGptError> {
40 let url = "https://api.openai.com/v1/models";
41 let value = HttpClient::get(self.oepenai_api_key.as_str(), self.org_id.as_str(), url, &Value::default()).await?;
42 Ok(ChatGptResponseModelList { value })
43 }
44 pub async fn models_retrieve(&self, model: &str) -> Result<ChatGptResponseModelRetrieve, ChatGptError> {
45 let url = format!("https://api.openai.com/v1/models/{}", model);
46 let value = HttpClient::get(self.oepenai_api_key.as_str(), self.org_id.as_str(), url.as_str(), &Value::default()).await?;
47 Ok(ChatGptResponseModelRetrieve { value })
48 }
49 pub async fn completions_create(&self, request: &ChatGptRequestCompletionsCreate) -> Result<ChatGptResponseModelRetrieve, ChatGptError> {
50 let url = "https://api.openai.com/v1/completions";
51
52 let value = HttpClient::post(self.oepenai_api_key.as_str(), self.org_id.as_str(), url, &request.to_value()).await?;
53 Ok(ChatGptResponseModelRetrieve { value })
54 }
55 pub async fn chat_completions(&self, request: &ChatGptRequestChatCompletions) -> Result<ChatGptResponseChatCompletions, ChatGptError> {
56 let url = "https://api.openai.com/v1/chat/completions";
57 let data = request.to_value();
58 let value = HttpClient::post(self.oepenai_api_key.as_str(), self.org_id.as_str(), url, &data).await?;
59 Ok(ChatGptResponseChatCompletions { value })
60 }
61 pub async fn edits(&self, request: &ChatGptRequestEdits) -> Result<Value, ChatGptError> {
62 let url = "https://api.openai.com/v1/edits";
63 let data = request.to_value();
64 HttpClient::post(self.oepenai_api_key.as_str(), self.org_id.as_str(), url, &data).await
65 }
66 pub async fn images_generations(&self, request: &ChatGptRequestImagesGenerations) -> Result<ChatGptResponseImagesGenerations, ChatGptError> {
67 let url = "https://api.openai.com/v1/images/generations";
68 let data = request.to_value();
69 let value = HttpClient::post(self.oepenai_api_key.as_str(), self.org_id.as_str(), url, &data).await?;
70 Ok(ChatGptResponseImagesGenerations { value })
71 }
72 pub async fn images_edits(&self, request: &ChatGptRequestImagesEdits) -> Result<ChatGptResponseImagesEdits, ChatGptError> {
73 let url = "https://api.openai.com/v1/images/edits";
74 let value = HttpClient::post_data(self.oepenai_api_key.as_str(), self.org_id.as_str(), url, request.clone().into()).await?;
75 Ok(ChatGptResponseImagesEdits { value })
76 }
77 pub async fn images_variations(&self, request: &ChatGptRequestImagesVariation) -> Result<ChatGptResponseImagesVariation, ChatGptError> {
78 let url = "https://api.openai.com/v1/images/variations";
79 let value = HttpClient::post_data(self.oepenai_api_key.as_str(), self.org_id.as_str(), url, request.clone().into()).await?;
80 Ok(ChatGptResponseImagesVariation { value })
81 }
82 pub async fn embeddings(&self, request: &ChatGptRequestEmbeddingsGenerations) -> Result<Value, ChatGptError> {
83 let url = "https://api.openai.com/v1/embeddings";
84 let data = request.to_value();
85 HttpClient::post(self.oepenai_api_key.as_str(), self.org_id.as_str(), url, &data).await
86 }
87 pub async fn audio_transcriptions(&self, request: &ChatGptRequestAudioTranscriptions) -> Result<ChatGptResponseAudioTranscriptions, ChatGptError> {
88 let url = "https://api.openai.com/v1/audio/transcriptions";
89 let value = HttpClient::post_data(
90 self.oepenai_api_key.as_str(),
91 self.org_id.as_str(),
92 url,
93 request.clone().into(),
94 ).await?;
95 Ok(ChatGptResponseAudioTranscriptions { value })
96 }
97 pub async fn audio_translations(&self, request: &ChatGptRequestAudioTranslations) -> Result<ChatGptResponseAudioTranslations, ChatGptError> {
98 let url = "https://api.openai.com/v1/audio/translations";
99 let value = HttpClient::post_data(
100 self.oepenai_api_key.as_str(),
101 self.org_id.as_str(),
102 url,
103 request.clone().into(),
104 ).await?;
105 Ok(ChatGptResponseAudioTranslations { value })
106 }
107}
108
109