openai/request/
create_completion.rs1use serde_json::json;
2use crate::model::*;
3use crate::OpenAiClient;
4#[derive(Clone)]
8pub struct CreateCompletionRequest<'a> {
9 pub(crate) http_client: &'a OpenAiClient,
10 pub best_of: Option<i64>,
11 pub echo: Option<bool>,
12 pub frequency_penalty: Option<f64>,
13 pub logit_bias: Option<serde_json::Value>,
14 pub logprobs: Option<i64>,
15 pub max_tokens: Option<i64>,
16 pub model: String,
17 pub n: Option<i64>,
18 pub presence_penalty: Option<f64>,
19 pub prompt: Option<serde_json::Value>,
20 pub stop: Option<serde_json::Value>,
21 pub stream: Option<bool>,
22 pub suffix: Option<String>,
23 pub temperature: Option<f64>,
24 pub top_p: Option<f64>,
25 pub user: Option<String>,
26}
27impl<'a> CreateCompletionRequest<'a> {
28 pub async fn send(self) -> ::httpclient::InMemoryResult<CreateCompletionResponse> {
29 let mut r = self.http_client.client.post("/completions");
30 if let Some(ref unwrapped) = self.best_of {
31 r = r.json(json!({ "best_of" : unwrapped }));
32 }
33 if let Some(ref unwrapped) = self.echo {
34 r = r.json(json!({ "echo" : unwrapped }));
35 }
36 if let Some(ref unwrapped) = self.frequency_penalty {
37 r = r.json(json!({ "frequency_penalty" : unwrapped }));
38 }
39 if let Some(ref unwrapped) = self.logit_bias {
40 r = r.json(json!({ "logit_bias" : unwrapped }));
41 }
42 if let Some(ref unwrapped) = self.logprobs {
43 r = r.json(json!({ "logprobs" : unwrapped }));
44 }
45 if let Some(ref unwrapped) = self.max_tokens {
46 r = r.json(json!({ "max_tokens" : unwrapped }));
47 }
48 r = r.json(json!({ "model" : self.model }));
49 if let Some(ref unwrapped) = self.n {
50 r = r.json(json!({ "n" : unwrapped }));
51 }
52 if let Some(ref unwrapped) = self.presence_penalty {
53 r = r.json(json!({ "presence_penalty" : unwrapped }));
54 }
55 if let Some(ref unwrapped) = self.prompt {
56 r = r.json(json!({ "prompt" : unwrapped }));
57 }
58 if let Some(ref unwrapped) = self.stop {
59 r = r.json(json!({ "stop" : unwrapped }));
60 }
61 if let Some(ref unwrapped) = self.stream {
62 r = r.json(json!({ "stream" : unwrapped }));
63 }
64 if let Some(ref unwrapped) = self.suffix {
65 r = r.json(json!({ "suffix" : unwrapped }));
66 }
67 if let Some(ref unwrapped) = self.temperature {
68 r = r.json(json!({ "temperature" : unwrapped }));
69 }
70 if let Some(ref unwrapped) = self.top_p {
71 r = r.json(json!({ "top_p" : unwrapped }));
72 }
73 if let Some(ref unwrapped) = self.user {
74 r = r.json(json!({ "user" : unwrapped }));
75 }
76 r = self.http_client.authenticate(r);
77 let res = r.send_awaiting_body().await?;
78 res.json()
79 }
80 pub fn best_of(mut self, best_of: i64) -> Self {
81 self.best_of = Some(best_of);
82 self
83 }
84 pub fn echo(mut self, echo: bool) -> Self {
85 self.echo = Some(echo);
86 self
87 }
88 pub fn frequency_penalty(mut self, frequency_penalty: f64) -> Self {
89 self.frequency_penalty = Some(frequency_penalty);
90 self
91 }
92 pub fn logit_bias(mut self, logit_bias: serde_json::Value) -> Self {
93 self.logit_bias = Some(logit_bias);
94 self
95 }
96 pub fn logprobs(mut self, logprobs: i64) -> Self {
97 self.logprobs = Some(logprobs);
98 self
99 }
100 pub fn max_tokens(mut self, max_tokens: i64) -> Self {
101 self.max_tokens = Some(max_tokens);
102 self
103 }
104 pub fn n(mut self, n: i64) -> Self {
105 self.n = Some(n);
106 self
107 }
108 pub fn presence_penalty(mut self, presence_penalty: f64) -> Self {
109 self.presence_penalty = Some(presence_penalty);
110 self
111 }
112 pub fn prompt(mut self, prompt: serde_json::Value) -> Self {
113 self.prompt = Some(prompt);
114 self
115 }
116 pub fn stop(mut self, stop: serde_json::Value) -> Self {
117 self.stop = Some(stop);
118 self
119 }
120 pub fn stream(mut self, stream: bool) -> Self {
121 self.stream = Some(stream);
122 self
123 }
124 pub fn suffix(mut self, suffix: &str) -> Self {
125 self.suffix = Some(suffix.to_owned());
126 self
127 }
128 pub fn temperature(mut self, temperature: f64) -> Self {
129 self.temperature = Some(temperature);
130 self
131 }
132 pub fn top_p(mut self, top_p: f64) -> Self {
133 self.top_p = Some(top_p);
134 self
135 }
136 pub fn user(mut self, user: &str) -> Self {
137 self.user = Some(user.to_owned());
138 self
139 }
140}
141impl<'a> ::std::future::IntoFuture for CreateCompletionRequest<'a> {
142 type Output = httpclient::InMemoryResult<CreateCompletionResponse>;
143 type IntoFuture = ::futures::future::BoxFuture<'a, Self::Output>;
144 fn into_future(self) -> Self::IntoFuture {
145 Box::pin(self.send())
146 }
147}