Skip to content

Commit 0b3a090

Browse files
committed
feat: Add support for Cartesia and OpenAI TTS vendor examples, including environment variable configurations and service region checks
1 parent 181a540 commit 0b3a090

File tree

3 files changed

+118
-13
lines changed

3 files changed

+118
-13
lines changed

examples/convoai/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,25 @@ export CONVOAI_TTS_ELEVENLABS_MODEL_ID=<Your tts elevenLabs model id>
3737
export CONVOAI_TTS_ELEVENLABS_VOICE_ID=<Your tts elevenLabs voice id>
3838
```
3939

40+
### cartesia
41+
42+
```bash
43+
export CONVOAI_TTS_CARTESIA_API_KEY=<Your tts cartesia api key>
44+
export CONVOAI_TTS_CARTESIA_MODEL_ID=<Your tts cartesia model id>
45+
export CONVOAI_TTS_CARTESIA_VOICE_MODE=<Your tts cartesia voice mode>
46+
export CONVOAI_TTS_CARTESIA_VOICE_ID=<Your tts cartesia voice id>
47+
```
48+
49+
### openai
50+
51+
```bash
52+
export CONVOAI_TTS_OPENAI_API_KEY=<Your tts openai api key>
53+
export CONVOAI_TTS_OPENAI_MODEL=<Your tts openai model>
54+
export CONVOAI_TTS_OPENAI_VOICE=<Your tts openai voice>
55+
export CONVOAI_TTS_OPENAI_INSTRUCTIONS=<Your tts openai instructions>
56+
export CONVOAI_TTS_OPENAI_SPEED=<Your tts openai speed>
57+
```
58+
4059
## Execution
4160

4261
Run the sample project with the following command:
@@ -49,5 +68,7 @@ go run main.go --ttsVendor=<ttsVendor> --serviceRegion=2
4968

5069
- `microsoft`
5170
- `elevenLabs`
71+
- `cartesia`
72+
- `openai`
5273

5374
Choose the appropriate TTS provider based on your requirements.

examples/convoai/main.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func main() {
3535
panic("BASIC_AUTH_PASSWORD is required")
3636
}
3737

38-
ttsVendor := flag.String("ttsVendor", "", "tts vendor, e.g. bytedance,microsoft,tencent,minimax,elevenLabs")
38+
ttsVendor := flag.String("ttsVendor", "", "tts vendor, e.g. bytedance,microsoft,tencent,minimax,elevenLabs,cartesia,openai")
3939
serviceRegion := flag.Int("serviceRegion", 0, "service region, e.g. 1: ChineseMainland, 2: Global")
4040
flag.Parse()
4141

@@ -54,10 +54,6 @@ func main() {
5454

5555
s.RunWithBytedanceTTS()
5656
case "microsoft":
57-
if *serviceRegion == 2 {
58-
log.Fatalln("Microsoft TTS is not supported in ChineseMainland service region")
59-
}
60-
6157
s.RunWithMicrosoftTTS()
6258
case "tencent":
6359
if *serviceRegion == 2 {
@@ -72,11 +68,23 @@ func main() {
7268

7369
s.RunWithMinimaxTTS()
7470
case "elevenLabs":
75-
if *serviceRegion == 2 {
71+
if *serviceRegion == 1 {
7672
log.Fatalln("ElevenLabs TTS is not supported in ChineseMainland service region")
7773
}
7874

7975
s.RunWithElevenLabsTTS()
76+
case "cartesia":
77+
if *serviceRegion == 1 {
78+
log.Fatalln("Cartesia TTS is not supported in ChineseMainland service region")
79+
}
80+
81+
s.RunWithCartesiaTTS()
82+
case "openai":
83+
if *serviceRegion == 1 {
84+
log.Fatalln("OpenAI TTS is not supported in ChineseMainland service region")
85+
}
86+
87+
s.RunWithOpenAITTS()
8088
default:
8189
log.Fatalln("Invalid tts vendor")
8290
}

examples/convoai/service/service.go

Lines changed: 83 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"log"
66
"os"
7+
"strconv"
78
"time"
89

910
"github.com/AgoraIO-Community/agora-rest-client-go/agora/auth"
@@ -34,7 +35,7 @@ func (s *Service) SetCredential(username string, password string) {
3435
s.credential = auth.NewBasicAuthCredential(username, password)
3536
}
3637

37-
func (s *Service) RunWithCustomTTS(ttsVendor req.TTSVendor, ttsParam req.TTSVendorParamsInterface) {
38+
func (s *Service) RunWithCustomTTS(ttsParam req.TTSVendorParamsInterface) {
3839
ctx := context.Background()
3940
config := &convoai.Config{
4041
AppID: s.appId,
@@ -114,7 +115,7 @@ func (s *Service) RunWithCustomTTS(ttsVendor req.TTSVendor, ttsParam req.TTSVend
114115
GreetingMessage: "Hello,how can I help you?",
115116
},
116117
TTS: &req.JoinPropertiesTTSBody{
117-
Vendor: ttsVendor,
118+
Vendor: ttsParam.GetVendorType(),
118119
Params: ttsParam,
119120
},
120121
Vad: &req.JoinPropertiesVadBody{
@@ -320,7 +321,7 @@ func (s *Service) RunWithBytedanceTTS() {
320321
PitchRatio: 1.0,
321322
Emotion: "happy",
322323
}
323-
s.RunWithCustomTTS(req.BytedanceTTSVendor, ttsParam)
324+
s.RunWithCustomTTS(ttsParam)
324325
}
325326

326327
func (s *Service) RunWithTencentTTS() {
@@ -349,7 +350,7 @@ func (s *Service) RunWithTencentTTS() {
349350
EmotionCategory: "happy",
350351
EmotionIntensity: 100,
351352
}
352-
s.RunWithCustomTTS(req.TencentTTSVendor, ttsParam)
353+
s.RunWithCustomTTS(ttsParam)
353354
}
354355

355356
func (s *Service) RunWithMinimaxTTS() {
@@ -384,7 +385,7 @@ func (s *Service) RunWithMinimaxTTS() {
384385
},
385386
}
386387

387-
s.RunWithCustomTTS(req.MinimaxTTSVendor, ttsParam)
388+
s.RunWithCustomTTS(ttsParam)
388389
}
389390

390391
func (s *Service) RunWithMicrosoftTTS() {
@@ -412,7 +413,7 @@ func (s *Service) RunWithMicrosoftTTS() {
412413
SampleRate: 24000,
413414
}
414415

415-
s.RunWithCustomTTS(req.MicrosoftTTSVendor, ttsParam)
416+
s.RunWithCustomTTS(ttsParam)
416417
}
417418

418419
func (s *Service) RunWithElevenLabsTTS() {
@@ -437,5 +438,80 @@ func (s *Service) RunWithElevenLabsTTS() {
437438
VoiceId: ttsVoiceId,
438439
}
439440

440-
s.RunWithCustomTTS(req.ElevenLabsTTSVendor, ttsParam)
441+
s.RunWithCustomTTS(ttsParam)
442+
}
443+
444+
func (s *Service) RunWithCartesiaTTS() {
445+
ttsApiKey := os.Getenv("CONVOAI_TTS_CARTESIA_API_KEY")
446+
if ttsApiKey == "" {
447+
log.Fatalln("CONVOAI_TTS_CARTESIA_API_KEY is required")
448+
}
449+
450+
ttsModelId := os.Getenv("CONVOAI_TTS_CARTESIA_MODEL_ID")
451+
if ttsModelId == "" {
452+
log.Fatalln("CONVOAI_TTS_CARTESIA_MODEL_ID is required")
453+
}
454+
455+
ttsVoiceMode := os.Getenv("CONVOAI_TTS_CARTESIA_VOICE_MODE")
456+
if ttsVoiceMode == "" {
457+
log.Fatalln("CONVOAI_TTS_CARTESIA_VOICE_MODE is required")
458+
}
459+
460+
ttsVoiceId := os.Getenv("CONVOAI_TTS_CARTESIA_VOICE_ID")
461+
if ttsVoiceId == "" {
462+
log.Fatalln("CONVOAI_TTS_CARTESIA_VOICE_ID is required")
463+
}
464+
465+
ttsParam := req.TTSCartesiaVendorParams{
466+
APIKey: ttsApiKey,
467+
ModelId: ttsModelId,
468+
Voice: &req.TTSCartesiaVendorVoice{
469+
Mode: ttsVoiceMode,
470+
Id: ttsVoiceId,
471+
},
472+
}
473+
474+
s.RunWithCustomTTS(ttsParam)
475+
}
476+
477+
func (s *Service) RunWithOpenAITTS() {
478+
ttsApiKey := os.Getenv("CONVOAI_TTS_OPENAI_API_KEY")
479+
if ttsApiKey == "" {
480+
log.Fatalln("CONVOAI_TTS_OPENAI_API_KEY is required")
481+
}
482+
483+
ttsModel := os.Getenv("CONVOAI_TTS_OPENAI_MODEL")
484+
if ttsModel == "" {
485+
log.Fatalln("CONVOAI_TTS_OPENAI_MODEL is required")
486+
}
487+
488+
ttsVoice := os.Getenv("CONVOAI_TTS_OPENAI_VOICE")
489+
if ttsVoice == "" {
490+
log.Fatalln("CONVOAI_TTS_OPENAI_VOICE is required")
491+
}
492+
493+
ttsInstructions := os.Getenv("CONVOAI_TTS_OPENAI_INSTRUCTIONS")
494+
if ttsInstructions == "" {
495+
log.Fatalln("CONVOAI_TTS_OPENAI_INSTRUCTIONS is required")
496+
}
497+
498+
ttsSpeed := os.Getenv("CONVOAI_TTS_OPENAI_SPEED")
499+
if ttsSpeed == "" {
500+
log.Fatalln("CONVOAI_TTS_OPENAI_SPEED is required")
501+
}
502+
503+
speed, err := strconv.ParseFloat(ttsSpeed, 32)
504+
if err != nil {
505+
log.Fatalln("CONVOAI_TTS_OPENAI_SPEED is not a valid float")
506+
}
507+
508+
ttsParam := req.TTSOpenAIVendorParams{
509+
APIKey: ttsApiKey,
510+
Model: ttsModel,
511+
Voice: ttsVoice,
512+
Instructions: ttsInstructions,
513+
Speed: float32(speed),
514+
}
515+
516+
s.RunWithCustomTTS(ttsParam)
441517
}

0 commit comments

Comments
 (0)