model_meta.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package controller
  2. import (
  3. "encoding/json"
  4. "strconv"
  5. "one-api/common"
  6. "one-api/model"
  7. "github.com/gin-gonic/gin"
  8. )
  9. // GetAllModelsMeta 获取模型列表(分页)
  10. func GetAllModelsMeta(c *gin.Context) {
  11. pageInfo := common.GetPageQuery(c)
  12. modelsMeta, err := model.GetAllModels(pageInfo.GetStartIdx(), pageInfo.GetPageSize())
  13. if err != nil {
  14. common.ApiError(c, err)
  15. return
  16. }
  17. // 填充附加字段
  18. for _, m := range modelsMeta {
  19. fillModelExtra(m)
  20. }
  21. var total int64
  22. model.DB.Model(&model.Model{}).Count(&total)
  23. // 统计供应商计数(全部数据,不受分页影响)
  24. vendorCounts, _ := model.GetVendorModelCounts()
  25. pageInfo.SetTotal(int(total))
  26. pageInfo.SetItems(modelsMeta)
  27. common.ApiSuccess(c, gin.H{
  28. "items": modelsMeta,
  29. "total": total,
  30. "page": pageInfo.GetPage(),
  31. "page_size": pageInfo.GetPageSize(),
  32. "vendor_counts": vendorCounts,
  33. })
  34. }
  35. // SearchModelsMeta 搜索模型列表
  36. func SearchModelsMeta(c *gin.Context) {
  37. keyword := c.Query("keyword")
  38. vendor := c.Query("vendor")
  39. pageInfo := common.GetPageQuery(c)
  40. modelsMeta, total, err := model.SearchModels(keyword, vendor, pageInfo.GetStartIdx(), pageInfo.GetPageSize())
  41. if err != nil {
  42. common.ApiError(c, err)
  43. return
  44. }
  45. for _, m := range modelsMeta {
  46. fillModelExtra(m)
  47. }
  48. pageInfo.SetTotal(int(total))
  49. pageInfo.SetItems(modelsMeta)
  50. common.ApiSuccess(c, pageInfo)
  51. }
  52. // GetModelMeta 根据 ID 获取单条模型信息
  53. func GetModelMeta(c *gin.Context) {
  54. idStr := c.Param("id")
  55. id, err := strconv.Atoi(idStr)
  56. if err != nil {
  57. common.ApiError(c, err)
  58. return
  59. }
  60. var m model.Model
  61. if err := model.DB.First(&m, id).Error; err != nil {
  62. common.ApiError(c, err)
  63. return
  64. }
  65. fillModelExtra(&m)
  66. common.ApiSuccess(c, &m)
  67. }
  68. // CreateModelMeta 新建模型
  69. func CreateModelMeta(c *gin.Context) {
  70. var m model.Model
  71. if err := c.ShouldBindJSON(&m); err != nil {
  72. common.ApiError(c, err)
  73. return
  74. }
  75. if m.ModelName == "" {
  76. common.ApiErrorMsg(c, "模型名称不能为空")
  77. return
  78. }
  79. // 名称冲突检查
  80. if dup, err := model.IsModelNameDuplicated(0, m.ModelName); err != nil {
  81. common.ApiError(c, err)
  82. return
  83. } else if dup {
  84. common.ApiErrorMsg(c, "模型名称已存在")
  85. return
  86. }
  87. if err := m.Insert(); err != nil {
  88. common.ApiError(c, err)
  89. return
  90. }
  91. model.RefreshPricing()
  92. common.ApiSuccess(c, &m)
  93. }
  94. // UpdateModelMeta 更新模型
  95. func UpdateModelMeta(c *gin.Context) {
  96. statusOnly := c.Query("status_only") == "true"
  97. var m model.Model
  98. if err := c.ShouldBindJSON(&m); err != nil {
  99. common.ApiError(c, err)
  100. return
  101. }
  102. if m.Id == 0 {
  103. common.ApiErrorMsg(c, "缺少模型 ID")
  104. return
  105. }
  106. if statusOnly {
  107. // 只更新状态,防止误清空其他字段
  108. if err := model.DB.Model(&model.Model{}).Where("id = ?", m.Id).Update("status", m.Status).Error; err != nil {
  109. common.ApiError(c, err)
  110. return
  111. }
  112. } else {
  113. // 名称冲突检查
  114. if dup, err := model.IsModelNameDuplicated(m.Id, m.ModelName); err != nil {
  115. common.ApiError(c, err)
  116. return
  117. } else if dup {
  118. common.ApiErrorMsg(c, "模型名称已存在")
  119. return
  120. }
  121. if err := m.Update(); err != nil {
  122. common.ApiError(c, err)
  123. return
  124. }
  125. }
  126. model.RefreshPricing()
  127. common.ApiSuccess(c, &m)
  128. }
  129. // DeleteModelMeta 删除模型
  130. func DeleteModelMeta(c *gin.Context) {
  131. idStr := c.Param("id")
  132. id, err := strconv.Atoi(idStr)
  133. if err != nil {
  134. common.ApiError(c, err)
  135. return
  136. }
  137. if err := model.DB.Delete(&model.Model{}, id).Error; err != nil {
  138. common.ApiError(c, err)
  139. return
  140. }
  141. model.RefreshPricing()
  142. common.ApiSuccess(c, nil)
  143. }
  144. // 辅助函数:填充 Endpoints 和 BoundChannels 和 EnableGroups
  145. func fillModelExtra(m *model.Model) {
  146. if m.Endpoints == "" {
  147. eps := model.GetModelSupportEndpointTypes(m.ModelName)
  148. if b, err := json.Marshal(eps); err == nil {
  149. m.Endpoints = string(b)
  150. }
  151. }
  152. if channels, err := model.GetBoundChannels(m.ModelName); err == nil {
  153. m.BoundChannels = channels
  154. }
  155. // 填充启用分组
  156. m.EnableGroups = model.GetModelEnableGroups(m.ModelName)
  157. // 填充计费类型
  158. m.QuotaType = model.GetModelQuotaType(m.ModelName)
  159. }