async_openai_alt/
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.
23    pub async fn create(
24        &self,
25        request: CreateChatCompletionRequest,
26    ) -> Result<CreateChatCompletionResponse, OpenAIError> {
27        if request.stream.is_some() && request.stream.unwrap() {
28            return Err(OpenAIError::InvalidArgument(
29                "When stream is true, use Chat::create_stream".into(),
30            ));
31        }
32        self.client.post("/chat/completions", request).await
33    }
34
35    /// Creates a completion for the chat message
36    ///
37    /// 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.
38    ///
39    /// [ChatCompletionResponseStream] is a parsed SSE stream until a \[DONE\] is received from server.
40    pub async fn create_stream(
41        &self,
42        mut request: CreateChatCompletionRequest,
43    ) -> Result<ChatCompletionResponseStream, OpenAIError> {
44        if request.stream.is_some() && !request.stream.unwrap() {
45            return Err(OpenAIError::InvalidArgument(
46                "When stream is false, use Chat::create".into(),
47            ));
48        }
49
50        request.stream = Some(true);
51
52        Ok(self.client.post_stream("/chat/completions", request).await)
53    }
54}