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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
use anyhow::{Context, Result};
use uuid::Uuid;

use crate::{
    chat_context::ChatContext, chat_response::ChatResponse,
    function_specification::FunctionSpecification, message::Message,
};

const DEFAULT_MODEL: &str = "gpt-3.5-turbo-0613";
const URL: &str = "https://api.openai.com/v1/chat/completions";

// Builder for ChatGPT
pub struct ChatGPTBuilder {
    model: Option<String>,
    openai_api_token: Option<String>,
    session_id: Option<String>,
    chat_context: Option<ChatContext>,
}

impl ChatGPTBuilder {
    pub fn new() -> Self {
        ChatGPTBuilder {
            model: None,
            openai_api_token: None,
            session_id: None,
            chat_context: None,
        }
    }

    pub fn model(mut self, model: String) -> Self {
        self.model = Some(model);
        self
    }

    pub fn openai_api_token(mut self, openai_api_token: String) -> Self {
        self.openai_api_token = Some(openai_api_token);
        self
    }

    pub fn session_id(mut self, session_id: String) -> Self {
        self.session_id = Some(session_id);
        self
    }

    pub fn chat_context(mut self, chat_context: ChatContext) -> Self {
        self.chat_context = Some(chat_context);
        self
    }

    pub fn build(self) -> Result<ChatGPT> {
        let client = reqwest::Client::new();
        let model = if let Some(m) = self.model {
            m
        } else {
            DEFAULT_MODEL.to_string()
        };
        let openai_api_token = self
            .openai_api_token
            .context("OpenAI API token is missing")?;
        let session_id = if let Some(s) = self.session_id {
            s
        } else {
            Uuid::new_v4().to_string()
        };
        let chat_context = if let Some(c) = self.chat_context {
            c
        } else {
            let mut c = ChatContext::new(model.clone());
            c.model = model.clone();
            c
        };

        Ok(ChatGPT {
            client,
            model,
            openai_api_token,
            session_id,
            chat_context,
        })
    }
}

/// The ChatGPT object
pub struct ChatGPT {
    client: reqwest::Client,
    pub model: String,
    openai_api_token: String,
    pub session_id: String,
    pub chat_context: ChatContext,
}

