channel_affinity_setting.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package operation_setting
  2. import "github.com/QuantumNous/new-api/setting/config"
  3. type ChannelAffinityKeySource struct {
  4. Type string `json:"type"` // context_int, context_string, gjson
  5. Key string `json:"key,omitempty"`
  6. Path string `json:"path,omitempty"`
  7. }
  8. type ChannelAffinityRule struct {
  9. Name string `json:"name"`
  10. ModelRegex []string `json:"model_regex"`
  11. PathRegex []string `json:"path_regex"`
  12. UserAgentInclude []string `json:"user_agent_include,omitempty"`
  13. KeySources []ChannelAffinityKeySource `json:"key_sources"`
  14. ValueRegex string `json:"value_regex"`
  15. TTLSeconds int `json:"ttl_seconds"`
  16. SkipRetryOnFailure bool `json:"skip_retry_on_failure,omitempty"`
  17. IncludeUsingGroup bool `json:"include_using_group"`
  18. IncludeRuleName bool `json:"include_rule_name"`
  19. }
  20. type ChannelAffinitySetting struct {
  21. Enabled bool `json:"enabled"`
  22. SwitchOnSuccess bool `json:"switch_on_success"`
  23. MaxEntries int `json:"max_entries"`
  24. DefaultTTLSeconds int `json:"default_ttl_seconds"`
  25. Rules []ChannelAffinityRule `json:"rules"`
  26. }
  27. var channelAffinitySetting = ChannelAffinitySetting{
  28. Enabled: true,
  29. SwitchOnSuccess: true,
  30. MaxEntries: 100_000,
  31. DefaultTTLSeconds: 3600,
  32. Rules: []ChannelAffinityRule{
  33. {
  34. Name: "codex trace",
  35. ModelRegex: []string{"^gpt-.*$"},
  36. PathRegex: []string{"/v1/responses"},
  37. KeySources: []ChannelAffinityKeySource{
  38. {Type: "gjson", Path: "prompt_cache_key"},
  39. },
  40. ValueRegex: "",
  41. TTLSeconds: 0,
  42. SkipRetryOnFailure: false,
  43. IncludeUsingGroup: true,
  44. IncludeRuleName: true,
  45. UserAgentInclude: nil,
  46. },
  47. {
  48. Name: "claude code trace",
  49. ModelRegex: []string{"^claude-.*$"},
  50. PathRegex: []string{"/v1/messages"},
  51. KeySources: []ChannelAffinityKeySource{
  52. {Type: "gjson", Path: "metadata.user_id"},
  53. },
  54. ValueRegex: "",
  55. TTLSeconds: 0,
  56. SkipRetryOnFailure: false,
  57. IncludeUsingGroup: true,
  58. IncludeRuleName: true,
  59. UserAgentInclude: nil,
  60. },
  61. },
  62. }
  63. func init() {
  64. config.GlobalConfig.Register("channel_affinity_setting", &channelAffinitySetting)
  65. }
  66. func GetChannelAffinitySetting() *ChannelAffinitySetting {
  67. return &channelAffinitySetting
  68. }