Skip to content

Commit 8f2cf4f

Browse files
committed
v4.0.1
1 parent 3ec43ee commit 8f2cf4f

17 files changed

+403
-15
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ main();
4242

4343
## Streaming Responses
4444

45-
We provide support for streaming responses using Server Side Events (SSE).
45+
We provide support for streaming responses using Server Sent Events (SSE).
4646

4747
```ts
4848
import OpenAI from 'openai';

ecosystem-tests/bun/.gitignore

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore
2+
3+
# Logs
4+
5+
logs
6+
_.log
7+
npm-debug.log_
8+
yarn-debug.log*
9+
yarn-error.log*
10+
lerna-debug.log*
11+
.pnpm-debug.log*
12+
13+
# Diagnostic reports (https://nodejs.org/api/report.html)
14+
15+
report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
16+
17+
# Runtime data
18+
19+
pids
20+
_.pid
21+
_.seed
22+
\*.pid.lock
23+
24+
# Directory for instrumented libs generated by jscoverage/JSCover
25+
26+
lib-cov
27+
28+
# Coverage directory used by tools like istanbul
29+
30+
coverage
31+
\*.lcov
32+
33+
# nyc test coverage
34+
35+
.nyc_output
36+
37+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
38+
39+
.grunt
40+
41+
# Bower dependency directory (https://bower.io/)
42+
43+
bower_components
44+
45+
# node-waf configuration
46+
47+
.lock-wscript
48+
49+
# Compiled binary addons (https://nodejs.org/api/addons.html)
50+
51+
build/Release
52+
53+
# Dependency directories
54+
55+
node_modules/
56+
jspm_packages/
57+
58+
# Snowpack dependency directory (https://snowpack.dev/)
59+
60+
web_modules/
61+
62+
# TypeScript cache
63+
64+
\*.tsbuildinfo
65+
66+
# Optional npm cache directory
67+
68+
.npm
69+
70+
# Optional eslint cache
71+
72+
.eslintcache
73+
74+
# Optional stylelint cache
75+
76+
.stylelintcache
77+
78+
# Microbundle cache
79+
80+
.rpt2_cache/
81+
.rts2_cache_cjs/
82+
.rts2_cache_es/
83+
.rts2_cache_umd/
84+
85+
# Optional REPL history
86+
87+
.node_repl_history
88+
89+
# Output of 'npm pack'
90+
91+
\*.tgz
92+
93+
# Yarn Integrity file
94+
95+
.yarn-integrity
96+
97+
# dotenv environment variable files
98+
99+
.env
100+
.env.development.local
101+
.env.test.local
102+
.env.production.local
103+
.env.local
104+
105+
# parcel-bundler cache (https://parceljs.org/)
106+
107+
.cache
108+
.parcel-cache
109+
110+
# Next.js build output
111+
112+
.next
113+
out
114+
115+
# Nuxt.js build / generate output
116+
117+
.nuxt
118+
dist
119+
120+
# Gatsby files
121+
122+
.cache/
123+
124+
# Comment in the public line in if your project uses Gatsby and not Next.js
125+
126+
# https://nextjs.org/blog/next-9-1#public-directory-support
127+
128+
# public
129+
130+
# vuepress build output
131+
132+
.vuepress/dist
133+
134+
# vuepress v2.x temp and cache directory
135+
136+
.temp
137+
.cache
138+
139+
# Docusaurus cache and generated files
140+
141+
.docusaurus
142+
143+
# Serverless directories
144+
145+
.serverless/
146+
147+
# FuseBox cache
148+
149+
.fusebox/
150+
151+
# DynamoDB Local files
152+
153+
.dynamodb/
154+
155+
# TernJS port file
156+
157+
.tern-port
158+
159+
# Stores VSCode versions used for testing VSCode extensions
160+
161+
.vscode-test
162+
163+
# yarn v2
164+
165+
.yarn/cache
166+
.yarn/unplugged
167+
.yarn/build-state.yml
168+
.yarn/install-state.gz
169+
.pnp.\*

ecosystem-tests/bun/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# openai-bun-test
2+
3+
To install dependencies:
4+
5+
```bash
6+
bun install
7+
```
8+
9+
To run:
10+
11+
```bash
12+
bun run index.ts
13+
```
14+
15+
This project was created using `bun init` in bun v0.6.3. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime.

ecosystem-tests/bun/bun.lockb

1.65 KB
Binary file not shown.

ecosystem-tests/bun/openai.test.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import OpenAI, { toFile } from 'openai';
2+
import { distance } from 'fastest-levenshtein';
3+
import { test, expect } from 'bun:test';
4+
5+
const client = new OpenAI();
6+
7+
function expectSimilar(received: any, comparedTo: string, expectedDistance: number) {
8+
const message = () =>
9+
[
10+
`Received: ${JSON.stringify(received)}`,
11+
`Expected: ${JSON.stringify(comparedTo)}`,
12+
`Expected distance: ${expectedDistance}`,
13+
`Received distance: ${actualDistance}`,
14+
].join('\n');
15+
16+
const actualDistance = distance(received, comparedTo);
17+
expect(actualDistance).toBeLessThan(expectedDistance);
18+
}
19+
20+
test(`basic request works`, async function () {
21+
const completion = await client.chat.completions.create({
22+
model: 'gpt-4',
23+
messages: [{ role: 'user', content: 'Say this is a test' }],
24+
});
25+
expectSimilar(completion.choices[0]?.message?.content, 'This is a test', 10);
26+
});
27+
28+
test(`streaming works`, async function () {
29+
const stream = await client.chat.completions.create({
30+
model: 'gpt-4',
31+
messages: [{ role: 'user', content: 'Say this is a test' }],
32+
stream: true,
33+
});
34+
const chunks = [];
35+
for await (const part of stream) {
36+
chunks.push(part);
37+
}
38+
expectSimilar(chunks.map((c) => c.choices[0]?.delta.content || '').join(''), 'This is a test', 10);
39+
});
40+
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+
}
48+
});

ecosystem-tests/bun/package.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "openai-bun-test",
3+
"module": "index.ts",
4+
"type": "module",
5+
"scripts": {
6+
"tsc": "tsc"
7+
},
8+
"devDependencies": {
9+
"fastest-levenshtein": "^1.0.16",
10+
"bun-types": "latest"
11+
},
12+
"peerDependencies": {
13+
"typescript": "^5.0.0"
14+
}
15+
}

ecosystem-tests/bun/tsconfig.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"compilerOptions": {
3+
"lib": ["ESNext"],
4+
"module": "esnext",
5+
"target": "esnext",
6+
"moduleResolution": "bundler",
7+
"moduleDetection": "force",
8+
"allowImportingTsExtensions": true,
9+
"strict": true,
10+
"downlevelIteration": true,
11+
"skipLibCheck": true,
12+
"jsx": "preserve",
13+
"allowSyntheticDefaultImports": true,
14+
"forceConsistentCasingInFileNames": true,
15+
"allowJs": true,
16+
"noEmit": true,
17+
"types": [
18+
"bun-types" // add Bun global
19+
]
20+
}
21+
}

ecosystem-tests/cli.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,22 @@ const projects = {
6565
await installPackage();
6666
await run('npm', ['run', 'tsc']);
6767
},
68+
bun: async () => {
69+
if (state.fromNpm) {
70+
await run('bun', ['install', '-D', state.fromNpm]);
71+
return;
72+
}
73+
74+
const packFile = getPackFile();
75+
await fs.copyFile(packFile, `./${TAR_NAME}`);
76+
await run('bun', ['install', '-D', `./${TAR_NAME}`]);
77+
78+
await run('npm', ['run', 'tsc']);
79+
80+
if (state.live) {
81+
await run('bun', ['test']);
82+
}
83+
},
6884
deno: async () => {
6985
await run('deno', ['task', 'install']);
7086
await installPackage();

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "openai",
3-
"version": "4.0.0",
3+
"version": "4.0.1",
44
"description": "Client library for the OpenAI API",
55
"author": "OpenAI <[email protected]>",
66
"types": "dist/index.d.ts",
@@ -86,6 +86,7 @@
8686
"@typescript-eslint/eslint-plugin": "^5.33.0",
8787
"@typescript-eslint/parser": "^5.33.0",
8888
"eslint": "^8.22.0",
89+
"eslint-plugin-prettier": "^4.0.0",
8990
"eslint-plugin-unused-imports": "^2.0.0",
9091
"jest": "^29.4.0",
9192
"openai": "link:.",

src/_shims/formdata.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,15 @@
33
*/
44

55
exports.FormData = FormData;
6-
exports.File = File;
76
exports.Blob = Blob;
7+
exports.File =
8+
typeof File !== 'undefined' ? File : (
9+
// Bun doesn't implement File yet, so just make a shim that throws a helpful error message
10+
class File extends Blob {
11+
constructor() {
12+
throw new Error(`file uploads aren't supported in this environment yet as 'File' is not defined`);
13+
}
14+
}
15+
);
816

