Download internal/config/model_profile.go from a3216/ds2api: direct link, hf CLI and curl.
- Browser
- Download file 9.73 kB
-
https://huggingface.co/spaces/a3216/ds2api/resolve/main/internal/config/model_profile.go
- Command line
-
hf download hf://spaces/a3216/ds2api/internal/config/model_profile.go
-
curl -L -o model_profile.go https://huggingface.co/spaces/a3216/ds2api/resolve/main/internal/config/model_profile.go
9.73 kB
| package config | |
| import ( | |
| "fmt" | |
| "strings" | |
| "unicode/utf8" | |
| ) | |
| // 内置提示词风格档位。 | |
| // | |
| // 「风格」只决定两件事:历史拆分后的衔接指令用哪一版文案、以及是否注入内置执行契约。 | |
| // 它不涉及特化层(prompt_profile)与技能层,那些仍然是独立开关。 | |
| const ( | |
| // PromptStyleV2 是当前版本:衔接指令带「执行要求」,并注入内置执行契约。 | |
| PromptStyleV2 = "v2" | |
| // PromptStyleLegacy 是旧版:衔接指令用短文案,且不注入内置执行契约 | |
| // (调用方显式配置过 thinking_injection.prompt 时仍然注入)。 | |
| PromptStyleLegacy = "legacy" | |
| ) | |
| // PromptStyleNames 返回可选风格名,顺序固定,供后台下拉与文档对照。 | |
| func PromptStyleNames() []string { | |
| return []string{PromptStyleV2, PromptStyleLegacy} | |
| } | |
| // NormalizePromptStyle 归一化风格名:空或未知一律回落 v2。 | |
| // | |
| // 配置文件里写错不会让加载失败(与 model_type 未知回落 default 一致), | |
| // 只有后台 PUT 会显式报 400。 | |
| func NormalizePromptStyle(style string) string { | |
| switch strings.ToLower(strings.TrimSpace(style)) { | |
| case PromptStyleLegacy: | |
| return PromptStyleLegacy | |
| default: | |
| return PromptStyleV2 | |
| } | |
| } | |
| // IsKnownPromptStyle 判断是否为可选风格名。 | |
| func IsKnownPromptStyle(style string) bool { | |
| switch strings.ToLower(strings.TrimSpace(style)) { | |
| case PromptStyleV2, PromptStyleLegacy: | |
| return true | |
| default: | |
| return false | |
| } | |
| } | |
| // 自定义模型条目的风格档位。 | |
| // | |
| // 一个条目既能自带提示词(custom),也能直接选一个内置风格,或跟随全局配置。 | |
| // 这样同一个 flash 基模可以挂出「日常 v2」「写作」「旧版」等多个模型名并排给客户端选。 | |
| const ( | |
| // ModelProfileStyleInherit 跟随全局:prompt_style 与 prompt_profile 都按全局设置。 | |
| ModelProfileStyleInherit = "inherit" | |
| // ModelProfileStyleV2 强制 v2 内置文案,不注入特化层。 | |
| ModelProfileStyleV2 = "v2" | |
| // ModelProfileStyleWriting 强制 v2 内置文案 + 写作规范正文。 | |
| ModelProfileStyleWriting = "writing" | |
| // ModelProfileStyleLegacy 强制旧版内置文案,不注入特化层。 | |
| ModelProfileStyleLegacy = "legacy" | |
| // ModelProfileStyleCustom 用本条目 prompt 作为特化层正文,文案风格跟随全局。 | |
| ModelProfileStyleCustom = "custom" | |
| ) | |
| // ModelProfileStyleNames 返回风格档位名,顺序固定,供后台下拉与文档对照。 | |
| func ModelProfileStyleNames() []string { | |
| return []string{ | |
| ModelProfileStyleInherit, | |
| ModelProfileStyleV2, | |
| ModelProfileStyleWriting, | |
| ModelProfileStyleLegacy, | |
| ModelProfileStyleCustom, | |
| } | |
| } | |
| // IsKnownModelProfileStyle 判断是否为可选风格档位。 | |
| func IsKnownModelProfileStyle(style string) bool { | |
| switch strings.ToLower(strings.TrimSpace(style)) { | |
| case ModelProfileStyleInherit, ModelProfileStyleV2, ModelProfileStyleWriting, | |
| ModelProfileStyleLegacy, ModelProfileStyleCustom: | |
| return true | |
| default: | |
| return false | |
| } | |
| } | |
| // NormalizeModelProfileStyle 归一化风格档位:空值按 inherit 处理,未知值同样回落 inherit。 | |
| func NormalizeModelProfileStyle(style string) string { | |
| normalized := strings.ToLower(strings.TrimSpace(style)) | |
| if IsKnownModelProfileStyle(normalized) { | |
| return normalized | |
| } | |
| return ModelProfileStyleInherit | |
| } | |
| // 自定义模型条目(model_profiles)的限制。 | |
| const ( | |
| // MaxModelProfiles 是自定义模型条目的数量上限。 | |
| MaxModelProfiles = 50 | |
| // MaxModelProfileIDChars 是自定义模型名的字符上限。 | |
| MaxModelProfileIDChars = 64 | |
| ) | |
| // ModelProfileConfig 是一条自定义模型:对外名称 + 基模 + 该模型专属的提示词风格/正文。 | |
| type ModelProfileConfig struct { | |
| // ID 是对外模型名,客户端在请求里写的就是它。 | |
| ID string `json:"id"` | |
| // Base 是实际调用上游的基模,必须是当前支持的 flash 系列之一。 | |
| Base string `json:"base"` | |
| // Name 是展示名(可选),仅用于后台与 /v1/models 的可读性。 | |
| Name string `json:"name,omitempty"` | |
| // Remark 是备注(可选)。 | |
| Remark string `json:"remark,omitempty"` | |
| // Style 是风格档位:inherit / v2 / writing / legacy / custom。 | |
| // 留空时:有 Prompt 视为 custom,否则视为 inherit(兼容旧配置)。 | |
| Style string `json:"style,omitempty"` | |
| // Prompt 是该模型专属的提示词正文,只在 Style=custom 时使用。 | |
| Prompt string `json:"prompt,omitempty"` | |
| } | |
| // EffectiveStyle 返回生效的风格档位。 | |
| // | |
| // 兼容旧配置:早期版本只有 prompt 字段,没有 style;那时填写 prompt 的语义就是 | |
| // 「用这段正文当特化层」,所以这里把它解释成 custom。 | |
| func (m ModelProfileConfig) EffectiveStyle() string { | |
| style := strings.ToLower(strings.TrimSpace(m.Style)) | |
| if style == "" { | |
| if strings.TrimSpace(m.Prompt) != "" { | |
| return ModelProfileStyleCustom | |
| } | |
| return ModelProfileStyleInherit | |
| } | |
| return NormalizeModelProfileStyle(style) | |
| } | |
| // ModelProfileReader 由配置中心实现,供模型解析与列表接口读取自定义模型。 | |
| type ModelProfileReader interface { | |
| ModelProfiles() []ModelProfileConfig | |
| } | |
| // SupportedBaseModels 返回可作为基模的模型名。 | |
| // | |
| // 只列 flash 系列:pro / vision 已经并进 flash,作为基模没有意义。 | |
| func SupportedBaseModels() []string { | |
| return []string{ | |
| "deepseek-v4-flash", | |
| "deepseek-v4-flash-search", | |
| "deepseek-v4-flash-nothinking", | |
| "deepseek-v4-flash-search-nothinking", | |
| } | |
| } | |
| // Normalize 归一化单条自定义模型:裁剪空白、基模转小写、风格档位转小写。 | |
| func (m ModelProfileConfig) Normalize() ModelProfileConfig { | |
| style := strings.ToLower(strings.TrimSpace(m.Style)) | |
| if style != "" { | |
| style = NormalizeModelProfileStyle(style) | |
| } | |
| return ModelProfileConfig{ | |
| ID: strings.TrimSpace(m.ID), | |
| Base: strings.ToLower(strings.TrimSpace(m.Base)), | |
| Name: strings.TrimSpace(m.Name), | |
| Remark: strings.TrimSpace(m.Remark), | |
| Style: style, | |
| Prompt: TruncatePromptProfileText(m.Prompt), | |
| } | |
| } | |
| // Clone 深拷贝(字段全是值类型,等价于 Normalize)。 | |
| func (m ModelProfileConfig) Clone() ModelProfileConfig { | |
| return m.Normalize() | |
| } | |
| // IsValidModelProfileID 判断模型名是否符合字符集要求:字母、数字、点、下划线、短横线。 | |
| func IsValidModelProfileID(id string) bool { | |
| id = strings.TrimSpace(id) | |
| if id == "" || utf8.RuneCountInString(id) > MaxModelProfileIDChars { | |
| return false | |
| } | |
| for _, r := range id { | |
| switch { | |
| case r >= 'a' && r <= 'z': | |
| case r >= 'A' && r <= 'Z': | |
| case r >= '0' && r <= '9': | |
| case r == '.', r == '_', r == '-': | |
| default: | |
| return false | |
| } | |
| } | |
| return true | |
| } | |
| // FindModelProfile 按模型名查找自定义模型,大小写不敏感。 | |
| func FindModelProfile(profiles []ModelProfileConfig, id string) (ModelProfileConfig, bool) { | |
| wanted := strings.ToLower(strings.TrimSpace(id)) | |
| if wanted == "" { | |
| return ModelProfileConfig{}, false | |
| } | |
| for _, profile := range profiles { | |
| if strings.ToLower(strings.TrimSpace(profile.ID)) == wanted { | |
| return profile, true | |
| } | |
| } | |
| return ModelProfileConfig{}, false | |
| } | |
| // NormalizeModelProfiles 归一化自定义模型列表:裁剪、去重(按模型名,先到先得)。 | |
| func NormalizeModelProfiles(items []ModelProfileConfig) []ModelProfileConfig { | |
| if len(items) == 0 { | |
| return nil | |
| } | |
| out := make([]ModelProfileConfig, 0, len(items)) | |
| seen := make(map[string]struct{}, len(items)) | |
| for _, item := range items { | |
| normalized := item.Normalize() | |
| if normalized.ID == "" { | |
| continue | |
| } | |
| key := strings.ToLower(normalized.ID) | |
| if _, ok := seen[key]; ok { | |
| continue | |
| } | |
| seen[key] = struct{}{} | |
| out = append(out, normalized) | |
| } | |
| if len(out) == 0 { | |
| return nil | |
| } | |
| return out | |
| } | |
| // ValidateModelProfiles 严格校验自定义模型列表,供后台写入使用。 | |
| func ValidateModelProfiles(items []ModelProfileConfig) error { | |
| if len(items) > MaxModelProfiles { | |
| return fmt.Errorf("model_profiles cannot exceed %d entries", MaxModelProfiles) | |
| } | |
| baseModels := make(map[string]struct{}, 4) | |
| for _, base := range SupportedBaseModels() { | |
| baseModels[base] = struct{}{} | |
| } | |
| seen := make(map[string]struct{}, len(items)) | |
| for i, raw := range items { | |
| // 长度上限要拿原始值判断:Normalize 会先截断,截断后再比永远不超限。 | |
| if utf8.RuneCountInString(strings.TrimSpace(raw.Prompt)) > MaxPromptProfileChars { | |
| return fmt.Errorf("model_profiles[%d].prompt cannot exceed %d characters", | |
| i, MaxPromptProfileChars) | |
| } | |
| item := raw.Normalize() | |
| if item.ID == "" { | |
| return fmt.Errorf("model_profiles[%d].id cannot be empty", i) | |
| } | |
| if !IsValidModelProfileID(item.ID) { | |
| return fmt.Errorf("model_profiles[%d].id %q must be 1-%d characters of [A-Za-z0-9._-]", | |
| i, item.ID, MaxModelProfileIDChars) | |
| } | |
| key := strings.ToLower(item.ID) | |
| if _, ok := seen[key]; ok { | |
| return fmt.Errorf("duplicate model_profiles id: %s", item.ID) | |
| } | |
| seen[key] = struct{}{} | |
| // 不允许覆盖内置模型名:这会静默改变 flash 系列的行为。 | |
| if _, builtin := resolveSupportedModel(item.ID); builtin { | |
| return fmt.Errorf("model_profiles id %q shadows a built-in model name", item.ID) | |
| } | |
| if item.Base == "" { | |
| return fmt.Errorf("model_profiles[%d].base cannot be empty", i) | |
| } | |
| if _, ok := baseModels[item.Base]; !ok { | |
| return fmt.Errorf("model_profiles[%d].base must be one of %s", | |
| i, strings.Join(SupportedBaseModels(), ", ")) | |
| } | |
| if rawStyle := strings.TrimSpace(raw.Style); rawStyle != "" && !IsKnownModelProfileStyle(rawStyle) { | |
| return fmt.Errorf("model_profiles[%d].style must be one of %s", | |
| i, strings.Join(ModelProfileStyleNames(), ", ")) | |
| } | |
| } | |
| return nil | |
| } | |