Skip to content

Commit e8b3cd3

Browse files
committed
v4.0.0-beta.1
1 parent 9d818a1 commit e8b3cd3

File tree

116 files changed

+17232
-863
lines changed

Some content is hidden

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

116 files changed

+17232
-863
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules
22
yarn-error.log
33
dist
4+
/*.tgz

.prettierignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
CHANGELOG.md
2+
/ecosystem-tests
3+
/node_modules

.stats.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
configured_endpoints: 25
1+
configured_endpoints: 23

README.md

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ yarn add openai
2424
```js
2525
import OpenAI from 'openai';
2626

27-
const openAI = new OpenAI({
27+
const openai = new OpenAI({
2828
apiKey: 'my api key', // defaults to process.env["OPENAI_API_KEY"]
2929
});
3030

3131
async function main() {
32-
const completion = await openAI.completions.create({
32+
const completion = await openai.completions.create({
3333
model: 'text-davinci-002',
3434
prompt: 'Say this is a test',
3535
max_tokens: 6,
@@ -71,7 +71,7 @@ If you like, you may reference our types directly:
7171
```ts
7272
import OpenAI from 'openai';
7373

74-
const openAI = new OpenAI({
74+
const openai = new OpenAI({
7575
apiKey: 'my api key', // defaults to process.env["OPENAI_API_KEY"]
7676
});
7777

@@ -82,7 +82,7 @@ async function main() {
8282
max_tokens: 6,
8383
temperature: 0,
8484
};
85-
const completion: OpenAI.Completion = await openAI.completions.create(params);
85+
const completion: OpenAI.Completion = await openai.completions.create(params);
8686
}
8787
main().catch(console.error);
8888
```
@@ -91,17 +91,38 @@ Documentation for each method, request param, and response field are available i
9191

9292
## File Uploads
9393

94-
Request parameters that correspond to file uploads can be passed as either a `FormData.Blob` or a `FormData.File` instance.
94+
Request parameters that correspond to file uploads can be passed in many different forms:
9595

96-
We provide a `fileFromPath` helper function to easily create `FormData.File` instances from a given class.
96+
- `File` (or an object with the same structure)
97+
- a `fetch` `Response` (or an object with the same structure)
98+
- an `fs.ReadStream`
99+
- the return value of our `toFile` helper
97100

98101
```ts
99-
import OpenAI, { fileFromPath } from 'openai';
102+
import fs from 'fs';
103+
import fetch from 'node-fetch';
104+
import OpenAI, { toFile } from 'openai';
100105

101-
const openAI = new OpenAI();
106+
const openai = new OpenAI();
102107

103-
const file = await fileFromPath('input.jsonl');
104-
await openAI.files.create({ file: file, purpose: 'fine-tune' });
108+
// If you have access to Node `fs` we recommend using `fs.createReadStream()`:
109+
await openai.files.create({ file: fs.createReadStream('input.jsonl'), purpose: 'fine-tune' });
110+
111+
// Or if you have the web `File` API you can pass a `File` instance:
112+
await openai.files.create({ file: new File(['my bytes'], 'input.jsonl'), purpose: 'fine-tune' });
113+
114+
// You can also pass a `fetch` `Response`:
115+
await openai.files.create({ file: await fetch('https://somesite/input.jsonl'), purpose: 'fine-tune' });
116+
117+
// Finally, if none of the above are convenient, you can use our `toFile` helper:
118+
await openai.files.create({
119+
file: await toFile(Buffer.from('my bytes'), 'input.jsonl'),
120+
purpose: 'fine-tune',
121+
});
122+
await openai.files.create({
123+
file: await toFile(new Uint8Array([0, 1, 2]), 'input.jsonl'),
124+
purpose: 'fine-tune',
125+
});
105126
```
106127

107128
## Handling errors
@@ -112,7 +133,7 @@ a subclass of `APIError` will be thrown:
112133

113134
```ts
114135
async function main() {
115-
const fineTune = await openAI.fineTunes
136+
const fineTune = await openai.fineTunes
116137
.create({ training_file: 'file-XGinujblHPwGLSztz8cPS8XY' })
117138
.catch((err) => {
118139
if (err instanceof OpenAI.APIError) {
@@ -150,12 +171,12 @@ You can use the `maxRetries` option to configure or disable this:
150171
<!-- prettier-ignore -->
151172
```js
152173
// Configure the default for all requests:
153-
const openAI = new OpenAI({
174+
const openai = new OpenAI({
154175
maxRetries: 0, // default is 2
155176
});
156177

157178
// Or, configure per-request:
158-
openAI.embeddings.create({ model: 'text-similarity-babbage-001',input: 'The food was delicious and the waiter...' }, {
179+
openai.embeddings.create({ model: 'text-similarity-babbage-001',input: 'The food was delicious and the waiter...' }, {
159180
maxRetries: 5,
160181
});
161182
```
@@ -167,12 +188,12 @@ Requests time out after 60 seconds by default. You can configure this with a `ti
167188
<!-- prettier-ignore -->
168189
```ts
169190
// Configure the default for all requests:
170-
const openAI = new OpenAI({
191+
const openai = new OpenAI({
171192
timeout: 20 * 1000, // 20 seconds (default is 60s)
172193
});
173194

174195
// Override per-request:
175-
openAI.edits.create({ model: 'text-davinci-edit-001',input: 'What day of the wek is it?',instruction: 'Fix the spelling mistakes' }, {
196+
openai.edits.create({ model: 'text-davinci-edit-001',input: 'What day of the wek is it?',instruction: 'Fix the spelling mistakes' }, {
176197
timeout: 5 * 1000,
177198
});
178199
```
@@ -193,12 +214,12 @@ import http from 'http';
193214
import HttpsProxyAgent from 'https-proxy-agent';
194215

195216
// Configure the default for all requests:
196-
const openAI = new OpenAI({
217+
const openai = new OpenAI({
197218
httpAgent: new HttpsProxyAgent(process.env.PROXY_URL),
198219
});
199220

200221
// Override per-request:
201-
openAI.models.list({
222+
openai.models.list({
202223
baseURL: 'http://localhost:8080/test-api',
203224
httpAgent: new http.Agent({ keepAlive: false }),
204225
})
@@ -216,7 +237,7 @@ We are keen for your feedback; please open an [issue](https://www.github.com/ope
216237

217238
The following runtimes are supported:
218239

219-
- Node.js version 12 or higher.
240+
- Node.js 16 LTS or later ([non-EOL](https://endoflife.date/nodejs)) versions.
220241
- Deno v1.28.0 or higher (experimental).
221242
Use `import OpenAI from "npm:openai"`.
222243

_shims/agent.node.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Disclaimer: modules in _shims aren't intended to be imported by SDK users.
3+
*/
4+
5+
import KeepAliveAgent from 'agentkeepalive';
6+
import type { Agent } from 'node:http';
7+
import { AbortController as AbortControllerPolyfill } from 'abort-controller';
8+
9+
const defaultHttpAgent: Agent = new KeepAliveAgent({ keepAlive: true, timeout: 5 * 60 * 1000 });
10+
const defaultHttpsAgent: Agent = new KeepAliveAgent.HttpsAgent({ keepAlive: true, timeout: 5 * 60 * 1000 });
11+
12+
// Polyfill global object if needed.
13+
if (typeof AbortController === 'undefined') {
14+
AbortController = AbortControllerPolyfill as any as typeof AbortController;
15+
}
16+
17+
export const getDefaultAgent = (url: string): Agent | undefined => {
18+
if (defaultHttpsAgent && url.startsWith('https')) return defaultHttpsAgent;
19+
return defaultHttpAgent;
20+
};

