relay.go 11 KB

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