model.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. package controller
  2. import (
  3. "fmt"
  4. "net/http"
  5. "time"
  6. "github.com/QuantumNous/new-api/common"
  7. "github.com/QuantumNous/new-api/constant"
  8. "github.com/QuantumNous/new-api/dto"
  9. "github.com/QuantumNous/new-api/model"
  10. "github.com/QuantumNous/new-api/relay"
  11. "github.com/QuantumNous/new-api/relay/channel/ai360"
  12. "github.com/QuantumNous/new-api/relay/channel/lingyiwanwu"
  13. "github.com/QuantumNous/new-api/relay/channel/minimax"
  14. "github.com/QuantumNous/new-api/relay/channel/moonshot"
  15. relaycommon "github.com/QuantumNous/new-api/relay/common"
  16. "github.com/QuantumNous/new-api/service"
  17. "github.com/gin-gonic/gin"
  18. "github.com/samber/lo"
  19. )
  20. // https://platform.openai.com/docs/api-reference/models/list
  21. var openAIModels []dto.OpenAIModels
  22. var openAIModelsMap map[string]dto.OpenAIModels
  23. var channelId2Models map[int][]string
  24. func init() {
  25. // https://platform.openai.com/docs/models/model-endpoint-compatibility
  26. for i := 0; i < constant.APITypeDummy; i++ {
  27. if i == constant.APITypeAIProxyLibrary {
  28. continue
  29. }
  30. adaptor := relay.GetAdaptor(i)
  31. channelName := adaptor.GetChannelName()
  32. modelNames := adaptor.GetModelList()
  33. for _, modelName := range modelNames {
  34. openAIModels = append(openAIModels, dto.OpenAIModels{
  35. Id: modelName,
  36. Object: "model",
  37. Created: 1626777600,
  38. OwnedBy: channelName,
  39. })
  40. }
  41. }
  42. for _, modelName := range ai360.ModelList {
  43. openAIModels = append(openAIModels, dto.OpenAIModels{
  44. Id: modelName,
  45. Object: "model",
  46. Created: 1626777600,
  47. OwnedBy: ai360.ChannelName,
  48. })
  49. }
  50. for _, modelName := range moonshot.ModelList {
  51. openAIModels = append(openAIModels, dto.OpenAIModels{
  52. Id: modelName,
  53. Object: "model",
  54. Created: 1626777600,
  55. OwnedBy: moonshot.ChannelName,
  56. })
  57. }
  58. for _, modelName := range lingyiwanwu.ModelList {
  59. openAIModels = append(openAIModels, dto.OpenAIModels{
  60. Id: modelName,
  61. Object: "model",
  62. Created: 1626777600,
  63. OwnedBy: lingyiwanwu.ChannelName,
  64. })
  65. }
  66. for _, modelName := range minimax.ModelList {
  67. openAIModels = append(openAIModels, dto.OpenAIModels{
  68. Id: modelName,
  69. Object: "model",
  70. Created: 1626777600,
  71. OwnedBy: minimax.ChannelName,
  72. })
  73. }
  74. for modelName, _ := range constant.MidjourneyModel2Action {
  75. openAIModels = append(openAIModels, dto.OpenAIModels{
  76. Id: modelName,
  77. Object: "model",
  78. Created: 1626777600,
  79. OwnedBy: "midjourney",
  80. })
  81. }
  82. openAIModelsMap = make(map[string]dto.OpenAIModels)
  83. for _, aiModel := range openAIModels {
  84. openAIModelsMap[aiModel.Id] = aiModel
  85. }
  86. channelId2Models = make(map[int][]string)
  87. for i := 1; i <= constant.ChannelTypeDummy; i++ {
  88. apiType, success := common.ChannelType2APIType(i)
  89. if !success || apiType == constant.APITypeAIProxyLibrary {
  90. continue
  91. }
  92. meta := &relaycommon.RelayInfo{ChannelMeta: &relaycommon.ChannelMeta{
  93. ChannelType: i,
  94. }}
  95. adaptor := relay.GetAdaptor(apiType)
  96. adaptor.Init(meta)
  97. channelId2Models[i] = adaptor.GetModelList()
  98. }
  99. openAIModels = lo.UniqBy(openAIModels, func(m dto.OpenAIModels) string {
  100. return m.Id
  101. })
  102. }
  103. func ListModels(c *gin.Context, modelType int) {
  104. userOpenAiModels := make([]dto.OpenAIModels, 0)
  105. modelLimitEnable := common.GetContextKeyBool(c, constant.ContextKeyTokenModelLimitEnabled)
  106. if modelLimitEnable {
  107. s, ok := common.GetContextKey(c, constant.ContextKeyTokenModelLimit)
  108. var tokenModelLimit map[string]bool
  109. if ok {
  110. tokenModelLimit = s.(map[string]bool)
  111. } else {
  112. tokenModelLimit = map[string]bool{}
  113. }
  114. for allowModel, _ := range tokenModelLimit {
  115. if oaiModel, ok := openAIModelsMap[allowModel]; ok {
  116. oaiModel.SupportedEndpointTypes = model.GetModelSupportEndpointTypes(allowModel)
  117. userOpenAiModels = append(userOpenAiModels, oaiModel)
  118. } else {
  119. userOpenAiModels = append(userOpenAiModels, dto.OpenAIModels{
  120. Id: allowModel,
  121. Object: "model",
  122. Created: 1626777600,
  123. OwnedBy: "custom",
  124. SupportedEndpointTypes: model.GetModelSupportEndpointTypes(allowModel),
  125. })
  126. }
  127. }
  128. } else {
  129. userId := c.GetInt("id")
  130. userGroup, err := model.GetUserGroup(userId, false)
  131. if err != nil {
  132. c.JSON(http.StatusOK, gin.H{
  133. "success": false,
  134. "message": "get user group failed",
  135. })
  136. return
  137. }
  138. group := userGroup
  139. tokenGroup := common.GetContextKeyString(c, constant.ContextKeyTokenGroup)
  140. if tokenGroup != "" {
  141. group = tokenGroup
  142. }
  143. var models []string
  144. if tokenGroup == "auto" {
  145. for _, autoGroup := range service.GetUserAutoGroup(userGroup) {
  146. groupModels := model.GetGroupEnabledModels(autoGroup)
  147. for _, g := range groupModels {
  148. if !common.StringsContains(models, g) {
  149. models = append(models, g)
  150. }
  151. }
  152. }
  153. } else {
  154. models = model.GetGroupEnabledModels(group)
  155. }
  156. for _, modelName := range models {
  157. if oaiModel, ok := openAIModelsMap[modelName]; ok {
  158. oaiModel.SupportedEndpointTypes = model.GetModelSupportEndpointTypes(modelName)
  159. userOpenAiModels = append(userOpenAiModels, oaiModel)
  160. } else {
  161. userOpenAiModels = append(userOpenAiModels, dto.OpenAIModels{
  162. Id: modelName,
  163. Object: "model",
  164. Created: 1626777600,
  165. OwnedBy: "custom",
  166. SupportedEndpointTypes: model.GetModelSupportEndpointTypes(modelName),
  167. })
  168. }
  169. }
  170. }
  171. switch modelType {
  172. case constant.ChannelTypeAnthropic:
  173. useranthropicModels := make([]dto.AnthropicModel, len(userOpenAiModels))
  174. for i, model := range userOpenAiModels {
  175. useranthropicModels[i] = dto.AnthropicModel{
  176. ID: model.Id,
  177. CreatedAt: time.Unix(int64(model.Created), 0).UTC().Format(time.RFC3339),
  178. DisplayName: model.Id,
  179. Type: "model",
  180. }
  181. }
  182. c.JSON(200, gin.H{
  183. "data": useranthropicModels,
  184. "first_id": useranthropicModels[0].ID,
  185. "has_more": false,
  186. "last_id": useranthropicModels[len(useranthropicModels)-1].ID,
  187. })
  188. case constant.ChannelTypeGemini:
  189. userGeminiModels := make([]dto.GeminiModel, len(userOpenAiModels))
  190. for i, model := range userOpenAiModels {
  191. userGeminiModels[i] = dto.GeminiModel{
  192. Name: model.Id,
  193. DisplayName: model.Id,
  194. }
  195. }
  196. c.JSON(200, gin.H{
  197. "models": userGeminiModels,
  198. "nextPageToken": nil,
  199. })
  200. default:
  201. c.JSON(200, gin.H{
  202. "success": true,
  203. "data": userOpenAiModels,
  204. "object": "list",
  205. })
  206. }
  207. }
  208. func ChannelListModels(c *gin.Context) {
  209. c.JSON(200, gin.H{
  210. "success": true,
  211. "data": openAIModels,
  212. })
  213. }
  214. func DashboardListModels(c *gin.Context) {
  215. c.JSON(200, gin.H{
  216. "success": true,
  217. "data": channelId2Models,
  218. })
  219. }
  220. func EnabledListModels(c *gin.Context) {
  221. c.JSON(200, gin.H{
  222. "success": true,
  223. "data": model.GetEnabledModels(),
  224. })
  225. }
  226. func RetrieveModel(c *gin.Context, modelType int) {
  227. modelId := c.Param("model")
  228. if aiModel, ok := openAIModelsMap[modelId]; ok {
  229. switch modelType {
  230. case constant.ChannelTypeAnthropic:
  231. c.JSON(200, dto.AnthropicModel{
  232. ID: aiModel.Id,
  233. CreatedAt: time.Unix(int64(aiModel.Created), 0).UTC().Format(time.RFC3339),
  234. DisplayName: aiModel.Id,
  235. Type: "model",
  236. })
  237. default:
  238. c.JSON(200, aiModel)
  239. }
  240. } else {
  241. openAIError := dto.OpenAIError{
  242. Message: fmt.Sprintf("The model '%s' does not exist", modelId),
  243. Type: "invalid_request_error",
  244. Param: "model",
  245. Code: "model_not_found",
  246. }
  247. c.JSON(200, gin.H{
  248. "error": openAIError,
  249. })
  250. }
  251. }