lm_studio_api/chat/
response.rs

1use crate::prelude::*;
2use super::{ Choice, StreamChoice, Usage, Role, Message };
3
4// Chat response
5#[derive(Debug, Clone, Deserialize)]
6pub struct Response {
7    pub id: String,
8    pub object: String,
9    pub created: u64,
10    pub model: String,
11    pub choices: Vec<Choice>,
12    pub usage: Usage,
13    #[serde(default)]
14    pub stats: HashMap<String, serde_json::Value>,
15    pub system_fingerprint: String,
16}
17
18impl Response {
19    // Get reponse text
20    pub fn text(&self) -> &str {
21        &self.choices[0].message.content
22    }
23}
24
25
26use futures::StreamExt;
27use tokio_stream::wrappers::UnboundedReceiverStream;
28
29// Chat response stream
30#[derive(Debug)]
31pub struct ResponseReader {
32    pub receiver: UnboundedReceiverStream<std::result::Result<StreamChoice, reqwest::Error>>,
33    pub message: Message,
34    pub is_ready: bool,
35    pub context: bool
36}
37
38impl ResponseReader {
39    pub fn new(receiver: UnboundedReceiverStream<std::result::Result<StreamChoice, reqwest::Error>>, context: bool) -> Self {
40        Self {
41            receiver,
42            message: Message { role: Role::Assistant, content: str!("") },
43            is_ready: false,
44            context
45        }
46    }
47
48    pub async fn next(&mut self) -> Option<std::result::Result<String, reqwest::Error>> {
49        let result = self.receiver.next().await;
50
51        match result {
52            Some(result) => {
53                match result {
54                    Ok(choice) => {
55                        if let Some(text) = choice.delta.content {
56                            self.message.content.push_str(&text);
57
58                            Some(Ok(text))
59                        } else {
60                            Some(Ok(String::new()))
61                        }
62                    },
63
64                    Err(e) => Some(Err(e))
65                }
66            },
67
68            _ => {
69                self.is_ready = true;
70                
71                None
72            }
73        }
74    }
75}