mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-11-18 06:03:42 +08:00
✨ 添加azure speech支持
This commit is contained in:
88
providers/azureSpeech/speech.go
Normal file
88
providers/azureSpeech/speech.go
Normal file
@@ -0,0 +1,88 @@
|
||||
package azureSpeech
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"one-api/common"
|
||||
"one-api/types"
|
||||
)
|
||||
|
||||
var outputFormatMap = map[string]string{
|
||||
"mp3": "audio-16khz-128kbitrate-mono-mp3",
|
||||
"opus": "audio-16khz-128kbitrate-mono-opus",
|
||||
"aac": "audio-24khz-160kbitrate-mono-mp3",
|
||||
"flac": "audio-48khz-192kbitrate-mono-mp3",
|
||||
}
|
||||
|
||||
func CreateSSML(text string, name string, role string) string {
|
||||
ssmlTemplate := `<speak version='1.0' xml:lang='en-US'>
|
||||
<voice xml:lang='en-US' %s name='%s'>
|
||||
%s
|
||||
</voice>
|
||||
</speak>`
|
||||
|
||||
roleAttribute := ""
|
||||
if role != "" {
|
||||
roleAttribute = fmt.Sprintf("role='%s'", role)
|
||||
}
|
||||
|
||||
return fmt.Sprintf(ssmlTemplate, roleAttribute, name, text)
|
||||
}
|
||||
|
||||
func (p *AzureSpeechProvider) getRequestBody(request *types.SpeechAudioRequest) *bytes.Buffer {
|
||||
voiceMap := map[string][]string{
|
||||
"alloy": {"zh-CN-YunxiNeural"},
|
||||
"echo": {"zh-CN-YunyangNeural"},
|
||||
"fable": {"zh-CN-YunxiNeural", "Boy"},
|
||||
"onyx": {"zh-CN-YunyeNeural"},
|
||||
"nova": {"zh-CN-XiaochenNeural"},
|
||||
"shimmer": {"zh-CN-XiaohanNeural"},
|
||||
}
|
||||
|
||||
voice := ""
|
||||
role := ""
|
||||
if voiceMap[request.Voice] != nil {
|
||||
voice = voiceMap[request.Voice][0]
|
||||
if len(voiceMap[request.Voice]) > 1 {
|
||||
role = voiceMap[request.Voice][1]
|
||||
}
|
||||
}
|
||||
|
||||
ssml := CreateSSML(request.Input, voice, role)
|
||||
|
||||
return bytes.NewBufferString(ssml)
|
||||
|
||||
}
|
||||
|
||||
func (p *AzureSpeechProvider) SpeechAction(request *types.SpeechAudioRequest, isModelMapped bool, promptTokens int) (usage *types.Usage, errWithCode *types.OpenAIErrorWithStatusCode) {
|
||||
|
||||
fullRequestURL := p.GetFullRequestURL(p.AudioSpeech, request.Model)
|
||||
headers := p.GetRequestHeaders()
|
||||
responseFormatr := outputFormatMap[request.ResponseFormat]
|
||||
if responseFormatr == "" {
|
||||
responseFormatr = outputFormatMap["mp3"]
|
||||
}
|
||||
headers["X-Microsoft-OutputFormat"] = responseFormatr
|
||||
|
||||
requestBody := p.getRequestBody(request)
|
||||
|
||||
client := common.NewClient()
|
||||
req, err := client.NewRequest(p.Context.Request.Method, fullRequestURL, common.WithBody(requestBody), common.WithHeader(headers))
|
||||
if err != nil {
|
||||
return nil, common.ErrorWrapper(err, "new_request_failed", http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
errWithCode = p.SendRequestRaw(req)
|
||||
if errWithCode != nil {
|
||||
return
|
||||
}
|
||||
|
||||
usage = &types.Usage{
|
||||
PromptTokens: promptTokens,
|
||||
CompletionTokens: 0,
|
||||
TotalTokens: promptTokens,
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user