_shims/agent.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Disclaimer: modules in _shims aren't intended to be imported by SDK users.
3+
*
4+
* This is a stub for non-node environments.
5+
* In node environments, it gets replaced agent.node.ts by the package export map
6+
*/
7+
8+
import type { Agent } from 'node:http';
9+
10+
export const getDefaultAgent = (url: string): Agent | undefined => {
11+
return undefined;
12+
};

_shims/fetch.node.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* Disclaimer: modules in _shims aren't intended to be imported by SDK users.
3+
*/
4+
5+
import fetch, { Request, Response, Headers } from 'node-fetch';
6+
import type { RequestInfo, RequestInit, BodyInit } from 'node-fetch';
7+
8+
export { fetch, Request, Response, Headers };
9+
export type { RequestInfo, RequestInit, BodyInit };
10+
11+
export const isPolyfilled = true;

_shims/fetch.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Disclaimer: modules in _shims aren't intended to be imported by SDK users.
3+
*/
4+
5+
import type * as nf from 'node-fetch';
6+
7+
const _fetch: typeof nf.default =
8+
// @ts-ignore
9+
fetch as any;
10+
type _fetch = typeof nf.default;
11+
const _Request: typeof nf.Request =
12+
// @ts-ignore
13+
Request as any;
14+
type _Request = nf.Request;
15+
const _Response: typeof nf.Response =
16+
// @ts-ignore
17+
Response as any;
18+
type _Response = nf.Response;
19+
const _Headers: typeof nf.Headers =
20+
// @ts-ignore
21+
Headers as any;
22+
type _Headers = nf.Headers;
23+
24+
export const isPolyfilled = false;
25+
26+
export { _fetch as fetch, _Request as Request, _Response as Response, _Headers as Headers };
27+
28+
type _RequestInfo = nf.RequestInfo;
29+
type _RequestInit = nf.RequestInit;
30+
type _BodyInit = nf.BodyInit;
31+
32+
export type { _RequestInit as RequestInit, _RequestInfo as RequestInfo, _BodyInit as BodyInit };

