forked from sashabaranov/go-openai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio.go
118 lines (101 loc) · 3.08 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
package openai
import (
"bytes"
"context"
"fmt"
"net/http"
"os"
)
// Whisper Defines the models provided by OpenAI to use when processing audio with OpenAI.
const (
Whisper1 = "whisper-1"
)
// AudioRequest represents a request structure for audio API.
// ResponseFormat is not supported for now. We only return JSON text, which may be sufficient.
type AudioRequest struct {
Model string
FilePath string
Prompt string // For translation, it should be in English
Temperature float32
Language string // For translation, just do not use it. It seems "en" works, not confirmed...
}
// AudioResponse represents a response structure for audio API.
type AudioResponse struct {
Text string `json:"text"`
}
// CreateTranscription — API call to create a transcription. Returns transcribed text.
func (c *Client) CreateTranscription(
ctx context.Context,
request AudioRequest,
) (response AudioResponse, err error) {
response, err = c.callAudioAPI(ctx, request, "transcriptions")
return
}
// CreateTranslation — API call to translate audio into English.
func (c *Client) CreateTranslation(
ctx context.Context,
request AudioRequest,
) (response AudioResponse, err error) {
response, err = c.callAudioAPI(ctx, request, "translations")
return
}
// callAudioAPI — API call to an audio endpoint.
func (c *Client) callAudioAPI(
ctx context.Context,
request AudioRequest,
endpointSuffix string,
) (response AudioResponse, err error) {
var formBody bytes.Buffer
builder := c.createFormBuilder(&formBody)
if err = audioMultipartForm(request, builder); err != nil {
return
}
urlSuffix := fmt.Sprintf("/audio/%s", endpointSuffix)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.fullURL(urlSuffix), &formBody)
if err != nil {
return
}
req.Header.Add("Content-Type", builder.formDataContentType())
err = c.sendRequest(req, &response)
return
}
// audioMultipartForm creates a form with audio file contents and the name of the model to use for
// audio processing.
func audioMultipartForm(request AudioRequest, b formBuilder) error {
f, err := os.Open(request.FilePath)
if err != nil {
return fmt.Errorf("opening audio file: %w", err)
}
defer f.Close()
err = b.createFormFile("file", f)
if err != nil {
return fmt.Errorf("creating form file: %w", err)
}
err = b.writeField("model", request.Model)
if err != nil {
return fmt.Errorf("writing model name: %w", err)
}
// Create a form field for the prompt (if provided)
if request.Prompt != "" {
err = b.writeField("prompt", request.Prompt)
if err != nil {
return fmt.Errorf("writing prompt: %w", err)
}
}
// Create a form field for the temperature (if provided)
if request.Temperature != 0 {
err = b.writeField("temperature", fmt.Sprintf("%.2f", request.Temperature))
if err != nil {
return fmt.Errorf("writing temperature: %w", err)
}
}
// Create a form field for the language (if provided)
if request.Language != "" {
err = b.writeField("language", request.Language)
if err != nil {
return fmt.Errorf("writing language: %w", err)
}
}
// Close the multipart writer
return b.close()
}