mistralai_client/v1/
chat_stream.rs

1use serde::{Deserialize, Serialize};
2use serde_json::from_str;
3
4use crate::v1::{chat, common, constants, error};
5
6// -----------------------------------------------------------------------------
7// Response
8
9#[derive(Clone, Debug, Deserialize, Serialize)]
10pub struct ChatStreamChunk {
11    pub id: String,
12    pub object: String,
13    /// Unix timestamp (in seconds).
14    pub created: u32,
15    pub model: constants::Model,
16    pub choices: Vec<ChatStreamChunkChoice>,
17    pub usage: Option<common::ResponseUsage>,
18    // TODO Check this prop (seen in API responses but undocumented).
19    // pub logprobs: ???,
20}
21
22#[derive(Clone, Debug, Deserialize, Serialize)]
23pub struct ChatStreamChunkChoice {
24    pub index: u32,
25    pub delta: ChatStreamChunkChoiceDelta,
26    pub finish_reason: Option<String>,
27    // TODO Check this prop (seen in API responses but undocumented).
28    // pub logprobs: ???,
29}
30
31#[derive(Clone, Debug, Deserialize, Serialize)]
32pub struct ChatStreamChunkChoiceDelta {
33    pub role: Option<chat::ChatMessageRole>,
34    pub content: String,
35}
36
37/// Extracts serialized chunks from a stream message.
38pub fn get_chunk_from_stream_message_line(
39    line: &str,
40) -> Result<Option<Vec<ChatStreamChunk>>, error::ApiError> {
41    if line.trim() == "data: [DONE]" {
42        return Ok(None);
43    }
44
45    let chunk_as_json = line.trim_start_matches("data: ").trim();
46    if chunk_as_json.is_empty() {
47        return Ok(Some(vec![]));
48    }
49
50    // Attempt to deserialize the JSON string into ChatStreamChunk
51    match from_str::<ChatStreamChunk>(chunk_as_json) {
52        Ok(chunk) => Ok(Some(vec![chunk])),
53        Err(e) => Err(error::ApiError {
54            message: e.to_string(),
55        }),
56    }
57}