Skip to content

Commit 28276d6

Browse files
committed
v4.0.0-beta.9
1 parent 61cd8e1 commit 28276d6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+5929
-18649
lines changed

.github/ISSUE_TEMPLATE/bug_report.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: Bug report
22
description: Create a report to help us improve
3-
labels: ["bug"]
3+
labels: ['bug']
44
body:
55
- type: markdown
66
attributes:
@@ -53,4 +53,4 @@ body:
5353
label: Library version
5454
placeholder: openai v3.0.1
5555
validations:
56-
required: true
56+
required: true

.github/ISSUE_TEMPLATE/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ contact_links:
44
url: https://help.openai.com/
55
about: |
66
Please only file issues here that you believe represent actual bugs or feature requests for the OpenAI Node library.
7-
If you're having general trouble with the OpenAI API, please visit our help center to get support.
7+
If you're having general trouble with the OpenAI API, please visit our help center to get support.

.github/ISSUE_TEMPLATE/feature_request.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: Feature request
22
description: Suggest an idea for this library
3-
labels: ["feature-request"]
3+
labels: ['feature-request']
44
body:
55
- type: markdown
66
attributes:
@@ -17,4 +17,4 @@ body:
1717
id: context
1818
attributes:
1919
label: Additional context
20-
description: Add any other context about the feature request here.
20+
description: Add any other context about the feature request here.

.github/workflows/test.yml

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,23 @@ name: Node.js CI
22

33
on:
44
push:
5-
branches: [ master ]
5+
branches: [master]
66
pull_request:
7-
branches: [ master ]
7+
branches: [master]
88

99
jobs:
1010
build:
11-
1211
runs-on: ubuntu-latest
1312

1413
strategy:
1514
matrix:
1615
node-version: [16.x, 18.x]
1716
steps:
18-
- uses: actions/checkout@v3
19-
- name: Use Node.js ${{ matrix.node-version }}
20-
uses: actions/setup-node@v3
21-
with:
22-
node-version: ${{ matrix.node-version }}
23-
cache: 'npm'
24-
- run: npm ci
25-
- run: npm run build
17+
- uses: actions/checkout@v3
18+
- name: Use Node.js ${{ matrix.node-version }}
19+
uses: actions/setup-node@v3
20+
with:
21+
node-version: ${{ matrix.node-version }}
22+
cache: 'npm'
23+
- run: npm ci
24+
- run: npm run build

README.md

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@
22

