async_openai/
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    /// Deletes a model response with the given ID.
85    #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
86    pub async fn delete(&self, response_id: &str) -> Result<DeleteResponse, OpenAIError> {
87        self.client
88            .delete(
89                &format!("/responses/{}", response_id),
90                &self.request_options,
91            )
92            .await
93    }
94
95    /// Cancels a model response with the given ID. Only responses created with the
96    /// `background` parameter set to `true` can be cancelled.
97    /// [Learn more](https://platform.openai.com/docs/guides/background).
98    #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
99    pub async fn cancel(&self, response_id: &str) -> Result<Response, OpenAIError> {
100        self.client
101            .post(
102                &format!("/responses/{}/cancel", response_id),
103                serde_json::json!({}),
104                &self.request_options,
105            )
106            .await
107    }
108
109    /// Returns a list of input items for a given response.
110    #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
111    pub async fn list_input_items(
112        &self,
113        response_id: &str,
114    ) -> Result<ResponseItemList, OpenAIError> {
115        self.client
116            .get(
117                &format!("/responses/{}/input_items", response_id),
118                &self.request_options,
119            )
120            .await
121    }
122
123    /// Get input token counts
124    #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)]
125    pub async fn get_input_token_counts(
126        &self,
127        request: TokenCountsBody,
128    ) -> Result<TokenCountsResource, OpenAIError> {
129        self.client
130            .post("/responses/input_tokens", request, &self.request_options)
131            .await
132    }
133}