impl ChatGPT {
    /// Create a new ChatGPT object
    /// # Arguments
    /// * `openai_api_token` - The API token from OpenAI
    /// * `chat_context` - The context of the chatbot.
    /// Optional. If not provided, it will start a new context with the default model
    /// * `session_id` - The session ID of the chatbot.
    /// Optional. If not provided, it will generate a new session ID. This will be useful to track the conversation history
    /// # Example
    /// ```
    /// use chatgpt_functions::chat_gpt::ChatGPTBuilder;
    /// use anyhow::Result;
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<()> {
    ///     let key = std::env::var("OPENAI_API_KEY").unwrap_or("test".to_string());
    ///     let mut gpt = ChatGPTBuilder::new().openai_api_token(key).build()?;
    ///     Ok(())
    /// }
    /// ```
    /// # Errors
    /// It returns an error if the API token is not valid
    /// # Panics
    /// It panics if the API token is not provided
    /// # Remarks
    /// The API token can be found on the [OpenAI API keys](https://platform.openai.com/account/api-keys)
    pub fn new(
        client: reqwest::Client,
        model: String,
        openai_api_token: String,
        session_id: String,
        chat_context: ChatContext,
    ) -> Result<ChatGPT> {
        Ok(ChatGPT {
            client,
            model,
            openai_api_token,
            session_id,
            chat_context,
        })
    }

    /// Calls the OpenAI API to get a response using the current context
    /// # Arguments
    /// * `message` - The message to send to the AI
    /// # Errors
    /// It returns an error if the API token is not valid
    /// It returns an error if the response from the API is not valid or if the content of the response is not valid
    /// # Panics
    /// It panics if the API token is not provided
    /// # Remarks
    /// The context is updated with the response from the AI
    pub async fn completion(&mut self) -> Result<ChatResponse> {
        let response = self
            .client
            .post(URL)
            .bearer_auth(&self.openai_api_token)
            .header("Content-Type", "application/json")
            // Use Display trait to avoid sending None fields that the API would reject
            .body(self.chat_context.to_string())
            .send()
            .await
            .context(format!("Failed to receive the response from {}", URL))?
            .text()
            .await
            .context("Failed to retrieve the content of the response")?;

        let answer = parse_removing_newlines(response)?;
        Ok(answer)
    }

    /// Calls the OpenAI API to get a response using the current context, adding the content provided by the user
    /// This is the preferred function to use for chat completions that work with context.
    ///
    /// This is a fully managed function, it does update the context with the message provided,
    /// and it does update the context with the response from the AI.
    /// It calls completion_with_user_content_updating_context internally, it's for convenience.
    /// # Arguments
    /// * `content` - The content of the message
    /// # Errors
    /// It returns an error if the API token is not valid
    /// It returns an error if the response from the API is not valid or if the content of the response is not valid
    /// # Panics
    /// It panics if the API token is not provided
    /// # Remarks
    /// This is a fully managed function, it does update the context with the message provided,
    /// and it does update the context with the response from the AI.
    pub async fn completion_managed(&mut self, content: String) -> Result<ChatResponse> {
        self.completion_with_user_content_updating_context(content)
            .await
    }

    /// This function is used to call the openai API, using a Message already prepared.
    /// It requires a Message object as an argument, so access to some internal work of the library.
    /// This gives more flexibility to the user, but it is not recommended to use it directly.
    /// It returns the response from the AI
    /// It does update the context with the message provided,
    /// but it does not update the context with the response from the AI
    /// # Arguments
    /// * `message` - The message to send to the AI
    /// # Errors
    /// It returns an error if the API token is not valid
    /// It returns an error if the response from the API is not valid or if the content of the response is not valid
    /// # Remarks
    /// The context is updated with the message provided
    /// The context is not updated with the response from the AI
    /// This function is used by the other functions of the library
    /// It is not recommended to use it directly
    pub async fn completion_with_message(&mut self, message: Message) -> Result<ChatResponse> {
        self.push_message(message);
        self.completion().await
    }

    /// This function is used to call the openai API, using a String as the content of the message.
    /// It returns the response from the AI
    /// It does update the context with the message provided,
    /// but it does not update the context with the response from the AI
    /// # Arguments
    /// * `content` - The content of the message
    /// # Errors
    /// It returns an error if the API token is not valid
    /// It returns an error if the response from the API is not valid or if the content of the response is not valid
    /// # Remarks
    /// The context is updated with the message provided
    /// The context is not updated with the response from the AI
    /// This function is used by the other functions of the library
    /// It is not recommended to use it directly
    pub async fn completion_with_user_content(&mut self, content: String) -> Result<ChatResponse> {
        let message = Message::new_user_message(content);
        self.completion_with_message(message).await
    }

    /// This function is used to call the openai API, using content as the content of the message.
    /// It returns the response from the AI
    /// It does update the context with the message provided and the response from the AI
    /// # Arguments
    /// * `content` - The content of the message
    /// # Errors
    /// It returns an error if the API token is not valid
    /// It returns an error if the response from the API is not valid or if the content of the response is not valid
    /// # Remarks
    /// The context is updated with the message provided
    /// The context is updated with the response from the AI
    /// This function is used by the other functions of the library
    /// It assumes that there will only be one choice in the response
    /// It returns the response from the AI
    pub async fn completion_with_user_content_updating_context(
        &mut self,
        content: String,
    ) -> Result<ChatResponse> {
        let message = Message::new_user_message(content);
        self.completion_with_message_updating_context(message).await
    }

    /// This function is used to update the context with the response from the AI
    /// It assumes that there will only be one choice in the response
    /// It returns the response from the AI
    /// It does update the context with the response from the AI
    /// # Arguments
    /// * `message` - The message to send to the AI
    /// # Errors
    /// It returns an error if the API token is not valid
    /// It returns an error if the response from the API is not valid or if the content of the response is not valid
    /// # Remarks
    /// Important: The message received from the AI has to be modified when it is a function
    /// This is because when a function is returned the model still says that it is an assistant message.
    /// This is a bug in the API.
    /// If this is inserted in the context, the next request to the API will fail since it won't conform with the rules of the model.
    /// https://platform.openai.com/docs/api-reference/chat/create#chat/create-messages
    ///
    /// The context is updated with the response from the AI
    /// This function is used by the other functions of the library
    /// It assumes that there will only be one choice in the response
    /// It panics if there is more than one choice in the response
    pub async fn completion_with_message_updating_context(
        &mut self,
        message: Message,
    ) -> Result<ChatResponse> {
        self.push_message(message);
        let response = self.completion().await?;
        if let Some(choice) = response.choices.last() {
            self.push_message(choice.message.clone());
        };
        Ok(response)
    }

    /// This function is used to push a message to the context
    /// This is a low level function, it is not recommended to use it directly
    /// # Arguments
    /// * `message` - The message to push to the context
    /// # Remarks
    /// This function is used by the other functions of the library
    pub fn push_message(&mut self, message: Message) {
        self.chat_context.push_message(message);
    }

    /// This function is used to set all the messages in the context
    /// This will override the current messages in the context
    /// This is a low level function, it is not recommended to use it directly
    /// # Arguments
    /// * `messages` - The messages to set in the context
    /// # Remarks
    /// This function is used by the other functions of the library
    pub fn set_messages(&mut self, messages: Vec<Message>) {
        self.chat_context.set_messages(messages);
    }

    /// This function is used to push a function to the context
    /// This is a low level function, it is not recommended to use it directly
    /// # Arguments
    /// * `function` - The function to push to the context
    /// # Remarks
    /// This function is used by the other functions of the library
    pub fn push_function(&mut self, function: FunctionSpecification) {
        self.chat_context.push_function(function);
    }

    /// This function is used to set all the functions in the context
    /// This will override the current functions in the context
    /// This is a low level function, it is not recommended to use it directly
    /// # Arguments
    /// * `functions` - The vec of functions to set in the context
    /// # Remarks
    /// This function is used by the other functions of the library
    pub fn set_functions(&mut self, functions: Vec<FunctionSpecification>) {
        self.chat_context.set_functions(functions);
    }

    /// This function is used to retrieve the content of the last message in the context
    pub fn last_content(&self) -> Option<String> {
        self.chat_context.last_content()
    }

    /// This function is used to retrieve the function_call of the last message in the context
    pub fn last_function(&self) -> Option<(String, String)> {
        self.chat_context.last_function_call()
    }
}

fn parse_removing_newlines(response: String) -> Result<ChatResponse> {
    let r = response.replace("\n", "");
    let response: ChatResponse = serde_json::from_str(&r)?;
    Ok(response)
}

#[cfg(test)]
mod tests {
    use std::collections::HashMap;

