model_meta.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. common.ApiSuccess(c, &m)
  76. }
  77. // UpdateModelMeta 更新模型
  78. func UpdateModelMeta(c *gin.Context) {
  79. statusOnly := c.Query("status_only") == "true"
  80. var m model.Model
  81. if err := c.ShouldBindJSON(&m); err != nil {
  82. common.ApiError(c, err)
  83. return
  84. }
  85. if m.Id == 0 {
  86. common.ApiErrorMsg(c, "缺少模型 ID")
  87. return
  88. }
  89. if statusOnly {
  90. // 只更新状态,防止误清空其他字段
  91. if err := model.DB.Model(&model.Model{}).Where("id = ?", m.Id).Update("status", m.Status).Error; err != nil {
  92. common.ApiError(c, err)
  93. return
  94. }
  95. } else {
  96. if err := m.Update(); err != nil {
  97. common.ApiError(c, err)
  98. return
  99. }
  100. }
  101. common.ApiSuccess(c, &m)
  102. }
  103. // DeleteModelMeta 删除模型
  104. func DeleteModelMeta(c *gin.Context) {
  105. idStr := c.Param("id")
  106. id, err := strconv.Atoi(idStr)
  107. if err != nil {
  108. common.ApiError(c, err)
  109. return
  110. }
  111. if err := model.DB.Delete(&model.Model{}, id).Error; err != nil {
  112. common.ApiError(c, err)
  113. return
  114. }
  115. common.ApiSuccess(c, nil)
  116. }
  117. // 辅助函数:填充 Endpoints 和 BoundChannels
  118. func fillModelExtra(m *model.Model) {
  119. if m.Endpoints == "" {
  120. eps := model.GetModelSupportEndpointTypes(m.ModelName)
  121. if b, err := json.Marshal(eps); err == nil {
  122. m.Endpoints = string(b)
  123. }
  124. }
  125. if channels, err := model.GetBoundChannels(m.ModelName); err == nil {
  126. m.BoundChannels = channels
  127. }
  128. }