2023-06-19 02:28:55 +00:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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"
|
|
|
|
"github.com/songquanpeng/one-api/relay/util"
|
2023-06-19 02:28:55 +00:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2024-01-28 11:13:11 +00:00
|
|
|
func RelayTextHelper(c *gin.Context) *openai.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-01-28 11:13:11 +00:00
|
|
|
// map model name
|
2024-01-21 15:21:42 +00:00
|
|
|
var isModelMapped bool
|
|
|
|
textRequest.Model, isModelMapped = util.GetMappedModelName(textRequest.Model, meta.ModelMapping)
|
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)
|
|
|
|
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
|
|
|
|
|
|
|
// get request body
|
|
|
|
requestBody, err := GetRequestBody(c, *textRequest, isModelMapped, meta.APIType, meta.Mode)
|
2024-01-21 15:21:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return openai.ErrorWrapper(err, "get_request_body_failed", http.StatusInternalServerError)
|
2023-07-22 08:18:03 +00:00
|
|
|
}
|
2024-01-28 11:13:11 +00:00
|
|
|
// do request
|
2023-07-29 13:55:57 +00:00
|
|
|
var resp *http.Response
|
|
|
|
isStream := textRequest.Stream
|
2024-01-28 11:13:11 +00:00
|
|
|
if meta.APIType != constant.APITypeXunfei { // cause xunfei use websocket
|
|
|
|
fullRequestURL, err := GetRequestURL(c.Request.URL.String(), meta, textRequest)
|
2023-07-29 13:55:57 +00:00
|
|
|
if err != nil {
|
2024-01-28 11:13:11 +00:00
|
|
|
logger.Error(ctx, fmt.Sprintf("util.GetRequestURL failed: %s", err.Error()))
|
|
|
|
return openai.ErrorWrapper(fmt.Errorf("util.GetRequestURL failed"), "get_request_url_failed", http.StatusInternalServerError)
|
2023-07-22 08:18:03 +00:00
|
|
|
}
|
2024-01-28 11:13:11 +00:00
|
|
|
|
|
|
|
resp, err = doRequest(ctx, c, meta, isStream, fullRequestURL, requestBody)
|
2023-07-29 13:55:57 +00:00
|
|
|
if err != nil {
|
2024-01-28 11:13:11 +00:00
|
|
|
logger.Errorf(ctx, "doRequest failed: %s", err.Error())
|
2024-01-14 11:21:03 +00:00
|
|
|
return openai.ErrorWrapper(err, "do_request_failed", http.StatusInternalServerError)
|
2023-07-28 15:45:08 +00:00
|
|
|
}
|
2023-08-06 10:09:00 +00:00
|
|
|
isStream = isStream || strings.HasPrefix(resp.Header.Get("Content-Type"), "text/event-stream")
|
2023-07-29 13:55:57 +00:00
|
|
|
|
2023-08-20 14:07:50 +00:00
|
|
|
if resp.StatusCode != http.StatusOK {
|
2024-01-21 15:21:42 +00:00
|
|
|
util.ReturnPreConsumedQuota(ctx, preConsumedQuota, meta.TokenId)
|
2024-01-14 11:21:03 +00:00
|
|
|
return util.RelayErrorHandler(resp)
|
2023-08-20 14:07:50 +00:00
|
|
|
}
|
2023-08-19 09:58:45 +00:00
|
|
|
}
|
2024-01-28 11:13:11 +00:00
|
|
|
// do response
|
|
|
|
usage, respErr := DoResponse(c, textRequest, resp, meta.Mode, meta.APIType, isStream, promptTokens)
|
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
|
|
|
}
|