dynamo_async_openai/
completion.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    client::Client,
13    config::Config,
14    error::OpenAIError,
15    types::{CompletionResponseStream, CreateCompletionRequest, CreateCompletionResponse},
16};
17
18/// Given a prompt, the model will return one or more predicted completions,
19/// and can also return the probabilities of alternative tokens at each position.
20/// We recommend most users use our Chat completions API.
21/// [Learn more](https://platform.openai.com/docs/deprecations/2023-07-06-gpt-and-embeddings)
22///
23/// Related guide: [Legacy Completions](https://platform.openai.com/docs/guides/gpt/completions-api)
24pub struct Completions<'c, C: Config> {
25    client: &'c Client<C>,
26}
27
28impl<'c, C: Config> Completions<'c, C> {
29    pub fn new(client: &'c Client<C>) -> Self {
30        Self { client }
31    }
32
33    /// Creates a completion for the provided prompt and parameters
34    ///
35    /// You must ensure that "stream: false" in serialized `request`
36    #[crate::byot(
37        T0 = serde::Serialize,
38        R = serde::de::DeserializeOwned
39    )]
40    pub async fn create(
41        &self,
42        request: CreateCompletionRequest,
43    ) -> Result<CreateCompletionResponse, OpenAIError> {
44        #[cfg(not(feature = "byot"))]
45        {
46            if request.stream.is_some() && request.stream.unwrap() {
47                return Err(OpenAIError::InvalidArgument(
48                    "When stream is true, use Completion::create_stream".into(),
49                ));
50            }
51        }
52        self.client.post("/completions", request).await
53    }
54
55    /// Creates a completion request for the provided prompt and parameters
56    ///
57    /// Stream back partial progress. Tokens will be sent as data-only
58    /// [server-sent events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#event_stream_format)
59    /// as they become available, with the stream terminated by a data: \[DONE\] message.
60    ///
61    /// [CompletionResponseStream] is a parsed SSE stream until a \[DONE\] is received from server.
62    ///
63    /// You must ensure that "stream: true" in serialized `request`
64    #[crate::byot(
65        T0 = serde::Serialize,
66        R = serde::de::DeserializeOwned,
67        stream = "true",
68        where_clause = "R: std::marker::Send + 'static"
69    )]
70    #[allow(unused_mut)]
71    pub async fn create_stream(
72        &self,
73        mut request: CreateCompletionRequest,
74    ) -> Result<CompletionResponseStream, OpenAIError> {
75        #[cfg(not(feature = "byot"))]
76        {
77            if request.stream.is_some() && !request.stream.unwrap() {
78                return Err(OpenAIError::InvalidArgument(
79                    "When stream is false, use Completion::create".into(),
80                ));
81            }
82
83            request.stream = Some(true);
84        }
85        Ok(self.client.post_stream("/completions", request).await)
86    }
87}