async_openai/
completion.rs

1use crate::{
2    client::Client,
3    config::Config,
4    error::OpenAIError,
5    types::completions::{CreateCompletionRequest, CreateCompletionResponse},
6    RequestOptions,
7};
8
9#[cfg(not(target_family = "wasm"))]
10use crate::types::completions::CompletionResponseStream;
11
12/// Given a prompt, the model will return one or more predicted completions,
13/// and can also return the probabilities of alternative tokens at each position.
14/// We recommend most users use our Chat completions API.
15/// [Learn more](https://platform.openai.com/docs/deprecations/2023-07-06-gpt-and-embeddings)
16///
17/// Related guide: [Legacy Completions](https://platform.openai.com/docs/guides/gpt/completions-api)
18pub struct Completions<'c, C: Config> {
19    client: &'c Client<C>,
20    pub(crate) request_options: RequestOptions,
21}
22
23impl<'c, C: Config> Completions<'c, C> {
24    pub fn new(client: &'c Client<C>) -> Self {
25        Self {
26            client,
27            request_options: RequestOptions::new(),
28        }
29    }
30
31    /// Creates a completion for the provided prompt and parameters
32    ///
33    /// You must ensure that "stream: false" in serialized `request`
34    #[crate::byot(
35        T0 = serde::Serialize,
36        R = serde::de::DeserializeOwned
37    )]
38    pub async fn create(
39        &self,
40        request: CreateCompletionRequest,
41    ) -> Result<CreateCompletionResponse, OpenAIError> {
42        #[cfg(not(feature = "byot"))]
43        {
44            if request.stream.is_some() && request.stream.unwrap() {
45                return Err(OpenAIError::InvalidArgument(
46                    "When stream is true, use Completion::create_stream".into(),
47                ));
48            }
49        }
50        self.client
51            .post("/completions", request, &self.request_options)
52            .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    #[cfg(not(target_family = "wasm"))]
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: CreateCompletionRequest,
75    ) -> Result<CompletionResponseStream, 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 Completion::create".into(),
81                ));
82            }
83
84            request.stream = Some(true);
85        }
86        Ok(self
87            .client
88            .post_stream("/completions", request, &self.request_options)
89            .await)
90    }
91}