langchain_rust/schemas/
stream.rs1use serde_json::Value;
2use std::io::{self, Write};
3
4use crate::language_models::TokenUsage;
5
6#[derive(Debug, Clone)]
7pub struct StreamData {
8 pub value: Value,
9 pub tokens: Option<TokenUsage>,
10 pub content: String,
11}
12
13impl StreamData {
14 pub fn new<S: Into<String>>(value: Value, tokens: Option<TokenUsage>, content: S) -> Self {
15 Self {
16 value,
17 tokens,
18 content: content.into(),
19 }
20 }
21
22 pub fn to_stdout(&self) -> io::Result<()> {
23 let stdout = io::stdout();
24 let mut handle = stdout.lock();
25 write!(handle, "{}", self.content)?;
26 handle.flush()
27 }
28}