relay.go 13 KB

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