    use crate::{function_specification::Parameters, message::FunctionCall};

    use super::*;

    #[test]
    fn test_chat_gpt_new() {
        let chat_gpt = ChatGPTBuilder::new()
            .openai_api_token("123".to_string())
            .build()
            .expect("Failed to create ChatGPT");
        assert_eq!(chat_gpt.session_id.len(), 36);
        assert_eq!(chat_gpt.chat_context.model, DEFAULT_MODEL);
        assert_eq!(chat_gpt.model, DEFAULT_MODEL);
    }

    #[test]
    fn test_chat_gpt_new_with_everything() {
        let chat_gpt = ChatGPTBuilder::new()
            .session_id("session_id".to_string())
            .model("model".to_string())
            .openai_api_token("1234".to_string())
            .build()
            .expect("Failed to create ChatGPT");
        assert_eq!(chat_gpt.session_id, "session_id");
        assert_eq!(chat_gpt.openai_api_token, "1234");
        assert_eq!(chat_gpt.chat_context.model, "model");
    }

    #[test]
    fn test_chat_gpt_push_message() {
        let mut chat_gpt = ChatGPTBuilder::new()
            .openai_api_token("key".to_string())
            .build()
            .expect("Failed to create ChatGPT");
        let message = Message::new_user_message("content".to_string());
        chat_gpt.push_message(message);
        assert_eq!(chat_gpt.chat_context.messages.len(), 1);
    }

    #[test]
    fn test_chat_gpt_set_message() {
        let mut chat_gpt = ChatGPTBuilder::new()
            .openai_api_token("key".to_string())
            .build()
            .expect("Failed to create ChatGPT");
        let message = Message::new_user_message("content".to_string());
        chat_gpt.set_messages(vec![message]);
        assert_eq!(chat_gpt.chat_context.messages.len(), 1);
    }

    #[test]
    fn test_chat_gpt_push_function() {
        let mut chat_gpt = ChatGPTBuilder::new()
            .openai_api_token("key".to_string())
            .build()
            .expect("Failed to create ChatGPT");
        let function = FunctionSpecification::new("function".to_string(), None, None);
        chat_gpt.push_function(function);
        assert_eq!(chat_gpt.chat_context.functions.len(), 1);
    }

    #[test]
    fn test_chat_gpt_set_function() {
        let mut chat_gpt = ChatGPTBuilder::new()
            .openai_api_token("key".to_string())
            .build()
            .expect("Failed to create ChatGPT");
        let function = FunctionSpecification::new(
            "function".to_string(),
            Some("Test function".to_string()),
            Some(Parameters {
                type_: "string".to_string(),
                properties: HashMap::new(),
                required: vec![],
            }),
        );
        chat_gpt.set_functions(vec![function]);
        assert_eq!(chat_gpt.chat_context.functions.len(), 1);

        let function = chat_gpt
            .chat_context
            .functions
            .get(0)
            .expect("Failed to get the function");
        assert_eq!(function.name, "function");
        assert_eq!(
            function
                .description
                .as_ref()
                .expect("Failed to get the description"),
            "Test function"
        );
        assert_eq!(
            function
                .parameters
                .as_ref()
                .expect("Failed to get the parameters")
                .type_,
            "string"
        );
    }

