1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
use crate::error::ApiResult;
use crate::chat_completion_delta::forward_stream;
use crate::DeltaReceiver;
use crate::error::ApiErrorWrapper;
use crate::{Chat, OPENAI_API_KEY};
use crate::{Function, Message};
use log::debug;
use reqwest::Method;
use reqwest_eventsource::RequestBuilderExt;
use schemars::JsonSchema;
use serde::Deserialize;
use std::{collections::HashMap, vec};
use tokio::sync::mpsc;

#[derive(Debug, Clone, serde_derive::Serialize, serde_derive::Deserialize)]
pub struct ChatCompletionRequest {
    pub model: String,
    pub messages: Vec<Message>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub functions: Option<Vec<Function>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub function_call: Option<String>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub temperature: Option<f64>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub top_p: Option<f64>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub n: Option<u64>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub stream: Option<bool>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub stop: Option<Vec<String>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_tokens: Option<u64>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub presence_penalty: Option<f64>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub frequency_penalty: Option<f64>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub logit_bias: Option<HashMap<String, f64>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub user: Option<String>,
}

impl ChatCompletionRequest {
    fn new() -> Self {
        Self {
            model: "gpt-3.5-turbo".to_string(),
            messages: vec![],
            functions: None,
            function_call: None,
            temperature: None,
            top_p: None,
            n: None,
            stream: None,
            stop: None,
            max_tokens: None,
            presence_penalty: None,
            frequency_penalty: None,
            logit_bias: None,
            user: None,
        }
    }
}

#[derive(Debug, Clone, serde_derive::Serialize, serde_derive::Deserialize)]
pub struct AiAgent {
    pub model: String,

    pub system_message: Option<Message>,

    pub messages: Vec<Message>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub functions: Option<Vec<Function>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub function_call: Option<String>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub temperature: Option<f64>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub top_p: Option<f64>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub n: Option<u64>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub stop: Option<Vec<String>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_tokens: Option<u64>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub presence_penalty: Option<f64>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub frequency_penalty: Option<f64>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub logit_bias: Option<HashMap<String, f64>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub user: Option<String>,
}

impl AiAgent {
    // request part
    pub fn build_request(&self, stream: bool) -> ChatCompletionRequest {
        let messages = if let Some(system_message) = &self.system_message {
            let mut messages = self.messages.clone();
            messages.push(system_message.clone());
            messages
        } else {
            self.messages.clone()
        };

        ChatCompletionRequest {
            model: self.model.clone(),
            messages,
            functions: self.functions.clone(),
            function_call: self.function_call.clone(),
            temperature: self.temperature,
            top_p: self.top_p,
            n: self.n,
            stream: Some(stream),
            stop: self.stop.clone(),
            max_tokens: self.max_tokens,
            presence_penalty: self.presence_penalty,
            frequency_penalty: self.frequency_penalty,
            logit_bias: self.logit_bias.clone(),
            user: self.user.clone(),
        }
    }

    #[allow(clippy::await_holding_lock)]
    pub async fn create(&self) -> ApiResult<Chat> {
        let api_key = OPENAI_API_KEY.read().unwrap();
        let req = reqwest::Client::new()
            .post("https://api.openai.com/v1/chat/completions")
            .json(&self.build_request(false))
            .bearer_auth(api_key.clone().expect("no api key found"))
            .header("Content-Type", "application/json")
            .send()
            .await.unwrap();

        let res = req.text().await.unwrap();

        serialize(&res)
    }

    #[allow(clippy::await_holding_lock)]
    pub fn create_stream(&self) -> anyhow::Result<DeltaReceiver> {
        let lock = OPENAI_API_KEY.read().unwrap();
        let api_key = (*lock).clone().unwrap();

        let (tx, rx) = mpsc::channel(64);

        let es = reqwest::Client::new()
            .request(Method::POST, "https://api.openai.com/v1/chat/completions")
            .json(&self.build_request(true))
            .bearer_auth(api_key)
            .eventsource()?;
        tokio::spawn(forward_stream(es, tx));
        
        
        Ok(DeltaReceiver::from(rx, self))
    }

