1use crate::types::{Choice, Usage, UsageStatus};
2use serde::{Deserialize, Serialize};
3
4#[derive(Debug, Clone, Serialize, Deserialize)]
5pub struct ChatCompletionResponse {
6 pub id: String,
7 pub object: String,
8 pub created: u64,
9 pub model: String,
10 pub choices: Vec<Choice>,
11 pub usage: Usage,
12 pub usage_status: UsageStatus,
14}
15
16impl ChatCompletionResponse {
17 pub fn first_text(&self) -> Result<&str, crate::types::AiLibError> {
20 let choice = self.choices.first().ok_or_else(|| {
21 crate::types::AiLibError::InvalidModelResponse("empty choices".into())
22 })?;
23 match &choice.message.content {
24 crate::types::common::Content::Text(t) => Ok(t.as_str()),
25 other => Err(crate::types::AiLibError::InvalidModelResponse(format!(
26 "expected text content, got {:?}",
27 other
28 ))),
29 }
30 }
31}