Skip to main content

TextStream

Struct TextStream 

Source
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

Source

pub fn events(body: Body, decode: Decoder) -> TextStream

A stream over server-sent events.

Source

pub fn lines(body: Body, decode: Decoder) -> TextStream

A stream over newline-delimited JSON.

Source

pub fn scripted(deltas: impl IntoIterator<Item = String>) -> TextStream

A stream of deltas decided in advance, for fakes and tests.

Source

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.

Source

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.

Source

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.

Source

pub fn usage(&self) -> Usage

The tokens reported so far. Complete only once the stream has ended.

Source

pub fn stop_reason(&self) -> Option<&StopReason>

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.