cache_ratio.go 2.2 KB

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