Browse Source

feat: add doubao tts usage token

feitianbubu 4 months ago
parent
commit
735386c0b9
2 changed files with 3 additions and 6 deletions
  1. 1 1
      relay/channel/volcengine/adaptor.go
  2. 2 5
      relay/channel/volcengine/tts.go

+ 1 - 1
relay/channel/volcengine/adaptor.go

@@ -319,7 +319,7 @@ func (a *Adaptor) DoRequest(c *gin.Context, info *relaycommon.RelayInfo, request
 func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (usage any, err *types.NewAPIError) {
 	if info.RelayMode == constant.RelayModeAudioSpeech {
 		encoding := mapEncoding(c.GetString("response_format"))
-		return handleTTSResponse(c, resp, encoding)
+		return handleTTSResponse(c, resp, info, encoding)
 	}
 
 	adaptor := openai.Adaptor{}

+ 2 - 5
relay/channel/volcengine/tts.go

@@ -119,6 +119,7 @@ func mapVoiceType(openAIVoice string) string {
 	return openAIVoice
 }
 
+// [0.1,2],默认为 1,通常保留一位小数即可
 func mapSpeedRatio(speed float64) float64 {
 	if speed == 0 {
 		return 1.0
@@ -133,9 +134,6 @@ func mapSpeedRatio(speed float64) float64 {
 }
 
 func mapEncoding(responseFormat string) string {
-	if responseFormat == "" {
-		return "mp3"
-	}
 	if encoding, ok := responseFormatToEncodingMap[responseFormat]; ok {
 		return encoding
 	}
@@ -155,7 +153,7 @@ func getContentTypeByEncoding(encoding string) string {
 	return "application/octet-stream"
 }
 
-func handleTTSResponse(c *gin.Context, resp *http.Response, encoding string) (usage any, err *types.NewAPIError) {
+func handleTTSResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo, encoding string) (usage any, err *types.NewAPIError) {
 	body, readErr := io.ReadAll(resp.Body)
 	if readErr != nil {
 		return nil, types.NewErrorWithStatusCode(
@@ -196,7 +194,6 @@ func handleTTSResponse(c *gin.Context, resp *http.Response, encoding string) (us
 	c.Header("Content-Type", contentType)
 	c.Data(http.StatusOK, contentType, audioData)
 
-	info := c.MustGet("relay_info").(*relaycommon.RelayInfo)
 	usage = &dto.Usage{
 		PromptTokens:     info.PromptTokens,
 		CompletionTokens: 0,