    #[test]
    fn test_parse_removing_newlines() {
        use crate::message::FunctionCall;

        let r = r#"{
    "id": "chatcmpl-7Ut7jsNlTUO9k9L5kBF0uDAyG19pK",
    "object": "chat.completion",
    "created": 1687596091,
    "model": "gpt-3.5-turbo-0613",
    "choices": [
        {
        "index": 0,
        "message": {
            "role": "assistant",
            "content": null,
            "function_call": {
                "name": "get_current_weather",
                "arguments": "{\n  \"location\": \"Madrid, Spain\"\n}"
            }
        },
        "finish_reason": "function_call"
        }
    ],
    "usage": {
        "prompt_tokens": 90,
        "completion_tokens": 19,
        "total_tokens": 109
    }
}"#
        .to_string();
        let response = parse_removing_newlines(r).expect("Failed to parse");
        let message = response
            .choices
            .first()
            .expect("There is no choice")
            .message
            .clone();

        assert_eq!(message.role, "assistant");
        assert_eq!(message.content, None);
        assert_eq!(message.name, None);
        assert_eq!(
            message.function_call,
            Some(FunctionCall {
                name: "get_current_weather".to_string(),
                arguments: "{\n  \"location\": \"Madrid, Spain\"\n}".to_string(),
            })
        );
    }

    #[test]
    fn test_fix_context_when_function_replied_with_content() {
        use crate::message::FunctionCall;

        let r = r#"{"id":"chatcmpl-7VneSVRn9qJ1crw3m0V0kmnCq8Pnn","object":"chat.completion","created":1687813384,"choices":[{"index":0,"message":{"role":"assistant","function_call":{"name":"completion_managed","arguments":"{
    \"content\": \"Hi, model!\"
}"}},"finish_reason":"function_call"}],"usage":{"prompt_tokens":61,"completion_tokens":18,"total_tokens":79}}"#.to_string();
        let response = parse_removing_newlines(r).expect("Failed to parse");
        let message = response
            .choices
            .last()
            .expect("There is no choice")
            .message
            .clone();

        assert_eq!(message.role, "assistant");
        assert_eq!(message.content, None);
        assert_eq!(message.name, None);
        assert_eq!(
            message.function_call,
            Some(FunctionCall {
                name: "completion_managed".to_string(),
                arguments: "{    \"content\": \"Hi, model!\"}".to_string(),
            })
        );
    }

    #[test]
    fn test_last_content() {
        let mut chat_gpt = ChatGPTBuilder::new()
            .openai_api_token("key".to_string())
            .build()
            .expect("Failed to create ChatGPT");
        let message = Message::new_user_message("content".to_string());
        chat_gpt.push_message(message);
        let message = Message::new_user_message("content2".to_string());
        chat_gpt.push_message(message);
        let message = Message::new_user_message("content3".to_string());
        chat_gpt.push_message(message);
        assert_eq!(chat_gpt.last_content(), Some("content3".to_string()));
    }

    #[test]
    fn test_last_content_empty() {
        let chat_gpt = ChatGPTBuilder::new()
            .openai_api_token("key".to_string())
            .build()
            .expect("Failed to create ChatGPT");
        assert_eq!(chat_gpt.last_content(), None);
    }

    #[test]
    fn test_last_function() {
        let mut chat_gpt = ChatGPTBuilder::new()
            .openai_api_token("key".to_string())
            .build()
            .expect("Failed to create ChatGPT");
        let mut msg = Message::new("function".to_string());
        msg.set_function_call(FunctionCall {
            name: "function".to_string(),
            arguments: "1".to_string(),
        });
        chat_gpt.push_message(msg);
        let mut msg = Message::new("function2".to_string());
        msg.set_function_call(FunctionCall {
            name: "function2".to_string(),
            arguments: "2".to_string(),
        });
        chat_gpt.push_message(msg);
        let mut msg = Message::new("function3".to_string());
        msg.set_function_call(FunctionCall {
            name: "function3".to_string(),
            arguments: "3".to_string(),
        });
        chat_gpt.push_message(msg);
        assert_eq!(
            chat_gpt.last_function(),
            Some(("function3".to_string(), "3".to_string()))
        );
    }
}