Skip to main content

openai_compat/resources/
chat.rs

1//! `POST /chat/completions` resource, mirroring
2//! `resources/chat/completions/completions.py`.
3
4use reqwest::Method;
5
6use crate::client::Client;
7use crate::error::OpenAIError;
8use crate::request::RequestOptions;
9use crate::streaming::EventStream;
10use crate::types::chat::{ChatCompletion, ChatCompletionChunk, ChatCompletionRequest};
11
12/// Accessor for chat endpoints: `client.chat().completions()`.
13#[derive(Debug, Clone)]
14pub struct Chat {
15    client: Client,
16}
17
18impl Chat {
19    pub(crate) fn new(client: Client) -> Self {
20        Self { client }
21    }
22
23    pub fn completions(&self) -> ChatCompletions {
24        ChatCompletions {
25            client: self.client.clone(),
26        }
27    }
28}
29
30/// The chat completions resource.
31#[derive(Debug, Clone)]
32pub struct ChatCompletions {
33    client: Client,
34}
35
36impl ChatCompletions {
37    /// Create a chat completion and wait for the full response.
38    pub async fn create(
39        &self,
40        mut request: ChatCompletionRequest,
41    ) -> Result<ChatCompletion, OpenAIError> {
42        request.stream = None;
43        let body = serde_json::to_value(&request)?;
44        self.client
45            .execute(Method::POST, "/chat/completions", RequestOptions::json(body))
46            .await
47    }
48
49    /// Create a chat completion and stream chunks as they are generated.
50    pub async fn create_stream(
51        &self,
52        mut request: ChatCompletionRequest,
53    ) -> Result<EventStream<ChatCompletionChunk>, OpenAIError> {
54        request.stream = Some(true);
55        let body = serde_json::to_value(&request)?;
56        let response = self
57            .client
58            .execute_raw(Method::POST, "/chat/completions", RequestOptions::json(body))
59            .await?;
60        Ok(EventStream::new(response))
61    }
62}