2023-08-27 07:28:23 +00:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2023-09-17 07:39:46 +00:00
|
|
|
"context"
|
2023-08-27 07:28:23 +00:00
|
|
|
"encoding/json"
|
2023-10-01 04:49:40 +00:00
|
|
|
"errors"
|
2023-11-24 13:10:18 +00:00
|
|
|
"fmt"
|
2023-10-22 09:50:52 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
2023-08-27 07:28:23 +00:00
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"one-api/common"
|
|
|
|
"one-api/model"
|
2023-11-24 13:10:18 +00:00
|
|
|
"strings"
|
2023-08-27 07:28:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func relayAudioHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
|
|
|
|
audioModel := "whisper-1"
|
|
|
|
|
|
|
|
tokenId := c.GetInt("token_id")
|
|
|
|
channelType := c.GetInt("channel")
|
2023-09-17 11:18:16 +00:00
|
|
|
channelId := c.GetInt("channel_id")
|
2023-08-27 07:28:23 +00:00
|
|
|
userId := c.GetInt("id")
|
|
|
|
group := c.GetString("group")
|
2023-11-17 13:18:51 +00:00
|
|
|
tokenName := c.GetString("token_name")
|
|
|
|
|
|
|
|
var ttsRequest TextToSpeechRequest
|
|
|
|
if relayMode == RelayModeAudioSpeech {
|
|
|
|
// Read JSON
|
|
|
|
err := common.UnmarshalBodyReusable(c, &ttsRequest)
|
|
|
|
// Check if JSON is valid
|
|
|
|
if err != nil {
|
|
|
|
return errorWrapper(err, "invalid_json", http.StatusBadRequest)
|
|
|
|
}
|
|
|
|
audioModel = ttsRequest.Model
|
|
|
|
// Check if text is too long 4096
|
|
|
|
if len(ttsRequest.Input) > 4096 {
|
|
|
|
return errorWrapper(errors.New("input is too long (over 4096 characters)"), "text_too_long", http.StatusBadRequest)
|
|
|
|
}
|
|
|
|
}
|
2023-08-27 07:28:23 +00:00
|
|
|
|
|
|
|
modelRatio := common.GetModelRatio(audioModel)
|
|
|
|
groupRatio := common.GetGroupRatio(group)
|
|
|
|
ratio := modelRatio * groupRatio
|
2023-11-26 04:05:16 +00:00
|
|
|
var quota int
|
|
|
|
var preConsumedQuota int
|
|
|
|
switch relayMode {
|
|
|
|
case RelayModeAudioSpeech:
|
|
|
|
preConsumedQuota = int(float64(len(ttsRequest.Input)) * ratio)
|
|
|
|
quota = preConsumedQuota
|
|
|
|
default:
|
|
|
|
preConsumedQuota = int(float64(common.PreConsumedQuota) * ratio)
|
|
|
|
}
|
2023-08-27 07:28:23 +00:00
|
|
|
userQuota, err := model.CacheGetUserQuota(userId)
|
|
|
|
if err != nil {
|
|
|
|
return errorWrapper(err, "get_user_quota_failed", http.StatusInternalServerError)
|
|
|
|
}
|
2023-11-17 13:18:51 +00:00
|
|
|
|
|
|
|
// Check if user quota is enough
|
2023-11-26 04:05:16 +00:00
|
|
|
if userQuota-preConsumedQuota < 0 {
|
|
|
|
return errorWrapper(errors.New("user quota is not enough"), "insufficient_user_quota", http.StatusForbidden)
|
|
|
|
}
|
|
|
|
err = model.CacheDecreaseUserQuota(userId, preConsumedQuota)
|
|
|
|
if err != nil {
|
|
|
|
return errorWrapper(err, "decrease_user_quota_failed", http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
if userQuota > 100*preConsumedQuota {
|
|
|
|
// in this case, we do not pre-consume quota
|
|
|
|
// because the user has enough quota
|
|
|
|
preConsumedQuota = 0
|
|
|
|
}
|
|
|
|
if preConsumedQuota > 0 {
|
|
|
|
err := model.PreConsumeTokenQuota(tokenId, preConsumedQuota)
|
2023-08-27 07:28:23 +00:00
|
|
|
if err != nil {
|
2023-11-26 04:05:16 +00:00
|
|
|
return errorWrapper(err, "pre_consume_token_quota_failed", http.StatusForbidden)
|
2023-08-27 07:28:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// map model name
|
|
|
|
modelMapping := c.GetString("model_mapping")
|
|
|
|
if modelMapping != "" {
|
|
|
|
modelMap := make(map[string]string)
|
|
|
|
err := json.Unmarshal([]byte(modelMapping), &modelMap)
|
|
|
|
if err != nil {
|
|
|
|
return errorWrapper(err, "unmarshal_model_mapping_failed", http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
if modelMap[audioModel] != "" {
|
|
|
|
audioModel = modelMap[audioModel]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
baseURL := common.ChannelBaseURLs[channelType]
|
|
|
|
requestURL := c.Request.URL.String()
|
|
|
|
if c.GetString("base_url") != "" {
|
|
|
|
baseURL = c.GetString("base_url")
|
|
|
|
}
|
|
|
|
|
2023-10-22 09:50:52 +00:00
|
|
|
fullRequestURL := getFullRequestURL(baseURL, requestURL, channelType)
|
2023-11-24 13:10:18 +00:00
|
|
|
if relayMode == RelayModeAudioTranscription && channelType == common.ChannelTypeAzure {
|
|
|
|
// https://learn.microsoft.com/en-us/azure/ai-services/openai/whisper-quickstart?tabs=command-line#rest-api
|
2023-12-03 09:34:59 +00:00
|
|
|
apiVersion := GetAPIVersion(c)
|
2023-11-24 13:10:18 +00:00
|
|
|
fullRequestURL = fmt.Sprintf("%s/openai/deployments/%s/audio/transcriptions?api-version=%s", baseURL, audioModel, apiVersion)
|
|
|
|
}
|
|
|
|
|
2023-08-27 07:28:23 +00:00
|
|
|
requestBody := c.Request.Body
|
|
|
|
|
|
|
|
req, err := http.NewRequest(c.Request.Method, fullRequestURL, requestBody)
|
|
|
|
if err != nil {
|
|
|
|
return errorWrapper(err, "new_request_failed", http.StatusInternalServerError)
|
|
|
|
}
|
2023-11-24 13:10:18 +00:00
|
|
|
|
|
|
|
if relayMode == RelayModeAudioTranscription && channelType == common.ChannelTypeAzure {
|
|
|
|
// https://learn.microsoft.com/en-us/azure/ai-services/openai/whisper-quickstart?tabs=command-line#rest-api
|
|
|
|
apiKey := c.Request.Header.Get("Authorization")
|
|
|
|
apiKey = strings.TrimPrefix(apiKey, "Bearer ")
|
|
|
|
req.Header.Set("api-key", apiKey)
|
|
|
|
req.ContentLength = c.Request.ContentLength
|
|
|
|
} else {
|
|
|
|
req.Header.Set("Authorization", c.Request.Header.Get("Authorization"))
|
|
|
|
}
|
2023-08-27 07:28:23 +00:00
|
|
|
req.Header.Set("Content-Type", c.Request.Header.Get("Content-Type"))
|
|
|
|
req.Header.Set("Accept", c.Request.Header.Get("Accept"))
|
|
|
|
|
|
|
|
resp, err := httpClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return errorWrapper(err, "do_request_failed", http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = req.Body.Close()
|
|
|
|
if err != nil {
|
|
|
|
return errorWrapper(err, "close_request_body_failed", http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
err = c.Request.Body.Close()
|
|
|
|
if err != nil {
|
|
|
|
return errorWrapper(err, "close_request_body_failed", http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
|
2023-11-26 04:05:16 +00:00
|
|
|
if relayMode != RelayModeAudioSpeech {
|
2023-11-17 13:18:51 +00:00
|
|
|
responseBody, err := io.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return errorWrapper(err, "read_response_body_failed", http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
err = resp.Body.Close()
|
|
|
|
if err != nil {
|
|
|
|
return errorWrapper(err, "close_response_body_failed", http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
var whisperResponse WhisperResponse
|
|
|
|
err = json.Unmarshal(responseBody, &whisperResponse)
|
|
|
|
if err != nil {
|
|
|
|
return errorWrapper(err, "unmarshal_response_body_failed", http.StatusInternalServerError)
|
|
|
|
}
|
2023-11-26 04:05:16 +00:00
|
|
|
quota = countTokenText(whisperResponse.Text, audioModel)
|
2023-11-17 13:18:51 +00:00
|
|
|
resp.Body = io.NopCloser(bytes.NewBuffer(responseBody))
|
2023-08-27 07:28:23 +00:00
|
|
|
}
|
2023-11-26 04:05:16 +00:00
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
if preConsumedQuota > 0 {
|
|
|
|
// we need to roll back the pre-consumed quota
|
|
|
|
defer func(ctx context.Context) {
|
|
|
|
go func() {
|
|
|
|
// negative means add quota back for token & user
|
|
|
|
err := model.PostConsumeTokenQuota(tokenId, -preConsumedQuota)
|
|
|
|
if err != nil {
|
|
|
|
common.LogError(ctx, fmt.Sprintf("error rollback pre-consumed quota: %s", err.Error()))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}(c.Request.Context())
|
|
|
|
}
|
|
|
|
return relayErrorHandler(resp)
|
|
|
|
}
|
|
|
|
quotaDelta := quota - preConsumedQuota
|
|
|
|
defer func(ctx context.Context) {
|
|
|
|
go postConsumeQuota(ctx, tokenId, quotaDelta, quota, userId, channelId, modelRatio, groupRatio, audioModel, tokenName)
|
|
|
|
}(c.Request.Context())
|
|
|
|
|
2023-08-27 07:28:23 +00:00
|
|
|
for k, v := range resp.Header {
|
|
|
|
c.Writer.Header().Set(k, v[0])
|
|
|
|
}
|
|
|
|
c.Writer.WriteHeader(resp.StatusCode)
|
|
|
|
|
|
|
|
_, err = io.Copy(c.Writer, resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return errorWrapper(err, "copy_response_body_failed", http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
err = resp.Body.Close()
|
|
|
|
if err != nil {
|
|
|
|
return errorWrapper(err, "close_response_body_failed", http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|