async_openai/
chat.rs

1use crate::{
2    config::Config,
3    error::OpenAIError,
4    types::{
5        ChatCompletionResponseStream, CreateChatCompletionRequest, CreateChatCompletionResponse,
6    },
7    Client,
8};
9
10/// Given a list of messages comprising a conversation, the model will return a response.
11///
12/// Related guide: [Chat completions](https://platform.openai.com//docs/guides/text-generation)
13pub struct Chat<'c, C: Config> {
14    client: &'c Client<C>,
15}
16
17impl<'c, C: Config> Chat<'c, C> {
18    pub fn new(client: &'c Client<C>) -> Self {
19        Self { client }
20    }
21
22    /// Creates a model response for the given chat conversation. Learn more in
23    /// the
24    ///
25    /// [text generation](https://platform.openai.com/docs/guides/text-generation),
26    /// [vision](https://platform.openai.com/docs/guides/vision),
27    ///
28    /// and [audio](https://platform.openai.com/docs/guides/audio) guides.
29    ///
30    ///
31    /// Parameter support can differ depending on the model used to generate the
32    /// response, particularly for newer reasoning models. Parameters that are
33    /// only supported for reasoning models are noted below. For the current state
34    /// of unsupported parameters in reasoning models,
35    ///
36    /// [refer to the reasoning guide](https://platform.openai.com/docs/guides/reasoning).
37    ///
38    /// byot: You must ensure "stream: false" in serialized `request`
39    #[crate::byot(
40        T0 = serde::Serialize,
41        R = serde::de::DeserializeOwned
42    )]
43    pub async fn create(
44        &self,
45        request: CreateChatCompletionRequest,
46    ) -> Result<CreateChatCompletionResponse, OpenAIError> {
47        #[cfg(not(feature = "byot"))]
48        {
49            if request.stream.is_some() && request.stream.unwrap() {
50                return Err(OpenAIError::InvalidArgument(
51                    "When stream is true, use Chat::create_stream".into(),
52                ));
53            }
54        }
55        self.client.post("/chat/completions", request).await
56    }
57
58    /// Creates a completion for the chat message
59    ///
60    /// partial message deltas will be sent, like in ChatGPT. Tokens will be sent as data-only [server-sent events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Event_stream_format) as they become available, with the stream terminated by a `data: [DONE]` message.
61    ///
62    /// [ChatCompletionResponseStream] is a parsed SSE stream until a \[DONE\] is received from server.
63    ///
64    /// byot: You must ensure "stream: true" in serialized `request`
65    #[crate::byot(
66        T0 = serde::Serialize,
67        R = serde::de::DeserializeOwned,
68        stream = "true",
69        where_clause = "R: std::marker::Send + 'static"
70    )]
71    #[allow(unused_mut)]
72    pub async fn create_stream(
73        &self,
74        mut request: CreateChatCompletionRequest,
75    ) -> Result<ChatCompletionResponseStream, OpenAIError> {
76        #[cfg(not(feature = "byot"))]
77        {
78            if request.stream.is_some() && !request.stream.unwrap() {
79                return Err(OpenAIError::InvalidArgument(
80                    "When stream is false, use Chat::create".into(),
81                ));
82            }
83
84            request.stream = Some(true);
85        }
86        Ok(self.client.post_stream("/chat/completions", request).await)
87    }
88}