Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(package): add Bun export map #269

Merged
merged 1 commit into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 93 additions & 7 deletions ecosystem-tests/bun/openai.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
import OpenAI, { toFile } from 'openai';
import fs from 'fs';
import { distance } from 'fastest-levenshtein';
import { test, expect } from 'bun:test';

const url = 'https://audio-samples.github.io/samples/mp3/blizzard_biased/sample-1.mp3';
const filename = 'sample-1.mp3';

const correctAnswer =
'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,';
const model = 'whisper-1';

const client = new OpenAI();

async function typeTests() {
// @ts-expect-error this should error if the `Uploadable` type was resolved correctly
await client.audio.transcriptions.create({ file: { foo: true }, model: 'whisper-1' });
// @ts-expect-error this should error if the `Uploadable` type was resolved correctly
await client.audio.transcriptions.create({ file: null, model: 'whisper-1' });
// @ts-expect-error this should error if the `Uploadable` type was resolved correctly
await client.audio.transcriptions.create({ file: 'test', model: 'whisper-1' });
}

function expectSimilar(received: any, comparedTo: string, expectedDistance: number) {
const message = () =>
[
Expand Down Expand Up @@ -38,11 +55,80 @@ test(`streaming works`, async function () {
expectSimilar(chunks.map((c) => c.choices[0]?.delta.content || '').join(''), 'This is a test', 10);
});

test(`toFile rejects`, async function () {
try {
await toFile(new TextEncoder().encode('foo'), 'foo.txt');
throw new Error(`expected toFile to reject`);
} catch (error) {
expect((error as any).message).toEqual(`file uploads aren't supported in this environment yet`);
}
// @ts-ignore avoid DOM lib for testing purposes
if (typeof File !== 'undefined') {
test.todo('handles builtinFile', async function () {
const file = await fetch(url)
.then((x) => x.arrayBuffer())
// @ts-ignore avoid DOM lib for testing purposes
.then((x) => new File([x], filename));

const result = await client.audio.transcriptions.create({ file, model });
expectSimilar(result.text, correctAnswer, 12);
});
}

test.todo('handles Response', async function () {
const file = await fetch(url);

const result = await client.audio.transcriptions.create({ file, model });
expectSimilar(result.text, correctAnswer, 12);
});

test.todo('handles fs.ReadStream', async function () {
const result = await client.audio.transcriptions.create({
file: fs.createReadStream('sample1.mp3'),
model,
});
expectSimilar(result.text, correctAnswer, 12);
});

const fineTune = `{"prompt": "<prompt text>", "completion": "<ideal generated text>"}`;

// @ts-ignore avoid DOM lib for testing purposes
if (typeof Blob !== 'undefined') {
test.todo('toFile handles builtin Blob', async function () {
const result = await client.files.create({
file: await toFile(
// @ts-ignore avoid DOM lib for testing purposes
new Blob([new TextEncoder().encode(fineTune)]),
'finetune.jsonl',
),
purpose: 'fine-tune',
});
expect(result.status).toEqual('uploaded');
});
}
test.todo('toFile handles Uint8Array', async function () {
const result = await client.files.create({
file: await toFile(
// @ts-ignore avoid DOM lib for testing purposes
new TextEncoder().encode(fineTune),
'finetune.jsonl',
),
purpose: 'fine-tune',
});
expect(result.status).toEqual('uploaded');
});
test.todo('toFile handles ArrayBuffer', async function () {
const result = await client.files.create({
file: await toFile(
// @ts-ignore avoid DOM lib for testing purposes
new TextEncoder().encode(fineTune).buffer,
'finetune.jsonl',
),
purpose: 'fine-tune',
});
expect(result.status).toEqual('uploaded');
});
test.todo('toFile handles DataView', async function () {
const result = await client.files.create({
file: await toFile(
// @ts-ignore avoid DOM lib for testing purposes
new DataView(new TextEncoder().encode(fineTune).buffer),
'finetune.jsonl',
),
purpose: 'fine-tune',
});
expect(result.status).toEqual('uploaded');
});
6 changes: 2 additions & 4 deletions ecosystem-tests/bun/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
},
"devDependencies": {
"fastest-levenshtein": "^1.0.16",
"bun-types": "latest"
},
"peerDependencies": {
"typescript": "^5.0.0"
"bun-types": "latest",
"typescript": "^5.1.0"
}
}
12 changes: 6 additions & 6 deletions ecosystem-tests/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ async function main() {
console.error('\n');

try {
await withRetry(fn, project, state.retry)
await withRetry(fn, project, state.retry);
console.error(`✅ - Successfully ran ${project}`);
} catch (err) {
if (err && (err as any).shortMessage) {
Expand All @@ -294,13 +294,13 @@ async function main() {
async function withRetry(fn: () => Promise<void>, identifier: string, retryAmount: number): Promise<void> {
do {
try {
return await fn()
return await fn();
} catch (err) {
console.error(`${identifier} failed due to ${err}; retries left ${retryAmount}`)
retryAmount--;
if (retryAmount === 0) throw err;
console.error(`${identifier} failed due to ${err}; retries left ${retryAmount}`);
}

retryAmount--;
} while (retryAmount > 0)
} while (retryAmount > 0);
}

function centerPad(text: string, width = text.length, char = ' '): string {
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
"require": "./dist/_shims/*.js",
"default": "./dist/_shims/*.mjs"
},
"bun": {
"types": "./dist/_shims/*.d.ts",
"require": "./dist/_shims/*.js",
"default": "./dist/_shims/*.mjs"
},
"browser": {
"types": "./dist/_shims/*.d.ts",
"require": "./dist/_shims/*.js",
Expand Down