async_openai/responses/
responses_.rs

1use 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    /// Constructs a new Responses client.
18    pub fn new(client: &'c Client<C>) -> Self {
19        Self {
20            client,
21            request_options: RequestOptions::new(),
22        }
23    }
24
25    /// Creates a model response. Provide [text](https://platform.openai.com/docs/guides/text) or
26    /// [image](https://platform.openai.com/docs/guides/images) inputs to generate
27    /// [text](https://platform.openai.com/docs/guides/text) or
28    /// [JSON](https://platform.openai.com/docs/guides/structured-outputs) outputs. Have the model call
29    /// your own [custom code](https://platform.openai.com/docs/guides/function-calling) or use
30    /// built-in [tools](https://platform.openai.com/docs/guides/tools) like
31    /// [web search](https://platform.openai.com/docs/guides/tools-web-search)
32    /// or [file search](https://platform.openai.com/docs/guides/tools-file-search) to use your own data
33    /// as input for the model's response.
34    #[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    /// Creates a model response for the given input with streaming.
45    ///
46    /// Response events will be sent as server-sent events as they become available,
47    #[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    /// Retrieves a model response with the given ID.
74    #[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    /// 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
103    /// Deletes a model response with the given ID.
104    #[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    /// Cancels a model response with the given ID. Only responses created with the
115    /// `background` parameter set to `true` can be cancelled.
116    /// [Learn more](https://platform.openai.com/docs/guides/background).
117    #[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    /// Returns a list of input items for a given response.
129    #[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    /// Get input token counts
143    #[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}