- Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathaudio.go
234 lines (199 loc) · 6.31 KB
/
audio.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package openai
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"os"
utils "github.com/sashabaranov/go-openai/internal"
)
// Whisper Defines the models provided by OpenAI to use when processing audio with OpenAI.
const (
Whisper1="whisper-1"
)
// Response formats; Whisper uses AudioResponseFormatJSON by default.
typeAudioResponseFormatstring
const (
AudioResponseFormatJSONAudioResponseFormat="json"
AudioResponseFormatTextAudioResponseFormat="text"
AudioResponseFormatSRTAudioResponseFormat="srt"
AudioResponseFormatVerboseJSONAudioResponseFormat="verbose_json"
AudioResponseFormatVTTAudioResponseFormat="vtt"
)
typeTranscriptionTimestampGranularitystring
const (
TranscriptionTimestampGranularityWordTranscriptionTimestampGranularity="word"
TranscriptionTimestampGranularitySegmentTranscriptionTimestampGranularity="segment"
)
// AudioRequest represents a request structure for audio API.
typeAudioRequeststruct {
Modelstring
// FilePath is either an existing file in your filesystem or a filename representing the contents of Reader.
FilePathstring
// Reader is an optional io.Reader when you do not want to use an existing file.
Reader io.Reader
Promptstring
Temperaturefloat32
Languagestring// Only for transcription.
FormatAudioResponseFormat
TimestampGranularities []TranscriptionTimestampGranularity// Only for transcription.
}
// AudioResponse represents a response structure for audio API.
typeAudioResponsestruct {
Taskstring`json:"task"`
Languagestring`json:"language"`
Durationfloat64`json:"duration"`
Segments []struct {
IDint`json:"id"`
Seekint`json:"seek"`
Startfloat64`json:"start"`
Endfloat64`json:"end"`
Textstring`json:"text"`
Tokens []int`json:"tokens"`
Temperaturefloat64`json:"temperature"`
AvgLogprobfloat64`json:"avg_logprob"`
CompressionRatiofloat64`json:"compression_ratio"`
NoSpeechProbfloat64`json:"no_speech_prob"`
Transientbool`json:"transient"`
} `json:"segments"`
Words []struct {
Wordstring`json:"word"`
Startfloat64`json:"start"`
Endfloat64`json:"end"`
} `json:"words"`
Textstring`json:"text"`
httpHeader
}
typeaudioTextResponsestruct {
Textstring`json:"text"`
httpHeader
}
func (r*audioTextResponse) ToAudioResponse() AudioResponse {
returnAudioResponse{
Text: r.Text,
httpHeader: r.httpHeader,
}
}
// CreateTranscription — API call to create a transcription. Returns transcribed text.
func (c*Client) CreateTranscription(
ctx context.Context,
requestAudioRequest,
) (responseAudioResponse, errerror) {
returnc.callAudioAPI(ctx, request, "transcriptions")
}
// CreateTranslation — API call to translate audio into English.
func (c*Client) CreateTranslation(
ctx context.Context,
requestAudioRequest,
) (responseAudioResponse, errerror) {
returnc.callAudioAPI(ctx, request, "translations")
}
// callAudioAPI — API call to an audio endpoint.
func (c*Client) callAudioAPI(
ctx context.Context,
requestAudioRequest,
endpointSuffixstring,
) (responseAudioResponse, errerror) {
varformBody bytes.Buffer
builder:=c.createFormBuilder(&formBody)
iferr=audioMultipartForm(request, builder); err!=nil {
returnAudioResponse{}, err
}
urlSuffix:=fmt.Sprintf("/audio/%s", endpointSuffix)
req, err:=c.newRequest(
ctx,
http.MethodPost,
c.fullURL(urlSuffix, withModel(request.Model)),
withBody(&formBody),
withContentType(builder.FormDataContentType()),
)
iferr!=nil {
returnAudioResponse{}, err
}
ifrequest.HasJSONResponse() {
err=c.sendRequest(req, &response)
} else {
vartextResponseaudioTextResponse
err=c.sendRequest(req, &textResponse)
response=textResponse.ToAudioResponse()
}
iferr!=nil {
returnAudioResponse{}, err
}
return
}
// HasJSONResponse returns true if the response format is JSON.
func (rAudioRequest) HasJSONResponse() bool {
returnr.Format==""||r.Format==AudioResponseFormatJSON||r.Format==AudioResponseFormatVerboseJSON
}
// audioMultipartForm creates a form with audio file contents and the name of the model to use for
// audio processing.
funcaudioMultipartForm(requestAudioRequest, b utils.FormBuilder) error {
err:=createFileField(request, b)
iferr!=nil {
returnerr
}
err=b.WriteField("model", request.Model)
iferr!=nil {
returnfmt.Errorf("writing model name: %w", err)
}
// Create a form field for the prompt (if provided)
ifrequest.Prompt!="" {
err=b.WriteField("prompt", request.Prompt)
iferr!=nil {
returnfmt.Errorf("writing prompt: %w", err)
}
}
// Create a form field for the format (if provided)
ifrequest.Format!="" {
err=b.WriteField("response_format", string(request.Format))
iferr!=nil {
returnfmt.Errorf("writing format: %w", err)
}
}
// Create a form field for the temperature (if provided)
ifrequest.Temperature!=0 {
err=b.WriteField("temperature", fmt.Sprintf("%.2f", request.Temperature))
iferr!=nil {
returnfmt.Errorf("writing temperature: %w", err)
}
}
// Create a form field for the language (if provided)
ifrequest.Language!="" {
err=b.WriteField("language", request.Language)
iferr!=nil {
returnfmt.Errorf("writing language: %w", err)
}
}
iflen(request.TimestampGranularities) >0 {
for_, tg:=rangerequest.TimestampGranularities {
err=b.WriteField("timestamp_granularities[]", string(tg))
iferr!=nil {
returnfmt.Errorf("writing timestamp_granularities[]: %w", err)
}
}
}
// Close the multipart writer
returnb.Close()
}
// createFileField creates the "file" form field from either an existing file or by using the reader.
funccreateFileField(requestAudioRequest, b utils.FormBuilder) error {
ifrequest.Reader!=nil {
err:=b.CreateFormFileReader("file", request.Reader, request.FilePath)
iferr!=nil {
returnfmt.Errorf("creating form using reader: %w", err)
}
returnnil
}
f, err:=os.Open(request.FilePath)
iferr!=nil {
returnfmt.Errorf("opening audio file: %w", err)
}
deferf.Close()
err=b.CreateFormFile("file", f)
iferr!=nil {
returnfmt.Errorf("creating form file: %w", err)
}
returnnil
}