relay.go 12 KB

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