1// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
3package openai
4
5import (
6 "context"
7 "net/http"
8
9 "github.com/openai/openai-go/internal/apijson"
10 "github.com/openai/openai-go/internal/requestconfig"
11 "github.com/openai/openai-go/option"
12 "github.com/openai/openai-go/packages/param"
13)
14
15// AudioSpeechService contains methods and other services that help with
16// interacting with the openai API.
17//
18// Note, unlike clients, this service does not read variables from the environment
19// automatically. You should not instantiate this service directly, and instead use
20// the [NewAudioSpeechService] method instead.
21type AudioSpeechService struct {
22 Options []option.RequestOption
23}
24
25// NewAudioSpeechService generates a new service that applies the given options to
26// each request. These options are applied after the parent client's options (if
27// there is one), and before any request-specific options.
28func NewAudioSpeechService(opts ...option.RequestOption) (r AudioSpeechService) {
29 r = AudioSpeechService{}
30 r.Options = opts
31 return
32}
33
34// Generates audio from the input text.
35func (r *AudioSpeechService) New(ctx context.Context, body AudioSpeechNewParams, opts ...option.RequestOption) (res *http.Response, err error) {
36 opts = append(r.Options[:], opts...)
37 opts = append([]option.RequestOption{option.WithHeader("Accept", "application/octet-stream")}, opts...)
38 path := "audio/speech"
39 err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...)
40 return
41}
42
43type SpeechModel = string
44
45const (
46 SpeechModelTTS1 SpeechModel = "tts-1"
47 SpeechModelTTS1HD SpeechModel = "tts-1-hd"
48 SpeechModelGPT4oMiniTTS SpeechModel = "gpt-4o-mini-tts"
49)
50
51type AudioSpeechNewParams struct {
52 // The text to generate audio for. The maximum length is 4096 characters.
53 Input string `json:"input,required"`
54 // One of the available [TTS models](https://platform.openai.com/docs/models#tts):
55 // `tts-1`, `tts-1-hd` or `gpt-4o-mini-tts`.
56 Model SpeechModel `json:"model,omitzero,required"`
57 // The voice to use when generating the audio. Supported voices are `alloy`, `ash`,
58 // `ballad`, `coral`, `echo`, `fable`, `onyx`, `nova`, `sage`, `shimmer`, and
59 // `verse`. Previews of the voices are available in the
60 // [Text to speech guide](https://platform.openai.com/docs/guides/text-to-speech#voice-options).
61 Voice AudioSpeechNewParamsVoice `json:"voice,omitzero,required"`
62 // Control the voice of your generated audio with additional instructions. Does not
63 // work with `tts-1` or `tts-1-hd`.
64 Instructions param.Opt[string] `json:"instructions,omitzero"`
65 // The speed of the generated audio. Select a value from `0.25` to `4.0`. `1.0` is
66 // the default.
67 Speed param.Opt[float64] `json:"speed,omitzero"`
68 // The format to audio in. Supported formats are `mp3`, `opus`, `aac`, `flac`,
69 // `wav`, and `pcm`.
70 //
71 // Any of "mp3", "opus", "aac", "flac", "wav", "pcm".
72 ResponseFormat AudioSpeechNewParamsResponseFormat `json:"response_format,omitzero"`
73 // The format to stream the audio in. Supported formats are `sse` and `audio`.
74 // `sse` is not supported for `tts-1` or `tts-1-hd`.
75 //
76 // Any of "sse", "audio".
77 StreamFormat AudioSpeechNewParamsStreamFormat `json:"stream_format,omitzero"`
78 paramObj
79}
80
81func (r AudioSpeechNewParams) MarshalJSON() (data []byte, err error) {
82 type shadow AudioSpeechNewParams
83 return param.MarshalObject(r, (*shadow)(&r))
84}
85func (r *AudioSpeechNewParams) UnmarshalJSON(data []byte) error {
86 return apijson.UnmarshalRoot(data, r)
87}
88
89// The voice to use when generating the audio. Supported voices are `alloy`, `ash`,
90// `ballad`, `coral`, `echo`, `fable`, `onyx`, `nova`, `sage`, `shimmer`, and
91// `verse`. Previews of the voices are available in the
92// [Text to speech guide](https://platform.openai.com/docs/guides/text-to-speech#voice-options).
93type AudioSpeechNewParamsVoice string
94
95const (
96 AudioSpeechNewParamsVoiceAlloy AudioSpeechNewParamsVoice = "alloy"
97 AudioSpeechNewParamsVoiceAsh AudioSpeechNewParamsVoice = "ash"
98 AudioSpeechNewParamsVoiceBallad AudioSpeechNewParamsVoice = "ballad"
99 AudioSpeechNewParamsVoiceCoral AudioSpeechNewParamsVoice = "coral"
100 AudioSpeechNewParamsVoiceEcho AudioSpeechNewParamsVoice = "echo"
101 AudioSpeechNewParamsVoiceFable AudioSpeechNewParamsVoice = "fable"
102 AudioSpeechNewParamsVoiceOnyx AudioSpeechNewParamsVoice = "onyx"
103 AudioSpeechNewParamsVoiceNova AudioSpeechNewParamsVoice = "nova"
104 AudioSpeechNewParamsVoiceSage AudioSpeechNewParamsVoice = "sage"
105 AudioSpeechNewParamsVoiceShimmer AudioSpeechNewParamsVoice = "shimmer"
106 AudioSpeechNewParamsVoiceVerse AudioSpeechNewParamsVoice = "verse"
107)
108
109// The format to audio in. Supported formats are `mp3`, `opus`, `aac`, `flac`,
110// `wav`, and `pcm`.
111type AudioSpeechNewParamsResponseFormat string
112
113const (
114 AudioSpeechNewParamsResponseFormatMP3 AudioSpeechNewParamsResponseFormat = "mp3"
115 AudioSpeechNewParamsResponseFormatOpus AudioSpeechNewParamsResponseFormat = "opus"
116 AudioSpeechNewParamsResponseFormatAAC AudioSpeechNewParamsResponseFormat = "aac"
117 AudioSpeechNewParamsResponseFormatFLAC AudioSpeechNewParamsResponseFormat = "flac"
118 AudioSpeechNewParamsResponseFormatWAV AudioSpeechNewParamsResponseFormat = "wav"
119 AudioSpeechNewParamsResponseFormatPCM AudioSpeechNewParamsResponseFormat = "pcm"
120)
121
122// The format to stream the audio in. Supported formats are `sse` and `audio`.
123// `sse` is not supported for `tts-1` or `tts-1-hd`.
124type AudioSpeechNewParamsStreamFormat string
125
126const (
127 AudioSpeechNewParamsStreamFormatSSE AudioSpeechNewParamsStreamFormat = "sse"
128 AudioSpeechNewParamsStreamFormatAudio AudioSpeechNewParamsStreamFormat = "audio"
129)