Skip to content

Commit 16f239c

Browse files
feat(package): add Bun export map (#269)
1 parent 1a71a39 commit 16f239c

File tree

4 files changed

+106
-17
lines changed

4 files changed

+106
-17
lines changed

ecosystem-tests/bun/openai.test.ts

Lines changed: 93 additions & 7 deletions
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

Lines changed: 2 additions & 4 deletions
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

Lines changed: 6 additions & 6 deletions
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 {

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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",

0 commit comments

Comments
 (0)