relay.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. package controller
  2. import (
  3. "bytes"
  4. "errors"
  5. "fmt"
  6. "github.com/gin-gonic/gin"
  7. "io"
  8. "log"
  9. "net/http"
  10. "one-api/common"
  11. "one-api/dto"
  12. "one-api/middleware"
  13. "one-api/model"
  14. "one-api/relay"
  15. "one-api/relay/constant"
  16. relayconstant "one-api/relay/constant"
  17. "one-api/service"
  18. "strings"
  19. )
  20. func relayHandler(c *gin.Context, relayMode int) *dto.OpenAIErrorWithStatusCode {
  21. var err *dto.OpenAIErrorWithStatusCode
  22. switch relayMode {
  23. case relayconstant.RelayModeImagesGenerations:
  24. err = relay.ImageHelper(c, relayMode)
  25. case relayconstant.RelayModeAudioSpeech:
  26. fallthrough
  27. case relayconstant.RelayModeAudioTranslation:
  28. fallthrough
  29. case relayconstant.RelayModeAudioTranscription:
  30. err = relay.AudioHelper(c)
  31. case relayconstant.RelayModeRerank:
  32. err = relay.RerankHelper(c, relayMode)
  33. default:
  34. err = relay.TextHelper(c)
  35. }
  36. return err
  37. }
  38. func Playground(c *gin.Context) {
  39. var openaiErr *dto.OpenAIErrorWithStatusCode
  40. defer func() {
  41. if openaiErr != nil {
  42. c.JSON(openaiErr.StatusCode, gin.H{
  43. "error": openaiErr.Error,
  44. })
  45. }
  46. }()
  47. useAccessToken := c.GetBool("use_access_token")
  48. if useAccessToken {
  49. openaiErr = service.OpenAIErrorWrapperLocal(errors.New("暂不支持使用 access token"), "access_token_not_supported", http.StatusBadRequest)
  50. return
  51. }
  52. playgroundRequest := &dto.PlayGroundRequest{}
  53. err := common.UnmarshalBodyReusable(c, playgroundRequest)
  54. if err != nil {
  55. openaiErr = service.OpenAIErrorWrapperLocal(err, "unmarshal_request_failed", http.StatusBadRequest)
  56. return
  57. }
  58. if playgroundRequest.Model == "" {
  59. openaiErr = service.OpenAIErrorWrapperLocal(errors.New("请选择模型"), "model_required", http.StatusBadRequest)
  60. return
  61. }
  62. c.Set("original_model", playgroundRequest.Model)
  63. group := playgroundRequest.Group
  64. userGroup := c.GetString("group")
  65. if group == "" {
  66. group = userGroup
  67. } else {
  68. if !common.GroupInUserUsableGroups(group) && group != userGroup {
  69. openaiErr = service.OpenAIErrorWrapperLocal(errors.New("无权访问该分组"), "group_not_allowed", http.StatusForbidden)
  70. return
  71. }
  72. c.Set("group", group)
  73. }
  74. c.Set("token_name", "playground-"+group)
  75. channel, err := model.CacheGetRandomSatisfiedChannel(group, playgroundRequest.Model, 0)
  76. if err != nil {
  77. message := fmt.Sprintf("当前分组 %s 下对于模型 %s 无可用渠道", group, playgroundRequest.Model)
  78. openaiErr = service.OpenAIErrorWrapperLocal(errors.New(message), "get_playground_channel_failed", http.StatusInternalServerError)
  79. return
  80. }
  81. middleware.SetupContextForSelectedChannel(c, channel, playgroundRequest.Model)
  82. Relay(c)
  83. }
  84. func Relay(c *gin.Context) {
  85. relayMode := constant.Path2RelayMode(c.Request.URL.Path)
  86. requestId := c.GetString(common.RequestIdKey)
  87. group := c.GetString("group")
  88. originalModel := c.GetString("original_model")
  89. var openaiErr *dto.OpenAIErrorWithStatusCode
  90. for i := 0; i <= common.RetryTimes; i++ {
  91. channel, err := getChannel(c, group, originalModel, i)
  92. if err != nil {
  93. common.LogError(c, err.Error())
  94. openaiErr = service.OpenAIErrorWrapperLocal(err, "get_channel_failed", http.StatusInternalServerError)
  95. break
  96. }
  97. openaiErr = relayRequest(c, relayMode, channel)
  98. if openaiErr == nil {
  99. return // 成功处理请求,直接返回
  100. }
  101. go processChannelError(c, channel.Id, channel.Type, channel.Name, channel.GetAutoBan(), openaiErr)
  102. if !shouldRetry(c, openaiErr, common.RetryTimes-i) {
  103. break
  104. }
  105. }
  106. useChannel := c.GetStringSlice("use_channel")
  107. if len(useChannel) > 1 {
  108. retryLogStr := fmt.Sprintf("重试:%s", strings.Trim(strings.Join(strings.Fields(fmt.Sprint(useChannel)), "->"), "[]"))
  109. common.LogInfo(c, retryLogStr)
  110. }
  111. if openaiErr != nil {
  112. if openaiErr.StatusCode == http.StatusTooManyRequests {
  113. openaiErr.Error.Message = "当前分组上游负载已饱和,请稍后再试"
  114. }
  115. openaiErr.Error.Message = common.MessageWithRequestId(openaiErr.Error.Message, requestId)
  116. c.JSON(openaiErr.StatusCode, gin.H{
  117. "error": openaiErr.Error,
  118. })
  119. }
  120. }
  121. func relayRequest(c *gin.Context, relayMode int, channel *model.Channel) *dto.OpenAIErrorWithStatusCode {
  122. addUsedChannel(c, channel.Id)
  123. requestBody, _ := common.GetRequestBody(c)
  124. c.Request.Body = io.NopCloser(bytes.NewBuffer(requestBody))
  125. return relayHandler(c, relayMode)
  126. }
  127. func addUsedChannel(c *gin.Context, channelId int) {
  128. useChannel := c.GetStringSlice("use_channel")
  129. useChannel = append(useChannel, fmt.Sprintf("%d", channelId))
  130. c.Set("use_channel", useChannel)
  131. }
  132. func getChannel(c *gin.Context, group, originalModel string, retryCount int) (*model.Channel, error) {
  133. if retryCount == 0 {
  134. autoBan := c.GetBool("auto_ban")
  135. autoBanInt := 1
  136. if !autoBan {
  137. autoBanInt = 0
  138. }
  139. return &model.Channel{
  140. Id: c.GetInt("channel_id"),
  141. Type: c.GetInt("channel_type"),
  142. Name: c.GetString("channel_name"),
  143. AutoBan: &autoBanInt,
  144. }, nil
  145. }
  146. channel, err := model.CacheGetRandomSatisfiedChannel(group, originalModel, retryCount)
  147. if err != nil {
  148. return nil, errors.New(fmt.Sprintf("获取重试渠道失败: %s", err.Error()))
  149. }
  150. middleware.SetupContextForSelectedChannel(c, channel, originalModel)
  151. return channel, nil
  152. }
  153. func shouldRetry(c *gin.Context, openaiErr *dto.OpenAIErrorWithStatusCode, retryTimes int) bool {
  154. if openaiErr == nil {
  155. return false
  156. }
  157. if openaiErr.LocalError {
  158. return false
  159. }
  160. if retryTimes <= 0 {
  161. return false
  162. }
  163. if _, ok := c.Get("specific_channel_id"); ok {
  164. return false
  165. }
  166. if openaiErr.StatusCode == http.StatusTooManyRequests {
  167. return true
  168. }
  169. if openaiErr.StatusCode == 307 {
  170. return true
  171. }
  172. if openaiErr.StatusCode/100 == 5 {
  173. // 超时不重试
  174. if openaiErr.StatusCode == 504 || openaiErr.StatusCode == 524 {
  175. return false
  176. }
  177. return true
  178. }
  179. if openaiErr.StatusCode == http.StatusBadRequest {
  180. channelType := c.GetInt("channel_type")
  181. if channelType == common.ChannelTypeAnthropic {
  182. return true
  183. }
  184. return false
  185. }
  186. if openaiErr.StatusCode == 408 {
  187. // azure处理超时不重试
  188. return false
  189. }
  190. if openaiErr.StatusCode/100 == 2 {
  191. return false
  192. }
  193. return true
  194. }
  195. func processChannelError(c *gin.Context, channelId int, channelType int, channelName string, autoBan bool, err *dto.OpenAIErrorWithStatusCode) {
  196. // 不要使用context获取渠道信息,异步处理时可能会出现渠道信息不一致的情况
  197. // do not use context to get channel info, there may be inconsistent channel info when processing asynchronously
  198. common.LogError(c, fmt.Sprintf("relay error (channel #%d, status code: %d): %s", channelId, err.StatusCode, err.Error.Message))
  199. if service.ShouldDisableChannel(channelType, err) && autoBan {
  200. service.DisableChannel(channelId, channelName, err.Error.Message)
  201. }
  202. }
  203. func RelayMidjourney(c *gin.Context) {
  204. relayMode := c.GetInt("relay_mode")
  205. var err *dto.MidjourneyResponse
  206. switch relayMode {
  207. case relayconstant.RelayModeMidjourneyNotify:
  208. err = relay.RelayMidjourneyNotify(c)
  209. case relayconstant.RelayModeMidjourneyTaskFetch, relayconstant.RelayModeMidjourneyTaskFetchByCondition:
  210. err = relay.RelayMidjourneyTask(c, relayMode)
  211. case relayconstant.RelayModeMidjourneyTaskImageSeed:
  212. err = relay.RelayMidjourneyTaskImageSeed(c)
  213. case relayconstant.RelayModeSwapFace:
  214. err = relay.RelaySwapFace(c)
  215. default:
  216. err = relay.RelayMidjourneySubmit(c, relayMode)
  217. }
  218. //err = relayMidjourneySubmit(c, relayMode)
  219. log.Println(err)
  220. if err != nil {
  221. statusCode := http.StatusBadRequest
  222. if err.Code == 30 {
  223. err.Result = "当前分组负载已饱和,请稍后再试,或升级账户以提升服务质量。"
  224. statusCode = http.StatusTooManyRequests
  225. }
  226. c.JSON(statusCode, gin.H{
  227. "description": fmt.Sprintf("%s %s", err.Description, err.Result),
  228. "type": "upstream_error",
  229. "code": err.Code,
  230. })
  231. channelId := c.GetInt("channel_id")
  232. common.LogError(c, fmt.Sprintf("relay error (channel #%d, status code %d): %s", channelId, statusCode, fmt.Sprintf("%s %s", err.Description, err.Result)))
  233. }
  234. }
  235. func RelayNotImplemented(c *gin.Context) {
  236. err := dto.OpenAIError{
  237. Message: "API not implemented",
  238. Type: "new_api_error",
  239. Param: "",
  240. Code: "api_not_implemented",
  241. }
  242. c.JSON(http.StatusNotImplemented, gin.H{
  243. "error": err,
  244. })
  245. }
  246. func RelayNotFound(c *gin.Context) {
  247. err := dto.OpenAIError{
  248. Message: fmt.Sprintf("Invalid URL (%s %s)", c.Request.Method, c.Request.URL.Path),
  249. Type: "invalid_request_error",
  250. Param: "",
  251. Code: "",
  252. }
  253. c.JSON(http.StatusNotFound, gin.H{
  254. "error": err,
  255. })
  256. }
  257. func RelayTask(c *gin.Context) {
  258. retryTimes := common.RetryTimes
  259. channelId := c.GetInt("channel_id")
  260. relayMode := c.GetInt("relay_mode")
  261. group := c.GetString("group")
  262. originalModel := c.GetString("original_model")
  263. c.Set("use_channel", []string{fmt.Sprintf("%d", channelId)})
  264. taskErr := taskRelayHandler(c, relayMode)
  265. if taskErr == nil {
  266. retryTimes = 0
  267. }
  268. for i := 0; shouldRetryTaskRelay(c, channelId, taskErr, retryTimes) && i < retryTimes; i++ {
  269. channel, err := model.CacheGetRandomSatisfiedChannel(group, originalModel, i)
  270. if err != nil {
  271. common.LogError(c, fmt.Sprintf("CacheGetRandomSatisfiedChannel failed: %s", err.Error()))
  272. break
  273. }
  274. channelId = channel.Id
  275. useChannel := c.GetStringSlice("use_channel")
  276. useChannel = append(useChannel, fmt.Sprintf("%d", channelId))
  277. c.Set("use_channel", useChannel)
  278. common.LogInfo(c, fmt.Sprintf("using channel #%d to retry (remain times %d)", channel.Id, i))
  279. middleware.SetupContextForSelectedChannel(c, channel, originalModel)
  280. requestBody, err := common.GetRequestBody(c)
  281. c.Request.Body = io.NopCloser(bytes.NewBuffer(requestBody))
  282. taskErr = taskRelayHandler(c, relayMode)
  283. }
  284. useChannel := c.GetStringSlice("use_channel")
  285. if len(useChannel) > 1 {
  286. retryLogStr := fmt.Sprintf("重试:%s", strings.Trim(strings.Join(strings.Fields(fmt.Sprint(useChannel)), "->"), "[]"))
  287. common.LogInfo(c, retryLogStr)
  288. }
  289. if taskErr != nil {
  290. if taskErr.StatusCode == http.StatusTooManyRequests {
  291. taskErr.Message = "当前分组上游负载已饱和,请稍后再试"
  292. }
  293. c.JSON(taskErr.StatusCode, taskErr)
  294. }
  295. }
  296. func taskRelayHandler(c *gin.Context, relayMode int) *dto.TaskError {
  297. var err *dto.TaskError
  298. switch relayMode {
  299. case relayconstant.RelayModeSunoFetch, relayconstant.RelayModeSunoFetchByID:
  300. err = relay.RelayTaskFetch(c, relayMode)
  301. default:
  302. err = relay.RelayTaskSubmit(c, relayMode)
  303. }
  304. return err
  305. }
  306. func shouldRetryTaskRelay(c *gin.Context, channelId int, taskErr *dto.TaskError, retryTimes int) bool {
  307. if taskErr == nil {
  308. return false
  309. }
  310. if retryTimes <= 0 {
  311. return false
  312. }
  313. if _, ok := c.Get("specific_channel_id"); ok {
  314. return false
  315. }
  316. if taskErr.StatusCode == http.StatusTooManyRequests {
  317. return true
  318. }
  319. if taskErr.StatusCode == 307 {
  320. return true
  321. }
  322. if taskErr.StatusCode/100 == 5 {
  323. // 超时不重试
  324. if taskErr.StatusCode == 504 || taskErr.StatusCode == 524 {
  325. return false
  326. }
  327. return true
  328. }
  329. if taskErr.StatusCode == http.StatusBadRequest {
  330. return false
  331. }
  332. if taskErr.StatusCode == 408 {
  333. // azure处理超时不重试
  334. return false
  335. }
  336. if taskErr.LocalError {
  337. return false
  338. }
  339. if taskErr.StatusCode/100 == 2 {
  340. return false
  341. }
  342. return true
  343. }