917
exports.isPolyfilled = false;

src/_shims/formdata.mjs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,18 @@
33
*/
44

55
const _FormData = FormData;
6-
const _File = File;
76
const _Blob = Blob;
87

8+
const _File =
9+
typeof File !== 'undefined' ? File : (
10+
// Bun doesn't implement File yet, so just make a shim that throws a helpful error message
11+
class File extends Blob {
12+
constructor() {
13+
throw new Error(`file uploads aren't supported in this environment yet as 'File' is not defined`);
14+
}
15+
}
16+
);
17+
918
export { _FormData as FormData, _File as File, _Blob as Blob };
1019

1120
export const isPolyfilled = false;

src/resources/chat/completions.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ export class Completions extends APIResource {
1616
body: CompletionCreateParamsStreaming,
1717
options?: Core.RequestOptions,
1818
): APIPromise<Stream<ChatCompletionChunk>>;
19+
create(
20+
body: CompletionCreateParams,
21+
options?: Core.RequestOptions,
22+
): APIPromise<Stream<ChatCompletionChunk> | ChatCompletion>;
1923
create(
2024
body: CompletionCreateParams,
2125
options?: Core.RequestOptions,

src/resources/completions.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ export class Completions extends APIResource {
1515
body: CompletionCreateParamsStreaming,
1616
options?: Core.RequestOptions,
1717
): APIPromise<Stream<Completion>>;
18+
create(
19+
body: CompletionCreateParams,
20+
options?: Core.RequestOptions,
21+
): APIPromise<Stream<Completion> | Completion>;
1822
create(
1923
body: CompletionCreateParams,
2024
options?: Core.RequestOptions,

src/resources/fine-tunes.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ export class FineTunes extends APIResource {
5757
query: FineTuneListEventsParamsStreaming,
5858
options?: Core.RequestOptions,
5959
): APIPromise<Stream<FineTuneEvent>>;
60+
listEvents(
61+
fineTuneId: string,
62+
query?: FineTuneListEventsParams | undefined,
63+
options?: Core.RequestOptions,
64+
): APIPromise<Stream<FineTuneEvent> | FineTuneEventsListResponse>;
6065
listEvents(
6166
fineTuneId: string,
6267
query?: FineTuneListEventsParams | undefined,

0 commit comments

Comments
 (0)