model.go 7.9 KB

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