Skip to content

Commit b722d3f

Browse files
authored
Merge pull request #273 from openai/release-please--branches--master--changes--next--components--openai
chore(next => master): release 4.4.0
2 parents 3f85654 + fca6fd9 commit b722d3f

19 files changed

+209
-95
lines changed

.github/workflows/open-release-prs.yml

-21
This file was deleted.

.release-please-manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
".": "4.3.1"
2+
".": "4.4.0"
33
}

CHANGELOG.md

+16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
# Changelog
22

3+
## 4.4.0 (2023-09-01)
4+
5+
Full Changelog: [v4.3.1...v4.4.0](https://github.com/openai/openai-node/compare/v4.3.1...v4.4.0)
6+
7+
### Features
8+
9+
* **package:** add Bun export map ([#269](https://github.com/openai/openai-node/issues/269)) ([16f239c](https://github.com/openai/openai-node/commit/16f239c6b4e8526371b01c511d2e0ebba4c5c8c6))
10+
* re-export chat completion types at the top level ([#268](https://github.com/openai/openai-node/issues/268)) ([1a71a39](https://github.com/openai/openai-node/commit/1a71a39421828fdde7b8605094363a5047d2fdc9))
11+
* **tests:** unskip multipart form data tests ([#275](https://github.com/openai/openai-node/issues/275)) ([47d3e18](https://github.com/openai/openai-node/commit/47d3e18a3ee987d04b958dad1a51821ad5472d54))
12+
* **types:** fix ambiguous auto-import for chat completions params ([#266](https://github.com/openai/openai-node/issues/266)) ([19c99fb](https://github.com/openai/openai-node/commit/19c99fb268d6d6c7fc7aaa66475c35f45d12b4bd))
13+
14+
15+
### Bug Fixes
16+
17+
* revert import change which triggered circular import bug in webpack ([#274](https://github.com/openai/openai-node/issues/274)) ([6534e36](https://github.com/openai/openai-node/commit/6534e3620d7e2983e98b42cf95fa966deab1ab1d))
18+
319
## 4.3.1 (2023-08-29)
420

521
Full Changelog: [v4.3.0...v4.3.1](https://github.com/openai/openai-node/compare/v4.3.0...v4.3.1)

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ const openai = new OpenAI({
7878
});
7979

8080
async function main() {
81-
const params: OpenAI.Chat.CompletionCreateParams = {
81+
const params: OpenAI.Chat.ChatCompletionCreateParams = {
8282
messages: [{ role: 'user', content: 'Say this is a test' }],
8383
model: 'gpt-3.5-turbo',
8484
};

api.md

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Types:
1919
- <code><a href="./src/resources/chat/completions.ts">ChatCompletion</a></code>
2020
- <code><a href="./src/resources/chat/completions.ts">ChatCompletionChunk</a></code>
2121
- <code><a href="./src/resources/chat/completions.ts">ChatCompletionMessage</a></code>
22+
- <code><a href="./src/resources/chat/completions.ts">ChatCompletionMessageParam</a></code>
2223
- <code><a href="./src/resources/chat/completions.ts">CreateChatCompletionRequestMessage</a></code>
2324

2425
Methods:

ecosystem-tests/bun/openai.test.ts

+93-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
11
import OpenAI, { toFile } from 'openai';
2+
import fs from 'fs';
23
import { distance } from 'fastest-levenshtein';
34
import { test, expect } from 'bun:test';
45

6+
const url = 'https://audio-samples.github.io/samples/mp3/blizzard_biased/sample-1.mp3';
7+
const filename = 'sample-1.mp3';
8+
9+
const correctAnswer =
10+
'It was anxious to find him no one that expectation of a man who were giving his father enjoyment. But he was avoided in sight in the minister to which indeed,';
11+
const model = 'whisper-1';
12+
513
const client = new OpenAI();
614

15+
async function typeTests() {
16+
// @ts-expect-error this should error if the `Uploadable` type was resolved correctly
17+
await client.audio.transcriptions.create({ file: { foo: true }, model: 'whisper-1' });
18+
// @ts-expect-error this should error if the `Uploadable` type was resolved correctly
19+
await client.audio.transcriptions.create({ file: null, model: 'whisper-1' });
20+
// @ts-expect-error this should error if the `Uploadable` type was resolved correctly
21+
await client.audio.transcriptions.create({ file: 'test', model: 'whisper-1' });
22+
}
23+
724
function expectSimilar(received: any, comparedTo: string, expectedDistance: number) {
825
const message = () =>
926
[
@@ -38,11 +55,80 @@ test(`streaming works`, async function () {
3855
expectSimilar(chunks.map((c) => c.choices[0]?.delta.content || '').join(''), 'This is a test', 10);
3956
});
4057

41-
test(`toFile rejects`, async function () {
42-
try {
43-
await toFile(new TextEncoder().encode('foo'), 'foo.txt');
44-
throw new Error(`expected toFile to reject`);
45-
} catch (error) {
46-
expect((error as any).message).toEqual(`file uploads aren't supported in this environment yet`);
47-
}
58+
// @ts-ignore avoid DOM lib for testing purposes
59+
if (typeof File !== 'undefined') {
60+
test.todo('handles builtinFile', async function () {
61+
const file = await fetch(url)
62+
.then((x) => x.arrayBuffer())
63+
// @ts-ignore avoid DOM lib for testing purposes
64+
.then((x) => new File([x], filename));
65+
66+
const result = await client.audio.transcriptions.create({ file, model });
67+
expectSimilar(result.text, correctAnswer, 12);
68+
});
69+
}
70+
71+
test.todo('handles Response', async function () {
72+
const file = await fetch(url);
73+
74+
const result = await client.audio.transcriptions.create({ file, model });
75+
expectSimilar(result.text, correctAnswer, 12);
76+
});
77+
78+
test.todo('handles fs.ReadStream', async function () {
79+
const result = await client.audio.transcriptions.create({
80+
file: fs.createReadStream('sample1.mp3'),
81+
model,
82+
});
83+
expectSimilar(result.text, correctAnswer, 12);
84+
});
85+
86+
const fineTune = `{"prompt": "<prompt text>", "completion": "<ideal generated text>"}`;
87+
88+
// @ts-ignore avoid DOM lib for testing purposes
89+
if (typeof Blob !== 'undefined') {
90+
test.todo('toFile handles builtin Blob', async function () {
91+
const result = await client.files.create({
92+
file: await toFile(
93+
// @ts-ignore avoid DOM lib for testing purposes
94+
new Blob([new TextEncoder().encode(fineTune)]),
95+
'finetune.jsonl',
96+
),
97+
purpose: 'fine-tune',
98+
});
99+
expect(result.status).toEqual('uploaded');
100+
});
101+
}
102+
test.todo('toFile handles Uint8Array', async function () {
103+
const result = await client.files.create({
104+
file: await toFile(
105+
// @ts-ignore avoid DOM lib for testing purposes
106+
new TextEncoder().encode(fineTune),
107+
'finetune.jsonl',
108+
),
109+
purpose: 'fine-tune',
110+
});
111+
expect(result.status).toEqual('uploaded');
112+
});
113+
test.todo('toFile handles ArrayBuffer', async function () {
114+
const result = await client.files.create({
115+
file: await toFile(
116+
// @ts-ignore avoid DOM lib for testing purposes
117+
new TextEncoder().encode(fineTune).buffer,
118+
'finetune.jsonl',
119+
),
120+
purpose: 'fine-tune',
121+
});
122+
expect(result.status).toEqual('uploaded');
123+
});
124+
test.todo('toFile handles DataView', async function () {
125+
const result = await client.files.create({
126+
file: await toFile(
127+
// @ts-ignore avoid DOM lib for testing purposes
128+
new DataView(new TextEncoder().encode(fineTune).buffer),
129+
'finetune.jsonl',
130+
),
131+
purpose: 'fine-tune',
132+
});
133+
expect(result.status).toEqual('uploaded');
48134
});

ecosystem-tests/bun/package.json

+2-4
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
},
88
"devDependencies": {
99
"fastest-levenshtein": "^1.0.16",
10-
"bun-types": "latest"
11-
},
12-
"peerDependencies": {
13-
"typescript": "^5.0.0"
10+
"bun-types": "latest",
11+
"typescript": "^5.1.0"
1412
}
1513
}

ecosystem-tests/cli.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ async function main() {
268268
console.error('\n');
269269

270270
try {
271-
await withRetry(fn, project, state.retry)
271+
await withRetry(fn, project, state.retry);
272272
console.error(`✅ - Successfully ran ${project}`);
273273
} catch (err) {
274274
if (err && (err as any).shortMessage) {
@@ -294,13 +294,13 @@ async function main() {
294294
async function withRetry(fn: () => Promise<void>, identifier: string, retryAmount: number): Promise<void> {
295295
do {
296296
try {
297-
return await fn()
297+
return await fn();
298298
} catch (err) {
299-
console.error(`${identifier} failed due to ${err}; retries left ${retryAmount}`)
299+
retryAmount--;
300+
if (retryAmount === 0) throw err;
301+
console.error(`${identifier} failed due to ${err}; retries left ${retryAmount}`);
300302
}
301-
302-
retryAmount--;
303-
} while (retryAmount > 0)
303+
} while (retryAmount > 0);
304304
}
305305

306306
function centerPad(text: string, width = text.length, char = ' '): string {

examples/chat-params-types.ts

+12-10
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const openai = new OpenAI();
99
async function main() {
1010
// ---------------- Explicit non-streaming params ------------
1111

12-
const params: OpenAI.Chat.CompletionCreateParams = {
12+
const params: OpenAI.Chat.ChatCompletionCreateParams = {
1313
model: 'gpt-4',
1414
messages: [{ role: 'user', content: 'Say this is a test!' }],
1515
};
@@ -18,7 +18,7 @@ async function main() {
1818

1919
// ---------------- Explicit streaming params ----------------
2020

21-
const streamingParams: OpenAI.Chat.CompletionCreateParams = {
21+
const streamingParams: OpenAI.Chat.ChatCompletionCreateParams = {
2222
model: 'gpt-4',
2323
messages: [{ role: 'user', content: 'Say this is a test!' }],
2424
stream: true,
@@ -32,12 +32,12 @@ async function main() {
3232

3333
// ---------------- Explicit (non)streaming types ----------------
3434

35-
const params1: OpenAI.Chat.CompletionCreateParamsNonStreaming = {
35+
const params1: OpenAI.Chat.ChatCompletionCreateParamsNonStreaming = {
3636
model: 'gpt-4',
3737
messages: [{ role: 'user', content: 'Say this is a test!' }],
3838
};
3939

40-
const params2: OpenAI.Chat.CompletionCreateParamsStreaming = {
40+
const params2: OpenAI.Chat.ChatCompletionCreateParamsStreaming = {
4141
model: 'gpt-4',
4242
messages: [{ role: 'user', content: 'Say this is a test!' }],
4343
stream: true,
@@ -52,9 +52,9 @@ async function main() {
5252
// `role: string` is not assignable.
5353
const streamingParams2 = {
5454
model: 'gpt-4',
55-
messages: [{ role: 'user', content: 'Say this is a test!' }],
56-
stream: true,
57-
} as const;
55+
messages: [{ role: 'user' as const, content: 'Say this is a test!' }],
56+
stream: true as const,
57+
};
5858

5959
// TS knows this is a Stream instance.
6060
const stream2 = await openai.chat.completions.create(streamingParams2);
@@ -95,11 +95,13 @@ async function main() {
9595
// not the response will be streamed.
9696
export async function createCompletionParams(
9797
stream: true,
98-
): Promise<OpenAI.Chat.CompletionCreateParamsStreaming>;
98+
): Promise<OpenAI.Chat.ChatCompletionCreateParamsStreaming>;
9999
export async function createCompletionParams(
100100
stream: false,
101-
): Promise<OpenAI.Chat.CompletionCreateParamsNonStreaming>;
102-
export async function createCompletionParams(stream: boolean): Promise<OpenAI.Chat.CompletionCreateParams> {
101+
): Promise<OpenAI.Chat.ChatCompletionCreateParamsNonStreaming>;
102+
export async function createCompletionParams(
103+
stream: boolean,
104+
): Promise<OpenAI.Chat.ChatCompletionCreateParams> {
103105
const params = {
104106
model: 'gpt-3.5-turbo',
105107
messages: [{ role: 'user' as const, content: 'Hello!' }],

examples/function-call-stream.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ import OpenAI from 'openai';
55
import {
66
ChatCompletionMessage,
77
ChatCompletionChunk,
8-
CreateChatCompletionRequestMessage,
8+
ChatCompletionMessageParam,
99
} from 'openai/resources/chat';
1010

1111
// gets API Key from environment variable OPENAI_API_KEY
1212
const openai = new OpenAI();
1313

14-
const functions: OpenAI.Chat.CompletionCreateParams.Function[] = [
14+
const functions: OpenAI.Chat.ChatCompletionCreateParams.Function[] = [
1515
{
1616
name: 'list',
1717
description: 'list queries books by genre, and returns a list of names of books',
@@ -63,7 +63,7 @@ async function callFunction(function_call: ChatCompletionMessage.FunctionCall):
6363
}
6464

6565
async function main() {
66-
const messages: CreateChatCompletionRequestMessage[] = [
66+
const messages: ChatCompletionMessageParam[] = [
6767
{
6868
role: 'system',
6969
content:

examples/function-call.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#!/usr/bin/env -S npm run tsn -T
22

33
import OpenAI from 'openai';
4-
import { ChatCompletionMessage, CreateChatCompletionRequestMessage } from 'openai/resources/chat';
4+
import { ChatCompletionMessage, ChatCompletionMessageParam } from 'openai/resources/chat';
55

66
// gets API Key from environment variable OPENAI_API_KEY
77
const openai = new OpenAI();
88

9-
const functions: OpenAI.Chat.CompletionCreateParams.Function[] = [
9+
const functions: OpenAI.Chat.ChatCompletionCreateParams.Function[] = [
1010
{
1111
name: 'list',
1212
description: 'list queries books by genre, and returns a list of names of books',
@@ -58,7 +58,7 @@ async function callFunction(function_call: ChatCompletionMessage.FunctionCall):
5858
}
5959

6060
async function main() {
61-
const messages: CreateChatCompletionRequestMessage[] = [
61+
const messages: ChatCompletionMessageParam[] = [
6262
{
6363
role: 'system',
6464
content:

package.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "openai",
3-
"version": "4.3.1",
3+
"version": "4.4.0",
44
"description": "Client library for the OpenAI API",
55
"author": "OpenAI <[email protected]>",
66
"types": "dist/index.d.ts",
@@ -16,6 +16,11 @@
1616
"require": "./dist/_shims/*.js",
1717
"default": "./dist/_shims/*.mjs"
1818
},
19+
"bun": {
20+
"types": "./dist/_shims/*.d.ts",
21+
"require": "./dist/_shims/*.js",
22+
"default": "./dist/_shims/*.mjs"
23+
},
1924
"browser": {
2025
"types": "./dist/_shims/*.d.ts",
2126
"require": "./dist/_shims/*.js",

src/resources/chat/chat.ts

+4
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@ export namespace Chat {
1313
export import ChatCompletion = API.ChatCompletion;
1414
export import ChatCompletionChunk = API.ChatCompletionChunk;
1515
export import ChatCompletionMessage = API.ChatCompletionMessage;
16+
export import ChatCompletionMessageParam = API.ChatCompletionMessageParam;
1617
export import CreateChatCompletionRequestMessage = API.CreateChatCompletionRequestMessage;
18+
export import ChatCompletionCreateParams = API.ChatCompletionCreateParams;
1719
export import CompletionCreateParams = API.CompletionCreateParams;
20+
export import ChatCompletionCreateParamsNonStreaming = API.ChatCompletionCreateParamsNonStreaming;
1821
export import CompletionCreateParamsNonStreaming = API.CompletionCreateParamsNonStreaming;
22+
export import ChatCompletionCreateParamsStreaming = API.ChatCompletionCreateParamsStreaming;
1923
export import CompletionCreateParamsStreaming = API.CompletionCreateParamsStreaming;
2024
}

0 commit comments

Comments
 (0)