-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio.go
100 lines (81 loc) · 2.32 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
package echoopenai
import (
"bytes"
"context"
"fmt"
"net/http"
"os"
)
type AudioFormat string
const (
AudioJSONFormat AudioFormat = "json"
AudioTextFormat AudioFormat = "text"
AudioSRTFormat AudioFormat = "srt"
AudioVerboseJSONFormat AudioFormat = "verbose_json"
AudioVTTFormat AudioFormat = "vtt"
)
type AudioTranslationRequest struct {
File *os.File `json:"file"`
Model OpenAIModel `json:"model"`
Prompt string `json:"prompt"`
ResponseFormat AudioFormat `json:"response_format"`
Temperature float32 `json:"temperature"`
Language string `json:"language"`
}
type AudiTranslationResponse struct {
Text string
}
func (c *Client) CreateAudioTranscription(request AudioTranslationRequest) (response AudiTranslationResponse, err error) {
return c.CreateAudioTranscriptionWithContext(context.Background(), request)
}
func (c *Client) CreateAudioTranscriptionWithContext(ctx context.Context, request AudioTranslationRequest) (response AudiTranslationResponse, err error) {
urlSuffix := "audio/translations"
body := &bytes.Buffer{}
builder := c.createFormBuilder(body)
err = builder.CreateFormFile("file", request.File)
if err != nil {
return
}
err = builder.WriteField("model", string(request.Model))
if err != nil {
return
}
err = builder.WriteField("prompt", request.Prompt)
if err != nil {
return
}
err = builder.WriteField("response_format", string(request.ResponseFormat))
if err != nil {
return
}
err = builder.WriteField("temperature", fmt.Sprintf("%.2f", request.Temperature))
if err != nil {
return
}
if len(request.Language) != 0 {
urlSuffix = "audio/transcriptions"
err = builder.WriteField("language", request.Language)
if err != nil {
return
}
}
err = builder.Close()
if err != nil {
return
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.getFullURL(urlSuffix), body)
if err != nil {
return
}
req.Header.Set("Content-Type", builder.FormDataContentType())
c.setCommonHeader(req)
if request.HasJSONResponse() {
err = c.sendRequestWithContext(ctx, req, &response)
} else {
err = c.sendRequestWithContext(ctx, req, &response.Text)
}
return
}
func (r AudioTranslationRequest) HasJSONResponse() bool {
return r.ResponseFormat == "" || r.ResponseFormat == AudioJSONFormat
}