cache_ratio.go 2.1 KB

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