cache_ratio.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. "gpt-4o-2024-11-20": 0.5,
  16. "gpt-4o-2024-08-06": 0.5,
  17. "gpt-4o": 0.5,
  18. "gpt-4o-mini-2024-07-18": 0.5,
  19. "gpt-4o-mini": 0.5,
  20. "gpt-4o-realtime-preview": 0.5,
  21. "gpt-4o-mini-realtime-preview": 0.5,
  22. "deepseek-chat": 0.1,
  23. "deepseek-reasoner": 0.1,
  24. "deepseek-coder": 0.1,
  25. }
  26. var defaultCreateCacheRatio = map[string]float64{}
  27. var cacheRatioMap map[string]float64
  28. var cacheRatioMapMutex sync.RWMutex
  29. // GetCacheRatioMap returns the cache ratio map
  30. func GetCacheRatioMap() map[string]float64 {
  31. cacheRatioMapMutex.Lock()
  32. defer cacheRatioMapMutex.Unlock()
  33. if cacheRatioMap == nil {
  34. cacheRatioMap = defaultCacheRatio
  35. }
  36. return cacheRatioMap
  37. }
  38. // CacheRatio2JSONString converts the cache ratio map to a JSON string
  39. func CacheRatio2JSONString() string {
  40. GetCacheRatioMap()
  41. jsonBytes, err := json.Marshal(cacheRatioMap)
  42. if err != nil {
  43. common.SysError("error marshalling cache ratio: " + err.Error())
  44. }
  45. return string(jsonBytes)
  46. }
  47. // UpdateCacheRatioByJSONString updates the cache ratio map from a JSON string
  48. func UpdateCacheRatioByJSONString(jsonStr string) error {
  49. cacheRatioMapMutex.Lock()
  50. defer cacheRatioMapMutex.Unlock()
  51. cacheRatioMap = make(map[string]float64)
  52. return json.Unmarshal([]byte(jsonStr), &cacheRatioMap)
  53. }
  54. // GetCacheRatio returns the cache ratio for a model
  55. func GetCacheRatio(name string) (float64, bool) {
  56. GetCacheRatioMap()
  57. ratio, ok := cacheRatioMap[name]
  58. if !ok {
  59. return 1, false // Default to 0.5 if not found
  60. }
  61. return ratio, true
  62. }
  63. // DefaultCacheRatio2JSONString converts the default cache ratio map to a JSON string
  64. func DefaultCacheRatio2JSONString() string {
  65. jsonBytes, err := json.Marshal(defaultCacheRatio)
  66. if err != nil {
  67. common.SysError("error marshalling default cache ratio: " + err.Error())
  68. }
  69. return string(jsonBytes)
  70. }
  71. // GetDefaultCacheRatioMap returns the default cache ratio map
  72. func GetDefaultCacheRatioMap() map[string]float64 {
  73. return defaultCacheRatio
  74. }