2023-06-19 02:28:55 +00:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
2024-02-17 16:15:31 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2023-06-19 02:28:55 +00:00
|
|
|
"fmt"
|
2024-01-14 11:21:03 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
2024-01-28 11:38:58 +00:00
|
|
|
"github.com/songquanpeng/one-api/common"
|
|
|
|
"github.com/songquanpeng/one-api/common/logger"
|
|
|
|
"github.com/songquanpeng/one-api/relay/channel/openai"
|
|
|
|
"github.com/songquanpeng/one-api/relay/constant"
|
2024-02-17 16:15:31 +00:00
|
|
|
"github.com/songquanpeng/one-api/relay/helper"
|
|
|
|
"github.com/songquanpeng/one-api/relay/model"
|
2024-01-28 11:38:58 +00:00
|
|
|
"github.com/songquanpeng/one-api/relay/util"
|
2024-02-17 16:15:31 +00:00
|
|
|
"io"
|
2023-06-19 02:28:55 +00:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2024-02-17 16:15:31 +00:00
|
|
|
func RelayTextHelper(c *gin.Context) *model.ErrorWithStatusCode {
|
2024-01-21 15:21:42 +00:00
|
|
|
ctx := c.Request.Context()
|
|
|
|
meta := util.GetRelayMeta(c)
|
2024-01-28 11:13:11 +00:00
|
|
|
// get & validate textRequest
|
|
|
|
textRequest, err := getAndValidateTextRequest(c, meta.Mode)
|
2024-01-21 15:21:42 +00:00
|
|
|
if err != nil {
|
2024-01-28 11:13:11 +00:00
|
|
|
logger.Errorf(ctx, "getAndValidateTextRequest failed: %s", err.Error())
|
2024-01-21 15:21:42 +00:00
|
|
|
return openai.ErrorWrapper(err, "invalid_text_request", http.StatusBadRequest)
|
2023-06-19 02:28:55 +00:00
|
|
|
}
|
2024-02-17 16:15:31 +00:00
|
|
|
meta.IsStream = textRequest.Stream
|
|
|
|
|
2024-01-28 11:13:11 +00:00
|
|
|
// map model name
|
2024-01-21 15:21:42 +00:00
|
|
|
var isModelMapped bool
|
2024-02-17 16:15:31 +00:00
|
|
|
meta.OriginModelName = textRequest.Model
|
2024-01-21 15:21:42 +00:00
|
|
|
textRequest.Model, isModelMapped = util.GetMappedModelName(textRequest.Model, meta.ModelMapping)
|
2024-02-17 16:15:31 +00:00
|
|
|
meta.ActualModelName = textRequest.Model
|
2024-01-28 11:13:11 +00:00
|
|
|
// get model ratio & group ratio
|
2023-06-19 02:28:55 +00:00
|
|
|
modelRatio := common.GetModelRatio(textRequest.Model)
|
2024-01-21 15:21:42 +00:00
|
|
|
groupRatio := common.GetGroupRatio(meta.Group)
|
2023-06-19 02:28:55 +00:00
|
|
|
ratio := modelRatio * groupRatio
|
2024-01-28 11:13:11 +00:00
|
|
|
// pre-consume quota
|
|
|
|
promptTokens := getPromptTokens(textRequest, meta.Mode)
|
2024-02-21 14:19:42 +00:00
|
|
|
meta.PromptTokens = promptTokens
|
2024-01-28 11:13:11 +00:00
|
|
|
preConsumedQuota, bizErr := preConsumeQuota(ctx, textRequest, promptTokens, ratio, meta)
|
|
|
|
if bizErr != nil {
|
|
|
|
logger.Warnf(ctx, "preConsumeQuota failed: %+v", *bizErr)
|
|
|
|
return bizErr
|
2023-08-16 15:40:24 +00:00
|
|
|
}
|
2024-01-28 11:13:11 +00:00
|
|
|
|
2024-02-17 16:15:31 +00:00
|
|
|
adaptor := helper.GetAdaptor(meta.APIType)
|
|
|
|
if adaptor == nil {
|
|
|
|
return openai.ErrorWrapper(fmt.Errorf("invalid api type: %d", meta.APIType), "invalid_api_type", http.StatusBadRequest)
|
2023-07-22 08:18:03 +00:00
|
|
|
}
|
2024-02-17 16:15:31 +00:00
|
|
|
|
|
|
|
// get request body
|
|
|
|
var requestBody io.Reader
|
|
|
|
if meta.APIType == constant.APITypeOpenAI {
|
|
|
|
// no need to convert request for openai
|
2024-03-01 16:55:48 +00:00
|
|
|
shouldResetRequestBody := isModelMapped || meta.ChannelType == common.ChannelTypeBaichuan // frequency_penalty 0 is not acceptable for baichuan
|
|
|
|
if shouldResetRequestBody {
|
2024-02-17 16:15:31 +00:00
|
|
|
jsonStr, err := json.Marshal(textRequest)
|
|
|
|
if err != nil {
|
|
|
|
return openai.ErrorWrapper(err, "json_marshal_failed", http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
requestBody = bytes.NewBuffer(jsonStr)
|
|
|
|
} else {
|
|
|
|
requestBody = c.Request.Body
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
convertedRequest, err := adaptor.ConvertRequest(c, meta.Mode, textRequest)
|
2023-07-29 13:55:57 +00:00
|
|
|
if err != nil {
|
2024-02-17 16:15:31 +00:00
|
|
|
return openai.ErrorWrapper(err, "convert_request_failed", http.StatusInternalServerError)
|
2023-07-22 08:18:03 +00:00
|
|
|
}
|
2024-02-17 16:15:31 +00:00
|
|
|
jsonData, err := json.Marshal(convertedRequest)
|
2023-07-29 13:55:57 +00:00
|
|
|
if err != nil {
|
2024-02-17 16:15:31 +00:00
|
|
|
return openai.ErrorWrapper(err, "json_marshal_failed", http.StatusInternalServerError)
|
2023-07-28 15:45:08 +00:00
|
|
|
}
|
2024-02-17 16:15:31 +00:00
|
|
|
requestBody = bytes.NewBuffer(jsonData)
|
|
|
|
}
|
2023-07-29 13:55:57 +00:00
|
|
|
|
2024-02-17 16:15:31 +00:00
|
|
|
// do request
|
|
|
|
resp, err := adaptor.DoRequest(c, meta, requestBody)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf(ctx, "DoRequest failed: %s", err.Error())
|
|
|
|
return openai.ErrorWrapper(err, "do_request_failed", http.StatusInternalServerError)
|
2023-08-19 09:58:45 +00:00
|
|
|
}
|
2024-03-10 06:59:57 +00:00
|
|
|
errorHappened := (resp.StatusCode != http.StatusOK) || (meta.IsStream && resp.Header.Get("Content-Type") == "application/json")
|
|
|
|
if errorHappened {
|
2024-02-17 16:15:31 +00:00
|
|
|
util.ReturnPreConsumedQuota(ctx, preConsumedQuota, meta.TokenId)
|
|
|
|
return util.RelayErrorHandler(resp)
|
|
|
|
}
|
2024-03-10 06:59:57 +00:00
|
|
|
meta.IsStream = meta.IsStream || strings.HasPrefix(resp.Header.Get("Content-Type"), "text/event-stream")
|
2024-02-17 16:15:31 +00:00
|
|
|
|
2024-01-28 11:13:11 +00:00
|
|
|
// do response
|
2024-02-17 16:15:31 +00:00
|
|
|
usage, respErr := adaptor.DoResponse(c, resp, meta)
|
2024-01-21 15:21:42 +00:00
|
|
|
if respErr != nil {
|
2024-01-28 11:13:11 +00:00
|
|
|
logger.Errorf(ctx, "respErr is not nil: %+v", respErr)
|
|
|
|
util.ReturnPreConsumedQuota(ctx, preConsumedQuota, meta.TokenId)
|
2024-01-21 15:21:42 +00:00
|
|
|
return respErr
|
2023-06-19 02:28:55 +00:00
|
|
|
}
|
2024-01-28 11:13:11 +00:00
|
|
|
// post-consume quota
|
|
|
|
go postConsumeQuota(ctx, usage, meta, textRequest, ratio, preConsumedQuota, modelRatio, groupRatio)
|
2024-01-21 15:21:42 +00:00
|
|
|
return nil
|
2023-06-19 02:28:55 +00:00
|
|
|
}
|