feat: publish v1.0.0-rc.36-custom.1
This commit is contained in:
commit
3a7e8f3b54
2381 files changed
+551494
No files matched your search
@@ -0,0 +1,111 @@
|
||||
package model_setting
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/QuantumNous/new-api/common"
|
||||
"github.com/QuantumNous/new-api/setting/config"
|
||||
)
|
||||
|
||||
//var claudeHeadersSettings = map[string][]string{}
|
||||
//
|
||||
//var ClaudeThinkingAdapterEnabled = true
|
||||
//var ClaudeThinkingAdapterMaxTokens = 8192
|
||||
//var ClaudeThinkingAdapterBudgetTokensPercentage = 0.8
|
||||
|
||||
// ClaudeSettings 定义Claude模型的配置
|
||||
type ClaudeSettings struct {
|
||||
HeadersSettings map[string]map[string][]string `json:"model_headers_settings"`
|
||||
DefaultMaxTokens map[string]int `json:"default_max_tokens"`
|
||||
ThinkingAdapterEnabled bool `json:"thinking_adapter_enabled"`
|
||||
ThinkingAdapterBudgetTokensPercentage float64 `json:"thinking_adapter_budget_tokens_percentage"`
|
||||
}
|
||||
|
||||
// 默认配置
|
||||
var defaultClaudeSettings = ClaudeSettings{
|
||||
HeadersSettings: map[string]map[string][]string{},
|
||||
ThinkingAdapterEnabled: true,
|
||||
DefaultMaxTokens: map[string]int{
|
||||
"default": 8192,
|
||||
},
|
||||
ThinkingAdapterBudgetTokensPercentage: 0.8,
|
||||
}
|
||||
|
||||
// 全局实例
|
||||
var claudeSettings = defaultClaudeSettings
|
||||
|
||||
func init() {
|
||||
// 注册到全局配置管理器
|
||||
config.GlobalConfig.Register("claude", &claudeSettings)
|
||||
}
|
||||
|
||||
// GetClaudeSettings 获取Claude配置
|
||||
func GetClaudeSettings() *ClaudeSettings {
|
||||
// check default max tokens must have default key
|
||||
if _, ok := claudeSettings.DefaultMaxTokens["default"]; !ok {
|
||||
claudeSettings.DefaultMaxTokens["default"] = 8192
|
||||
}
|
||||
return &claudeSettings
|
||||
}
|
||||
|
||||
func (c *ClaudeSettings) WriteHeaders(originModel string, httpHeader *http.Header) {
|
||||
if headers, ok := c.HeadersSettings[originModel]; ok {
|
||||
for headerKey, headerValues := range headers {
|
||||
mergedValues := normalizeHeaderListValues(
|
||||
append(append([]string(nil), httpHeader.Values(headerKey)...), headerValues...),
|
||||
)
|
||||
if len(mergedValues) == 0 {
|
||||
continue
|
||||
}
|
||||
httpHeader.Set(headerKey, strings.Join(mergedValues, ","))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func normalizeHeaderListValues(values []string) []string {
|
||||
normalizedValues := make([]string, 0, len(values))
|
||||
seenValues := make(map[string]struct{}, len(values))
|
||||
for _, value := range values {
|
||||
for item := range strings.SplitSeq(value, ",") {
|
||||
normalizedItem := strings.TrimSpace(item)
|
||||
if normalizedItem == "" {
|
||||
continue
|
||||
}
|
||||
if _, exists := seenValues[normalizedItem]; exists {
|
||||
continue
|
||||
}
|
||||
seenValues[normalizedItem] = struct{}{}
|
||||
normalizedValues = append(normalizedValues, normalizedItem)
|
||||
}
|
||||
}
|
||||
return normalizedValues
|
||||
}
|
||||
|
||||
func (c *ClaudeSettings) GetDefaultMaxTokens(model string) int {
|
||||
if maxTokens, ok := c.DefaultMaxTokens[model]; ok {
|
||||
return maxTokens
|
||||
}
|
||||
return c.DefaultMaxTokens["default"]
|
||||
}
|
||||
|
||||
// ValidateClaudeDefaultMaxTokens validates the JSON persisted by the option
|
||||
// API. Zero stays allowed — the current Messages API accepts max_tokens: 0 as
|
||||
// cache pre-warming — but negative values are rejected because they would
|
||||
// wrap into huge unsigned values during request conversion.
|
||||
func ValidateClaudeDefaultMaxTokens(value string) error {
|
||||
var settings map[string]int
|
||||
if err := common.UnmarshalJsonStr(value, &settings); err != nil {
|
||||
return fmt.Errorf("Claude default max tokens must be a JSON map of model to integer: %w", err)
|
||||
}
|
||||
if settings == nil {
|
||||
return fmt.Errorf("Claude default max tokens must be a JSON map of model to integer")
|
||||
}
|
||||
for model, maxTokens := range settings {
|
||||
if maxTokens < 0 {
|
||||
return fmt.Errorf("negative Claude default max_tokens %d for %q", maxTokens, model)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
package model_setting
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestClaudeSettingsWriteHeadersMergesConfiguredValuesIntoSingleHeader(t *testing.T) {
|
||||
settings := &ClaudeSettings{
|
||||
HeadersSettings: map[string]map[string][]string{
|
||||
"claude-3-7-sonnet-20250219-thinking": {
|
||||
"anthropic-beta": {
|
||||
"token-efficient-tools-2025-02-19",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
headers := http.Header{}
|
||||
headers.Set("anthropic-beta", "output-128k-2025-02-19")
|
||||
|
||||
settings.WriteHeaders("claude-3-7-sonnet-20250219-thinking", &headers)
|
||||
|
||||
got := headers.Values("anthropic-beta")
|
||||
if len(got) != 1 {
|
||||
t.Fatalf("expected a single merged header value, got %v", got)
|
||||
}
|
||||
expected := "output-128k-2025-02-19,token-efficient-tools-2025-02-19"
|
||||
if got[0] != expected {
|
||||
t.Fatalf("expected merged header %q, got %q", expected, got[0])
|
||||
}
|
||||
}
|
||||
|
||||
func TestClaudeSettingsWriteHeadersDeduplicatesAcrossCommaSeparatedAndRepeatedValues(t *testing.T) {
|
||||
settings := &ClaudeSettings{
|
||||
HeadersSettings: map[string]map[string][]string{
|
||||
"claude-3-7-sonnet-20250219-thinking": {
|
||||
"anthropic-beta": {
|
||||
"token-efficient-tools-2025-02-19",
|
||||
"computer-use-2025-01-24",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
headers := http.Header{}
|
||||
headers.Add("anthropic-beta", "output-128k-2025-02-19, token-efficient-tools-2025-02-19")
|
||||
headers.Add("anthropic-beta", "token-efficient-tools-2025-02-19")
|
||||
|
||||
settings.WriteHeaders("claude-3-7-sonnet-20250219-thinking", &headers)
|
||||
|
||||
got := headers.Values("anthropic-beta")
|
||||
if len(got) != 1 {
|
||||
t.Fatalf("expected duplicate values to collapse into one header, got %v", got)
|
||||
}
|
||||
expected := "output-128k-2025-02-19,token-efficient-tools-2025-02-19,computer-use-2025-01-24"
|
||||
if got[0] != expected {
|
||||
t.Fatalf("expected deduplicated merged header %q, got %q", expected, got[0])
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateClaudeDefaultMaxTokens(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
value string
|
||||
wantErr string
|
||||
}{
|
||||
{name: "positive default", value: `{"default": 8192}`},
|
||||
{name: "zero allowed", value: `{"default": 0}`},
|
||||
{name: "zero model override allowed", value: `{"default": 8192, "claude-test": 0}`},
|
||||
{name: "empty map allowed", value: `{}`},
|
||||
{name: "negative default rejected", value: `{"default": -1}`, wantErr: `negative Claude default max_tokens -1 for "default"`},
|
||||
{name: "negative model override rejected", value: `{"default": 8192, "claude-test": -5}`, wantErr: `negative Claude default max_tokens -5 for "claude-test"`},
|
||||
{name: "non-integer rejected", value: `{"default": "high"}`, wantErr: "JSON map of model to integer"},
|
||||
{name: "null rejected", value: `null`, wantErr: "JSON map of model to integer"},
|
||||
{name: "malformed rejected", value: `{`, wantErr: "JSON map of model to integer"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
err := ValidateClaudeDefaultMaxTokens(tt.value)
|
||||
if tt.wantErr == "" {
|
||||
require.NoError(t, err)
|
||||
return
|
||||
}
|
||||
require.Error(t, err)
|
||||
assert.Contains(t, err.Error(), tt.wantErr)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
package model_setting
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"slices"
|
||||
|
||||
"github.com/QuantumNous/new-api/common"
|
||||
"github.com/QuantumNous/new-api/setting/config"
|
||||
)
|
||||
|
||||
const defaultGeminiSafetySetting = "OFF"
|
||||
|
||||
var validGeminiSafetySettings = map[string]struct{}{
|
||||
"OFF": {},
|
||||
"BLOCK_NONE": {},
|
||||
"BLOCK_ONLY_HIGH": {},
|
||||
"BLOCK_MEDIUM_AND_ABOVE": {},
|
||||
"BLOCK_LOW_AND_ABOVE": {},
|
||||
"HARM_BLOCK_THRESHOLD_UNSPECIFIED": {},
|
||||
}
|
||||
|
||||
// GeminiSettings defines Gemini model configuration. 注意bool要以enabled结尾才可以生效编辑
|
||||
type GeminiSettings struct {
|
||||
SafetySettings map[string]string `json:"safety_settings"`
|
||||
VersionSettings map[string]string `json:"version_settings"`
|
||||
SupportedImagineModels []string `json:"supported_imagine_models"`
|
||||
ThinkingAdapterEnabled bool `json:"thinking_adapter_enabled"`
|
||||
ThinkingAdapterBudgetTokensPercentage float64 `json:"thinking_adapter_budget_tokens_percentage"`
|
||||
FunctionCallThoughtSignatureEnabled bool `json:"function_call_thought_signature_enabled"`
|
||||
RemoveFunctionResponseIdEnabled bool `json:"remove_function_response_id_enabled"`
|
||||
}
|
||||
|
||||
// 默认配置
|
||||
var defaultGeminiSettings = GeminiSettings{
|
||||
SafetySettings: map[string]string{
|
||||
"default": defaultGeminiSafetySetting,
|
||||
},
|
||||
VersionSettings: map[string]string{
|
||||
"default": "v1beta",
|
||||
"gemini-1.0-pro": "v1",
|
||||
},
|
||||
SupportedImagineModels: []string{
|
||||
"gemini-2.0-flash-exp-image-generation",
|
||||
"gemini-2.0-flash-exp",
|
||||
"gemini-3-pro-image-preview",
|
||||
"gemini-3-pro-image",
|
||||
"gemini-2.5-flash-image",
|
||||
"gemini-3.1-flash-image",
|
||||
"gemini-3.1-flash-image-preview",
|
||||
},
|
||||
ThinkingAdapterEnabled: false,
|
||||
ThinkingAdapterBudgetTokensPercentage: 0.6,
|
||||
FunctionCallThoughtSignatureEnabled: true,
|
||||
RemoveFunctionResponseIdEnabled: true,
|
||||
}
|
||||
|
||||
// 全局实例
|
||||
var geminiSettings = defaultGeminiSettings
|
||||
|
||||
func init() {
|
||||
// 注册到全局配置管理器
|
||||
config.GlobalConfig.Register("gemini", &geminiSettings)
|
||||
}
|
||||
|
||||
// GetGeminiSettings 获取Gemini配置
|
||||
func GetGeminiSettings() *GeminiSettings {
|
||||
return &geminiSettings
|
||||
}
|
||||
|
||||
// GetGeminiSafetySetting 获取安全设置
|
||||
func GetGeminiSafetySetting(key string) string {
|
||||
settings := geminiSettings.SafetySettings
|
||||
if value := settings[key]; value != "" {
|
||||
return value
|
||||
}
|
||||
if value := settings["default"]; value != "" {
|
||||
return value
|
||||
}
|
||||
return defaultGeminiSafetySetting
|
||||
}
|
||||
|
||||
// ValidateGeminiSafetySettings validates the JSON persisted by the option API.
|
||||
// Empty values remain valid because read-time fallback returns the default.
|
||||
func ValidateGeminiSafetySettings(value string) error {
|
||||
var settings map[string]string
|
||||
if err := common.UnmarshalJsonStr(value, &settings); err != nil {
|
||||
return fmt.Errorf("Gemini safety settings must be a JSON string map: %w", err)
|
||||
}
|
||||
if settings == nil {
|
||||
return fmt.Errorf("Gemini safety settings must be a JSON string map")
|
||||
}
|
||||
for category, threshold := range settings {
|
||||
if threshold == "" {
|
||||
continue
|
||||
}
|
||||
if _, ok := validGeminiSafetySettings[threshold]; !ok {
|
||||
return fmt.Errorf("invalid Gemini safety threshold %q for %q", threshold, category)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetGeminiVersionSetting 获取版本设置
|
||||
func GetGeminiVersionSetting(key string) string {
|
||||
if value, ok := geminiSettings.VersionSettings[key]; ok {
|
||||
return value
|
||||
}
|
||||
return geminiSettings.VersionSettings["default"]
|
||||
}
|
||||
|
||||
func IsGeminiModelSupportImagine(model string) bool {
|
||||
return slices.Contains(geminiSettings.SupportedImagineModels, model)
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
package model_setting
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestGeminiSafetySettingsReadNormalization(t *testing.T) {
|
||||
original := geminiSettings.SafetySettings
|
||||
t.Cleanup(func() {
|
||||
geminiSettings.SafetySettings = original
|
||||
})
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
settings map[string]string
|
||||
key string
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "nil map gets OFF default",
|
||||
settings: nil,
|
||||
key: "HARM_CATEGORY_HATE_SPEECH",
|
||||
want: "OFF",
|
||||
},
|
||||
{
|
||||
name: "missing default gets OFF without replacing existing values",
|
||||
settings: map[string]string{
|
||||
"HARM_CATEGORY_HATE_SPEECH": "BLOCK_SOME",
|
||||
},
|
||||
key: "HARM_CATEGORY_HATE_SPEECH",
|
||||
want: "BLOCK_SOME",
|
||||
},
|
||||
{
|
||||
name: "empty default gets OFF",
|
||||
settings: map[string]string{
|
||||
"default": "",
|
||||
},
|
||||
key: "HARM_CATEGORY_HATE_SPEECH",
|
||||
want: "OFF",
|
||||
},
|
||||
{
|
||||
name: "empty override falls back to configured default",
|
||||
settings: map[string]string{
|
||||
"default": "BLOCK_ONLY_HIGH",
|
||||
"HARM_CATEGORY_HATE_SPEECH": "",
|
||||
},
|
||||
key: "HARM_CATEGORY_HATE_SPEECH",
|
||||
want: "BLOCK_ONLY_HIGH",
|
||||
},
|
||||
{
|
||||
name: "historical invalid nonempty default is preserved",
|
||||
settings: map[string]string{
|
||||
"default": "BLOCK_SOME",
|
||||
},
|
||||
key: "HARM_CATEGORY_HATE_SPEECH",
|
||||
want: "BLOCK_SOME",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
geminiSettings.SafetySettings = test.settings
|
||||
|
||||
assert.Equal(t, test.want, GetGeminiSafetySetting(test.key))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateGeminiSafetySettings(t *testing.T) {
|
||||
valid := []string{
|
||||
`{}`,
|
||||
`{"default":""}`,
|
||||
`{"HARM_CATEGORY_HATE_SPEECH":""}`,
|
||||
`{"default":"OFF"}`,
|
||||
`{"default":"BLOCK_NONE"}`,
|
||||
`{"default":"BLOCK_ONLY_HIGH"}`,
|
||||
`{"default":"BLOCK_MEDIUM_AND_ABOVE"}`,
|
||||
`{"default":"BLOCK_LOW_AND_ABOVE"}`,
|
||||
`{"default":"HARM_BLOCK_THRESHOLD_UNSPECIFIED"}`,
|
||||
}
|
||||
for _, value := range valid {
|
||||
require.NoError(t, ValidateGeminiSafetySettings(value), value)
|
||||
}
|
||||
|
||||
invalid := []string{
|
||||
`null`,
|
||||
`[]`,
|
||||
`{"default":1}`,
|
||||
`{"default":"BLOCK_SOME"}`,
|
||||
`{"default":" off "}`,
|
||||
`{"default":`,
|
||||
}
|
||||
for _, value := range invalid {
|
||||
assert.Error(t, ValidateGeminiSafetySettings(value), value)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,187 @@
|
||||
package model_setting
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"slices"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/QuantumNous/new-api/common"
|
||||
"github.com/QuantumNous/new-api/setting/config"
|
||||
)
|
||||
|
||||
type ChatCompletionsToResponsesPolicy struct {
|
||||
Enabled bool `json:"enabled"`
|
||||
AllChannels bool `json:"all_channels"`
|
||||
ChannelIDs []int `json:"channel_ids,omitempty"`
|
||||
ChannelTypes []int `json:"channel_types,omitempty"`
|
||||
ModelPatterns []string `json:"model_patterns,omitempty"`
|
||||
}
|
||||
|
||||
func (p ChatCompletionsToResponsesPolicy) IsChannelEnabled(channelID int, channelType int) bool {
|
||||
if !p.Enabled {
|
||||
return false
|
||||
}
|
||||
if p.AllChannels {
|
||||
return true
|
||||
}
|
||||
|
||||
if channelID > 0 && len(p.ChannelIDs) > 0 && slices.Contains(p.ChannelIDs, channelID) {
|
||||
return true
|
||||
}
|
||||
if channelType > 0 && len(p.ChannelTypes) > 0 && slices.Contains(p.ChannelTypes, channelType) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
type GlobalSettings struct {
|
||||
PassThroughRequestEnabled bool `json:"pass_through_request_enabled"`
|
||||
ThinkingModelBlacklist []string `json:"thinking_model_blacklist"`
|
||||
// EffortTailModelIDs lists real model IDs that sit inside the GPT/o-series
|
||||
// family whitelist but whose names already end in an effort word.
|
||||
EffortTailModelIDs []string `json:"effort_tail_model_ids"`
|
||||
ChatCompletionsToResponsesPolicy ChatCompletionsToResponsesPolicy `json:"chat_completions_to_responses_policy"`
|
||||
}
|
||||
|
||||
// 默认配置
|
||||
var defaultOpenaiSettings = GlobalSettings{
|
||||
PassThroughRequestEnabled: false,
|
||||
ThinkingModelBlacklist: []string{
|
||||
"moonshotai/kimi-k2-thinking",
|
||||
"kimi-k2-thinking",
|
||||
},
|
||||
EffortTailModelIDs: []string{
|
||||
"gpt-5.1-codex-max",
|
||||
"qwen-image-edit-max",
|
||||
"qwen-max",
|
||||
"stable-diffusion-3-medium",
|
||||
"yi-medium",
|
||||
},
|
||||
ChatCompletionsToResponsesPolicy: ChatCompletionsToResponsesPolicy{
|
||||
Enabled: false,
|
||||
AllChannels: true,
|
||||
},
|
||||
}
|
||||
|
||||
// 全局实例
|
||||
var globalSettings = defaultOpenaiSettings
|
||||
|
||||
func init() {
|
||||
// 注册到全局配置管理器
|
||||
config.GlobalConfig.Register("global", &globalSettings)
|
||||
}
|
||||
|
||||
func GetGlobalSettings() *GlobalSettings {
|
||||
return &globalSettings
|
||||
}
|
||||
|
||||
const thinkingBlacklistRegexPrefix = "re:"
|
||||
|
||||
type thinkingBlacklistCompiled struct {
|
||||
source string
|
||||
exact []string
|
||||
regexes []*regexp.Regexp
|
||||
}
|
||||
|
||||
var (
|
||||
thinkingBlacklistMu sync.RWMutex
|
||||
thinkingBlacklistCache thinkingBlacklistCompiled
|
||||
)
|
||||
|
||||
func thinkingBlacklistSourceKey(entries []string) string {
|
||||
return strings.Join(entries, "\x00")
|
||||
}
|
||||
|
||||
func compiledThinkingBlacklist() ([]string, []*regexp.Regexp) {
|
||||
entries := globalSettings.ThinkingModelBlacklist
|
||||
key := thinkingBlacklistSourceKey(entries)
|
||||
|
||||
thinkingBlacklistMu.RLock()
|
||||
if thinkingBlacklistCache.source == key {
|
||||
exact, regexes := thinkingBlacklistCache.exact, thinkingBlacklistCache.regexes
|
||||
thinkingBlacklistMu.RUnlock()
|
||||
return exact, regexes
|
||||
}
|
||||
thinkingBlacklistMu.RUnlock()
|
||||
|
||||
thinkingBlacklistMu.Lock()
|
||||
defer thinkingBlacklistMu.Unlock()
|
||||
if thinkingBlacklistCache.source == key {
|
||||
return thinkingBlacklistCache.exact, thinkingBlacklistCache.regexes
|
||||
}
|
||||
|
||||
exact := make([]string, 0, len(entries))
|
||||
var regexes []*regexp.Regexp
|
||||
for _, entry := range entries {
|
||||
entry = strings.TrimSpace(entry)
|
||||
if entry == "" {
|
||||
continue
|
||||
}
|
||||
if after, ok := strings.CutPrefix(entry, thinkingBlacklistRegexPrefix); ok {
|
||||
pattern := after
|
||||
if pattern == "" {
|
||||
common.SysError(fmt.Sprintf("invalid thinking_model_blacklist regex %q: pattern is empty", entry))
|
||||
continue
|
||||
}
|
||||
re, err := regexp.Compile(pattern)
|
||||
if err != nil {
|
||||
common.SysError(fmt.Sprintf("invalid thinking_model_blacklist regex %q: %v", entry, err))
|
||||
continue
|
||||
}
|
||||
regexes = append(regexes, re)
|
||||
continue
|
||||
}
|
||||
exact = append(exact, entry)
|
||||
}
|
||||
thinkingBlacklistCache = thinkingBlacklistCompiled{source: key, exact: exact, regexes: regexes}
|
||||
return exact, regexes
|
||||
}
|
||||
|
||||
// ShouldPreserveThinkingSuffix reports whether the full model name is exempt
|
||||
// from host thinking-suffix and @-modifier parsing. Exact blacklist entries
|
||||
// match the complete name; entries prefixed with re: are Go regular expressions
|
||||
// matched with MatchString against the same full name.
|
||||
func ShouldPreserveThinkingSuffix(modelName string) bool {
|
||||
target := strings.TrimSpace(modelName)
|
||||
if target == "" {
|
||||
return false
|
||||
}
|
||||
|
||||
exact, regexes := compiledThinkingBlacklist()
|
||||
if slices.Contains(exact, target) {
|
||||
return true
|
||||
}
|
||||
for _, re := range regexes {
|
||||
if re.MatchString(target) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// ShouldPreserveEffortTail reports whether modelName is a real model ID whose
|
||||
// name already ends in an effort word. Entries match the complete name and the
|
||||
// de-namespaced bare name.
|
||||
func ShouldPreserveEffortTail(modelName string) bool {
|
||||
target := strings.TrimSpace(modelName)
|
||||
if target == "" {
|
||||
return false
|
||||
}
|
||||
bare := target
|
||||
if slash := strings.LastIndex(target, "/"); slash >= 0 {
|
||||
bare = target[slash+1:]
|
||||
}
|
||||
|
||||
for _, entry := range globalSettings.EffortTailModelIDs {
|
||||
entry = strings.TrimSpace(entry)
|
||||
if entry == "" {
|
||||
continue
|
||||
}
|
||||
if entry == target || entry == bare {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package model_setting
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestShouldPreserveThinkingSuffixExactAndRegex(t *testing.T) {
|
||||
settings := GetGlobalSettings()
|
||||
original := append([]string(nil), settings.ThinkingModelBlacklist...)
|
||||
t.Cleanup(func() { settings.ThinkingModelBlacklist = original })
|
||||
|
||||
assert.True(t, ShouldPreserveThinkingSuffix("kimi-k2-thinking"))
|
||||
assert.True(t, ShouldPreserveThinkingSuffix("moonshotai/kimi-k2-thinking"))
|
||||
assert.False(t, ShouldPreserveThinkingSuffix("m@sha256:abc"))
|
||||
|
||||
settings.ThinkingModelBlacklist = []string{
|
||||
"kimi-k2-thinking",
|
||||
"re:[",
|
||||
"re:",
|
||||
"re:.*@sha256:.*",
|
||||
}
|
||||
|
||||
var logged bytes.Buffer
|
||||
previous := gin.DefaultErrorWriter
|
||||
gin.DefaultErrorWriter = &logged
|
||||
t.Cleanup(func() { gin.DefaultErrorWriter = previous })
|
||||
|
||||
assert.True(t, ShouldPreserveThinkingSuffix("kimi-k2-thinking"))
|
||||
assert.True(t, ShouldPreserveThinkingSuffix("m@sha256:abc"))
|
||||
assert.False(t, ShouldPreserveThinkingSuffix("m@sha256"))
|
||||
assert.False(t, ShouldPreserveThinkingSuffix("qwen3-max@thinking:on"))
|
||||
require.Contains(t, logged.String(), `invalid thinking_model_blacklist regex "re:["`)
|
||||
require.Contains(t, logged.String(), `invalid thinking_model_blacklist regex "re:"`)
|
||||
|
||||
settings.ThinkingModelBlacklist = []string{"re:^beta@"}
|
||||
assert.False(t, ShouldPreserveThinkingSuffix("m@sha256:abc"))
|
||||
assert.True(t, ShouldPreserveThinkingSuffix("beta@sha256:abc"))
|
||||
assert.False(t, ShouldPreserveThinkingSuffix("alpha@sha256:abc"))
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package model_setting
|
||||
|
||||
import "github.com/QuantumNous/new-api/setting/config"
|
||||
|
||||
// GrokSettings defines Grok model configuration.
|
||||
type GrokSettings struct {
|
||||
ViolationDeductionEnabled bool `json:"violation_deduction_enabled"`
|
||||
ViolationDeductionAmount float64 `json:"violation_deduction_amount"`
|
||||
}
|
||||
|
||||
var defaultGrokSettings = GrokSettings{
|
||||
ViolationDeductionEnabled: true,
|
||||
ViolationDeductionAmount: 0.05,
|
||||
}
|
||||
|
||||
var grokSettings = defaultGrokSettings
|
||||
|
||||
func init() {
|
||||
config.GlobalConfig.Register("grok", &grokSettings)
|
||||
}
|
||||
|
||||
func GetGrokSettings() *GrokSettings {
|
||||
return &grokSettings
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package model_setting
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/QuantumNous/new-api/setting/config"
|
||||
)
|
||||
|
||||
// QwenSettings defines Qwen model configuration. 注意bool要以enabled结尾才可以生效编辑
|
||||
type QwenSettings struct {
|
||||
SyncImageModels []string `json:"sync_image_models"`
|
||||
}
|
||||
|
||||
// 默认配置
|
||||
var defaultQwenSettings = QwenSettings{
|
||||
SyncImageModels: []string{
|
||||
"z-image",
|
||||
"qwen-image",
|
||||
"wan2.6",
|
||||
"wan2.7",
|
||||
"qwen-image-edit",
|
||||
"qwen-image-edit-max",
|
||||
"qwen-image-edit-max-2026-01-16",
|
||||
"qwen-image-edit-plus",
|
||||
"qwen-image-edit-plus-2025-12-15",
|
||||
"qwen-image-edit-plus-2025-10-30",
|
||||
},
|
||||
}
|
||||
|
||||
// 全局实例
|
||||
var qwenSettings = defaultQwenSettings
|
||||
|
||||
func init() {
|
||||
// 注册到全局配置管理器
|
||||
config.GlobalConfig.Register("qwen", &qwenSettings)
|
||||
}
|
||||
|
||||
// GetQwenSettings
|
||||
func GetQwenSettings() *QwenSettings {
|
||||
return &qwenSettings
|
||||
}
|
||||
|
||||
// IsSyncImageModel
|
||||
func IsSyncImageModel(model string) bool {
|
||||
for _, m := range qwenSettings.SyncImageModels {
|
||||
if strings.Contains(model, m) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
Reference in new issue
Block a user