Skip to content

Commit

Permalink
v4.0.0-beta.9
Browse files Browse the repository at this point in the history
  • Loading branch information
schnerd committed Aug 11, 2023
1 parent 61cd8e1 commit 28276d6
Show file tree
Hide file tree
Showing 63 changed files with 5,929 additions and 18,649 deletions.
4 changes: 2 additions & 2 deletions .github/ISSUE_TEMPLATE/bug_report.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: Bug report
description: Create a report to help us improve
labels: ["bug"]
labels: ['bug']
body:
- type: markdown
attributes:
Expand Down Expand Up @@ -53,4 +53,4 @@ body:
label: Library version
placeholder: openai v3.0.1
validations:
required: true
required: true
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ contact_links:
url: https://help.openai.com/
about: |
Please only file issues here that you believe represent actual bugs or feature requests for the OpenAI Node library.
If you're having general trouble with the OpenAI API, please visit our help center to get support.
If you're having general trouble with the OpenAI API, please visit our help center to get support.
4 changes: 2 additions & 2 deletions .github/ISSUE_TEMPLATE/feature_request.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: Feature request
description: Suggest an idea for this library
labels: ["feature-request"]
labels: ['feature-request']
body:
- type: markdown
attributes:
Expand All @@ -17,4 +17,4 @@ body:
id: context
attributes:
label: Additional context
description: Add any other context about the feature request here.
description: Add any other context about the feature request here.
21 changes: 10 additions & 11 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,23 @@ name: Node.js CI

on:
push:
branches: [ master ]
branches: [master]
pull_request:
branches: [ master ]
branches: [master]

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [16.x, 18.x]
steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm ci
- run: npm run build
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm ci
- run: npm run build
45 changes: 35 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@

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

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

> ⚠️ **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.**
## Documentation
It is generated from our [OpenAPI specification](https://github.com/openai/openai-openapi) with [Stainless](https://stainlessapi.com/).

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).

Expand All @@ -21,6 +18,9 @@ yarn add openai

## Usage

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

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

### Usage with TypeScript
### Request & Response types

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

```ts
import OpenAI from 'openai';
Expand Down Expand Up @@ -207,6 +206,30 @@ On timeout, an `APIConnectionTimeoutError` is thrown.

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

## Advanced Usage

### Accessing raw Response data (e.g., headers)

The "raw" `Response` returned by `fetch()` can be accessed through the `.asResponse()` method on the `APIPromise` type that all methods return.

You can also use the `.withResponse()` method to get the raw `Response` along with the parsed data.

```ts
const openai = new OpenAI();

const response = await openai.chat.completions
.create({ messages: [{ role: 'user', content: 'Say this is a test' }], model: 'gpt-3.5-turbo' })
.asResponse();
console.log(response.headers.get('X-My-Header'));
console.log(response.statusText); // access the underlying Response object

