Skip to content

Commit 511b8dc

Browse files
authored
Merge pull request #398 from openai/release-please--branches--master--changes--next--components--openai
2 parents 78d0936 + 7cebba4 commit 511b8dc

File tree

8 files changed

+61
-18
lines changed

8 files changed

+61
-18
lines changed

.github/workflows/ci.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: CI
2+
on:
3+
push:
4+
branches:
5+
- master
6+
pull_request:
7+
branches:
8+
- master
9+
10+
jobs:
11+
lint:
12+
name: lint
13+
runs-on: ubuntu-latest
14+
if: github.repository == 'openai/openai-node'
15+
16+
steps:
17+
- uses: actions/checkout@v3
18+
19+
- name: Set up Node
20+
uses: actions/setup-node@v3
21+
with:
22+
node-version: '16'
23+
24+
- name: Install dependencies
25+
run: |
26+
yarn install
27+
28+
- name: Check types
29+
run: |
30+
yarn build

.release-please-manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
".": "4.13.0"
2+
".": "4.14.0"
33
}

CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
# Changelog
22

3+
## 4.14.0 (2023-10-25)
4+
5+
Full Changelog: [v4.13.0...v4.14.0](https://github.com/openai/openai-node/compare/v4.13.0...v4.14.0)
6+
7+
### Features
8+
9+
* **client:** adjust retry behavior to be exponential backoff ([#400](https://github.com/openai/openai-node/issues/400)) ([2bc14ce](https://github.com/openai/openai-node/commit/2bc14ce300ef020bc045199fe3d76dd352d78ef9))
10+
11+
12+
### Chores
13+
14+
* **docs:** update deno version ([#399](https://github.com/openai/openai-node/issues/399)) ([cdee077](https://github.com/openai/openai-node/commit/cdee0770690d4b66b357d970827e9ba1597ffb89))
15+
316
## 4.13.0 (2023-10-22)
417

518
Full Changelog: [v4.12.4...v4.13.0](https://github.com/openai/openai-node/compare/v4.12.4...v4.13.0)

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,14 @@ yarn add openai
1818

1919
You can import in Deno via:
2020

21+
<!-- x-release-please-start-version -->
22+
2123
```ts
22-
import OpenAI from 'https://raw.githubusercontent.com/openai/openai-node/v4.12.4-deno/mod.ts';
24+
import OpenAI from 'https://raw.githubusercontent.com/openai/openai-node/v4.13.0-deno/mod.ts';
2325
```
2426

27+
<!-- x-release-please-end -->
28+
2529
## Usage
2630

2731
The full API of this library can be found in [api.md file](https://github.com/openai/openai-node/blob/master/api.md). The code below shows how to get started using the chat completions API.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "openai",
3-
"version": "4.13.0",
3+
"version": "4.14.0",
44
"description": "Client library for the OpenAI API",
55
"author": "OpenAI <[email protected]>",
66
"types": "dist/index.d.ts",

src/core.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ export {
2727
type Uploadable,
2828
} from './uploads';
2929

30-
const MAX_RETRIES = 2;
31-
3230
export type Fetch = (url: RequestInfo, init?: RequestInit) => Promise<Response>;
3331

3432
type PromiseOrValue<T> = T | Promise<T>;
@@ -162,7 +160,7 @@ export abstract class APIClient {
162160

163161
constructor({
164162
baseURL,
165-
maxRetries,
163+
maxRetries = 2,
166164
timeout = 600000, // 10 minutes
167165
httpAgent,
168166
fetch: overridenFetch,
@@ -174,7 +172,7 @@ export abstract class APIClient {
174172
fetch: Fetch | undefined;
175173
}) {
176174
this.baseURL = baseURL;
177-
this.maxRetries = validatePositiveInteger('maxRetries', maxRetries ?? MAX_RETRIES);
175+
this.maxRetries = validatePositiveInteger('maxRetries', maxRetries);
178176
this.timeout = validatePositiveInteger('timeout', timeout);
179177
this.httpAgent = httpAgent;
180178

@@ -513,8 +511,6 @@ export abstract class APIClient {
513511
retriesRemaining: number,
514512
responseHeaders?: Headers | undefined,
515513
): Promise<APIResponseProps> {
516-
retriesRemaining -= 1;
517-
518514
// About the Retry-After header: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Retry-After
519515
let timeoutMillis: number | undefined;
520516
const retryAfterHeader = responseHeaders?.['retry-after'];
@@ -540,22 +536,22 @@ export abstract class APIClient {
540536
}
541537
await sleep(timeoutMillis);
542538

543-
return this.makeRequest(options, retriesRemaining);
539+
return this.makeRequest(options, retriesRemaining - 1);
544540
}
545541

546542
private calculateDefaultRetryTimeoutMillis(retriesRemaining: number, maxRetries: number): number {
547543
const initialRetryDelay = 0.5;
548-
const maxRetryDelay = 2;
544+
const maxRetryDelay = 8.0;
549545

550546
const numRetries = maxRetries - retriesRemaining;
551547

552548
// Apply exponential backoff, but not more than the max.
553-
const sleepSeconds = Math.min(initialRetryDelay * Math.pow(numRetries - 1, 2), maxRetryDelay);
549+
const sleepSeconds = Math.min(initialRetryDelay * Math.pow(2, numRetries), maxRetryDelay);
554550

555-
// Apply some jitter, plus-or-minus half a second.
556-
const jitter = Math.random() - 0.5;
551+
// Apply some jitter, take up to at most 25 percent of the retry time.
552+
const jitter = 1 - Math.random() * 0.25;
557553

558-
return (sleepSeconds + jitter) * 1000;
554+
return sleepSeconds * jitter * 1000;
559555
}
560556

561557
private getUserAgent(): string {

src/version.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export const VERSION = '4.13.0'; // x-release-please-version
1+
export const VERSION = '4.14.0'; // x-release-please-version

tests/index.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ describe('instantiate client', () => {
135135
});
136136

137137
test('maxRetries option is correctly set', () => {
138-
const client = new OpenAI({ maxRetries: 1, apiKey: 'My API Key' });
139-
expect(client.maxRetries).toEqual(1);
138+
const client = new OpenAI({ maxRetries: 4, apiKey: 'My API Key' });
139+
expect(client.maxRetries).toEqual(4);
140140

141141
// default
142142
const client2 = new OpenAI({ apiKey: 'My API Key' });

0 commit comments

Comments
 (0)