33
[![NPM version](https://img.shields.io/npm/v/openai.svg)](https://npmjs.org/package/openai)
44

5-
The OpenAI Node library provides convenient access to the OpenAI REST API from applications written in server-side JavaScript.
6-
It includes TypeScript definitions for all request params and response fields.
5+
This library provides convenient access to the OpenAI REST API from TypeScript or JavaScript.
76

8-
> ⚠️ **Important note: this library is meant for server-side usage only, as using it in client-side browser code will expose your secret API key. [See here](https://platform.openai.com/docs/api-reference/authentication) for more details.**
9-
10-
## Documentation
7+
It is generated from our [OpenAPI specification](https://github.com/openai/openai-openapi) with [Stainless](https://stainlessapi.com/).
118

129
To learn how to use the OpenAI API, check out our [API Reference](https://platform.openai.com/docs/api-reference) and [Documentation](https://platform.openai.com/docs).
1310

@@ -21,6 +18,9 @@ yarn add openai
2118

2219
## Usage
2320

21+
> [!IMPORTANT]
22+
> Previous versions of this SDK used a `Configuration` class. See the [v3 to v4 migration guide](https://github.com/openai/openai-node/discussions/217).
23+
2424
```js
2525
import OpenAI from 'openai';
2626

@@ -66,10 +66,9 @@ main();
6666
If you need to cancel a stream, you can `break` from the loop
6767
or call `stream.controller.abort()`.
6868

69-
### Usage with TypeScript
69+
### Request & Response types
7070

71-
Importing, instantiating, and interacting with the library are the same as above.
72-
If you like, you may reference our types directly:
71+
This library includes TypeScript definitions for all request params and response fields. You may import and use them like so:
7372

7473
```ts
7574
import OpenAI from 'openai';
@@ -207,6 +206,30 @@ On timeout, an `APIConnectionTimeoutError` is thrown.
207206

208207
Note that requests which time out will be [retried twice by default](#retries).
209208

209+
## Advanced Usage
210+
211+
### Accessing raw Response data (e.g., headers)
212+
213+
The "raw" `Response` returned by `fetch()` can be accessed through the `.asResponse()` method on the `APIPromise` type that all methods return.
214+
215+
You can also use the `.withResponse()` method to get the raw `Response` along with the parsed data.
216+
217+
```ts
218+
const openai = new OpenAI();
219+
220+
const response = await openai.chat.completions
221+
.create({ messages: [{ role: 'user', content: 'Say this is a test' }], model: 'gpt-3.5-turbo' })
222+
.asResponse();
223+
console.log(response.headers.get('X-My-Header'));
224+
console.log(response.statusText); // access the underlying Response object
225+
226+
const { data: completions, response: raw } = await openai.chat.completions
227+
.create({ messages: [{ role: 'user', content: 'Say this is a test' }], model: 'gpt-3.5-turbo' })
228+
.withResponse();
229+
console.log(raw.headers.get('X-My-Header'));
230+
console.log(completions.choices);
231+
```
232+
210233
## Configuring an HTTP(S) Agent (e.g., for proxies)
211234

212235
By default, this library uses a stable agent for all http/https requests to reuse TCP connections, eliminating many TCP & TLS handshakes and shaving around 100ms off most requests.
@@ -247,7 +270,9 @@ We are keen for your feedback; please open an [issue](https://www.github.com/ope
247270
The following runtimes are supported:
248271

249272
- Node.js 16 LTS or later ([non-EOL](https://endoflife.date/nodejs)) versions.
250-
- Deno v1.28.0 or higher (experimental).
251-
Use `import OpenAI from "npm:openai"`.
273+
- Deno v1.28.0 or higher, using `import OpenAI from "npm:openai"`.
274+
Deno Deploy is not yet supported.
275+
- Cloudflare Workers.
276+
- Vercel Edge Runtime.
252277

253278
If you are interested in other runtime environments, please open or upvote an issue on GitHub.

api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Types:
1717

1818
- <code><a href="./src/resources/chat/completions.ts">ChatCompletion</a></code>
1919
- <code><a href="./src/resources/chat/completions.ts">ChatCompletionChunk</a></code>
20+
- <code><a href="./src/resources/chat/completions.ts">ChatCompletionMessage</a></code>
2021
- <code><a href="./src/resources/chat/completions.ts">CreateChatCompletionRequestMessage</a></code>
2122

2223
Methods:

bin/cli

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env bash
2+
set -eou pipefail
3+
4+
if [ $# -eq 0 ]; then
5+
echo "Usage: $0 <subcommand>"
6+
echo
7+
echo "Subcommands:"
8+
echo " migrate Run migrations to update from openai v3 to v4"
9+
echo
10+
exit 1
11+
fi
12+
13+
if [ "$1" = "migrate" ]; then
14+
echo "This automatic code migration is provided by grit.io"
15+
echo "Visit https://app.grit.io/studio?preset=openai_v4 for more details."
16+
shift
17+
npx -y @getgrit/launcher apply openai_v4 "$@"
18+
else
19+
echo "Unknown subcommand $1; Expected 'migrate'" >&2
20+
exit 1
21+
fi

build

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ cp -rp src README.md dist
1515
for file in LICENSE CHANGELOG.md; do
1616
if [ -e "${file}" ]; then cp "${file}" dist; fi
1717
done
18+
if [ -e "bin/cli" ]; then
19+
mkdir dist/bin
20+
cp -p "bin/cli" dist/bin/;
21+
fi
1822
# this converts the export map paths for the dist directory
1923
# and does a few other minor things
2024
node scripts/make-dist-package-json.cjs > dist/package.json

ecosystem-tests/cloudflare-worker/package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ecosystem-tests/cloudflare-worker/src/uploadWebApiTestCases.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,19 @@ export function uploadWebApiTestCases({
4545
await client.audio.transcriptions.create({ file: 'test', model: 'whisper-1' });
4646
}
4747

48+
it(`streaming works`, async function () {
49+
const stream = await client.chat.completions.create({
50+
model: 'gpt-4',
51+
messages: [{ role: 'user', content: 'Say this is a test' }],
52+
stream: true,
53+
});
54+
const chunks = [];
55+
for await (const part of stream) {
56+
chunks.push(part);
57+
}
58+
expectSimilar(chunks.map((c) => c.choices[0]?.delta.content || '').join(''), 'This is a test', 10);
59+
});
60+
4861
it('handles File', async () => {
4962
const file = await fetch(url)
5063
.then((x) => x.arrayBuffer())

ecosystem-tests/deno/main_test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,19 @@ function assertSimilar(received: string, expected: string, maxDistance: number)
3636
throw new AssertionError(message);
3737
}
3838

39+
Deno.test(async function streamingWorks() {
40+
const stream = await client.chat.completions.create({
41+
model: 'gpt-4',
42+
messages: [{ role: 'user', content: 'Say this is a test' }],
43+
stream: true,
44+
});
45+
const chunks = [];
46+
for await (const part of stream) {
47+
chunks.push(part);
48+
}
49+
assertSimilar(chunks.map((c) => c.choices[0]?.delta.content || '').join(''), 'This is a test', 10);
50+
});
51+
3952
Deno.test(async function handlesFile() {
4053
const file = await fetch(url)
4154
.then((x) => x.arrayBuffer())

ecosystem-tests/node-ts-cjs-dom/package-lock.json

Lines changed: 7 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ecosystem-tests/node-ts-cjs-dom/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"dependencies": {
1111
"formdata-node": "^4.4.1",
1212
"node-fetch": "^2.6.1",
13-
"tsconfig-paths": "^3.12.0"
13+
"tsconfig-paths": "^4.0.0"
1414
},
1515
"devDependencies": {
1616
"@types/node": "^17.0.9",

ecosystem-tests/node-ts-cjs-dom/tests/test.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { TranscriptionCreateParams } from 'openai/resources/audio/transcriptions
33
import fetch from 'node-fetch';
44
import { File as FormDataFile, Blob as FormDataBlob } from 'formdata-node';
55
import * as fs from 'fs';
6-
import { distance } from 'fastest-levenshtein'
6+
import { distance } from 'fastest-levenshtein';
77

88
const url = 'https://audio-samples.github.io/samples/mp3/blizzard_biased/sample-1.mp3';
99
const filename = 'sample-1.mp3';
@@ -52,7 +52,20 @@ expect.extend({
5252
message,
5353
pass: false,
5454
};
55+
},
56+
});
57+
58+
it(`streaming works`, async function () {
59+
const stream = await client.chat.completions.create({
60+
model: 'gpt-4',
61+
messages: [{ role: 'user', content: 'Say this is a test' }],
62+
stream: true,
63+
});
64+
const chunks = [];
65+
for await (const part of stream) {
66+
chunks.push(part);
5567
}
68+
expect(chunks.map((c) => c.choices[0]?.delta.content || '').join('')).toBeSimilarTo('This is a test', 10);
5669
});
5770

5871
it('handles formdata-node File', async function () {

0 commit comments

Comments
 (0)