model_meta.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. pageInfo.SetTotal(int(total))
  24. pageInfo.SetItems(modelsMeta)
  25. common.ApiSuccess(c, pageInfo)
  26. }
  27. // SearchModelsMeta 搜索模型列表
  28. func SearchModelsMeta(c *gin.Context) {
  29. keyword := c.Query("keyword")
  30. vendor := c.Query("vendor")
  31. pageInfo := common.GetPageQuery(c)
  32. modelsMeta, total, err := model.SearchModels(keyword, vendor, pageInfo.GetStartIdx(), pageInfo.GetPageSize())
  33. if err != nil {
  34. common.ApiError(c, err)
  35. return
  36. }
  37. for _, m := range modelsMeta {
  38. fillModelExtra(m)
  39. }
  40. pageInfo.SetTotal(int(total))
  41. pageInfo.SetItems(modelsMeta)
  42. common.ApiSuccess(c, pageInfo)
  43. }
  44. // GetModelMeta 根据 ID 获取单条模型信息
  45. func GetModelMeta(c *gin.Context) {
  46. idStr := c.Param("id")
  47. id, err := strconv.Atoi(idStr)
  48. if err != nil {
  49. common.ApiError(c, err)
  50. return
  51. }
  52. var m model.Model
  53. if err := model.DB.First(&m, id).Error; err != nil {
  54. common.ApiError(c, err)
  55. return
  56. }
  57. fillModelExtra(&m)
  58. common.ApiSuccess(c, &m)
  59. }
  60. // CreateModelMeta 新建模型
  61. func CreateModelMeta(c *gin.Context) {
  62. var m model.Model
  63. if err := c.ShouldBindJSON(&m); err != nil {
  64. common.ApiError(c, err)
  65. return
  66. }
  67. if m.ModelName == "" {
  68. common.ApiErrorMsg(c, "模型名称不能为空")
  69. return
  70. }
  71. if err := m.Insert(); err != nil {
  72. common.ApiError(c, err)
  73. return
  74. }
  75. model.RefreshPricing()
  76. common.ApiSuccess(c, &m)
  77. }
  78. // UpdateModelMeta 更新模型
  79. func UpdateModelMeta(c *gin.Context) {
  80. statusOnly := c.Query("status_only") == "true"
  81. var m model.Model
  82. if err := c.ShouldBindJSON(&m); err != nil {
  83. common.ApiError(c, err)
  84. return
  85. }
  86. if m.Id == 0 {
  87. common.ApiErrorMsg(c, "缺少模型 ID")
  88. return
  89. }
  90. if statusOnly {
  91. // 只更新状态,防止误清空其他字段
  92. if err := model.DB.Model(&model.Model{}).Where("id = ?", m.Id).Update("status", m.Status).Error; err != nil {
  93. common.ApiError(c, err)
  94. return
  95. }
  96. } else {
  97. if err := m.Update(); err != nil {
  98. common.ApiError(c, err)
  99. return
  100. }
  101. }
  102. model.RefreshPricing()
  103. common.ApiSuccess(c, &m)
  104. }
  105. // DeleteModelMeta 删除模型
  106. func DeleteModelMeta(c *gin.Context) {
  107. idStr := c.Param("id")
  108. id, err := strconv.Atoi(idStr)
  109. if err != nil {
  110. common.ApiError(c, err)
  111. return
  112. }
  113. if err := model.DB.Delete(&model.Model{}, id).Error; err != nil {
  114. common.ApiError(c, err)
  115. return
  116. }
  117. model.RefreshPricing()
  118. common.ApiSuccess(c, nil)
  119. }
  120. // 辅助函数:填充 Endpoints 和 BoundChannels 和 EnableGroups
  121. func fillModelExtra(m *model.Model) {
  122. if m.Endpoints == "" {
  123. eps := model.GetModelSupportEndpointTypes(m.ModelName)
  124. if b, err := json.Marshal(eps); err == nil {
  125. m.Endpoints = string(b)
  126. }
  127. }
  128. if channels, err := model.GetBoundChannels(m.ModelName); err == nil {
  129. m.BoundChannels = channels
  130. }
  131. // 填充启用分组
  132. m.EnableGroups = model.GetModelEnableGroups(m.ModelName)
  133. // 填充计费类型
  134. m.QuotaType = model.GetModelQuotaType(m.ModelName)
  135. }