-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathaudio.go
159 lines (148 loc) · 4.62 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
package openai
import (
"bytes"
"context"
"fmt"
"io"
"mime/multipart"
)
type AudioOptions struct {
// The audio file to process, in one of these formats:
// mp3, mp4, mpeg, mpga, m4a, wav, or webm.
File io.Reader `binding:"required"`
// The format of the audio file.
AudioFormat string `binding:"required"`
// ID of the model to use. Only whisper-1 is currently available.
Model Model `binding:"required"`
// An optional text to guide the model's style or continue a previous audio segment.
// The prompt should match the audio language for transcriptions and English for translations.
Prompt string
// The sampling temperature, between 0 and 1.
// Higher values like 0.8 will make the output more random, while lower values
// like 0.2 will make it more focused and deterministic.
// If set to 0, the model will use log probability to automatically increase
// the temperature until certain thresholds are hit.
Temperature float32
}
type TranscribeOptions struct {
*AudioOptions
// The language of the input audio. Supplying the input language in ISO-639-1
// format will improve accuracy and latency.
Language string
}
type TranscribeResponse struct {
Text string `json:"text"`
}
// Transcribe audio into the input language.
//
// Docs: https://platform.openai.com/docs/api-reference/audio/create
func (e *Engine) Transcribe(ctx context.Context, opts *TranscribeOptions) (*TranscribeResponse, error) {
if err := e.validate.StructCtx(ctx, opts); err != nil {
return nil, err
}
url := e.apiBaseURL + "/audio/transcriptions"
body, contentType, err := newTranscribeBody(opts)
if err != nil {
return nil, err
}
req, err := e.newReq(ctx, "POST", url, contentType, body)
if err != nil {
return nil, err
}
resp, err := e.doReq(req)
if err != nil {
return nil, err
}
var jsonResp TranscribeResponse
if err := unmarshal(resp, &jsonResp); err != nil {
return nil, err
}
return &jsonResp, nil
}
func newTranscribeBody(opts *TranscribeOptions) (io.Reader, string, error) {
body := &bytes.Buffer{}
writer, err := newAudioMultipartWriter(body, opts.AudioOptions)
if err != nil {
return nil, "", err
}
if opts.Language != "" {
if err := writer.WriteField("language", opts.Language); err != nil {
return nil, "", fmt.Errorf("write language: %w", err)
}
}
if err := writer.Close(); err != nil {
return nil, "", fmt.Errorf("close writer: %w", err)
}
return body, writer.FormDataContentType(), nil
}
type TranslateOptions struct {
*AudioOptions
}
type TranslateResponse struct {
Text string `json:"text"`
}
// Translate audio into English.
//
// Docs: https://platform.openai.com/docs/api-reference/audio/create
func (e *Engine) Translate(ctx context.Context, opts *TranslateOptions) (*TranslateResponse, error) {
if err := e.validate.StructCtx(ctx, opts); err != nil {
return nil, err
}
url := e.apiBaseURL + "/audio/translations"
body, contentType, err := newTranslateBody(opts)
if err != nil {
return nil, err
}
req, err := e.newReq(ctx, "POST", url, contentType, body)
if err != nil {
return nil, err
}
resp, err := e.doReq(req)
if err != nil {
return nil, err
}
var jsonResp TranslateResponse
if err := unmarshal(resp, &jsonResp); err != nil {
return nil, err
}
return &jsonResp, nil
}
func newTranslateBody(opts *TranslateOptions) (io.Reader, string, error) {
body := &bytes.Buffer{}
writer, err := newAudioMultipartWriter(body, opts.AudioOptions)
if err != nil {
return nil, "", err
}
if err := writer.Close(); err != nil {
return nil, "", fmt.Errorf("close writer: %w", err)
}
return body, writer.FormDataContentType(), nil
}
func newAudioMultipartWriter(body *bytes.Buffer, opts *AudioOptions) (*multipart.Writer, error) {
writer := multipart.NewWriter(body)
if err := writer.WriteField("model", string(opts.Model)); err != nil {
return nil, fmt.Errorf("write model: %w", err)
}
// TODO: support additional response data via verbose_json? what about other formats (vtt, srt)?g
if err := writer.WriteField("response_format", "json"); err != nil {
return nil, fmt.Errorf("write response format: %w", err)
}
file, err := writer.CreateFormFile("file", "file."+opts.AudioFormat)
if err != nil {
return nil, fmt.Errorf("create form file: %w", err)
}
if _, err := io.Copy(file, opts.File); err != nil {
return nil, fmt.Errorf("copy file: %w", err)
}
if opts.Prompt != "" {
if err := writer.WriteField("prompt", opts.Prompt); err != nil {
return nil, fmt.Errorf("write prompt: %w", err)
}
}
if opts.Temperature != 0 {
if err := writer.WriteField("temperature", fmt.Sprintf("%f", opts.Temperature)); err != nil {
return nil, fmt.Errorf("write temperature: %w", err)
}
}
return writer, nil
}