lm_studio_api/chat/
response.rs1use crate::prelude::*;
2use super::{ Choice, StreamChoice, Usage, Role, Message };
3
4#[derive(Debug, Clone, Serialize, 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 #[serde(skip_serializing_if = "Option::is_none")]
16 pub system_fingerprint: Option<String>,
17}
18
19impl Response {
20 pub fn text(&self) -> String {
22 self.choices[0].text().clone().unwrap()
23 }
24}
25
26
27use futures::StreamExt;
28use tokio_stream::wrappers::UnboundedReceiverStream;
29
30#[derive(Debug)]
32pub struct ResponseReader {
33 pub receiver: UnboundedReceiverStream<Result<StreamChoice>>,
34 pub message: Message,
35 pub is_ready: bool,
36 pub context: bool
37}
38
39impl ResponseReader {
40 pub fn new(receiver: UnboundedReceiverStream<Result<StreamChoice>>, context: bool) -> Self {
42 Self {
43 receiver,
44 message: Message { role: Role::Assistant, content: str!("") },
45 is_ready: false,
46 context
47 }
48 }
49
50 pub async fn next(&mut self) -> Option<Result<StreamChoice>> {
52 let result = self.receiver.next().await;
53
54 match result {
55 Some(result) => {
56 match result {
57 Ok(choice) => {
58 if let Some(text) = choice.text() {
59 self.message.content.push_str(&text);
60 }
61
62 Some(Ok(choice))
63 },
64
65 Err(e) => Some(Err(e))
66 }
67 },
68
69 _ => {
70 self.is_ready = true;
71 None
72 }
73 }
74 }
75}