2023-04-23 10:24:11 +00:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-01-21 15:21:42 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
2023-04-23 10:24:11 +00:00
|
|
|
"net/http"
|
2024-01-21 15:21:42 +00:00
|
|
|
"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) {
|
2024-01-21 15:21:42 +00:00
|
|
|
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:
|
2023-11-17 13:18:51 +00:00
|
|
|
fallthrough
|
2024-01-14 11:21:03 +00:00
|
|
|
case constant.RelayModeAudioTranslation:
|
2023-11-17 13:18:51 +00:00
|
|
|
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)
|
2023-06-08 06:54:02 +00:00
|
|
|
}
|
2023-04-28 09:11:57 +00:00
|
|
|
if err != nil {
|
2024-01-21 15:21:42 +00:00
|
|
|
requestId := c.GetString(logger.RequestIdKey)
|
2023-07-15 11:06:51 +00:00
|
|
|
retryTimesStr := c.Query("retry")
|
|
|
|
retryTimes, _ := strconv.Atoi(retryTimesStr)
|
|
|
|
if retryTimesStr == "" {
|
2024-01-21 15:21:42 +00:00
|
|
|
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
|
|
|
}
|
2024-01-21 15:21:42 +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
|
|
|
})
|
2023-05-18 07:27:15 +00:00
|
|
|
}
|
2023-05-17 12:20:48 +00:00
|
|
|
channelId := c.GetInt("channel_id")
|
2024-01-21 15:21:42 +00:00
|
|
|
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) {
|
2023-05-15 09:34:09 +00:00
|
|
|
channelId := c.GetInt("channel_id")
|
|
|
|
channelName := c.GetString("channel_name")
|
2023-05-18 03:11:15 +00:00
|
|
|
disableChannel(channelId, channelName, err.Message)
|
2023-05-15 09:34:09 +00:00
|
|
|
}
|
2023-04-28 09:11:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-28 08:58:55 +00:00
|
|
|
func RelayNotImplemented(c *gin.Context) {
|
2024-01-14 11:21:03 +00:00
|
|
|
err := openai.Error{
|
2023-05-18 03:11:15 +00:00
|
|
|
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{
|
2023-05-18 03:11:15 +00:00
|
|
|
"error": err,
|
2023-04-28 08:58:55 +00:00
|
|
|
})
|
|
|
|
}
|
2023-06-17 01:46:07 +00:00
|
|
|
|
|
|
|
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",
|
2023-06-17 01:46:07 +00:00
|
|
|
Param: "",
|
2023-08-11 11:53:01 +00:00
|
|
|
Code: "",
|
2023-06-17 01:46:07 +00:00
|
|
|
}
|
2023-06-23 14:59:44 +00:00
|
|
|
c.JSON(http.StatusNotFound, gin.H{
|
2023-06-17 01:46:07 +00:00
|
|
|
"error": err,
|
|
|
|
})
|
|
|
|
}
|