ai-gateway/providers/openai/speech.go

38 lines
1016 B
Go
Raw Permalink Normal View History

2023-11-29 10:11:15 +00:00
package openai
import (
"net/http"
"one-api/common"
"one-api/types"
)
func (p *OpenAIProvider) SpeechAction(request *types.SpeechAudioRequest, isModelMapped bool, promptTokens int) (usage *types.Usage, errWithCode *types.OpenAIErrorWithStatusCode) {
2023-12-01 09:20:22 +00:00
requestBody, err := p.GetRequestBody(&request, isModelMapped)
2023-11-29 10:11:15 +00:00
if err != nil {
2023-12-01 19:28:18 +00:00
return nil, common.ErrorWrapper(err, "json_marshal_failed", http.StatusInternalServerError)
2023-11-29 10:11:15 +00:00
}
fullRequestURL := p.GetFullRequestURL(p.AudioSpeech, request.Model)
headers := p.GetRequestHeaders()
client := common.NewClient()
req, err := client.NewRequest(p.Context.Request.Method, fullRequestURL, common.WithBody(requestBody), common.WithHeader(headers))
if err != nil {
2023-12-01 19:28:18 +00:00
return nil, common.ErrorWrapper(err, "new_request_failed", http.StatusInternalServerError)
2023-11-29 10:11:15 +00:00
}
errWithCode = p.SendRequestRaw(req)
if errWithCode != nil {
return
}
usage = &types.Usage{
PromptTokens: promptTokens,
CompletionTokens: 0,
TotalTokens: promptTokens,
}
return
}