pricing.go 7.3 KB

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