model.go 7.9 KB

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