global.go 2.1 KB

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