Skip to main content

burncloud_service_models/
model.rs

1use serde::{Deserialize, Serialize};
2use uuid::Uuid;
3use chrono::{DateTime, Utc};
4use std::collections::HashMap;
5
6/// 模型类型
7#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
8pub enum ModelType {
9    /// 对话模型
10    Chat,
11    /// 代码生成模型
12    Code,
13    /// 文本生成模型
14    Text,
15    /// 嵌入模型
16    Embedding,
17    /// 多模态模型
18    Multimodal,
19    /// 图像生成模型
20    ImageGeneration,
21    /// 语音模型
22    Speech,
23}
24
25/// 模型大小分类
26#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
27pub enum ModelSize {
28    /// 小型 (< 3B)
29    Small,
30    /// 中型 (3B - 8B)
31    Medium,
32    /// 大型 (8B - 30B)
33    Large,
34    /// 超大型 (> 30B)
35    XLarge,
36}
37
38/// 模型运行状态
39#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
40pub enum ModelStatus {
41    /// 运行中
42    Running,
43    /// 已停止
44    Stopped,
45    /// 启动中
46    Starting,
47    /// 停止中
48    Stopping,
49    /// 错误状态
50    Error,
51    /// 下载中
52    Downloading,
53    /// 安装中
54    Installing,
55}
56
57/// 模型信息
58#[derive(Debug, Clone, Serialize, Deserialize)]
59pub struct Model {
60    /// 模型ID
61    pub id: Uuid,
62    /// 模型名称
63    pub name: String,
64    /// 模型显示名称
65    pub display_name: String,
66    /// 模型描述
67    pub description: Option<String>,
68    /// 模型版本
69    pub version: String,
70    /// 模型类型
71    pub model_type: ModelType,
72    /// 模型大小分类
73    pub size_category: ModelSize,
74    /// 模型文件大小 (字节)
75    pub file_size: u64,
76    /// 模型提供商
77    pub provider: String,
78    /// 模型许可证
79    pub license: Option<String>,
80    /// 模型标签
81    pub tags: Vec<String>,
82    /// 支持的语言
83    pub languages: Vec<String>,
84    /// 创建时间
85    pub created_at: DateTime<Utc>,
86    /// 更新时间
87    pub updated_at: DateTime<Utc>,
88    /// 模型文件路径
89    pub file_path: Option<String>,
90    /// 模型检验和
91    pub checksum: Option<String>,
92    /// 下载URL
93    pub download_url: Option<String>,
94    /// 模型配置参数
95    pub config: HashMap<String, serde_json::Value>,
96    /// 模型评分
97    pub rating: Option<f32>,
98    /// 下载次数
99    pub download_count: u64,
100    /// 是否为官方模型
101    pub is_official: bool,
102}
103
104/// 已安装的模型实例
105#[derive(Debug, Clone, Serialize, Deserialize)]
106pub struct InstalledModel {
107    /// 基础模型信息
108    #[serde(flatten)]
109    pub model: Model,
110    /// 安装路径
111    pub install_path: String,
112    /// 安装时间
113    pub installed_at: DateTime<Utc>,
114    /// 当前状态
115    pub status: ModelStatus,
116    /// 运行端口
117    pub port: Option<u16>,
118    /// 进程ID
119    pub process_id: Option<u32>,
120    /// 最后使用时间
121    pub last_used: Option<DateTime<Utc>>,
122    /// 使用次数
123    pub usage_count: u64,
124}
125
126/// 可下载的模型
127#[derive(Debug, Clone, Serialize, Deserialize)]
128pub struct AvailableModel {
129    /// 基础模型信息
130    #[serde(flatten)]
131    pub model: Model,
132    /// 是否已安装
133    pub is_installed: bool,
134    /// 发布时间
135    pub published_at: DateTime<Utc>,
136    /// 最后更新时间
137    pub last_updated: DateTime<Utc>,
138    /// 系统要求
139    pub system_requirements: SystemRequirements,
140}
141
142/// 系统要求
143#[derive(Debug, Clone, Serialize, Deserialize)]
144pub struct SystemRequirements {
145    /// 最小内存 (GB)
146    pub min_memory_gb: f32,
147    /// 推荐内存 (GB)
148    pub recommended_memory_gb: f32,
149    /// 最小磁盘空间 (GB)
150    pub min_disk_space_gb: f32,
151    /// 是否需要GPU
152    pub requires_gpu: bool,
153    /// 支持的操作系统
154    pub supported_os: Vec<String>,
155    /// 支持的架构
156    pub supported_architectures: Vec<String>,
157}
158
159impl Model {
160    /// 创建新模型
161    pub fn new(
162        name: String,
163        display_name: String,
164        version: String,
165        model_type: ModelType,
166        provider: String,
167        file_size: u64,
168    ) -> Self {
169        let now = Utc::now();
170        Self {
171            id: Uuid::new_v4(),
172            name,
173            display_name,
174            description: None,
175            version,
176            model_type,
177            size_category: Self::calculate_size_category(file_size),
178            file_size,
179            provider,
180            license: None,
181            tags: Vec::new(),
182            languages: Vec::new(),
183            created_at: now,
184            updated_at: now,
185            file_path: None,
186            checksum: None,
187            download_url: None,
188            config: HashMap::new(),
189            rating: None,
190            download_count: 0,
191            is_official: false,
192        }
193    }
194
195    /// 根据文件大小计算模型大小分类
196    fn calculate_size_category(file_size: u64) -> ModelSize {
197        let size_gb = file_size as f64 / 1024.0 / 1024.0 / 1024.0;
198        match size_gb {
199            s if s < 3.0 => ModelSize::Small,
200            s if s < 8.0 => ModelSize::Medium,
201            s if s < 30.0 => ModelSize::Large,
202            _ => ModelSize::XLarge,
203        }
204    }
205
206    /// 格式化文件大小
207    pub fn formatted_size(&self) -> String {
208        let size_gb = self.file_size as f64 / 1024.0 / 1024.0 / 1024.0;
209        format!("{:.1}GB", size_gb)
210    }
211}
212
213impl InstalledModel {
214    /// 从模型创建已安装实例
215    pub fn from_model(model: Model, install_path: String) -> Self {
216        Self {
217            model,
218            install_path,
219            installed_at: Utc::now(),
220            status: ModelStatus::Stopped,
221            port: None,
222            process_id: None,
223            last_used: None,
224            usage_count: 0,
225        }
226    }
227
228    /// 标记为已使用
229    pub fn mark_used(&mut self) {
230        self.last_used = Some(Utc::now());
231        self.usage_count += 1;
232    }
233
234    /// 检查是否正在运行
235    pub fn is_running(&self) -> bool {
236        matches!(self.status, ModelStatus::Running)
237    }
238}
239
240impl AvailableModel {
241    /// 从模型创建可下载实例
242    pub fn from_model(model: Model, system_requirements: SystemRequirements) -> Self {
243        Self {
244            is_installed: false,
245            published_at: model.created_at,
246            last_updated: model.updated_at,
247            system_requirements,
248            model,
249        }
250    }
251
252    /// 检查系统兼容性
253    pub fn is_compatible(&self, available_memory_gb: f32, os: &str, arch: &str) -> bool {
254        available_memory_gb >= self.system_requirements.min_memory_gb
255            && self.system_requirements.supported_os.iter().any(|supported_os| supported_os == os)
256            && self.system_requirements.supported_architectures.iter().any(|supported_arch| supported_arch == arch)
257    }
258}