Skip to content

Commit

Permalink
stream mapping function handles errors better
Browse files Browse the repository at this point in the history
  • Loading branch information
jpalvarezl committed Sep 18, 2024
1 parent d7834a1 commit 445607f
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions sdk/openai/inference/src/clients/chat_completions_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use azure_core::{
headers::{ACCEPT, CONTENT_TYPE},
Context, Method, Response, Result,
};
use futures::{Stream, TryStreamExt};
use futures::{Stream, StreamExt};

pub trait ChatCompletionsClientMethods {
#[allow(async_fn_in_trait)]
Expand Down Expand Up @@ -101,11 +101,11 @@ impl EventStreamer<CreateChatCompletionsStreamResponse> for ChatCompletionsStrea
response_body: azure_core::ResponseBody,
) -> impl Stream<Item = Result<CreateChatCompletionsStreamResponse>> {
let stream_event_delimiter = "\n\n";
// TODO: is there something like try_map_ok?
let stream = string_chunks(response_body, stream_event_delimiter).map_ok(|event| {
serde_json::from_str::<CreateChatCompletionsStreamResponse>(&event)
.expect("Deserialization failed")
});
stream

string_chunks(response_body, stream_event_delimiter).map(|event| match event {
Ok(event) => serde_json::from_str::<CreateChatCompletionsStreamResponse>(&event)
.map_err(|e| e.into()),
Err(e) => Err(e),
})
}
}

0 comments on commit 445607f

Please sign in to comment.