pub struct TextStream { /* private fields */ }Expand description
A streaming answer, read one delta at a time.
ⓘ
let mut stream = ai.prompt("Tell me a story").stream().await?;
while let Some(delta) = stream.next().await? {
print!("{delta}");
}Implementations§
Source§impl TextStream
impl TextStream
Sourcepub fn events(body: Body, decode: Decoder) -> TextStream
pub fn events(body: Body, decode: Decoder) -> TextStream
A stream over server-sent events.
Sourcepub fn lines(body: Body, decode: Decoder) -> TextStream
pub fn lines(body: Body, decode: Decoder) -> TextStream
A stream over newline-delimited JSON.
Sourcepub fn scripted(deltas: impl IntoIterator<Item = String>) -> TextStream
pub fn scripted(deltas: impl IntoIterator<Item = String>) -> TextStream
A stream of deltas decided in advance, for fakes and tests.
Sourcepub fn measured(
self,
provider: &'static str,
model: impl Into<String>,
) -> TextStream
pub fn measured( self, provider: &'static str, model: impl Into<String>, ) -> TextStream
Report an ai.call event when this stream runs out.
Only then are the token counts known: providers put them in the last frame, so a stream cannot be measured when it is opened.
Sourcepub async fn next(&mut self) -> Result<Option<String>>
pub async fn next(&mut self) -> Result<Option<String>>
The next piece of text, or None at the end of the answer.
Frames that carry only bookkeeping are consumed silently, so a caller
never has to know what a message_delta is.
Sourcepub async fn collect(&mut self) -> Result<String>
pub async fn collect(&mut self) -> Result<String>
Read the whole answer, for a caller who wanted streaming for the latency rather than for the display.
Sourcepub fn usage(&self) -> Usage
pub fn usage(&self) -> Usage
The tokens reported so far. Complete only once the stream has ended.
pub fn stop_reason(&self) -> Option<&StopReason>
Auto Trait Implementations§
impl !RefUnwindSafe for TextStream
impl !UnwindSafe for TextStream
impl Freeze for TextStream
impl Send for TextStream
impl Sync for TextStream
impl Unpin for TextStream
impl UnsafeUnpin for TextStream
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more