relay.go 13 KB

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