pub struct OpenAICompletionStream { /* private fields */ }
Expand description
This structs wraps the EventSource and parses returned messages into prompt messages on demand.
Implementations§
Source§impl OpenAICompletionStream
impl OpenAICompletionStream
Sourcepub fn new(event_source: EventSource) -> Self
pub fn new(event_source: EventSource) -> Self
§OpenAICompletionStream::new
This struct just wraps the EventSource when from the context of streaming chat completions.
Trait Implementations§
Source§impl ChatCompletionStream for OpenAICompletionStream
impl ChatCompletionStream for OpenAICompletionStream
Source§async fn next(&mut self) -> Option<Result<Self::Item, Self::ErrorType>>
async fn next(&mut self) -> Option<Result<Self::Item, Self::ErrorType>>
§ChatCompletionStream::next
Method to iterate over the completion stream. Note it blocks
until the next message is received. At which point it will
parse the response into a CompletionStreamValue
.
§Examples
use rag_toolchain::clients::*;
async fn stream_chat_completions(client: OpenAIChatCompletionClient) {
let user_message: PromptMessage = PromptMessage::HumanMessage("Please ask me a question".into());
let mut stream: OpenAICompletionStream = client.invoke_stream(vec![user_message]).await.unwrap();
while let Some(response) = stream.next().await {
match response {
Ok(CompletionStreamValue::Connecting) => {},
Ok(CompletionStreamValue::Message(msg)) => {
println!("{:?}", msg.content());
}
Err(e) => {
println!("{:?}", e);
break;
}
}
}
}
§Errors
OpenAIError::ErrorReadingStream
- if there was an error reading from the stream.OpenAIError::ErrorDeserializingResponseBody
- if there was an error deserializing the response body.
§Returns
Option<Result<CompletionStreamValue, OpenAIError>>
- the response from the chat client. None represents the stream is finished..
type ErrorType = OpenAIError
type Item = CompletionStreamValue
Auto Trait Implementations§
impl !Freeze for OpenAICompletionStream
impl !RefUnwindSafe for OpenAICompletionStream
impl Send for OpenAICompletionStream
impl !Sync for OpenAICompletionStream
impl Unpin for OpenAICompletionStream
impl !UnwindSafe for OpenAICompletionStream
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
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more