_shims/fileFromPath.node.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Disclaimer: modules in _shims aren't intended to be imported by SDK users.
3+
*/
4+
5+
import type { FilePropertyBag } from 'formdata-node';
6+
import { fileFromPath as _fileFromPath } from 'formdata-node/file-from-path';
7+
import type { File } from './formdata.node';
8+
9+
export type FileFromPathOptions = Omit<FilePropertyBag, 'lastModified'>;
10+
11+
let warned = false;
12+
13+
/**
14+
* @deprecated use fs.createReadStream('./my/file.txt') instead
15+
*/
16+
export async function fileFromPath(path: string): Promise<File>;
17+
export async function fileFromPath(path: string, filename?: string): Promise<File>;
18+
export async function fileFromPath(path: string, options?: FileFromPathOptions): Promise<File>;
19+
export async function fileFromPath(
20+
path: string,
21+
filename?: string,
22+
options?: FileFromPathOptions,
23+
): Promise<File>;
24+
export async function fileFromPath(path: string, ...args: any[]): Promise<File> {
25+
if (!warned) {
26+
console.warn(`fileFromPath is deprecated; use fs.createReadStream(${JSON.stringify(path)}) instead`);
27+
warned = true;
28+
}
29+
return await _fileFromPath(path, ...args);
30+
}

_shims/fileFromPath.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Disclaimer: modules in _shims aren't intended to be imported by SDK users.
3+
*
4+
* This is a stub that gets replaced by fileFromPath.node.js for node environments
5+
* in the package export map
6+
*/
7+
8+
import type { FilePropertyBag } from 'formdata-node';
9+
import type { File } from './formdata';
10+
11+
export type FileFromPathOptions = Omit<FilePropertyBag, 'lastModified'>;
12+
13+
/**
14+
* This is a stub for non-node environments that just throws an error.
15+
* In node environments, this module will be replaced by util/node/fileFromPath by the
16+
* package import map.
17+
*/
18+
export async function fileFromPath(path: string): Promise<File>;
19+
export async function fileFromPath(path: string, filename?: string): Promise<File>;
20+
export async function fileFromPath(path: string, options?: FileFromPathOptions): Promise<File>;
21+
export async function fileFromPath(
22+
path: string,
23+
filename?: string,
24+
options?: FileFromPathOptions,
25+
): Promise<File>;
26+
export async function fileFromPath(): Promise<File> {
27+
throw new Error(
28+
'The `fileFromPath` function is only supported in Node. See the README for more details: https://www.github.com/openai/openai-node#file-uploads',
29+
);
30+
}

0 commit comments

Comments
 (0)