|
1 | 1 | import OpenAI, { toFile } from 'openai';
|
| 2 | +import fs from 'fs'; |
2 | 3 | import { distance } from 'fastest-levenshtein';
|
3 | 4 | import { test, expect } from 'bun:test';
|
4 | 5 |
|
| 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 | + |
5 | 13 | const client = new OpenAI();
|
6 | 14 |
|
| 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 | + |
7 | 24 | function expectSimilar(received: any, comparedTo: string, expectedDistance: number) {
|
8 | 25 | const message = () =>
|
9 | 26 | [
|
@@ -38,11 +55,80 @@ test(`streaming works`, async function () {
|
38 | 55 | expectSimilar(chunks.map((c) => c.choices[0]?.delta.content || '').join(''), 'This is a test', 10);
|
39 | 56 | });
|
40 | 57 |
|
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'); |
48 | 134 | });
|
0 commit comments