Skip to content

Commit ddb2815

Browse files
committed
v4.1.0
1 parent b0c9fdb commit ddb2815

20 files changed

+665
-42
lines changed

.github/pull_request_template.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,4 @@
77

88
## Changes being requested
99

10-
1110
## Additional context & links

.stats.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
configured_endpoints: 23
1+
configured_endpoints: 28

api.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,23 @@ Methods:
121121
- <code title="get /models">client.models.<a href="./src/resources/models.ts">list</a>() -> ModelsPage</code>
122122
- <code title="delete /models/{model}">client.models.<a href="./src/resources/models.ts">del</a>(model) -> ModelDeleted</code>
123123

124+
# FineTuning
125+
126+
## Jobs
127+
128+
Types:
129+
130+
- <code><a href="./src/resources/fine-tuning/jobs.ts">FineTuningJob</a></code>
131+
- <code><a href="./src/resources/fine-tuning/jobs.ts">FineTuningJobEvent</a></code>
132+
133+
Methods:
134+
135+
- <code title="post /fine_tuning/jobs">client.fineTuning.jobs.<a href="./src/resources/fine-tuning/jobs.ts">create</a>({ ...params }) -> FineTuningJob</code>
136+
- <code title="get /fine_tuning/jobs/{fine_tuning_job_id}">client.fineTuning.jobs.<a href="./src/resources/fine-tuning/jobs.ts">retrieve</a>(fineTuningJobId) -> FineTuningJob</code>
137+
- <code title="get /fine_tuning/jobs">client.fineTuning.jobs.<a href="./src/resources/fine-tuning/jobs.ts">list</a>({ ...params }) -> FineTuningJobsPage</code>
138+
- <code title="post /fine_tuning/jobs/{fine_tuning_job_id}/cancel">client.fineTuning.jobs.<a href="./src/resources/fine-tuning/jobs.ts">cancel</a>(fineTuningJobId) -> FineTuningJob</code>
139+
- <code title="get /fine_tuning/jobs/{fine_tuning_job_id}/events">client.fineTuning.jobs.<a href="./src/resources/fine-tuning/jobs.ts">listEvents</a>(fineTuningJobId, { ...params }) -> FineTuningJobEventsPage</code>
140+
124141
# FineTunes
125142

126143
Types:

examples/chat-params-types.ts

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#!/usr/bin/env -S npm run tsn -T
2+
3+
import OpenAI from 'openai';
4+
import { Stream } from 'openai/streaming';
5+
6+
// gets API Key from environment variable OPENAI_API_KEY
7+
const openai = new OpenAI();
8+
9+
async function main() {
10+
// ---------------- Explicit non-streaming params ------------
11+
12+
const params: OpenAI.Chat.CompletionCreateParams = {
13+
model: 'gpt-4',
14+
messages: [{ role: 'user', content: 'Say this is a test!' }],
15+
};
16+
const completion = await openai.chat.completions.create(params);
17+
console.log(completion.choices[0]?.message?.content);
18+
19+
// ---------------- Explicit streaming params ----------------
20+
21+
const streamingParams: OpenAI.Chat.CompletionCreateParams = {
22+
model: 'gpt-4',
23+
messages: [{ role: 'user', content: 'Say this is a test!' }],
24+
stream: true,
25+
};
26+
27+
const stream = await openai.chat.completions.create(streamingParams);
28+
for await (const chunk of stream) {
29+
process.stdout.write(chunk.choices[0]?.delta?.content || '');
30+
}
31+
process.stdout.write('\n');
32+
33+
// ---------------- Explicit (non)streaming types ----------------
34+
35+
const params1: OpenAI.Chat.CompletionCreateParamsNonStreaming = {
36+
model: 'gpt-4',
37+
messages: [{ role: 'user', content: 'Say this is a test!' }],
38+
};
39+
40+
const params2: OpenAI.Chat.CompletionCreateParamsStreaming = {
41+
model: 'gpt-4',
42+
messages: [{ role: 'user', content: 'Say this is a test!' }],
43+
stream: true,
44+
};
45+
46+
// ---------------- Implicit params type -------------------
47+
48+
// Note: the `as const` is required here so that TS can properly infer
49+
// the right params type.
50+
//
51+
// If you didn't include it then you'd also get an error saying that
52+
// `role: string` is not assignable.
53+
const streamingParams2 = {
54+
model: 'gpt-4',
55+
messages: [{ role: 'user', content: 'Say this is a test!' }],
56+
stream: true,
57+
} as const;
58+
59+
// TS knows this is a Stream instance.
60+
const stream2 = await openai.chat.completions.create(streamingParams2);
61+
for await (const chunk of stream2) {
62+
process.stdout.write(chunk.choices[0]?.delta?.content || '');
63+
}
64+
process.stdout.write('\n');
65+
66+
// Without the `as const` for `stream`.
67+
const streamingParams3 = {
68+
model: 'gpt-4',
69+
messages: [{ role: 'user' as const, content: 'Say this is a test!' }],
70+
stream: true,
71+
};
72+
73+
// TS doesn't know if this is a `Stream` or a direct response
74+
const response = await openai.chat.completions.create(streamingParams3);
75+
if (response instanceof Stream) {
76+
// here TS knows the response type is a `Stream`
77+
} else {
78+
// here TS knows the response type is a `ChatCompletion`
79+
}
80+
81+
// ---------------- Dynamic params type -------------------
82+
83+
// TS knows this is a `Stream`
84+
const streamParamsFromFn = await createCompletionParams(true);
85+
const streamFromFn = await openai.chat.completions.create(streamParamsFromFn);
86+
console.log(streamFromFn);
87+
88+
// TS knows this is a `ChatCompletion`
89+
const paramsFromFn = await createCompletionParams(false);
90+
const completionFromFn = await openai.chat.completions.create(paramsFromFn);
91+
console.log(completionFromFn);
92+
}
93+
94+
// Dynamically construct the params object while retaining whether or
95+
// not the response will be streamed.
96+
export async function createCompletionParams(
97+
stream: true,
98+
): Promise<OpenAI.Chat.CompletionCreateParamsStreaming>;
99+
export async function createCompletionParams(
100+
stream: false,
101+
): Promise<OpenAI.Chat.CompletionCreateParamsNonStreaming>;
102+
export async function createCompletionParams(stream: boolean): Promise<OpenAI.Chat.CompletionCreateParams> {
103+
const params = {
104+
model: 'gpt-3.5-turbo',
105+
messages: [{ role: 'user' as const, content: 'Hello!' }],
106+
stream: stream,
107+
};
108+
109+
// <your logic here>
110+
111+
return params;
112+
}
113+
114+
main();

