openai/request/
create_chat_completion.rs1use serde_json::json;
2use crate::model::*;
3use crate::OpenAiClient;
4#[derive(Clone)]
8pub struct CreateChatCompletionRequest<'a> {
9 pub(crate) http_client: &'a OpenAiClient,
10 pub frequency_penalty: Option<f64>,
11 pub logit_bias: Option<serde_json::Value>,
12 pub max_tokens: Option<i64>,
13 pub messages: Vec<ChatCompletionRequestMessage>,
14 pub model: String,
15 pub n: Option<i64>,
16 pub presence_penalty: Option<f64>,
17 pub stop: Option<serde_json::Value>,
18 pub stream: Option<bool>,
19 pub temperature: Option<f64>,
20 pub top_p: Option<f64>,
21 pub user: Option<String>,
22}
23impl<'a> CreateChatCompletionRequest<'a> {
24 pub async fn send(
25 self,
26 ) -> ::httpclient::InMemoryResult<CreateChatCompletionResponse> {
27 let mut r = self.http_client.client.post("/chat/completions");
28 if let Some(ref unwrapped) = self.frequency_penalty {
29 r = r.json(json!({ "frequency_penalty" : unwrapped }));
30 }
31 if let Some(ref unwrapped) = self.logit_bias {
32 r = r.json(json!({ "logit_bias" : unwrapped }));
33 }
34 if let Some(ref unwrapped) = self.max_tokens {
35 r = r.json(json!({ "max_tokens" : unwrapped }));
36 }
37 r = r.json(json!({ "messages" : self.messages }));
38 r = r.json(json!({ "model" : self.model }));
39 if let Some(ref unwrapped) = self.n {
40 r = r.json(json!({ "n" : unwrapped }));
41 }
42 if let Some(ref unwrapped) = self.presence_penalty {
43 r = r.json(json!({ "presence_penalty" : unwrapped }));
44 }
45 if let Some(ref unwrapped) = self.stop {
46 r = r.json(json!({ "stop" : unwrapped }));
47 }
48 if let Some(ref unwrapped) = self.stream {
49 r = r.json(json!({ "stream" : unwrapped }));
50 }
51 if let Some(ref unwrapped) = self.temperature {
52 r = r.json(json!({ "temperature" : unwrapped }));
53 }
54 if let Some(ref unwrapped) = self.top_p {
55 r = r.json(json!({ "top_p" : unwrapped }));
56 }
57 if let Some(ref unwrapped) = self.user {
58 r = r.json(json!({ "user" : unwrapped }));
59 }
60 r = self.http_client.authenticate(r);
61 let res = r.send_awaiting_body().await?;
62 res.json()
63 }
64 pub fn frequency_penalty(mut self, frequency_penalty: f64) -> Self {
65 self.frequency_penalty = Some(frequency_penalty);
66 self
67 }
68 pub fn logit_bias(mut self, logit_bias: serde_json::Value) -> Self {
69 self.logit_bias = Some(logit_bias);
70 self
71 }
72 pub fn max_tokens(mut self, max_tokens: i64) -> Self {
73 self.max_tokens = Some(max_tokens);
74 self
75 }
76 pub fn n(mut self, n: i64) -> Self {
77 self.n = Some(n);
78 self
79 }
80 pub fn presence_penalty(mut self, presence_penalty: f64) -> Self {
81 self.presence_penalty = Some(presence_penalty);
82 self
83 }
84 pub fn stop(mut self, stop: serde_json::Value) -> Self {
85 self.stop = Some(stop);
86 self
87 }
88 pub fn stream(mut self, stream: bool) -> Self {
89 self.stream = Some(stream);
90 self
91 }
92 pub fn temperature(mut self, temperature: f64) -> Self {
93 self.temperature = Some(temperature);
94 self
95 }
96 pub fn top_p(mut self, top_p: f64) -> Self {
97 self.top_p = Some(top_p);
98 self
99 }
100 pub fn user(mut self, user: &str) -> Self {
101 self.user = Some(user.to_owned());
102 self
103 }
104}
105impl<'a> ::std::future::IntoFuture for CreateChatCompletionRequest<'a> {
106 type Output = httpclient::InMemoryResult<CreateChatCompletionResponse>;
107 type IntoFuture = ::futures::future::BoxFuture<'a, Self::Output>;
108 fn into_future(self) -> Self::IntoFuture {
109 Box::pin(self.send())
110 }
111}