pricing.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package controller
  2. import (
  3. "github.com/QuantumNous/new-api/model"
  4. "github.com/QuantumNous/new-api/service"
  5. "github.com/QuantumNous/new-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 = service.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. "auto_groups": service.GetUserAutoGroup(group),
  44. "_": "a42d372ccf0b5dd13ecf71203521f9d2",
  45. })
  46. }
  47. func ResetModelRatio(c *gin.Context) {
  48. defaultStr := ratio_setting.DefaultModelRatio2JSONString()
  49. err := model.UpdateOption("ModelRatio", defaultStr)
  50. if err != nil {
  51. c.JSON(200, gin.H{
  52. "success": false,
  53. "message": err.Error(),
  54. })
  55. return
  56. }
  57. err = ratio_setting.UpdateModelRatioByJSONString(defaultStr)
  58. if err != nil {
  59. c.JSON(200, gin.H{
  60. "success": false,
  61. "message": err.Error(),
  62. })
  63. return
  64. }
  65. c.JSON(200, gin.H{
  66. "success": true,
  67. "message": "重置模型倍率成功",
  68. })
  69. }