pricing.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. package model
  2. import (
  3. "fmt"
  4. "strings"
  5. "one-api/common"
  6. "one-api/constant"
  7. "one-api/setting/ratio_setting"
  8. "one-api/types"
  9. "sync"
  10. "time"
  11. )
  12. type Pricing struct {
  13. ModelName string `json:"model_name"`
  14. Description string `json:"description,omitempty"`
  15. Tags string `json:"tags,omitempty"`
  16. VendorID int `json:"vendor_id,omitempty"`
  17. QuotaType int `json:"quota_type"`
  18. ModelRatio float64 `json:"model_ratio"`
  19. ModelPrice float64 `json:"model_price"`
  20. OwnerBy string `json:"owner_by"`
  21. CompletionRatio float64 `json:"completion_ratio"`
  22. EnableGroup []string `json:"enable_groups"`
  23. SupportedEndpointTypes []constant.EndpointType `json:"supported_endpoint_types"`
  24. }
  25. type PricingVendor struct {
  26. ID int `json:"id"`
  27. Name string `json:"name"`
  28. Description string `json:"description,omitempty"`
  29. Icon string `json:"icon,omitempty"`
  30. }
  31. var (
  32. pricingMap []Pricing
  33. vendorsList []PricingVendor
  34. lastGetPricingTime time.Time
  35. updatePricingLock sync.Mutex
  36. )
  37. var (
  38. modelSupportEndpointTypes = make(map[string][]constant.EndpointType)
  39. modelSupportEndpointsLock = sync.RWMutex{}
  40. )
  41. func GetPricing() []Pricing {
  42. if time.Since(lastGetPricingTime) > time.Minute*1 || len(pricingMap) == 0 {
  43. updatePricingLock.Lock()
  44. defer updatePricingLock.Unlock()
  45. // Double check after acquiring the lock
  46. if time.Since(lastGetPricingTime) > time.Minute*1 || len(pricingMap) == 0 {
  47. modelSupportEndpointsLock.Lock()
  48. defer modelSupportEndpointsLock.Unlock()
  49. updatePricing()
  50. }
  51. }
  52. return pricingMap
  53. }
  54. // GetVendors 返回当前定价接口使用到的供应商信息
  55. func GetVendors() []PricingVendor {
  56. if time.Since(lastGetPricingTime) > time.Minute*1 || len(pricingMap) == 0 {
  57. // 保证先刷新一次
  58. GetPricing()
  59. }
  60. return vendorsList
  61. }
  62. func GetModelSupportEndpointTypes(model string) []constant.EndpointType {
  63. if model == "" {
  64. return make([]constant.EndpointType, 0)
  65. }
  66. modelSupportEndpointsLock.RLock()
  67. defer modelSupportEndpointsLock.RUnlock()
  68. if endpoints, ok := modelSupportEndpointTypes[model]; ok {
  69. return endpoints
  70. }
  71. return make([]constant.EndpointType, 0)
  72. }
  73. func updatePricing() {
  74. //modelRatios := common.GetModelRatios()
  75. enableAbilities, err := GetAllEnableAbilityWithChannels()
  76. if err != nil {
  77. common.SysError(fmt.Sprintf("GetAllEnableAbilityWithChannels error: %v", err))
  78. return
  79. }
  80. // 预加载模型元数据与供应商一次,避免循环查询
  81. var allMeta []Model
  82. _ = DB.Find(&allMeta).Error
  83. metaMap := make(map[string]*Model)
  84. prefixList := make([]*Model, 0)
  85. suffixList := make([]*Model, 0)
  86. containsList := make([]*Model, 0)
  87. for i := range allMeta {
  88. m := &allMeta[i]
  89. if m.NameRule == NameRuleExact {
  90. metaMap[m.ModelName] = m
  91. } else {
  92. switch m.NameRule {
  93. case NameRulePrefix:
  94. prefixList = append(prefixList, m)
  95. case NameRuleSuffix:
  96. suffixList = append(suffixList, m)
  97. case NameRuleContains:
  98. containsList = append(containsList, m)
  99. }
  100. }
  101. }
  102. // 将非精确规则模型匹配到 metaMap
  103. for _, m := range prefixList {
  104. for _, pricingModel := range enableAbilities {
  105. if strings.HasPrefix(pricingModel.Model, m.ModelName) {
  106. metaMap[pricingModel.Model] = m
  107. }
  108. }
  109. }
  110. for _, m := range suffixList {
  111. for _, pricingModel := range enableAbilities {
  112. if strings.HasSuffix(pricingModel.Model, m.ModelName) {
  113. metaMap[pricingModel.Model] = m
  114. }
  115. }
  116. }
  117. for _, m := range containsList {
  118. for _, pricingModel := range enableAbilities {
  119. if strings.Contains(pricingModel.Model, m.ModelName) {
  120. if _, exists := metaMap[pricingModel.Model]; !exists {
  121. metaMap[pricingModel.Model] = m
  122. }
  123. }
  124. }
  125. }
  126. // 预加载供应商
  127. var vendors []Vendor
  128. _ = DB.Find(&vendors).Error
  129. vendorMap := make(map[int]*Vendor)
  130. for i := range vendors {
  131. vendorMap[vendors[i].Id] = &vendors[i]
  132. }
  133. // 构建对前端友好的供应商列表
  134. vendorsList = make([]PricingVendor, 0, len(vendors))
  135. for _, v := range vendors {
  136. vendorsList = append(vendorsList, PricingVendor{
  137. ID: v.Id,
  138. Name: v.Name,
  139. Description: v.Description,
  140. Icon: v.Icon,
  141. })
  142. }
  143. modelGroupsMap := make(map[string]*types.Set[string])
  144. for _, ability := range enableAbilities {
  145. groups, ok := modelGroupsMap[ability.Model]
  146. if !ok {
  147. groups = types.NewSet[string]()
  148. modelGroupsMap[ability.Model] = groups
  149. }
  150. groups.Add(ability.Group)
  151. }
  152. //这里使用切片而不是Set,因为一个模型可能支持多个端点类型,并且第一个端点是优先使用端点
  153. modelSupportEndpointsStr := make(map[string][]string)
  154. for _, ability := range enableAbilities {
  155. endpoints, ok := modelSupportEndpointsStr[ability.Model]
  156. if !ok {
  157. endpoints = make([]string, 0)
  158. modelSupportEndpointsStr[ability.Model] = endpoints
  159. }
  160. channelTypes := common.GetEndpointTypesByChannelType(ability.ChannelType, ability.Model)
  161. for _, channelType := range channelTypes {
  162. if !common.StringsContains(endpoints, string(channelType)) {
  163. endpoints = append(endpoints, string(channelType))
  164. }
  165. }
  166. modelSupportEndpointsStr[ability.Model] = endpoints
  167. }
  168. modelSupportEndpointTypes = make(map[string][]constant.EndpointType)
  169. for model, endpoints := range modelSupportEndpointsStr {
  170. supportedEndpoints := make([]constant.EndpointType, 0)
  171. for _, endpointStr := range endpoints {
  172. endpointType := constant.EndpointType(endpointStr)
  173. supportedEndpoints = append(supportedEndpoints, endpointType)
  174. }
  175. modelSupportEndpointTypes[model] = supportedEndpoints
  176. }
  177. pricingMap = make([]Pricing, 0)
  178. for model, groups := range modelGroupsMap {
  179. pricing := Pricing{
  180. ModelName: model,
  181. EnableGroup: groups.Items(),
  182. SupportedEndpointTypes: modelSupportEndpointTypes[model],
  183. }
  184. // 补充模型元数据(描述、标签、供应商等)
  185. if meta, ok := metaMap[model]; ok {
  186. pricing.Description = meta.Description
  187. pricing.Tags = meta.Tags
  188. pricing.VendorID = meta.VendorID
  189. }
  190. modelPrice, findPrice := ratio_setting.GetModelPrice(model, false)
  191. if findPrice {
  192. pricing.ModelPrice = modelPrice
  193. pricing.QuotaType = 1
  194. } else {
  195. modelRatio, _, _ := ratio_setting.GetModelRatio(model)
  196. pricing.ModelRatio = modelRatio
  197. pricing.CompletionRatio = ratio_setting.GetCompletionRatio(model)
  198. pricing.QuotaType = 0
  199. }
  200. pricingMap = append(pricingMap, pricing)
  201. }
  202. lastGetPricingTime = time.Now()
  203. }