lm_studio_api/chat/
choice.rs

1use crate::prelude::*;
2use super::{ Message, Delta };
3
4// Response choice
5#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq)]
6pub struct Choice {
7    pub index: usize,
8    pub logprobs: Option<serde_json::Value>,
9    pub finish_reason: String,
10    #[serde(skip_serializing_if = "Option::is_none")]
11    pub message: Option<Message>,
12    #[serde(skip_serializing_if = "Option::is_none")]
13    pub text: Option<String>,
14}
15
16impl Choice {
17    /// Returns response string
18    pub fn text(&self) -> String {
19        self.message
20            .as_ref()
21            .map_or_else(
22                || self.text.clone().unwrap_or_default(),
23                |msg| msg.text()
24            )
25    }
26}
27
28
29// Response choice stream
30#[derive(Debug, Clone, Deserialize)]
31pub struct StreamChoice {
32    pub delta: Delta,
33}
34
35impl StreamChoice {
36    /// Returns response string
37    pub fn text(&self) -> Option<&String> {
38        self.delta.content.as_ref()
39    }
40
41    /// Returns mutable response string
42    pub fn text_mut(&mut self) -> Option<&mut String> {
43        self.delta.content.as_mut()
44    }
45}