model.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. package controller
  2. import (
  3. "fmt"
  4. "github.com/gin-gonic/gin"
  5. "github.com/samber/lo"
  6. "net/http"
  7. "one-api/common"
  8. "one-api/constant"
  9. "one-api/dto"
  10. "one-api/model"
  11. "one-api/relay"
  12. "one-api/relay/channel/ai360"
  13. "one-api/relay/channel/lingyiwanwu"
  14. "one-api/relay/channel/minimax"
  15. "one-api/relay/channel/moonshot"
  16. relaycommon "one-api/relay/common"
  17. "one-api/setting"
  18. "time"
  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{ChannelType: i}
  93. adaptor := relay.GetAdaptor(apiType)
  94. adaptor.Init(meta)
  95. channelId2Models[i] = adaptor.GetModelList()
  96. }
  97. openAIModels = lo.UniqBy(openAIModels, func(m dto.OpenAIModels) string {
  98. return m.Id
  99. })
  100. }
  101. func ListModels(c *gin.Context, modelType int) {
  102. userOpenAiModels := make([]dto.OpenAIModels, 0)
  103. modelLimitEnable := common.GetContextKeyBool(c, constant.ContextKeyTokenModelLimitEnabled)
  104. if modelLimitEnable {
  105. s, ok := common.GetContextKey(c, constant.ContextKeyTokenModelLimit)
  106. var tokenModelLimit map[string]bool
  107. if ok {
  108. tokenModelLimit = s.(map[string]bool)
  109. } else {
  110. tokenModelLimit = map[string]bool{}
  111. }
  112. for allowModel, _ := range tokenModelLimit {
  113. if oaiModel, ok := openAIModelsMap[allowModel]; ok {
  114. oaiModel.SupportedEndpointTypes = model.GetModelSupportEndpointTypes(allowModel)
  115. userOpenAiModels = append(userOpenAiModels, oaiModel)
  116. } else {
  117. userOpenAiModels = append(userOpenAiModels, dto.OpenAIModels{
  118. Id: allowModel,
  119. Object: "model",
  120. Created: 1626777600,
  121. OwnedBy: "custom",
  122. SupportedEndpointTypes: model.GetModelSupportEndpointTypes(allowModel),
  123. })
  124. }
  125. }
  126. } else {
  127. userId := c.GetInt("id")
  128. userGroup, err := model.GetUserGroup(userId, false)
  129. if err != nil {
  130. c.JSON(http.StatusOK, gin.H{
  131. "success": false,
  132. "message": "get user group failed",
  133. })
  134. return
  135. }
  136. group := userGroup
  137. tokenGroup := common.GetContextKeyString(c, constant.ContextKeyTokenGroup)
  138. if tokenGroup != "" {
  139. group = tokenGroup
  140. }
  141. var models []string
  142. if tokenGroup == "auto" {
  143. for _, autoGroup := range setting.AutoGroups {
  144. groupModels := model.GetGroupEnabledModels(autoGroup)
  145. for _, g := range groupModels {
  146. if !common.StringsContains(models, g) {
  147. models = append(models, g)
  148. }
  149. }
  150. }
  151. } else {
  152. models = model.GetGroupEnabledModels(group)
  153. }
  154. for _, modelName := range models {
  155. if oaiModel, ok := openAIModelsMap[modelName]; ok {
  156. oaiModel.SupportedEndpointTypes = model.GetModelSupportEndpointTypes(modelName)
  157. userOpenAiModels = append(userOpenAiModels, oaiModel)
  158. } else {
  159. userOpenAiModels = append(userOpenAiModels, dto.OpenAIModels{
  160. Id: modelName,
  161. Object: "model",
  162. Created: 1626777600,
  163. OwnedBy: "custom",
  164. SupportedEndpointTypes: model.GetModelSupportEndpointTypes(modelName),
  165. })
  166. }
  167. }
  168. }
  169. switch modelType {
  170. case constant.ChannelTypeAnthropic:
  171. useranthropicModels := make([]dto.AnthropicModel, len(userOpenAiModels))
  172. for i, model := range userOpenAiModels {
  173. useranthropicModels[i] = dto.AnthropicModel{
  174. ID: model.Id,
  175. CreatedAt: time.Unix(int64(model.Created), 0).UTC().Format(time.RFC3339),
  176. DisplayName: model.Id,
  177. Type: "model",
  178. }
  179. }
  180. c.JSON(200, gin.H{
  181. "data": useranthropicModels,
  182. "first_id": useranthropicModels[0].ID,
  183. "has_more": false,
  184. "last_id": useranthropicModels[len(useranthropicModels)-1].ID,
  185. })
  186. case constant.ChannelTypeGemini:
  187. userGeminiModels := make([]dto.GeminiModel, len(userOpenAiModels))
  188. for i, model := range userOpenAiModels {
  189. userGeminiModels[i] = dto.GeminiModel{
  190. Name: model.Id,
  191. DisplayName: model.Id,
  192. }
  193. }
  194. c.JSON(200, gin.H{
  195. "models": userGeminiModels,
  196. "nextPageToken": nil,
  197. })
  198. default:
  199. c.JSON(200, gin.H{
  200. "success": true,
  201. "data": userOpenAiModels,
  202. })
  203. }
  204. }
  205. func ChannelListModels(c *gin.Context) {
  206. c.JSON(200, gin.H{
  207. "success": true,
  208. "data": openAIModels,
  209. })
  210. }
  211. func DashboardListModels(c *gin.Context) {
  212. c.JSON(200, gin.H{
  213. "success": true,
  214. "data": channelId2Models,
  215. })
  216. }
  217. func EnabledListModels(c *gin.Context) {
  218. c.JSON(200, gin.H{
  219. "success": true,
  220. "data": model.GetEnabledModels(),
  221. })
  222. }
  223. func RetrieveModel(c *gin.Context, modelType int) {
  224. modelId := c.Param("model")
  225. if aiModel, ok := openAIModelsMap[modelId]; ok {
  226. switch modelType {
  227. case constant.ChannelTypeAnthropic:
  228. c.JSON(200, dto.AnthropicModel{
  229. ID: aiModel.Id,
  230. CreatedAt: time.Unix(int64(aiModel.Created), 0).UTC().Format(time.RFC3339),
  231. DisplayName: aiModel.Id,
  232. Type: "model",
  233. })
  234. default:
  235. c.JSON(200, aiModel)
  236. }
  237. } else {
  238. openAIError := dto.OpenAIError{
  239. Message: fmt.Sprintf("The model '%s' does not exist", modelId),
  240. Type: "invalid_request_error",
  241. Param: "model",
  242. Code: "model_not_found",
  243. }
  244. c.JSON(200, gin.H{
  245. "error": openAIError,
  246. })
  247. }
  248. }