global.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package model_setting
  2. import (
  3. "slices"
  4. "strings"
  5. "github.com/QuantumNous/new-api/setting/config"
  6. )
  7. type ChatCompletionsToResponsesPolicy struct {
  8. Enabled bool `json:"enabled"`
  9. AllChannels bool `json:"all_channels"`
  10. ChannelIDs []int `json:"channel_ids,omitempty"`
  11. ModelPatterns []string `json:"model_patterns,omitempty"`
  12. }
  13. func (p ChatCompletionsToResponsesPolicy) IsChannelEnabled(channelID int) bool {
  14. if !p.Enabled {
  15. return false
  16. }
  17. if p.AllChannels {
  18. return true
  19. }
  20. if channelID == 0 || len(p.ChannelIDs) == 0 {
  21. return false
  22. }
  23. return slices.Contains(p.ChannelIDs, channelID)
  24. }
  25. type GlobalSettings struct {
  26. PassThroughRequestEnabled bool `json:"pass_through_request_enabled"`
  27. ThinkingModelBlacklist []string `json:"thinking_model_blacklist"`
  28. ChatCompletionsToResponsesPolicy ChatCompletionsToResponsesPolicy `json:"chat_completions_to_responses_policy"`
  29. }
  30. // 默认配置
  31. var defaultOpenaiSettings = GlobalSettings{
  32. PassThroughRequestEnabled: false,
  33. ThinkingModelBlacklist: []string{
  34. "moonshotai/kimi-k2-thinking",
  35. "kimi-k2-thinking",
  36. },
  37. ChatCompletionsToResponsesPolicy: ChatCompletionsToResponsesPolicy{
  38. Enabled: false,
  39. AllChannels: true,
  40. },
  41. }
  42. // 全局实例
  43. var globalSettings = defaultOpenaiSettings
  44. func init() {
  45. // 注册到全局配置管理器
  46. config.GlobalConfig.Register("global", &globalSettings)
  47. }
  48. func GetGlobalSettings() *GlobalSettings {
  49. return &globalSettings
  50. }
  51. // ShouldPreserveThinkingSuffix 判断模型是否配置为保留 thinking/-nothinking/-low/-high/-medium 后缀
  52. func ShouldPreserveThinkingSuffix(modelName string) bool {
  53. target := strings.TrimSpace(modelName)
  54. if target == "" {
  55. return false
  56. }
  57. for _, entry := range globalSettings.ThinkingModelBlacklist {
  58. if strings.TrimSpace(entry) == target {
  59. return true
  60. }
  61. }
  62. return false
  63. }