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
use json::JsonValue;
use crate::chat::Chat;
use crate::images::Images;

/// 聊天
mod chat;
/// 图像生成
mod images;

#[derive(Clone, Debug)]
pub struct OpenAi {
    token: String,
    /// 用户ID
    user_id: String,
    /// 型号
    model: Model,
}

impl OpenAi {
    pub fn new(token: &str, user_id: &str) -> Self {
        Self {
            token: token.to_string(),
            user_id: user_id.to_string(),
            model:Model::Gbt35Turbo,
        }
    }
    /// 模式设置
    pub fn model(&mut self, model: Model) -> &mut Self {
        self.model = model;
        self
    }
    /// 聊天
    pub fn chat(&mut self, prompt: &str) -> Result<JsonValue, String> {
        Chat::new(self.clone()).request(prompt).clone()
    }
}

/// 型号
#[derive(Clone, Debug)]
pub enum Model {
    Gbt35Turbo,
    Gbt4,
    Gpt35Turbo0613,
    Gpt35Turbo16K0613,
}

impl Model {
    pub fn info(self) -> &'static str {
        match self {
            Model::Gbt35Turbo => "gpt-3.5-turbo",
            Model::Gbt4 => "gpt-4",
            Model::Gpt35Turbo0613 => "gpt-3.5-turbo-0613",
            Model::Gpt35Turbo16K0613 => "gpt-3.5-turbo-16k-0613"
        }
    }
}