ai_lib/types/
response.rs

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    /// Indicates the reliability and source of usage data
13    pub usage_status: UsageStatus,
14}
15
16impl ChatCompletionResponse {
17    /// Get the first textual content from the first choice.
18    /// Returns an error if choices are empty or content is non-text.
19    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}