/*
* OpenAI API
*
* The OpenAI REST API. Please see https://platform.openai.com/docs/api-reference for more details.
*
* The version of the OpenAPI document: 2.3.0
*
* Generated by: https://openapi-generator.tech
*/
#![allow(clippy::needless_return, clippy::into_iter_on_ref)]
use super::{configuration, ContentType, Error};
use crate::{apis::ResponseContent, models};
use reqwest;
use serde::{de::Error as _, Deserialize, Serialize};
/// struct for typed errors of method [`create_chat_completion`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum CreateChatCompletionError {
UnknownValue(serde_json::Value),
}
/// struct for typed errors of method [`delete_chat_completion`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum DeleteChatCompletionError {
UnknownValue(serde_json::Value),
}
/// struct for typed errors of method [`get_chat_completion`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum GetChatCompletionError {
UnknownValue(serde_json::Value),
}
/// struct for typed errors of method [`get_chat_completion_messages`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum GetChatCompletionMessagesError {
UnknownValue(serde_json::Value),
}
/// struct for typed errors of method [`list_chat_completions`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum ListChatCompletionsError {
UnknownValue(serde_json::Value),
}
/// struct for typed errors of method [`update_chat_completion`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum UpdateChatCompletionError {
UnknownValue(serde_json::Value),
}
/// **Starting a new project?** We recommend trying [Responses](https://platform.openai.com/docs/api-reference/responses) to take advantage of the latest OpenAI platform features. Compare [Chat Completions with Responses](https://platform.openai.com/docs/guides/responses-vs-chat-completions?api-mode=responses). --- Creates a model response for the given chat conversation. Learn more in the [text generation](https://platform.openai.com/docs/guides/text-generation), [vision](https://platform.openai.com/docs/guides/vision), and [audio](https://platform.openai.com/docs/guides/audio) guides. Parameter support can differ depending on the model used to generate the response, particularly for newer reasoning models. Parameters that are only supported for reasoning models are noted below. For the current state of unsupported parameters in reasoning models, [refer to the reasoning guide](https://platform.openai.com/docs/guides/reasoning).
#[bon::builder]
pub async fn create_chat_completion(
configuration: &configuration::Configuration,
create_chat_completion_request: models::CreateChatCompletionRequest,
) -> Result<models::CreateChatCompletionResponse, Error<CreateChatCompletionError>> {
// add a prefix to parameters to efficiently prevent name collisions
let p_body_create_chat_completion_request = create_chat_completion_request;
let uri_str = format!("{}/chat/completions", configuration.base_path);
let mut req_builder = configuration
.client
.request(reqwest::Method::POST, &uri_str);
if let Some(ref user_agent) = configuration.user_agent {
req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
}
if let Some(ref token) = configuration.bearer_access_token {
req_builder = req_builder.bearer_auth(token.to_owned());
};
req_builder = req_builder.json(&p_body_create_chat_completion_request);
let req = req_builder.build()?;
let resp = configuration.client.execute(req).await?;
let status = resp.status();
let content_type = resp
.headers()
.get("content-type")
.and_then(|v| v.to_str().ok())
.unwrap_or("application/octet-stream");
let content_type = super::ContentType::from(content_type);
if !status.is_client_error() && !status.is_server_error() {
let content = resp.text().await?;
match content_type {
ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::CreateChatCompletionResponse`"))),
ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::CreateChatCompletionResponse`")))),
}
} else {
let content = resp.text().await?;
let entity: Option<CreateChatCompletionError> = serde_json::from_str(&content).ok();
Err(Error::ResponseError(ResponseContent {
status,
content,
entity,
}))
}
}
/// Delete a stored chat completion. Only Chat Completions that have been created with the `store` parameter set to `true` can be deleted.
#[bon::builder]
pub async fn delete_chat_completion(
configuration: &configuration::Configuration,
completion_id: &str,
) -> Result<models::ChatCompletionDeleted, Error<DeleteChatCompletionError>> {
// add a prefix to parameters to efficiently prevent name collisions
let p_path_completion_id = completion_id;
let uri_str = format!(
"{}/chat/completions/{completion_id}",
configuration.base_path,
completion_id = crate::apis::urlencode(p_path_completion_id)
);
let mut req_builder = configuration
.client
.request(reqwest::Method::DELETE, &uri_str);
if let Some(ref user_agent) = configuration.user_agent {
req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
}
if let Some(ref token) = configuration.bearer_access_token {
req_builder = req_builder.bearer_auth(token.to_owned());
};
let req = req_builder.build()?;
let resp = configuration.client.execute(req).await?;
let status = resp.status();
let content_type = resp
.headers()
.get("content-type")
.and_then(|v| v.to_str().ok())
.unwrap_or("application/octet-stream");
let content_type = super::ContentType::from(content_type);
if !status.is_client_error() && !status.is_server_error() {
let content = resp.text().await?;
match content_type {
ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::ChatCompletionDeleted`"))),
ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::ChatCompletionDeleted`")))),
}
} else {
let content = resp.text().await?;
let entity: Option<DeleteChatCompletionError> = serde_json::from_str(&content).ok();
Err(Error::ResponseError(ResponseContent {
status,
content,
entity,
}))
}
}
/// Get a stored chat completion. Only Chat Completions that have been created with the `store` parameter set to `true` will be returned.
#[bon::builder]
pub async fn get_chat_completion(
configuration: &configuration::Configuration,
completion_id: &str,
) -> Result<models::CreateChatCompletionResponse, Error<GetChatCompletionError>> {
// add a prefix to parameters to efficiently prevent name collisions
let p_path_completion_id = completion_id;
let uri_str = format!(
"{}/chat/completions/{completion_id}",
configuration.base_path,
completion_id = crate::apis::urlencode(p_path_completion_id)
);
let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
if let Some(ref user_agent) = configuration.user_agent {
req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
}
if let Some(ref token) = configuration.bearer_access_token {
req_builder = req_builder.bearer_auth(token.to_owned());
};
let req = req_builder.build()?;
let resp = configuration.client.execute(req).await?;
let status = resp.status();
let content_type = resp
.headers()
.get("content-type")
.and_then(|v| v.to_str().ok())
.unwrap_or("application/octet-stream");
let content_type = super::ContentType::from(content_type);
if !status.is_client_error() && !status.is_server_error() {
let content = resp.text().await?;
match content_type {
ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::CreateChatCompletionResponse`"))),
ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::CreateChatCompletionResponse`")))),
}
} else {
let content = resp.text().await?;
let entity: Option<GetChatCompletionError> = serde_json::from_str(&content).ok();
Err(Error::ResponseError(ResponseContent {
status,
content,
entity,
}))
}
}
/// Get the messages in a stored chat completion. Only Chat Completions that have been created with the `store` parameter set to `true` will be returned.
#[bon::builder]
pub async fn get_chat_completion_messages(
configuration: &configuration::Configuration,
completion_id: &str,
after: Option<&str>,
limit: Option<i32>,
order: Option<&str>,
) -> Result<models::ChatCompletionMessageList, Error<GetChatCompletionMessagesError>> {
// add a prefix to parameters to efficiently prevent name collisions
let p_path_completion_id = completion_id;
let p_query_after = after;
let p_query_limit = limit;
let p_query_order = order;
let uri_str = format!(
"{}/chat/completions/{completion_id}/messages",
configuration.base_path,
completion_id = crate::apis::urlencode(p_path_completion_id)
);
let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
if let Some(ref param_value) = p_query_after {
req_builder = req_builder.query(&[("after", ¶m_value.to_string())]);
}
if let Some(ref param_value) = p_query_limit {
req_builder = req_builder.query(&[("limit", ¶m_value.to_string())]);
}
if let Some(ref param_value) = p_query_order {
req_builder = req_builder.query(&[("order", ¶m_value.to_string())]);
}
if let Some(ref user_agent) = configuration.user_agent {
req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
}
if let Some(ref token) = configuration.bearer_access_token {
req_builder = req_builder.bearer_auth(token.to_owned());
};
let req = req_builder.build()?;
let resp = configuration.client.execute(req).await?;
let status = resp.status();
let content_type = resp
.headers()
.get("content-type")
.and_then(|v| v.to_str().ok())
.unwrap_or("application/octet-stream");
let content_type = super::ContentType::from(content_type);
if !status.is_client_error() && !status.is_server_error() {
let content = resp.text().await?;
match content_type {
ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::ChatCompletionMessageList`"))),
ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::ChatCompletionMessageList`")))),
}
} else {
let content = resp.text().await?;
let entity: Option<GetChatCompletionMessagesError> = serde_json::from_str(&content).ok();
Err(Error::ResponseError(ResponseContent {
status,
content,
entity,
}))
}
}
/// List stored Chat Completions. Only Chat Completions that have been stored with the `store` parameter set to `true` will be returned.
#[bon::builder]
pub async fn list_chat_completions(
configuration: &configuration::Configuration,
model: Option<&str>,
metadata: Option<&str>,
after: Option<&str>,
limit: Option<i32>,
order: Option<&str>,
) -> Result<models::ChatCompletionList, Error<ListChatCompletionsError>> {
// add a prefix to parameters to efficiently prevent name collisions
let p_query_model = model;
let p_query_metadata = metadata;
let p_query_after = after;
let p_query_limit = limit;
let p_query_order = order;
let uri_str = format!("{}/chat/completions", configuration.base_path);
let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
if let Some(ref param_value) = p_query_model {
req_builder = req_builder.query(&[("model", ¶m_value.to_string())]);
}
if let Some(ref param_value) = p_query_metadata {
req_builder = req_builder.query(&[("metadata", ¶m_value.to_string())]);
}
if let Some(ref param_value) = p_query_after {
req_builder = req_builder.query(&[("after", ¶m_value.to_string())]);
}
if let Some(ref param_value) = p_query_limit {
req_builder = req_builder.query(&[("limit", ¶m_value.to_string())]);
}
if let Some(ref param_value) = p_query_order {
req_builder = req_builder.query(&[("order", ¶m_value.to_string())]);
}
if let Some(ref user_agent) = configuration.user_agent {
req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
}
if let Some(ref token) = configuration.bearer_access_token {
req_builder = req_builder.bearer_auth(token.to_owned());
};
let req = req_builder.build()?;
let resp = configuration.client.execute(req).await?;
let status = resp.status();
let content_type = resp
.headers()
.get("content-type")
.and_then(|v| v.to_str().ok())
.unwrap_or("application/octet-stream");
let content_type = super::ContentType::from(content_type);
if !status.is_client_error() && !status.is_server_error() {
let content = resp.text().await?;
match content_type {
ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::ChatCompletionList`"))),
ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::ChatCompletionList`")))),
}
} else {
let content = resp.text().await?;
let entity: Option<ListChatCompletionsError> = serde_json::from_str(&content).ok();
Err(Error::ResponseError(ResponseContent {
status,
content,
entity,
}))
}
}
/// Modify a stored chat completion. Only Chat Completions that have been created with the `store` parameter set to `true` can be modified. Currently, the only supported modification is to update the `metadata` field.
#[bon::builder]
pub async fn update_chat_completion(
configuration: &configuration::Configuration,
completion_id: &str,
update_chat_completion_request: models::UpdateChatCompletionRequest,
) -> Result<models::CreateChatCompletionResponse, Error<UpdateChatCompletionError>> {
// add a prefix to parameters to efficiently prevent name collisions
let p_path_completion_id = completion_id;
let p_body_update_chat_completion_request = update_chat_completion_request;
let uri_str = format!(
"{}/chat/completions/{completion_id}",
configuration.base_path,
completion_id = crate::apis::urlencode(p_path_completion_id)
);
let mut req_builder = configuration
.client
.request(reqwest::Method::POST, &uri_str);
if let Some(ref user_agent) = configuration.user_agent {
req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
}
if let Some(ref token) = configuration.bearer_access_token {
req_builder = req_builder.bearer_auth(token.to_owned());
};
req_builder = req_builder.json(&p_body_update_chat_completion_request);
let req = req_builder.build()?;
let resp = configuration.client.execute(req).await?;
let status = resp.status();
let content_type = resp
.headers()
.get("content-type")
.and_then(|v| v.to_str().ok())
.unwrap_or("application/octet-stream");
let content_type = super::ContentType::from(content_type);
if !status.is_client_error() && !status.is_server_error() {
let content = resp.text().await?;
match content_type {
ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::CreateChatCompletionResponse`"))),
ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::CreateChatCompletionResponse`")))),
}
} else {
let content = resp.text().await?;
let entity: Option<UpdateChatCompletionError> = serde_json::from_str(&content).ok();
Err(Error::ResponseError(ResponseContent {
status,
content,
entity,
}))
}
}