examples/types.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env -S npm run tsn -T
2+
3+
import OpenAI from 'openai';
4+
5+
// gets API Key from environment variable OPENAI_API_KEY
6+
const openai = new OpenAI();
7+
8+
async function main() {
9+
// Explicit non streaming params type:
10+
const params: OpenAI.Chat.CompletionCreateParams = {
11+
model: 'gpt-4',
12+
messages: [{ role: 'user', content: 'Say this is a test!' }],
13+
};
14+
const completion = await openai.chat.completions.create(params);
15+
console.log(completion.choices[0]?.message?.content);
16+
17+
// Explicit streaming params type:
18+
const streaming_params: OpenAI.Chat.CompletionCreateParams = {
19+
model: 'gpt-4',
20+
messages: [{ role: 'user', content: 'Say this is a test!' }],
21+
stream: true,
22+
};
23+
24+
const stream = await openai.chat.completions.create(streaming_params);
25+
for await (const chunk of stream) {
26+
process.stdout.write(chunk.choices[0]?.delta?.content || '');
27+
}
28+
process.stdout.write('\n');
29+
}
30+
31+
main();

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "openai",
3-
"version": "4.0.1",
3+
"version": "4.1.0",
44
"description": "Client library for the OpenAI API",
55
"author": "OpenAI <[email protected]>",
66
"types": "dist/index.d.ts",

src/core.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,7 @@ declare const navigator: { userAgent: string } | undefined;
803803

