use openai_client_base::models::{
ChatCompletionMessageToolCallsInner, ChatCompletionResponseMessage,
CreateChatCompletionResponse, CreateChatCompletionResponseChoicesInner,
};
pub trait ChatCompletionResponseExt {
fn content(&self) -> Option<&str>;
fn tool_calls(&self) -> Vec<&ChatCompletionMessageToolCallsInner>;
fn has_tool_calls(&self) -> bool;
fn first_choice(&self) -> Option<&CreateChatCompletionResponseChoicesInner>;
fn first_message(&self) -> Option<&ChatCompletionResponseMessage>;
fn is_refusal(&self) -> bool;
fn refusal(&self) -> Option<&str>;
fn finish_reason(&self) -> Option<String>;
}
impl ChatCompletionResponseExt for CreateChatCompletionResponse {
fn content(&self) -> Option<&str> {
self.choices
.first()
.and_then(|choice| choice.message.content.as_deref())
}
fn tool_calls(&self) -> Vec<&ChatCompletionMessageToolCallsInner> {
self.choices
.first()
.and_then(|choice| choice.message.tool_calls.as_ref())
.map(|calls| calls.iter().collect())
.unwrap_or_default()
}
fn has_tool_calls(&self) -> bool {
!self.tool_calls().is_empty()
}
fn first_choice(&self) -> Option<&CreateChatCompletionResponseChoicesInner> {
self.choices.first()
}
fn first_message(&self) -> Option<&ChatCompletionResponseMessage> {
self.first_choice().map(|choice| choice.message.as_ref())
}
fn is_refusal(&self) -> bool {
self.first_message()
.and_then(|msg| msg.refusal.as_ref())
.is_some()
}
fn refusal(&self) -> Option<&str> {
self.first_message()
.and_then(|msg| msg.refusal.as_ref())
.map(std::string::String::as_str)
}
fn finish_reason(&self) -> Option<String> {
use openai_client_base::models::create_chat_completion_response_choices_inner::FinishReason;
self.first_choice()
.map(|choice| match &choice.finish_reason {
FinishReason::Stop => "stop".to_string(),
FinishReason::Length => "length".to_string(),
FinishReason::ToolCalls => "tool_calls".to_string(),
FinishReason::ContentFilter => "content_filter".to_string(),
FinishReason::FunctionCall => "function_call".to_string(),
})
}
}
pub trait ToolCallExt {
fn id(&self) -> &str;
fn function_name(&self) -> &str;
fn function_arguments(&self) -> &str;
fn parse_arguments<T: serde::de::DeserializeOwned>(&self) -> Result<T, serde_json::Error>;
}
impl ToolCallExt for ChatCompletionMessageToolCallsInner {
fn id(&self) -> &str {
match self {
ChatCompletionMessageToolCallsInner::ChatCompletionMessageToolCall(tool_call) => {
&tool_call.id
}
ChatCompletionMessageToolCallsInner::ChatCompletionMessageCustomToolCall(tool_call) => {
&tool_call.id
}
}
}
fn function_name(&self) -> &str {
match self {
ChatCompletionMessageToolCallsInner::ChatCompletionMessageToolCall(tool_call) => {
&tool_call.function.name
}
ChatCompletionMessageToolCallsInner::ChatCompletionMessageCustomToolCall(_) => {
""
}
}
}
fn function_arguments(&self) -> &str {
match self {
ChatCompletionMessageToolCallsInner::ChatCompletionMessageToolCall(tool_call) => {
&tool_call.function.arguments
}
ChatCompletionMessageToolCallsInner::ChatCompletionMessageCustomToolCall(_) => {
""
}
}
}
fn parse_arguments<T: serde::de::DeserializeOwned>(&self) -> Result<T, serde_json::Error> {
serde_json::from_str(self.function_arguments())
}
}
pub use openai_client_base::models::{
ChatCompletionResponseMessage as ChatMessage,
CreateChatCompletionResponse as ChatCompletionResponse,
CreateChatCompletionResponseChoicesInner as ChatChoice,
};
pub use openai_client_base::models::{
ChatCompletionMessageToolCallFunction as FunctionCall,
ChatCompletionResponseMessageFunctionCall,
};
pub use openai_client_base::models::ChatCompletionMessageToolCallsInner as ToolCall;