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) -> Option<String> {
19        Some(
20            self.message
21            .as_ref()
22            .map_or_else(
23                || self.text.clone().unwrap_or_default(),
24                |msg| msg.content.clone(),
25            )
26        )
27    }
28
29    /// Returns mutable response string
30    pub fn text_mut(&mut self) -> Option<&mut String> {
31        self.message
32            .as_mut()
33            .map_or_else(
34                || self.text.as_mut(),
35                |msg| Some(&mut msg.content),
36            )
37    }
38}
39
40
41// Response choice stream
42#[derive(Debug, Clone, Deserialize)]
43pub struct StreamChoice {
44    pub delta: Delta,
45}
46
47impl StreamChoice {
48    /// Returns response string
49    pub fn text(&self) -> Option<&String> {
50        self.delta.content.as_ref()
51    }
52
53    /// Returns mutable response string
54    pub fn text_mut(&mut self) -> Option<&mut String> {
55        self.delta.content.as_mut()
56    }
57}