Repository navigation
Expand file tree
/
Copy pathspeech.ts
More file actions
147 lines (134 loc) · 4.41 KB
/
Copy pathspeech.ts
File metadata and controls
147 lines (134 loc) · 4.41 KB
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
// File generated from our OpenAPI spec by Castiron. See CONTRIBUTING.md for details.
import { APIResource } from '../../core/resource';
import { APIPromise } from '../../core/api-promise';
import { buildHeaders } from '../../internal/headers';
import { RequestOptions } from '../../internal/request-options';
function resolveResourceRequestOptions(
options: RequestOptions | undefined,
buildOptions: (options: RequestOptions | undefined) => RequestOptions | Promise<RequestOptions>,
): Promise<RequestOptions> {
return Promise.resolve(options).then(buildOptions);
}
/**
* Turn audio into text or text into audio.
*/
export class Speech extends APIResource {
/**
* Generates audio from the input text.
*
* Returns the audio file content, or a stream of audio events.
*
* The `tts-1`, `tts-1-hd`, `gpt-4o-mini-tts`, `gpt-4o-mini-tts-2025-03-20`, and
* `gpt-4o-mini-tts-2025-12-15` models are deprecated and scheduled to shut down on
* January 6, 2027. These examples use the existing Speech API; for new
* integrations, migrate to `gpt-realtime-2.1-mini` through the
* [Realtime API](https://developers.openai.com/api/docs/guides/realtime). See the
* [deprecation schedule](https://developers.openai.com/api/docs/deprecations) for
* migration details.
*
* @example
* ```ts
* const speech = await client.audio.speech.create({
* input: 'input',
* model: 'tts-1',
* voice: 'ash',
* });
*
* const content = await speech.blob();
* console.log(content);
* ```
*/
create(body: SpeechCreateParams, options?: RequestOptions): APIPromise<Response> {
return this._client.post(
'/audio/speech',
resolveResourceRequestOptions(options, (options) => ({
body,
...options,
headers: buildHeaders([{ Accept: 'application/octet-stream' }, options?.headers]),
__security: { bearerAuth: true },
__binaryResponse: true,
})),
);
}
}
/**
* Deprecated values:
*
* - "tts-1", "tts-1-hd", "gpt-4o-mini-tts", "gpt-4o-mini-tts-2025-12-15":
* Announced shutdown date: 2027-01-06. See
* https://developers.openai.com/api/docs/deprecations for details and
* recommended replacements.
*/
export type SpeechModel = 'tts-1' | 'tts-1-hd' | 'gpt-4o-mini-tts' | 'gpt-4o-mini-tts-2025-12-15';
export interface SpeechCreateParams {
/**
* The text to generate audio for. The maximum length is 4096 characters.
*/
input: string;
/**
* One of the available
* [TTS models](https://developers.openai.com/api/docs/guides/text-to-speech):
* `tts-1`, `tts-1-hd`, `gpt-4o-mini-tts`, or `gpt-4o-mini-tts-2025-12-15`.
*/
model: (string & {}) | SpeechModel;
/**
* The voice to use when generating the audio. Supported built-in voices are
* `alloy`, `ash`, `ballad`, `coral`, `echo`, `fable`, `onyx`, `nova`, `sage`,
* `shimmer`, `verse`, `marin`, and `cedar`. You may also provide a custom voice
* object with an `id`, for example `{ "id": "voice_1234" }`. Previews of the
* voices are available in the
* [Text to speech guide](https://developers.openai.com/api/docs/guides/text-to-speech#voice-options).
* Custom voices must be created from audio samples. Voices created from text
* prompts are supported only in Live.
*/
voice:
| string
| 'alloy'
| 'ash'
| 'ballad'
| 'coral'
| 'echo'
| 'sage'
| 'shimmer'
| 'verse'
| 'marin'
| 'cedar'
| 'fable'
| 'onyx'
| 'nova'
| SpeechCreateParams.ID;
/**
* Control the voice of your generated audio with additional instructions. Does not
* work with `tts-1` or `tts-1-hd`.
*/
instructions?: string;
/**
* The format to audio in. Supported formats are `mp3`, `opus`, `aac`, `flac`,
* `wav`, and `pcm`.
*/
response_format?: 'mp3' | 'opus' | 'aac' | 'flac' | 'wav' | 'pcm';
/**
* The speed of the generated audio. Select a value from `0.25` to `4.0`. `1.0` is
* the default.
*/
speed?: number;
/**
* The format to stream the audio in. Supported formats are `sse` and `audio`.
* `sse` is not supported for `tts-1` or `tts-1-hd`.
*/
stream_format?: 'sse' | 'audio';
}
export namespace SpeechCreateParams {
/**
* Custom voice reference.
*/
export interface ID {
/**
* The custom voice ID, e.g. `voice_1234`.
*/
id: string;
}
}
export declare namespace Speech {
export { type SpeechModel as SpeechModel, type SpeechCreateParams as SpeechCreateParams };
}