kind_openai/endpoints/
chat.rsmod standard;
mod structured;
pub use standard::*;
pub use structured::*;
use std::borrow::Cow;
use bon::{builder, Builder};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Clone, Copy)]
#[allow(non_camel_case_types)]
pub enum Model {
#[serde(rename = "gpt-4o-2024-11-20")]
Gpt4o_2024_11_20,
#[serde(rename = "gpt-4o-2024-08-06")]
Gpt4o_2024_08_06,
#[serde(rename = "gpt-4o")]
Gpt4o,
#[serde(rename = "gpt-4o-mini")]
Gpt4oMini,
}
pub use standard::{ChatCompletion, ChatCompletionBuilder};
pub use structured::StructuredChatCompletion;
use crate::{OpenAIError, OpenAIResult};
#[derive(Serialize, Debug, Deserialize, Clone, Copy)]
#[serde(rename_all = "snake_case")]
pub enum Role {
System,
User,
Assistant,
}
#[derive(Serialize, Debug, Builder)]
#[builder(start_fn = role)]
pub struct Message<'a> {
#[builder(start_fn)]
role: Role,
content: Cow<'a, str>,
refusal: Option<&'a str>,
name: Option<Cow<'a, str>>,
}
#[macro_export]
macro_rules! system_message {
($($arg:tt)*) => {
::kind_openai::endpoints::chat::Message::role(
::kind_openai::endpoints::chat::Role::System
)
.content(format!($($arg)*).into())
.build();
};
}
#[macro_export]
macro_rules! user_message {
($($arg:tt)*) => {
::kind_openai::endpoints::chat::Message::role(
::kind_openai::endpoints::chat::Role::User
)
.content(format!($($arg)*).into())
.build();
};
}
#[macro_export]
macro_rules! assistant_message {
($($arg:tt)*) => {
::kind_openai::endpoints::chat::Message::role(
::kind_openai::endpoints::chat::Role::Assistant
)
.content(format!($($arg)*).into())
.build();
};
}
pub struct UnifiedChatCompletionResponseMessage<T> {
content: T,
refusal: Option<String>,
}
impl<T> From<UnifiedChatCompletionResponseMessage<T>> for OpenAIResult<T> {
fn from(value: UnifiedChatCompletionResponseMessage<T>) -> Self {
match value.refusal {
Some(refusal) => Err(OpenAIError::Refusal(refusal)),
None => Ok(value.content),
}
}
}
#[derive(Deserialize, Clone, Copy)]
#[serde(rename_all = "snake_case")]
pub enum FinishReason {
Stop,
Length,
ContentFilter,
ToolCalls,
}