2023-04-23 10:24:11 +00:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"one-api/common"
|
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-25 12:27:53 +00:00
|
|
|
"strings"
|
2023-07-15 04:03:23 +00:00
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
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-14 11:21:03 +00:00
|
|
|
relayMode := constant.RelayModeUnknown
|
2023-06-08 06:54:02 +00:00
|
|
|
if strings.HasPrefix(c.Request.URL.Path, "/v1/chat/completions") {
|
2024-01-14 11:21:03 +00:00
|
|
|
relayMode = constant.RelayModeChatCompletions
|
2023-06-08 06:54:02 +00:00
|
|
|
} else if strings.HasPrefix(c.Request.URL.Path, "/v1/completions") {
|
2024-01-14 11:21:03 +00:00
|
|
|
relayMode = constant.RelayModeCompletions
|
2023-06-08 06:54:02 +00:00
|
|
|
} else if strings.HasPrefix(c.Request.URL.Path, "/v1/embeddings") {
|
2024-01-14 11:21:03 +00:00
|
|
|
relayMode = constant.RelayModeEmbeddings
|
2023-07-15 04:03:23 +00:00
|
|
|
} else if strings.HasSuffix(c.Request.URL.Path, "embeddings") {
|
2024-01-14 11:21:03 +00:00
|
|
|
relayMode = constant.RelayModeEmbeddings
|
2023-06-11 01:37:36 +00:00
|
|
|
} else if strings.HasPrefix(c.Request.URL.Path, "/v1/moderations") {
|
2024-01-14 11:21:03 +00:00
|
|
|
relayMode = constant.RelayModeModerations
|
2023-06-19 02:28:55 +00:00
|
|
|
} else if strings.HasPrefix(c.Request.URL.Path, "/v1/images/generations") {
|
2024-01-14 11:21:03 +00:00
|
|
|
relayMode = constant.RelayModeImagesGenerations
|
2023-06-25 03:46:23 +00:00
|
|
|
} else if strings.HasPrefix(c.Request.URL.Path, "/v1/edits") {
|
2024-01-14 11:21:03 +00:00
|
|
|
relayMode = constant.RelayModeEdits
|
2023-11-17 13:18:51 +00:00
|
|
|
} else if strings.HasPrefix(c.Request.URL.Path, "/v1/audio/speech") {
|
2024-01-14 11:21:03 +00:00
|
|
|
relayMode = constant.RelayModeAudioSpeech
|
2023-11-19 08:11:39 +00:00
|
|
|
} else if strings.HasPrefix(c.Request.URL.Path, "/v1/audio/transcriptions") {
|
2024-01-14 11:21:03 +00:00
|
|
|
relayMode = constant.RelayModeAudioTranscription
|
2023-11-19 08:11:39 +00:00
|
|
|
} else if strings.HasPrefix(c.Request.URL.Path, "/v1/audio/translations") {
|
2024-01-14 11:21:03 +00:00
|
|
|
relayMode = constant.RelayModeAudioTranslation
|
2023-06-19 02:28:55 +00:00
|
|
|
}
|
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 {
|
2023-09-17 07:39:46 +00:00
|
|
|
requestId := c.GetString(common.RequestIdKey)
|
2023-07-15 11:06:51 +00:00
|
|
|
retryTimesStr := c.Query("retry")
|
|
|
|
retryTimes, _ := strconv.Atoi(retryTimesStr)
|
|
|
|
if retryTimesStr == "" {
|
|
|
|
retryTimes = common.RetryTimes
|
|
|
|
}
|
|
|
|
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-14 11:21:03 +00:00
|
|
|
err.Error.Message = common.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")
|
2023-09-17 07:39:46 +00:00
|
|
|
common.LogError(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,
|
|
|
|
})
|
|
|
|
}
|