ai_lib/types/
response.rs

1use crate::types::{Choice, Usage};
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}
13
14impl ChatCompletionResponse {
15    /// Get the first textual content from the first choice.
16    /// Returns an error if choices are empty or content is non-text.
17    pub fn first_text(&self) -> Result<&str, crate::types::AiLibError> {
18        let choice = self
19            .choices
20            .first()
21            .ok_or_else(|| crate::types::AiLibError::InvalidModelResponse("empty choices".into()))?;
22        match &choice.message.content {
23            crate::types::common::Content::Text(t) => Ok(t.as_str()),
24            other => Err(crate::types::AiLibError::InvalidModelResponse(format!(
25                "expected text content, got {:?}", other
26            ))),
27        }
28    }
29}