forked from sashabaranov/go-openai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
speech_test.go
115 lines (100 loc) · 3.06 KB
/
speech_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package openai_test
import (
"context"
"encoding/json"
"fmt"
"io"
"mime"
"net/http"
"os"
"path/filepath"
"testing"
"github.com/sashabaranov/go-openai"
"github.com/sashabaranov/go-openai/internal/test"
"github.com/sashabaranov/go-openai/internal/test/checks"
)
func TestSpeechIntegration(t *testing.T) {
client, server, teardown := setupOpenAITestServer()
defer teardown()
server.RegisterHandler("/v1/audio/speech", func(w http.ResponseWriter, r *http.Request) {
dir, cleanup := test.CreateTestDirectory(t)
path := filepath.Join(dir, "fake.mp3")
test.CreateTestFile(t, path)
defer cleanup()
// audio endpoints only accept POST requests
if r.Method != "POST" {
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
mediaType, _, err := mime.ParseMediaType(r.Header.Get("Content-Type"))
if err != nil {
http.Error(w, "failed to parse media type", http.StatusBadRequest)
return
}
if mediaType != "application/json" {
http.Error(w, "request is not json", http.StatusBadRequest)
return
}
// Parse the JSON body of the request
var params map[string]interface{}
err = json.NewDecoder(r.Body).Decode(¶ms)
if err != nil {
http.Error(w, "failed to parse request body", http.StatusBadRequest)
return
}
// Check if each required field is present in the parsed JSON object
reqParams := []string{"model", "input", "voice"}
for _, param := range reqParams {
_, ok := params[param]
if !ok {
http.Error(w, fmt.Sprintf("no %s in params", param), http.StatusBadRequest)
return
}
}
// read audio file content
audioFile, err := os.ReadFile(path)
if err != nil {
http.Error(w, "failed to read audio file", http.StatusInternalServerError)
return
}
// write audio file content to response
w.Header().Set("Content-Type", "audio/mpeg")
w.Header().Set("Transfer-Encoding", "chunked")
w.Header().Set("Connection", "keep-alive")
_, err = w.Write(audioFile)
if err != nil {
http.Error(w, "failed to write body", http.StatusInternalServerError)
return
}
})
t.Run("happy path", func(t *testing.T) {
res, err := client.CreateSpeech(context.Background(), openai.CreateSpeechRequest{
Model: openai.TTSModel1,
Input: "Hello!",
Voice: openai.VoiceAlloy,
})
checks.NoError(t, err, "CreateSpeech error")
defer res.Close()
buf, err := io.ReadAll(res)
checks.NoError(t, err, "ReadAll error")
// save buf to file as mp3
err = os.WriteFile("test.mp3", buf, 0644)
checks.NoError(t, err, "Create error")
})
t.Run("invalid model", func(t *testing.T) {
_, err := client.CreateSpeech(context.Background(), openai.CreateSpeechRequest{
Model: "invalid_model",
Input: "Hello!",
Voice: openai.VoiceAlloy,
})
checks.ErrorIs(t, err, openai.ErrInvalidSpeechModel, "CreateSpeech error")
})
t.Run("invalid voice", func(t *testing.T) {
_, err := client.CreateSpeech(context.Background(), openai.CreateSpeechRequest{
Model: openai.TTSModel1,
Input: "Hello!",
Voice: "invalid_voice",
})
checks.ErrorIs(t, err, openai.ErrInvalidVoice, "CreateSpeech error")
})
}