async_openai/
completion.rs1use 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
12pub 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 #[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 #[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}