ai-gateway/controller/relay.go

99 lines
3.2 KiB
Go
Raw Normal View History

2023-04-23 10:24:11 +00:00
package controller
import (
"fmt"
"net/http"
"one-api/common"
2023-11-28 10:32:26 +00:00
"one-api/types"
2023-07-15 11:06:51 +00:00
"strconv"
2023-04-25 12:27:53 +00:00
"strings"
"github.com/gin-gonic/gin"
2023-04-23 10:24:11 +00:00
)
func Relay(c *gin.Context) {
2023-12-01 02:54:07 +00:00
defer c.Request.Body.Close()
2023-11-28 10:32:26 +00:00
var err *types.OpenAIErrorWithStatusCode
relayMode := common.RelayModeUnknown
if strings.HasPrefix(c.Request.URL.Path, "/v1/chat/completions") {
relayMode = common.RelayModeChatCompletions
} else if strings.HasPrefix(c.Request.URL.Path, "/v1/completions") {
relayMode = common.RelayModeCompletions
} else if strings.HasPrefix(c.Request.URL.Path, "/v1/embeddings") {
relayMode = common.RelayModeEmbeddings
} else if strings.HasSuffix(c.Request.URL.Path, "embeddings") {
relayMode = common.RelayModeEmbeddings
} else if strings.HasPrefix(c.Request.URL.Path, "/v1/moderations") {
relayMode = common.RelayModeModerations
2023-11-29 10:11:15 +00:00
} else if strings.HasPrefix(c.Request.URL.Path, "/v1/audio/speech") {
relayMode = common.RelayModeAudioSpeech
2023-12-01 02:54:07 +00:00
} else if strings.HasPrefix(c.Request.URL.Path, "/v1/audio/transcriptions") {
relayMode = common.RelayModeAudioTranscription
2023-12-01 03:38:35 +00:00
} else if strings.HasPrefix(c.Request.URL.Path, "/v1/audio/translations") {
relayMode = common.RelayModeAudioTranslation
2023-12-01 09:20:22 +00:00
} else if strings.HasPrefix(c.Request.URL.Path, "/v1/images/generations") {
relayMode = common.RelayModeImagesGenerations
} else if strings.HasPrefix(c.Request.URL.Path, "/v1/images/edits") {
relayMode = common.RelayModeImagesEdits
} else if strings.HasPrefix(c.Request.URL.Path, "/v1/images/variations") {
relayMode = common.RelayModeImagesVariations
}
2023-11-28 10:32:26 +00:00
// } else if strings.HasPrefix(c.Request.URL.Path, "/v1/edits") {
// relayMode = RelayModeEdits
2023-12-01 02:54:07 +00:00
2023-12-01 10:36:30 +00:00
err = relayHelper(c, relayMode)
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 {
2023-08-12 02:20:54 +00:00
err.OpenAIError.Message = "当前分组上游负载已饱和,请稍后再试"
2023-07-15 11:06:51 +00:00
}
2023-09-17 07:39:46 +00:00
err.OpenAIError.Message = common.MessageWithRequestId(err.OpenAIError.Message, requestId)
2023-07-15 11:06:51 +00:00
c.JSON(err.StatusCode, gin.H{
"error": err.OpenAIError,
})
}
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
if shouldDisableChannel(&err.OpenAIError, err.StatusCode) {
channelId := c.GetInt("channel_id")
channelName := c.GetString("channel_name")
disableChannel(channelId, channelName, err.Message)
}
}
}
func RelayNotImplemented(c *gin.Context) {
2023-12-01 10:36:30 +00:00
err := types.OpenAIError{
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) {
2023-12-01 10:36:30 +00:00
err := types.OpenAIError{
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,
})
}