async_openai/responses/
responses_.rs1use crate::{
2 config::Config,
3 error::OpenAIError,
4 types::responses::{
5 CreateResponse, DeleteResponse, Response, ResponseItemList, ResponseStream,
6 TokenCountsBody, TokenCountsResource,
7 },
8 Client, RequestOptions,
9};
10
11pub struct Responses<'c, C: Config> {
12 client: &'c Client<C>,
13 pub(crate) request_options: RequestOptions,
14}
15
16impl<'c, C: Config> Responses<'c, C> {
17 pub fn new(client: &'c Client<C>) -> Self {
19 Self {
20 client,
21 request_options: RequestOptions::new(),
22 }
23 }
24
25 #[crate::byot(
35 T0 = serde::Serialize,
36 R = serde::de::DeserializeOwned
37 )]
38 pub async fn create(&self, request: CreateResponse) -> Result<Response, OpenAIError> {
39 self.client
40 .post("/responses", request, &self.request_options)
41 .await
42 }
43
44 #[crate::byot(
48 T0 = serde::Serialize,
49 R = serde::de::DeserializeOwned,
50 stream = "true",
51 where_clause = "R: std::marker::Send + 'static"
52 )]
53 #[allow(unused_mut)]
54 pub async fn create_stream(
55 &self,
56 mut request: CreateResponse,
57 ) -> Result<ResponseStream, OpenAIError> {
58 #[cfg(not(feature = "byot"))]
59 {
60 if matches!(request.stream, Some(false)) {
61 return Err(OpenAIError::InvalidArgument(
62 "When stream is false, use Responses::create".into(),
63 ));
64 }
65 request.stream = Some(true);
66 }
67 Ok(self
68 .client
69 .post_stream("/responses", request, &self.request_options)
70 .await)
71 }
72
73 #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
75 pub async fn retrieve(&self, response_id: &str) -> Result<Response, OpenAIError> {
76 self.client
77 .get(
78 &format!("/responses/{}", response_id),
79 &self.request_options,
80 )
81 .await
82 }
83
84 #[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
103 #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
105 pub async fn delete(&self, response_id: &str) -> Result<DeleteResponse, OpenAIError> {
106 self.client
107 .delete(
108 &format!("/responses/{}", response_id),
109 &self.request_options,
110 )
111 .await
112 }
113
114 #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
118 pub async fn cancel(&self, response_id: &str) -> Result<Response, OpenAIError> {
119 self.client
120 .post(
121 &format!("/responses/{}/cancel", response_id),
122 serde_json::json!({}),
123 &self.request_options,
124 )
125 .await
126 }
127
128 #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
130 pub async fn list_input_items(
131 &self,
132 response_id: &str,
133 ) -> Result<ResponseItemList, OpenAIError> {
134 self.client
135 .get(
136 &format!("/responses/{}/input_items", response_id),
137 &self.request_options,
138 )
139 .await
140 }
141
142 #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)]
144 pub async fn get_input_token_counts(
145 &self,
146 request: TokenCountsBody,
147 ) -> Result<TokenCountsResource, OpenAIError> {
148 self.client
149 .post("/responses/input_tokens", request, &self.request_options)
150 .await
151 }
152}