Files
compute-engine/service/openai_chat_responses_mode.go
Pine 28e53d90fc feat: 算力引擎 Go 后端核心(new-api)
- 统一 OpenAI 兼容 /v1 中继 + 渠道/额度/令牌/流水
- 各领域包:relay 模型网关、model 数据层、controller 管理 API
2026-08-23 22:38:46 +08:00

55 lines
1.4 KiB
Go

package service
import (
"regexp"
"sync"
"github.com/QuantumNous/new-api/setting/model_setting"
)
// Chat→Responses upgrade policy is host routing logic (it decides *whether*
// to convert, reading host settings), so it lives here, not in relayconvert.
var chatResponsesRegexCache sync.Map // map[string]*regexp.Regexp
func matchAnyModelPattern(patterns []string, model string) bool {
if len(patterns) == 0 || model == "" {
return false
}
for _, pattern := range patterns {
if pattern == "" {
continue
}
re, ok := chatResponsesRegexCache.Load(pattern)
if !ok {
compiled, err := regexp.Compile(pattern)
if err != nil {
// Treat invalid patterns as non-matching to avoid breaking runtime traffic.
continue
}
re = compiled
chatResponsesRegexCache.Store(pattern, re)
}
if re.(*regexp.Regexp).MatchString(model) {
return true
}
}
return false
}
func ShouldChatCompletionsUseResponsesPolicy(policy model_setting.ChatCompletionsToResponsesPolicy, channelID int, channelType int, model string) bool {
if !policy.IsChannelEnabled(channelID, channelType) {
return false
}
return matchAnyModelPattern(policy.ModelPatterns, model)
}
func ShouldChatCompletionsUseResponsesGlobal(channelID int, channelType int, model string) bool {
return ShouldChatCompletionsUseResponsesPolicy(
model_setting.GetGlobalSettings().ChatCompletionsToResponsesPolicy,
channelID,
channelType,
model,
)
}