Skip to content

Commit 03851d2

Browse files
allow custom voice and speech models (#691)
1 parent e311859 commit 03851d2

File tree

2 files changed

+0
-48
lines changed

2 files changed

+0
-48
lines changed

speech.go

-31
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package openai
22

33
import (
44
"context"
5-
"errors"
65
"net/http"
76
)
87

@@ -36,11 +35,6 @@ const (
3635
SpeechResponseFormatPcm SpeechResponseFormat = "pcm"
3736
)
3837

39-
var (
40-
ErrInvalidSpeechModel = errors.New("invalid speech model")
41-
ErrInvalidVoice = errors.New("invalid voice")
42-
)
43-
4438
type CreateSpeechRequest struct {
4539
Model SpeechModel `json:"model"`
4640
Input string `json:"input"`
@@ -49,32 +43,7 @@ type CreateSpeechRequest struct {
4943
Speed float64 `json:"speed,omitempty"` // Optional, default to 1.0
5044
}
5145

52-
func contains[T comparable](s []T, e T) bool {
53-
for _, v := range s {
54-
if v == e {
55-
return true
56-
}
57-
}
58-
return false
59-
}
60-
61-
func isValidSpeechModel(model SpeechModel) bool {
62-
return contains([]SpeechModel{TTSModel1, TTSModel1HD, TTSModelCanary}, model)
63-
}
64-
65-
func isValidVoice(voice SpeechVoice) bool {
66-
return contains([]SpeechVoice{VoiceAlloy, VoiceEcho, VoiceFable, VoiceOnyx, VoiceNova, VoiceShimmer}, voice)
67-
}
68-
6946
func (c *Client) CreateSpeech(ctx context.Context, request CreateSpeechRequest) (response RawResponse, err error) {
70-
if !isValidSpeechModel(request.Model) {
71-
err = ErrInvalidSpeechModel
72-
return
73-
}
74-
if !isValidVoice(request.Voice) {
75-
err = ErrInvalidVoice
76-
return
77-
}
7847
req, err := c.newRequest(ctx, http.MethodPost, c.fullURL("/audio/speech", string(request.Model)),
7948
withBody(request),
8049
withContentType("application/json"),

speech_test.go

-17
Original file line numberDiff line numberDiff line change
@@ -95,21 +95,4 @@ func TestSpeechIntegration(t *testing.T) {
9595
err = os.WriteFile("test.mp3", buf, 0644)
9696
checks.NoError(t, err, "Create error")
9797
})
98-
t.Run("invalid model", func(t *testing.T) {
99-
_, err := client.CreateSpeech(context.Background(), openai.CreateSpeechRequest{
100-
Model: "invalid_model",
101-
Input: "Hello!",
102-
Voice: openai.VoiceAlloy,
103-
})
104-
checks.ErrorIs(t, err, openai.ErrInvalidSpeechModel, "CreateSpeech error")
105-
})
106-
107-
t.Run("invalid voice", func(t *testing.T) {
108-
_, err := client.CreateSpeech(context.Background(), openai.CreateSpeechRequest{
109-
Model: openai.TTSModel1,
110-
Input: "Hello!",
111-
Voice: "invalid_voice",
112-
})
113-
checks.ErrorIs(t, err, openai.ErrInvalidVoice, "CreateSpeech error")
114-
})
11598
}

0 commit comments

Comments
 (0)