relay.go 12 KB

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