dynamo_async_openai/
responses.rs

1// SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2// SPDX-License-Identifier: Apache-2.0
3//
4// Based on https://github.com/64bit/async-openai/ by Himanshu Neema
5// Original Copyright (c) 2022 Himanshu Neema
6// Licensed under MIT License (see ATTRIBUTIONS-Rust.md)
7//
8// Modifications Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES.
9// Licensed under Apache 2.0
10
11use crate::{
12    config::Config,
13    error::OpenAIError,
14    types::responses::{CreateResponse, Response, ResponseStream},
15    Client,
16};
17
18/// Given text input or a list of context items, the model will generate a response.
19///
20/// Related guide: [Responses](https://platform.openai.com/docs/api-reference/responses)
21pub struct Responses<'c, C: Config> {
22    client: &'c Client<C>,
23}
24
25impl<'c, C: Config> Responses<'c, C> {
26    /// Constructs a new Responses client.
27    pub fn new(client: &'c Client<C>) -> Self {
28        Self { client }
29    }
30
31    /// Creates a model response for the given input.
32    #[crate::byot(
33        T0 = serde::Serialize,
34        R = serde::de::DeserializeOwned
35    )]
36    pub async fn create(&self, request: CreateResponse) -> Result<Response, OpenAIError> {
37        self.client.post("/responses", request).await
38    }
39
40    /// Creates a model response for the given input with streaming.
41    ///
42    /// Response events will be sent as server-sent events as they become available,
43    #[crate::byot(
44        T0 = serde::Serialize,
45        R = serde::de::DeserializeOwned,
46        stream = "true",
47        where_clause = "R: std::marker::Send + 'static"
48    )]
49    #[allow(unused_mut)]
50    pub async fn create_stream(
51        &self,
52        mut request: CreateResponse,
53    ) -> Result<ResponseStream, OpenAIError> {
54        #[cfg(not(feature = "byot"))]
55        {
56            if matches!(request.stream, Some(false)) {
57                return Err(OpenAIError::InvalidArgument(
58                    "When stream is false, use Responses::create".into(),
59                ));
60            }
61            request.stream = Some(true);
62        }
63        Ok(self.client.post_stream("/responses", request).await)
64    }
65}