const { data: completions, response: raw } = await openai.chat.completions
.create({ messages: [{ role: 'user', content: 'Say this is a test' }], model: 'gpt-3.5-turbo' })
.withResponse();
console.log(raw.headers.get('X-My-Header'));
console.log(completions.choices);
```

## Configuring an HTTP(S) Agent (e.g., for proxies)

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.
Expand Down Expand Up @@ -247,7 +270,9 @@ We are keen for your feedback; please open an [issue](https://www.github.com/ope
The following runtimes are supported:

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

If you are interested in other runtime environments, please open or upvote an issue on GitHub.
1 change: 1 addition & 0 deletions api.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Types:

- <code><a href="./src/resources/chat/completions.ts">ChatCompletion</a></code>
- <code><a href="./src/resources/chat/completions.ts">ChatCompletionChunk</a></code>
- <code><a href="./src/resources/chat/completions.ts">ChatCompletionMessage</a></code>
- <code><a href="./src/resources/chat/completions.ts">CreateChatCompletionRequestMessage</a></code>

Methods:
Expand Down
21 changes: 21 additions & 0 deletions bin/cli
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env bash
set -eou pipefail

if [ $# -eq 0 ]; then
echo "Usage: $0 <subcommand>"
echo
echo "Subcommands:"
echo " migrate Run migrations to update from openai v3 to v4"
echo
exit 1
fi

if [ "$1" = "migrate" ]; then
echo "This automatic code migration is provided by grit.io"
echo "Visit https://app.grit.io/studio?preset=openai_v4 for more details."
shift
npx -y @getgrit/launcher apply openai_v4 "$@"
else
echo "Unknown subcommand $1; Expected 'migrate'" >&2
exit 1
fi
4 changes: 4 additions & 0 deletions build
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ cp -rp src README.md dist
for file in LICENSE CHANGELOG.md; do
if [ -e "${file}" ]; then cp "${file}" dist; fi
done
if [ -e "bin/cli" ]; then
mkdir dist/bin
cp -p "bin/cli" dist/bin/;
fi
# this converts the export map paths for the dist directory
# and does a few other minor things
node scripts/make-dist-package-json.cjs > dist/package.json
Expand Down
8 changes: 4 additions & 4 deletions ecosystem-tests/cloudflare-worker/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions ecosystem-tests/cloudflare-worker/src/uploadWebApiTestCases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@ export function uploadWebApiTestCases({
await client.audio.transcriptions.create({ file: 'test', model: 'whisper-1' });
}

it(`streaming works`, async function () {
const stream = await client.chat.completions.create({
model: 'gpt-4',
messages: [{ role: 'user', content: 'Say this is a test' }],
stream: true,
});
const chunks = [];
for await (const part of stream) {
chunks.push(part);
}
expectSimilar(chunks.map((c) => c.choices[0]?.delta.content || '').join(''), 'This is a test', 10);
});

it('handles File', async () => {
const file = await fetch(url)
.then((x) => x.arrayBuffer())
Expand Down
13 changes: 13 additions & 0 deletions ecosystem-tests/deno/main_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,19 @@ function assertSimilar(received: string, expected: string, maxDistance: number)
throw new AssertionError(message);
}

Deno.test(async function streamingWorks() {
const stream = await client.chat.completions.create({
model: 'gpt-4',
messages: [{ role: 'user', content: 'Say this is a test' }],
stream: true,
});
const chunks = [];
for await (const part of stream) {
chunks.push(part);
}
assertSimilar(chunks.map((c) => c.choices[0]?.delta.content || '').join(''), 'This is a test', 10);
});

Deno.test(async function handlesFile() {
const file = await fetch(url)
.then((x) => x.arrayBuffer())
Expand Down
29 changes: 7 additions & 22 deletions ecosystem-tests/node-ts-cjs-dom/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ecosystem-tests/node-ts-cjs-dom/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"dependencies": {
"formdata-node": "^4.4.1",
"node-fetch": "^2.6.1",
"tsconfig-paths": "^3.12.0"
"tsconfig-paths": "^4.0.0"
},
"devDependencies": {
"@types/node": "^17.0.9",
Expand Down
15 changes: 14 additions & 1 deletion ecosystem-tests/node-ts-cjs-dom/tests/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { TranscriptionCreateParams } from 'openai/resources/audio/transcriptions
import fetch from 'node-fetch';
import { File as FormDataFile, Blob as FormDataBlob } from 'formdata-node';
import * as fs from 'fs';
import { distance } from 'fastest-levenshtein'
import { distance } from 'fastest-levenshtein';

const url = 'https://audio-samples.github.io/samples/mp3/blizzard_biased/sample-1.mp3';
const filename = 'sample-1.mp3';
Expand Down Expand Up @@ -52,7 +52,20 @@ expect.extend({
message,
pass: false,
};
},
});

it(`streaming works`, async function () {
const stream = await client.chat.completions.create({
model: 'gpt-4',
messages: [{ role: 'user', content: 'Say this is a test' }],
stream: true,
});
const chunks = [];
for await (const part of stream) {
chunks.push(part);
}
expect(chunks.map((c) => c.choices[0]?.delta.content || '').join('')).toBeSimilarTo('This is a test', 10);
});

it('handles formdata-node File', async function () {
Expand Down
Loading

0 comments on commit 28276d6

Please sign in to comment.