1
1
use crate :: {
2
2
config:: Config ,
3
3
error:: OpenAIError ,
4
- types:: responses:: { CreateResponse , Response } ,
4
+ types:: responses:: { CreateResponse , Response , ResponseStream } ,
5
5
Client ,
6
6
} ;
7
7
8
8
/// Given text input or a list of context items, the model will generate a response.
9
9
///
10
- /// Related guide: [Responses API ](https://platform.openai.com/docs/guides /responses)
10
+ /// Related guide: [Responses](https://platform.openai.com/docs/api-reference /responses)
11
11
pub struct Responses < ' c , C : Config > {
12
12
client : & ' c Client < C > ,
13
13
}
@@ -26,4 +26,30 @@ impl<'c, C: Config> Responses<'c, C> {
26
26
pub async fn create ( & self , request : CreateResponse ) -> Result < Response , OpenAIError > {
27
27
self . client . post ( "/responses" , request) . await
28
28
}
29
+
30
+ /// Creates a model response for the given input with streaming.
31
+ ///
32
+ /// Response events will be sent as server-sent events as they become available,
33
+ #[ crate :: byot(
34
+ T0 = serde:: Serialize ,
35
+ R = serde:: de:: DeserializeOwned ,
36
+ stream = "true" ,
37
+ where_clause = "R: std::marker::Send + 'static"
38
+ ) ]
39
+ #[ allow( unused_mut) ]
40
+ pub async fn create_stream (
41
+ & self ,
42
+ mut request : CreateResponse ,
43
+ ) -> Result < ResponseStream , OpenAIError > {
44
+ #[ cfg( not( feature = "byot" ) ) ]
45
+ {
46
+ if matches ! ( request. stream, Some ( false ) ) {
47
+ return Err ( OpenAIError :: InvalidArgument (
48
+ "When stream is false, use Responses::create" . into ( ) ,
49
+ ) ) ;
50
+ }
51
+ request. stream = Some ( true ) ;
52
+ }
53
+ Ok ( self . client . post_stream ( "/responses" , request) . await )
54
+ }
29
55
}
0 commit comments