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
use json::JsonValue;
use crate::chat::Chat;
use crate::files::Files;
use crate::models::Models;

/// 聊天
mod chat;
/// 图像生成
mod images;
/// 模型列表
mod models;
/// 文件
mod files;
#[derive(Clone, Debug)]
pub struct OpenAi {
    token: String,
    /// 用户ID
    user_id: String,
    /// 模型名称
    model: String,
}

impl OpenAi {
    pub fn new(token: &str, user_id: &str, model: &str) -> Self {
        Self {
            token: token.to_string(),
            user_id: user_id.to_string(),
            model: model.parse().unwrap(),
        }
    }
    /// 聊天
    pub fn chat(&mut self, history: JsonValue, prompt: &str) -> Result<JsonValue, String> {
        Chat::new(self.clone()).request(history, prompt).clone()
    }
    /// 模型列表
    pub fn models(&mut self) -> Result<JsonValue, String> {
        Models::new(self.clone()).request()
    }
    /// 文件
    pub fn files(&mut self, path: String) -> Result<JsonValue, String> {
        Files::new(self.clone()).request(path)
    }
}