804804
// Note: modified from https://github.com/JS-DevTools/host-environment/blob/b1ab79ecde37db5d6e163c050e54fe7d287d7c92/src/isomorphic.browser.ts
805805
function getBrowserInfo(): BrowserInfo | null {
806-
if (!navigator || typeof navigator === 'undefined') {
806+
if (typeof navigator === 'undefined' || !navigator) {
807807
return null;
808808
}
809809

src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ export class OpenAI extends Core.APIClient {
143143
audio: API.Audio = new API.Audio(this);
144144
moderations: API.Moderations = new API.Moderations(this);
145145
models: API.Models = new API.Models(this);
146+
fineTuning: API.FineTuning = new API.FineTuning(this);
146147
fineTunes: API.FineTunes = new API.FineTunes(this);
147148

148149
protected override defaultQuery(): Core.DefaultQuery | undefined {
@@ -248,6 +249,8 @@ export namespace OpenAI {
248249
export import ModelDeleted = API.ModelDeleted;
249250
export import ModelsPage = API.ModelsPage;
250251

252+
export import FineTuning = API.FineTuning;
253+
251254
export import FineTunes = API.FineTunes;
252255
export import FineTune = API.FineTune;
253256
export import FineTuneEvent = API.FineTuneEvent;

src/resources/chat/completions.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export class Completions extends APIResource {
1717
options?: Core.RequestOptions,
1818
): APIPromise<Stream<ChatCompletionChunk>>;
1919
create(
20-
body: CompletionCreateParams,
20+
body: CompletionCreateParamsBase,
2121
options?: Core.RequestOptions,
2222
): APIPromise<Stream<ChatCompletionChunk> | ChatCompletion>;
2323
create(
@@ -278,7 +278,9 @@ export namespace CreateChatCompletionRequestMessage {
278278
}
279279
}
280280

281-
export interface CompletionCreateParams {
281+
export type CompletionCreateParams = CompletionCreateParamsNonStreaming | CompletionCreateParamsStreaming;
282+
283+
export interface CompletionCreateParamsBase {
282284
/**
283285
* A list of messages comprising the conversation so far.
284286
* [Example Python code](https://github.com/openai/openai-cookbook/blob/main/examples/How_to_format_inputs_to_ChatGPT_models.ipynb).
@@ -441,7 +443,7 @@ export namespace CompletionCreateParams {
441443
export type CompletionCreateParamsStreaming = API.CompletionCreateParamsStreaming;
442444
}
443445

444-
export interface CompletionCreateParamsNonStreaming extends CompletionCreateParams {
446+
export interface CompletionCreateParamsNonStreaming extends CompletionCreateParamsBase {
445447
/**
446448
* If set, partial message deltas will be sent, like in ChatGPT. Tokens will be
447449
* sent as data-only
@@ -453,7 +455,7 @@ export interface CompletionCreateParamsNonStreaming extends CompletionCreatePara
453455
stream?: false | null;
454456
}
455457

456-
export interface CompletionCreateParamsStreaming extends CompletionCreateParams {
458+
export interface CompletionCreateParamsStreaming extends CompletionCreateParamsBase {
457459
/**
458460
* If set, partial message deltas will be sent, like in ChatGPT. Tokens will be
459461
* sent as data-only

src/resources/completions.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export class Completions extends APIResource {
1616
options?: Core.RequestOptions,
1717
): APIPromise<Stream<Completion>>;
1818
create(
19-
body: CompletionCreateParams,
19+
body: CompletionCreateParamsBase,
2020
options?: Core.RequestOptions,
2121
): APIPromise<Stream<Completion> | Completion>;
2222
create(
@@ -112,7 +112,9 @@ export interface CompletionUsage {
112112
total_tokens: number;
113113
}
114114

115-
export interface CompletionCreateParams {
115+
export type CompletionCreateParams = CompletionCreateParamsNonStreaming | CompletionCreateParamsStreaming;
116+
117+
export interface CompletionCreateParamsBase {
116118
/**
117119
* ID of the model to use. You can use the
118120
* [List models](/docs/api-reference/models/list) API to see all of your available
@@ -121,6 +123,8 @@ export interface CompletionCreateParams {
121123
*/
122124
model:
123125
| (string & {})
126+
| 'babbage-002'
127+
| 'davinci-002'
124128
| 'text-davinci-003'
125129
| 'text-davinci-002'
126130
| 'text-davinci-001'
@@ -272,7 +276,7 @@ export namespace CompletionCreateParams {
272276
export type CompletionCreateParamsStreaming = API.CompletionCreateParamsStreaming;
273277
}
274278

275-
export interface CompletionCreateParamsNonStreaming extends CompletionCreateParams {
279+
export interface CompletionCreateParamsNonStreaming extends CompletionCreateParamsBase {
276280
/**
277281
* Whether to stream back partial progress. If set, tokens will be sent as
278282
* data-only
@@ -284,7 +288,7 @@ export interface CompletionCreateParamsNonStreaming extends CompletionCreatePara
284288
stream?: false | null;
285289
}
286290

287-
export interface CompletionCreateParamsStreaming extends CompletionCreateParams {
291+
export interface CompletionCreateParamsStreaming extends CompletionCreateParamsBase {
288292
/**
289293
* Whether to stream back partial progress. If set, tokens will be sent as
290294
* data-only

src/resources/files.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,16 +118,14 @@ export interface FileCreateParams {
118118
* Name of the [JSON Lines](https://jsonlines.readthedocs.io/en/latest/) file to be
119119
* uploaded.
120120
*
121-
* If the `purpose` is set to "fine-tune", each line is a JSON record with "prompt"
122-
* and "completion" fields representing your
123-
* [training examples](/docs/guides/fine-tuning/prepare-training-data).
121+
* If the `purpose` is set to "fine-tune", the file will be used for fine-tuning.
124122
*/
125123
file: Uploadable;
126124

127125
/**
128126
* The intended purpose of the uploaded documents.
129127
*
130-
* Use "fine-tune" for [Fine-tuning](/docs/api-reference/fine-tunes). This allows
128+
* Use "fine-tune" for [fine-tuning](/docs/api-reference/fine-tuning). This allows
131129
* us to validate the format of the uploaded file.
132130
*/
133131
purpose: string;

0 commit comments

Comments
 (0)