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 pub fn first_text(&self) -> Result<&str, crate::types::AiLibError> {
18 let choice = self.choices.first().ok_or_else(|| {
19 crate::types::AiLibError::InvalidModelResponse("empty choices".into())
20 })?;
21 match &choice.message.content {
22 crate::types::common::Content::Text(t) => Ok(t.as_str()),
23 other => Err(crate::types::AiLibError::InvalidModelResponse(format!(
24 "expected text content, got {:?}",
25 other
26 ))),
27 }
28 }
29}