pricing.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package controller
  2. import (
  3. "one-api/model"
  4. "one-api/setting"
  5. "one-api/setting/ratio_setting"
  6. "github.com/gin-gonic/gin"
  7. )
  8. func GetPricing(c *gin.Context) {
  9. pricing := model.GetPricing()
  10. userId, exists := c.Get("id")
  11. usableGroup := map[string]string{}
  12. groupRatio := map[string]float64{}
  13. for s, f := range ratio_setting.GetGroupRatioCopy() {
  14. groupRatio[s] = f
  15. }
  16. var group string
  17. if exists {
  18. user, err := model.GetUserCache(userId.(int))
  19. if err == nil {
  20. group = user.Group
  21. for g := range groupRatio {
  22. ratio, ok := ratio_setting.GetGroupGroupRatio(group, g)
  23. if ok {
  24. groupRatio[g] = ratio
  25. }
  26. }
  27. }
  28. }
  29. usableGroup = setting.GetUserUsableGroups(group)
  30. // check groupRatio contains usableGroup
  31. for group := range ratio_setting.GetGroupRatioCopy() {
  32. if _, ok := usableGroup[group]; !ok {
  33. delete(groupRatio, group)
  34. }
  35. }
  36. c.JSON(200, gin.H{
  37. "success": true,
  38. "data": pricing,
  39. "vendors": model.GetVendors(),
  40. "group_ratio": groupRatio,
  41. "usable_group": usableGroup,
  42. "supported_endpoint": model.GetSupportedEndpointMap(),
  43. })
  44. }
  45. func ResetModelRatio(c *gin.Context) {
  46. defaultStr := ratio_setting.DefaultModelRatio2JSONString()
  47. err := model.UpdateOption("ModelRatio", defaultStr)
  48. if err != nil {
  49. c.JSON(200, gin.H{
  50. "success": false,
  51. "message": err.Error(),
  52. })
  53. return
  54. }
  55. err = ratio_setting.UpdateModelRatioByJSONString(defaultStr)
  56. if err != nil {
  57. c.JSON(200, gin.H{
  58. "success": false,
  59. "message": err.Error(),
  60. })
  61. return
  62. }
  63. c.JSON(200, gin.H{
  64. "success": true,
  65. "message": "重置模型倍率成功",
  66. })
  67. }