model.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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/setting/ratio_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. _, _, exist := ratio_setting.GetModelRatioOrPrice(allowModel)
  130. if !exist {
  131. continue
  132. }
  133. }
  134. if oaiModel, ok := openAIModelsMap[allowModel]; ok {
  135. oaiModel.SupportedEndpointTypes = model.GetModelSupportEndpointTypes(allowModel)
  136. userOpenAiModels = append(userOpenAiModels, oaiModel)
  137. } else {
  138. userOpenAiModels = append(userOpenAiModels, dto.OpenAIModels{
  139. Id: allowModel,
  140. Object: "model",
  141. Created: 1626777600,
  142. OwnedBy: "custom",
  143. SupportedEndpointTypes: model.GetModelSupportEndpointTypes(allowModel),
  144. })
  145. }
  146. }
  147. } else {
  148. userId := c.GetInt("id")
  149. userGroup, err := model.GetUserGroup(userId, false)
  150. if err != nil {
  151. c.JSON(http.StatusOK, gin.H{
  152. "success": false,
  153. "message": "get user group failed",
  154. })
  155. return
  156. }
  157. group := userGroup
  158. tokenGroup := common.GetContextKeyString(c, constant.ContextKeyTokenGroup)
  159. if tokenGroup != "" {
  160. group = tokenGroup
  161. }
  162. var models []string
  163. if tokenGroup == "auto" {
  164. for _, autoGroup := range service.GetUserAutoGroup(userGroup) {
  165. groupModels := model.GetGroupEnabledModels(autoGroup)
  166. for _, g := range groupModels {
  167. if !common.StringsContains(models, g) {
  168. models = append(models, g)
  169. }
  170. }
  171. }
  172. } else {
  173. models = model.GetGroupEnabledModels(group)
  174. }
  175. for _, modelName := range models {
  176. if !acceptUnsetRatioModel {
  177. _, _, exist := ratio_setting.GetModelRatioOrPrice(modelName)
  178. if !exist {
  179. continue
  180. }
  181. }
  182. if oaiModel, ok := openAIModelsMap[modelName]; ok {
  183. oaiModel.SupportedEndpointTypes = model.GetModelSupportEndpointTypes(modelName)
  184. userOpenAiModels = append(userOpenAiModels, oaiModel)
  185. } else {
  186. userOpenAiModels = append(userOpenAiModels, dto.OpenAIModels{
  187. Id: modelName,
  188. Object: "model",
  189. Created: 1626777600,
  190. OwnedBy: "custom",
  191. SupportedEndpointTypes: model.GetModelSupportEndpointTypes(modelName),
  192. })
  193. }
  194. }
  195. }
  196. switch modelType {
  197. case constant.ChannelTypeAnthropic:
  198. useranthropicModels := make([]dto.AnthropicModel, len(userOpenAiModels))
  199. for i, model := range userOpenAiModels {
  200. useranthropicModels[i] = dto.AnthropicModel{
  201. ID: model.Id,
  202. CreatedAt: time.Unix(int64(model.Created), 0).UTC().Format(time.RFC3339),
  203. DisplayName: model.Id,
  204. Type: "model",
  205. }
  206. }
  207. c.JSON(200, gin.H{
  208. "data": useranthropicModels,
  209. "first_id": useranthropicModels[0].ID,
  210. "has_more": false,
  211. "last_id": useranthropicModels[len(useranthropicModels)-1].ID,
  212. })
  213. case constant.ChannelTypeGemini:
  214. userGeminiModels := make([]dto.GeminiModel, len(userOpenAiModels))
  215. for i, model := range userOpenAiModels {
  216. userGeminiModels[i] = dto.GeminiModel{
  217. Name: model.Id,
  218. DisplayName: model.Id,
  219. }
  220. }
  221. c.JSON(200, gin.H{
  222. "models": userGeminiModels,
  223. "nextPageToken": nil,
  224. })
  225. default:
  226. c.JSON(200, gin.H{
  227. "success": true,
  228. "data": userOpenAiModels,
  229. "object": "list",
  230. })
  231. }
  232. }
  233. func ChannelListModels(c *gin.Context) {
  234. c.JSON(200, gin.H{
  235. "success": true,
  236. "data": openAIModels,
  237. })
  238. }
  239. func DashboardListModels(c *gin.Context) {
  240. c.JSON(200, gin.H{
  241. "success": true,
  242. "data": channelId2Models,
  243. })
  244. }
  245. func EnabledListModels(c *gin.Context) {
  246. c.JSON(200, gin.H{
  247. "success": true,
  248. "data": model.GetEnabledModels(),
  249. })
  250. }
  251. func RetrieveModel(c *gin.Context, modelType int) {
  252. modelId := c.Param("model")
  253. if aiModel, ok := openAIModelsMap[modelId]; ok {
  254. switch modelType {
  255. case constant.ChannelTypeAnthropic:
  256. c.JSON(200, dto.AnthropicModel{
  257. ID: aiModel.Id,
  258. CreatedAt: time.Unix(int64(aiModel.Created), 0).UTC().Format(time.RFC3339),
  259. DisplayName: aiModel.Id,
  260. Type: "model",
  261. })
  262. default:
  263. c.JSON(200, aiModel)
  264. }
  265. } else {
  266. openAIError := types.OpenAIError{
  267. Message: fmt.Sprintf("The model '%s' does not exist", modelId),
  268. Type: "invalid_request_error",
  269. Param: "model",
  270. Code: "model_not_found",
  271. }
  272. c.JSON(200, gin.H{
  273. "error": openAIError,
  274. })
  275. }
  276. }