cache_ratio.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package operation_setting
  2. import (
  3. "encoding/json"
  4. "one-api/common"
  5. "sync"
  6. )
  7. var defaultCacheRatio = map[string]float64{
  8. "gpt-4": 0.5,
  9. "o1": 0.5,
  10. "o1-2024-12-17": 0.5,
  11. "o1-preview-2024-09-12": 0.5,
  12. "o1-preview": 0.5,
  13. "o1-mini-2024-09-12": 0.5,
  14. "o1-mini": 0.5,
  15. "o3-mini": 0.5,
  16. "o3-mini-2025-01-31": 0.5,
  17. "gpt-4o-2024-11-20": 0.5,
  18. "gpt-4o-2024-08-06": 0.5,
  19. "gpt-4o": 0.5,
  20. "gpt-4o-mini-2024-07-18": 0.5,
  21. "gpt-4o-mini": 0.5,
  22. "gpt-4o-realtime-preview": 0.5,
  23. "gpt-4o-mini-realtime-preview": 0.5,
  24. "gpt-4.5-preview": 0.5,
  25. "gpt-4.5-preview-2025-02-27": 0.5,
  26. "deepseek-chat": 0.25,
  27. "deepseek-reasoner": 0.25,
  28. "deepseek-coder": 0.25,
  29. "claude-3-sonnet-20240229": 0.1,
  30. "claude-3-opus-20240229": 0.1,
  31. "claude-3-haiku-20240307": 0.1,
  32. "claude-3-5-haiku-20241022": 0.1,
  33. "claude-3-5-sonnet-20240620": 0.1,
  34. "claude-3-5-sonnet-20241022": 0.1,
  35. "claude-3-7-sonnet-20250219": 0.1,
  36. "claude-3-7-sonnet-20250219-thinking": 0.1,
  37. }
  38. var defaultCreateCacheRatio = map[string]float64{
  39. "claude-3-sonnet-20240229": 1.25,
  40. "claude-3-opus-20240229": 1.25,
  41. "claude-3-haiku-20240307": 1.25,
  42. "claude-3-5-haiku-20241022": 1.25,
  43. "claude-3-5-sonnet-20240620": 1.25,
  44. "claude-3-5-sonnet-20241022": 1.25,
  45. "claude-3-7-sonnet-20250219": 1.25,
  46. "claude-3-7-sonnet-20250219-thinking": 1.25,
  47. }
  48. //var defaultCreateCacheRatio = map[string]float64{}
  49. var cacheRatioMap map[string]float64
  50. var cacheRatioMapMutex sync.RWMutex
  51. // GetCacheRatioMap returns the cache ratio map
  52. func GetCacheRatioMap() map[string]float64 {
  53. cacheRatioMapMutex.Lock()
  54. defer cacheRatioMapMutex.Unlock()
  55. if cacheRatioMap == nil {
  56. cacheRatioMap = defaultCacheRatio
  57. }
  58. return cacheRatioMap
  59. }
  60. // CacheRatio2JSONString converts the cache ratio map to a JSON string
  61. func CacheRatio2JSONString() string {
  62. GetCacheRatioMap()
  63. jsonBytes, err := json.Marshal(cacheRatioMap)
  64. if err != nil {
  65. common.SysError("error marshalling cache ratio: " + err.Error())
  66. }
  67. return string(jsonBytes)
  68. }
  69. // UpdateCacheRatioByJSONString updates the cache ratio map from a JSON string
  70. func UpdateCacheRatioByJSONString(jsonStr string) error {
  71. cacheRatioMapMutex.Lock()
  72. defer cacheRatioMapMutex.Unlock()
  73. cacheRatioMap = make(map[string]float64)
  74. return json.Unmarshal([]byte(jsonStr), &cacheRatioMap)
  75. }
  76. // GetCacheRatio returns the cache ratio for a model
  77. func GetCacheRatio(name string) (float64, bool) {
  78. GetCacheRatioMap()
  79. ratio, ok := cacheRatioMap[name]
  80. if !ok {
  81. return 1, false // Default to 0.5 if not found
  82. }
  83. return ratio, true
  84. }
  85. func GetCreateCacheRatio(name string) (float64, bool) {
  86. ratio, ok := defaultCreateCacheRatio[name]
  87. if !ok {
  88. return 1.25, false // Default to 1.25 if not found
  89. }
  90. return ratio, true
  91. }