Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions async-openai/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,12 @@ impl<C: Config> Client<C> {
let message: String = String::from_utf8_lossy(&bytes).into_owned();
tracing::warn!("Server error: {status} - {message}");
return Err(backoff::Error::Transient {
err: OpenAIError::ApiError(ApiError { message, r#type: None, param: None, code: None }),
err: OpenAIError::ApiError(ApiError {
message,
r#type: None,
param: None,
code: None,
}),
retry_after: None,
});
}
Expand Down Expand Up @@ -475,7 +480,7 @@ where
while let Some(ev) = event_source.next().await {
match ev {
Err(e) => {
if let Err(_e) = tx.send(Err(OpenAIError::StreamError(e.to_string()))) {
if let Err(_e) = tx.send(Err(OpenAIError::StreamError(e))) {
// rx dropped
break;
}
Expand Down Expand Up @@ -520,7 +525,7 @@ where
while let Some(ev) = event_source.next().await {
match ev {
Err(e) => {
if let Err(_e) = tx.send(Err(OpenAIError::StreamError(e.to_string()))) {
if let Err(_e) = tx.send(Err(OpenAIError::StreamError(e))) {
// rx dropped
break;
}
Expand Down
1 change: 0 additions & 1 deletion async-openai/src/embedding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ impl<'c, C: Config> Embeddings<'c, C> {

#[cfg(test)]
mod tests {
use crate::error::OpenAIError;
use crate::types::{CreateEmbeddingResponse, Embedding, EncodingFormat};
use crate::{types::CreateEmbeddingRequestArgs, Client};

Expand Down
2 changes: 1 addition & 1 deletion async-openai/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub enum OpenAIError {
FileReadError(String),
/// Error on SSE streaming
#[error("stream failed: {0}")]
StreamError(String),
StreamError(reqwest_eventsource::Error),
/// Error from client side validation
/// or when builder fails to build request before making API call
#[error("invalid args: {0}")]
Expand Down
3 changes: 3 additions & 0 deletions async-openai/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,6 @@ pub use users::Users;
pub use vector_store_file_batches::VectorStoreFileBatches;
pub use vector_store_files::VectorStoreFiles;
pub use vector_stores::VectorStores;

// re-exports
pub use reqwest_eventsource;
29 changes: 24 additions & 5 deletions async-openai/src/types/assistant_stream.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::pin::Pin;
use std::{pin::Pin, time::Duration};

use futures::Stream;
use serde::Deserialize;
Expand Down Expand Up @@ -108,13 +108,22 @@ pub enum AssistantStreamEvent {
/// Occurs when a stream ends.
#[serde(rename = "done")]
Done(String),
/// Unknown event type.
#[serde(rename = "unknown")]
Unknown {
event: String,
data: String,
id: String,
retry: Option<Duration>,
},
}

pub type AssistantEventStream =
Pin<Box<dyn Stream<Item = Result<AssistantStreamEvent, OpenAIError>> + Send>>;

impl TryFrom<eventsource_stream::Event> for AssistantStreamEvent {
type Error = OpenAIError;

fn try_from(value: eventsource_stream::Event) -> Result<Self, Self::Error> {
match value.event.as_str() {
"thread.created" => serde_json::from_str::<ThreadObject>(value.data.as_str())
Expand Down Expand Up @@ -206,10 +215,20 @@ impl TryFrom<eventsource_stream::Event> for AssistantStreamEvent {
.map_err(|e| map_deserialization_error(e, value.data.as_bytes()))
.map(AssistantStreamEvent::ErrorEvent),
"done" => Ok(AssistantStreamEvent::Done(value.data)),

_ => Err(OpenAIError::StreamError(
"Unrecognized event: {value:?#}".into(),
)),
_ => {
let eventsource_stream::Event {
id,
event,
data,
retry,
} = value;
Ok(AssistantStreamEvent::Unknown {
event,
data,
id,
retry,
})
}
}
}
}
Loading