    // builder part

    pub fn new(model: impl Into<String>) -> Self {
        Self {
            model: model.into(),
            system_message: None,
            messages: vec![],
            functions: None,
            function_call: None,
            temperature: None,
            top_p: None,
            n: None,
            stop: None,
            max_tokens: None,
            presence_penalty: None,
            frequency_penalty: None,
            logit_bias: None,
            user: None,
        }
    }

    pub fn with_system_message<'a>(mut self, system_message: impl Into<&'a str>) -> Self {
        self.system_message =
            Some(Message::new("system").with_content(system_message.into()));
        self
    }

    pub fn with_messages(mut self, messages: Vec<Message>) -> Self {
        self.messages = messages;
        self
    }

    pub fn with_functions<FunctionArgs, Func>(mut self, functions: Vec<Func>) -> Self
    where
        FunctionArgs: JsonSchema,
        Func: Fn(FunctionArgs) + 'static,
    {
        let functions = functions.into_iter().map(|f| Function::from(f)).collect();
        self.functions = Some(functions);
        self
    }

    pub fn with_function_call(mut self, function_call: impl Into<String>) -> Self {
        self.function_call = Some(function_call.into());
        self
    }

    pub fn with_temperature(mut self, temperature: f64) -> Self {
        self.temperature = Some(temperature);
        self
    }

    pub fn with_top_p(mut self, top_p: f64) -> Self {
        self.top_p = Some(top_p);
        self
    }

    pub fn with_n(mut self, n: u64) -> Self {
        self.n = Some(n);
        self
    }

    pub fn with_stop(mut self, stop: Vec<String>) -> Self {
        self.stop = Some(stop);
        self
    }

    pub fn with_max_tokens(mut self, max_tokens: u64) -> Self {
        self.max_tokens = Some(max_tokens);
        self
    }

    pub fn with_presence_penalty(mut self, presence_penalty: f64) -> Self {
        self.presence_penalty = Some(presence_penalty);
        self
    }

    pub fn with_frequency_penalty(mut self, frequency_penalty: f64) -> Self {
        self.frequency_penalty = Some(frequency_penalty);
        self
    }

    pub fn with_logit_bias(mut self, logit_bias: HashMap<String, f64>) -> Self {
        self.logit_bias = Some(logit_bias);
        self
    }

    pub fn with_user(mut self, user: impl Into<String>) -> Self {
        self.user = Some(user.into());
        self
    }

    // mutably update part

    pub fn push_message(&mut self, message: Message) {
        self.messages.push(message);
    }

    pub fn push_function<FunctionArgs, Func>(&mut self, function: Func)
    where
        FunctionArgs: JsonSchema,
        Func: Fn(FunctionArgs) + 'static,
    {
        if let Some(functions) = &mut self.functions {
            functions.push(Function::from(function));
        } else {
            self.functions = Some(vec![Function::from(function)]);
        }
    }

    pub fn push_stop(&mut self, stop: impl Into<String>) {
        if let Some(stops) = &mut self.stop {
            stops.push(stop.into());
        } else {
            self.stop = Some(vec![stop.into()]);
        }
    }

    pub fn push_logit_bias(&mut self, logit_bias: (String, f64)) {
        if let Some(logit_biases) = &mut self.logit_bias {
            logit_biases.insert(logit_bias.0, logit_bias.1);
        } else {
            let mut logit_biases = HashMap::new();
            logit_biases.insert(logit_bias.0, logit_bias.1);
            self.logit_bias = Some(logit_biases);
        }
    }
}

pub fn serialize<'a, T: Deserialize<'a>>(res: &'a str) -> ApiResult<T> {
    debug!("response: {}", res);
    match serde_json::from_str::<T>(res) {
        Ok(chat) => Ok(chat),
        Err(_) => {
            let err =
                serde_json::from_str::<ApiErrorWrapper>(res).unwrap_or_else(|_| panic!("{}", res));
            Err(err.error.into())
        }
    }
}