ai-gateway/controller/relay.go

86 lines
2.3 KiB
Go
Raw Normal View History

2023-04-23 10:24:11 +00:00
package controller
import (
"fmt"
"github.com/gin-gonic/gin"
2023-04-23 10:24:11 +00:00
"net/http"
"one-api/common/config"
"one-api/common/helper"
"one-api/common/logger"
2024-01-14 11:21:03 +00:00
"one-api/relay/channel/openai"
"one-api/relay/constant"
"one-api/relay/controller"
"one-api/relay/util"
2023-07-15 11:06:51 +00:00
"strconv"
2023-04-23 10:24:11 +00:00
)
2023-05-21 06:26:59 +00:00
// https://platform.openai.com/docs/api-reference/chat
2023-04-23 10:24:11 +00:00
func Relay(c *gin.Context) {
relayMode := constant.Path2RelayMode(c.Request.URL.Path)
2024-01-14 11:21:03 +00:00
var err *openai.ErrorWithStatusCode
2023-06-19 02:28:55 +00:00
switch relayMode {
2024-01-14 11:21:03 +00:00
case constant.RelayModeImagesGenerations:
err = controller.RelayImageHelper(c, relayMode)
case constant.RelayModeAudioSpeech:
fallthrough
2024-01-14 11:21:03 +00:00
case constant.RelayModeAudioTranslation:
fallthrough
2024-01-14 11:21:03 +00:00
case constant.RelayModeAudioTranscription:
err = controller.RelayAudioHelper(c, relayMode)
2023-06-19 02:28:55 +00:00
default:
2024-01-14 11:21:03 +00:00
err = controller.RelayTextHelper(c, relayMode)
}
if err != nil {
requestId := c.GetString(logger.RequestIdKey)
2023-07-15 11:06:51 +00:00
retryTimesStr := c.Query("retry")
retryTimes, _ := strconv.Atoi(retryTimesStr)
if retryTimesStr == "" {
retryTimes = config.RetryTimes
2023-07-15 11:06:51 +00:00
}
if retryTimes > 0 {
c.Redirect(http.StatusTemporaryRedirect, fmt.Sprintf("%s?retry=%d", c.Request.URL.Path, retryTimes-1))
} else {
if err.StatusCode == http.StatusTooManyRequests {
2024-01-14 11:21:03 +00:00
err.Error.Message = "当前分组上游负载已饱和,请稍后再试"
2023-07-15 11:06:51 +00:00
}
err.Error.Message = helper.MessageWithRequestId(err.Error.Message, requestId)
2023-07-15 11:06:51 +00:00
c.JSON(err.StatusCode, gin.H{
2024-01-14 11:21:03 +00:00
"error": err.Error,
2023-07-15 11:06:51 +00:00
})
}
channelId := c.GetInt("channel_id")
logger.Error(c.Request.Context(), fmt.Sprintf("relay error (channel #%d): %s", channelId, err.Message))
2023-05-21 12:58:00 +00:00
// https://platform.openai.com/docs/guides/error-codes/api-errors
2024-01-14 11:21:03 +00:00
if util.ShouldDisableChannel(&err.Error, err.StatusCode) {
channelId := c.GetInt("channel_id")
channelName := c.GetString("channel_name")
disableChannel(channelId, channelName, err.Message)
}
}
}
func RelayNotImplemented(c *gin.Context) {
2024-01-14 11:21:03 +00:00
err := openai.Error{
Message: "API not implemented",
Type: "one_api_error",
Param: "",
Code: "api_not_implemented",
}
2023-06-23 14:59:44 +00:00
c.JSON(http.StatusNotImplemented, gin.H{
"error": err,
})
}
func RelayNotFound(c *gin.Context) {
2024-01-14 11:21:03 +00:00
err := openai.Error{
2023-08-11 11:53:01 +00:00
Message: fmt.Sprintf("Invalid URL (%s %s)", c.Request.Method, c.Request.URL.Path),
Type: "invalid_request_error",
Param: "",
2023-08-11 11:53:01 +00:00
Code: "",
}
2023-06-23 14:59:44 +00:00
c.JSON(http.StatusNotFound, gin.H{
"error": err,
})
}