Skip to content

Commit cbdf6c6

Browse files
committed
support for GET call to return a stream; use it in Responses GET with stream
1 parent 5092e5b commit cbdf6c6

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

async-openai/src/client.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -544,23 +544,18 @@ impl<C: Config> Client<C> {
544544
}
545545

546546
/// Make HTTP GET request to receive SSE
547-
pub(crate) async fn _get_stream<Q, O>(
547+
pub(crate) async fn get_stream<O>(
548548
&self,
549549
path: &str,
550-
query: &Q,
550+
request_options: &RequestOptions,
551551
) -> Pin<Box<dyn Stream<Item = Result<O, OpenAIError>> + Send>>
552552
where
553-
Q: Serialize + ?Sized,
554553
O: DeserializeOwned + std::marker::Send + 'static,
555554
{
556-
let event_source = self
557-
.http_client
558-
.get(self.config.url(path))
559-
.query(query)
560-
.query(&self.config.query())
561-
.headers(self.config.headers())
562-
.eventsource()
563-
.unwrap();
555+
let request_builder =
556+
self.build_request_builder(reqwest::Method::GET, path, request_options);
557+
558+
let event_source = request_builder.eventsource().unwrap();
564559

565560
stream(event_source).await
566561
}

async-openai/src/responses.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,25 @@ impl<'c, C: Config> Responses<'c, C> {
8181
.await
8282
}
8383

84+
/// Retrieves a model response with the given ID with streaming.
85+
///
86+
/// Response events will be sent as server-sent events as they become available.
87+
#[crate::byot(
88+
T0 = std::fmt::Display,
89+
R = serde::de::DeserializeOwned,
90+
stream = "true",
91+
where_clause = "R: std::marker::Send + 'static"
92+
)]
93+
pub async fn retrieve_stream(&self, response_id: &str) -> Result<ResponseStream, OpenAIError> {
94+
let mut request_options = self.request_options.clone();
95+
request_options.with_query(&[("stream", "true")])?;
96+
97+
Ok(self
98+
.client
99+
.get_stream(&format!("/responses/{}", response_id), &request_options)
100+
.await)
101+
}
102+
84103
/// Deletes a model response with the given ID.
85104
#[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
86105
pub async fn delete(&self, response_id: &str) -> Result<DeleteResponse, OpenAIError> {

0 commit comments

Comments
 (0)