model.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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/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. _, _, exist := ratio_setting.GetModelRatioOrPrice(allowModel)
  129. if !exist {
  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. _, _, exist := ratio_setting.GetModelRatioOrPrice(modelName)
  177. if !exist {
  178. continue
  179. }
  180. }
  181. if oaiModel, ok := openAIModelsMap[modelName]; ok {
  182. oaiModel.SupportedEndpointTypes = model.GetModelSupportEndpointTypes(modelName)
  183. userOpenAiModels = append(userOpenAiModels, oaiModel)
  184. } else {
  185. userOpenAiModels = append(userOpenAiModels, dto.OpenAIModels{
  186. Id: modelName,
  187. Object: "model",
  188. Created: 1626777600,
  189. OwnedBy: "custom",
  190. SupportedEndpointTypes: model.GetModelSupportEndpointTypes(modelName),
  191. })
  192. }
  193. }
  194. }
  195. switch modelType {
  196. case constant.ChannelTypeAnthropic:
  197. useranthropicModels := make([]dto.AnthropicModel, len(userOpenAiModels))
  198. for i, model := range userOpenAiModels {
  199. useranthropicModels[i] = dto.AnthropicModel{
  200. ID: model.Id,
  201. CreatedAt: time.Unix(int64(model.Created), 0).UTC().Format(time.RFC3339),
  202. DisplayName: model.Id,
  203. Type: "model",
  204. }
  205. }
  206. c.JSON(200, gin.H{
  207. "data": useranthropicModels,
  208. "first_id": useranthropicModels[0].ID,
  209. "has_more": false,
  210. "last_id": useranthropicModels[len(useranthropicModels)-1].ID,
  211. })
  212. case constant.ChannelTypeGemini:
  213. userGeminiModels := make([]dto.GeminiModel, len(userOpenAiModels))
  214. for i, model := range userOpenAiModels {
  215. userGeminiModels[i] = dto.GeminiModel{
  216. Name: model.Id,
  217. DisplayName: model.Id,
  218. }
  219. }
  220. c.JSON(200, gin.H{
  221. "models": userGeminiModels,
  222. "nextPageToken": nil,
  223. })
  224. default:
  225. c.JSON(200, gin.H{
  226. "success": true,
  227. "data": userOpenAiModels,
  228. "object": "list",
  229. })
  230. }
  231. }
  232. func ChannelListModels(c *gin.Context) {
  233. c.JSON(200, gin.H{
  234. "success": true,
  235. "data": openAIModels,
  236. })
  237. }
  238. func DashboardListModels(c *gin.Context) {
  239. c.JSON(200, gin.H{
  240. "success": true,
  241. "data": channelId2Models,
  242. })
  243. }
  244. func EnabledListModels(c *gin.Context) {
  245. c.JSON(200, gin.H{
  246. "success": true,
  247. "data": model.GetEnabledModels(),
  248. })
  249. }
  250. func RetrieveModel(c *gin.Context, modelType int) {
  251. modelId := c.Param("model")
  252. if aiModel, ok := openAIModelsMap[modelId]; ok {
  253. switch modelType {
  254. case constant.ChannelTypeAnthropic:
  255. c.JSON(200, dto.AnthropicModel{
  256. ID: aiModel.Id,
  257. CreatedAt: time.Unix(int64(aiModel.Created), 0).UTC().Format(time.RFC3339),
  258. DisplayName: aiModel.Id,
  259. Type: "model",
  260. })
  261. default:
  262. c.JSON(200, aiModel)
  263. }
  264. } else {
  265. openAIError := dto.OpenAIError{
  266. Message: fmt.Sprintf("The model '%s' does not exist", modelId),
  267. Type: "invalid_request_error",
  268. Param: "model",
  269. Code: "model_not_found",
  270. }
  271. c.JSON(200, gin.H{
  272. "error": openAIError,
  273. })
  274. }
  275. }