2
0

pricing.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. })
  45. }
  46. func ResetModelRatio(c *gin.Context) {
  47. defaultStr := ratio_setting.DefaultModelRatio2JSONString()
  48. err := model.UpdateOption("ModelRatio", defaultStr)
  49. if err != nil {
  50. c.JSON(200, gin.H{
  51. "success": false,
  52. "message": err.Error(),
  53. })
  54. return
  55. }
  56. err = ratio_setting.UpdateModelRatioByJSONString(defaultStr)
  57. if err != nil {
  58. c.JSON(200, gin.H{
  59. "success": false,
  60. "message": err.Error(),
  61. })
  62. return
  63. }
  64. c.JSON(200, gin.H{
  65. "success": true,
  66. "message": "重置模型倍率成功",
  67. })
  68. }