diff --git a/README.md b/README.md index 856f3e171..3ebda69ca 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ The full API of this library can be found in [api.md file](https://github.com/op import OpenAI from 'openai'; const openai = new OpenAI({ - apiKey: 'my api key', // defaults to process.env["OPENAI_API_KEY"] + apiKey: 'My API Key', // defaults to process.env["OPENAI_API_KEY"] }); async function main() { @@ -73,7 +73,7 @@ This library includes TypeScript definitions for all request params and response import OpenAI from 'openai'; const openai = new OpenAI({ - apiKey: 'my api key', // defaults to process.env["OPENAI_API_KEY"] + apiKey: 'My API Key', // defaults to process.env["OPENAI_API_KEY"] }); async function main() { diff --git a/src/index.ts b/src/index.ts index 05f41b882..4c6d0ba7a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -9,10 +9,15 @@ import * as API from 'openai/resources/index'; export interface ClientOptions { /** - * Defaults to process.env["OPENAI_API_KEY"]. + * Defaults to process.env['OPENAI_API_KEY']. */ apiKey?: string; + /** + * Defaults to process.env['OPENAI_ORG_ID']. + */ + organization?: string | null; + /** * Override the default base URL for the API, e.g., "https://api.example.com/v2/" */ @@ -72,21 +77,20 @@ export interface ClientOptions { * Only set this option to `true` if you understand the risks and have appropriate mitigations in place. */ dangerouslyAllowBrowser?: boolean; - - organization?: string | null; } /** API Client for interfacing with the OpenAI API. */ export class OpenAI extends Core.APIClient { apiKey: string; - organization?: string | null; + organization: string | null; private _options: ClientOptions; /** * API Client for interfacing with the OpenAI API. * - * @param {string} [opts.apiKey=process.env['OPENAI_API_KEY']] - The API Key to send to the API. + * @param {string} [opts.apiKey==process.env['OPENAI_API_KEY'] ?? undefined] + * @param {string | null} [opts.organization==process.env['OPENAI_ORG_ID'] ?? null] * @param {string} [opts.baseURL] - Override the default base URL for the API. * @param {number} [opts.timeout=10 minutes] - The maximum amount of time (in milliseconds) the client will wait for a response before timing out. * @param {number} [opts.httpAgent] - An HTTP agent used to manage HTTP(s) connections. @@ -95,7 +99,6 @@ export class OpenAI extends Core.APIClient { * @param {Core.Headers} opts.defaultHeaders - Default headers to include with every request to the API. * @param {Core.DefaultQuery} opts.defaultQuery - Default query parameters to include with every request to the API. * @param {boolean} [opts.dangerouslyAllowBrowser=false] - By default, client-side use of this library is not allowed, as it risks exposing your secret API credentials to attackers. - * @param {string | null} [opts.organization] */ constructor({ apiKey = Core.readEnv('OPENAI_API_KEY'), @@ -104,7 +107,7 @@ export class OpenAI extends Core.APIClient { }: ClientOptions = {}) { if (apiKey === undefined) { throw new Errors.OpenAIError( - "The OPENAI_API_KEY environment variable is missing or empty; either provide it, or instantiate the OpenAI client with an apiKey option, like new OpenAI({ apiKey: 'my apiKey' }).", + "The OPENAI_API_KEY environment variable is missing or empty; either provide it, or instantiate the OpenAI client with an apiKey option, like new OpenAI({ apiKey: 'My API Key' }).", ); } diff --git a/tests/api-resources/audio/transcriptions.test.ts b/tests/api-resources/audio/transcriptions.test.ts index ce0f5e389..cc23c130f 100644 --- a/tests/api-resources/audio/transcriptions.test.ts +++ b/tests/api-resources/audio/transcriptions.test.ts @@ -4,7 +4,7 @@ import OpenAI, { toFile } from 'openai'; import { Response } from 'node-fetch'; const openai = new OpenAI({ - apiKey: 'something1234', + apiKey: 'My API Key', baseURL: process.env['TEST_API_BASE_URL'] ?? 'http://127.0.0.1:4010', }); diff --git a/tests/api-resources/audio/translations.test.ts b/tests/api-resources/audio/translations.test.ts index 903c18909..723625f6e 100644 --- a/tests/api-resources/audio/translations.test.ts +++ b/tests/api-resources/audio/translations.test.ts @@ -4,7 +4,7 @@ import OpenAI, { toFile } from 'openai'; import { Response } from 'node-fetch'; const openai = new OpenAI({ - apiKey: 'something1234', + apiKey: 'My API Key', baseURL: process.env['TEST_API_BASE_URL'] ?? 'http://127.0.0.1:4010', }); diff --git a/tests/api-resources/chat/completions.test.ts b/tests/api-resources/chat/completions.test.ts index 0725211a8..4a5616cae 100644 --- a/tests/api-resources/chat/completions.test.ts +++ b/tests/api-resources/chat/completions.test.ts @@ -4,7 +4,7 @@ import OpenAI from 'openai'; import { Response } from 'node-fetch'; const openai = new OpenAI({ - apiKey: 'something1234', + apiKey: 'My API Key', baseURL: process.env['TEST_API_BASE_URL'] ?? 'http://127.0.0.1:4010', }); diff --git a/tests/api-resources/completions.test.ts b/tests/api-resources/completions.test.ts index c4d09793d..b3f083727 100644 --- a/tests/api-resources/completions.test.ts +++ b/tests/api-resources/completions.test.ts @@ -4,7 +4,7 @@ import OpenAI from 'openai'; import { Response } from 'node-fetch'; const openai = new OpenAI({ - apiKey: 'something1234', + apiKey: 'My API Key', baseURL: process.env['TEST_API_BASE_URL'] ?? 'http://127.0.0.1:4010', }); diff --git a/tests/api-resources/edits.test.ts b/tests/api-resources/edits.test.ts index 8acf37951..add95f051 100644 --- a/tests/api-resources/edits.test.ts +++ b/tests/api-resources/edits.test.ts @@ -4,7 +4,7 @@ import OpenAI from 'openai'; import { Response } from 'node-fetch'; const openai = new OpenAI({ - apiKey: 'something1234', + apiKey: 'My API Key', baseURL: process.env['TEST_API_BASE_URL'] ?? 'http://127.0.0.1:4010', }); diff --git a/tests/api-resources/embeddings.test.ts b/tests/api-resources/embeddings.test.ts index 23a33b30c..1e2af9297 100644 --- a/tests/api-resources/embeddings.test.ts +++ b/tests/api-resources/embeddings.test.ts @@ -4,7 +4,7 @@ import OpenAI from 'openai'; import { Response } from 'node-fetch'; const openai = new OpenAI({ - apiKey: 'something1234', + apiKey: 'My API Key', baseURL: process.env['TEST_API_BASE_URL'] ?? 'http://127.0.0.1:4010', }); diff --git a/tests/api-resources/files.test.ts b/tests/api-resources/files.test.ts index daa70c2ae..dc8a6da00 100644 --- a/tests/api-resources/files.test.ts +++ b/tests/api-resources/files.test.ts @@ -4,7 +4,7 @@ import OpenAI, { toFile } from 'openai'; import { Response } from 'node-fetch'; const openai = new OpenAI({ - apiKey: 'something1234', + apiKey: 'My API Key', baseURL: process.env['TEST_API_BASE_URL'] ?? 'http://127.0.0.1:4010', }); diff --git a/tests/api-resources/fine-tunes.test.ts b/tests/api-resources/fine-tunes.test.ts index 1221b6bc6..c82898ff2 100644 --- a/tests/api-resources/fine-tunes.test.ts +++ b/tests/api-resources/fine-tunes.test.ts @@ -4,7 +4,7 @@ import OpenAI from 'openai'; import { Response } from 'node-fetch'; const openai = new OpenAI({ - apiKey: 'something1234', + apiKey: 'My API Key', baseURL: process.env['TEST_API_BASE_URL'] ?? 'http://127.0.0.1:4010', }); diff --git a/tests/api-resources/fine-tuning/jobs.test.ts b/tests/api-resources/fine-tuning/jobs.test.ts index 6222a6a3e..9bcb4b085 100644 --- a/tests/api-resources/fine-tuning/jobs.test.ts +++ b/tests/api-resources/fine-tuning/jobs.test.ts @@ -4,7 +4,7 @@ import OpenAI from 'openai'; import { Response } from 'node-fetch'; const openai = new OpenAI({ - apiKey: 'something1234', + apiKey: 'My API Key', baseURL: process.env['TEST_API_BASE_URL'] ?? 'http://127.0.0.1:4010', }); diff --git a/tests/api-resources/images.test.ts b/tests/api-resources/images.test.ts index 55a5c22cd..c9291b258 100644 --- a/tests/api-resources/images.test.ts +++ b/tests/api-resources/images.test.ts @@ -4,7 +4,7 @@ import OpenAI, { toFile } from 'openai'; import { Response } from 'node-fetch'; const openai = new OpenAI({ - apiKey: 'something1234', + apiKey: 'My API Key', baseURL: process.env['TEST_API_BASE_URL'] ?? 'http://127.0.0.1:4010', }); diff --git a/tests/api-resources/models.test.ts b/tests/api-resources/models.test.ts index d3c06b8a7..91eb0d055 100644 --- a/tests/api-resources/models.test.ts +++ b/tests/api-resources/models.test.ts @@ -4,7 +4,7 @@ import OpenAI from 'openai'; import { Response } from 'node-fetch'; const openai = new OpenAI({ - apiKey: 'something1234', + apiKey: 'My API Key', baseURL: process.env['TEST_API_BASE_URL'] ?? 'http://127.0.0.1:4010', }); diff --git a/tests/api-resources/moderations.test.ts b/tests/api-resources/moderations.test.ts index 798710ce7..ad315df5d 100644 --- a/tests/api-resources/moderations.test.ts +++ b/tests/api-resources/moderations.test.ts @@ -4,7 +4,7 @@ import OpenAI from 'openai'; import { Response } from 'node-fetch'; const openai = new OpenAI({ - apiKey: 'something1234', + apiKey: 'My API Key', baseURL: process.env['TEST_API_BASE_URL'] ?? 'http://127.0.0.1:4010', }); diff --git a/tests/index.test.ts b/tests/index.test.ts index bbb4b2135..192d26c6c 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -23,7 +23,7 @@ describe('instantiate client', () => { const client = new OpenAI({ baseURL: 'http://localhost:5000/', defaultHeaders: { 'X-My-Default-Header': '2' }, - apiKey: 'my api key', + apiKey: 'My API Key', }); test('they are used in the request', () => { @@ -55,7 +55,7 @@ describe('instantiate client', () => { const client = new OpenAI({ baseURL: 'http://localhost:5000/', defaultQuery: { apiVersion: 'foo' }, - apiKey: 'my api key', + apiKey: 'My API Key', }); expect(client.buildURL('/foo', null)).toEqual('http://localhost:5000/foo?apiVersion=foo'); }); @@ -64,7 +64,7 @@ describe('instantiate client', () => { const client = new OpenAI({ baseURL: 'http://localhost:5000/', defaultQuery: { apiVersion: 'foo', hello: 'world' }, - apiKey: 'my api key', + apiKey: 'My API Key', }); expect(client.buildURL('/foo', null)).toEqual('http://localhost:5000/foo?apiVersion=foo&hello=world'); }); @@ -73,7 +73,7 @@ describe('instantiate client', () => { const client = new OpenAI({ baseURL: 'http://localhost:5000/', defaultQuery: { hello: 'world' }, - apiKey: 'my api key', + apiKey: 'My API Key', }); expect(client.buildURL('/foo', { hello: undefined })).toEqual('http://localhost:5000/foo'); }); @@ -82,7 +82,7 @@ describe('instantiate client', () => { test('custom fetch', async () => { const client = new OpenAI({ baseURL: 'http://localhost:5000/', - apiKey: 'my api key', + apiKey: 'My API Key', fetch: (url) => { return Promise.resolve( new Response(JSON.stringify({ url, custom: true }), { @@ -99,7 +99,7 @@ describe('instantiate client', () => { test('custom signal', async () => { const client = new OpenAI({ baseURL: process.env['TEST_API_BASE_URL'] ?? 'http://127.0.0.1:4010', - apiKey: 'my api key', + apiKey: 'My API Key', fetch: (...args) => { return new Promise((resolve, reject) => setTimeout( @@ -124,57 +124,42 @@ describe('instantiate client', () => { describe('baseUrl', () => { test('trailing slash', () => { - const client = new OpenAI({ baseURL: 'http://localhost:5000/custom/path/', apiKey: 'my api key' }); + const client = new OpenAI({ baseURL: 'http://localhost:5000/custom/path/', apiKey: 'My API Key' }); expect(client.buildURL('/foo', null)).toEqual('http://localhost:5000/custom/path/foo'); }); test('no trailing slash', () => { - const client = new OpenAI({ baseURL: 'http://localhost:5000/custom/path', apiKey: 'my api key' }); + const client = new OpenAI({ baseURL: 'http://localhost:5000/custom/path', apiKey: 'My API Key' }); expect(client.buildURL('/foo', null)).toEqual('http://localhost:5000/custom/path/foo'); }); }); test('maxRetries option is correctly set', () => { - const client = new OpenAI({ maxRetries: 1, apiKey: 'my api key' }); + const client = new OpenAI({ maxRetries: 1, apiKey: 'My API Key' }); expect(client.maxRetries).toEqual(1); // default - const client2 = new OpenAI({ apiKey: 'my api key' }); + const client2 = new OpenAI({ apiKey: 'My API Key' }); expect(client2.maxRetries).toEqual(2); }); - test('with minimal arguments', () => { - // set API Key via env var - process.env['OPENAI_API_KEY'] = 'env var api key'; + test('with environment variable arguments', () => { + // set options via env var + process.env['OPENAI_API_KEY'] = 'My API Key'; const client = new OpenAI(); - expect(client.apiKey).toBe('env var api key'); + expect(client.apiKey).toBe('My API Key'); }); - test('with apiKey argument', () => { - process.env['OPENAI_API_KEY'] = 'env var api key'; - - const client = new OpenAI({ apiKey: 'another api key' }); - expect(client.apiKey).toBe('another api key'); - }); - - test('with options argument', () => { - process.env['OPENAI_API_KEY'] = 'env var api key'; - - // apiKey - const client = new OpenAI({ apiKey: 'my api key' }); - expect(client.apiKey).toBe('my api key'); - }); - - test('with disabled authentication', () => { - // fails if no API Key provided - expect(() => { - new OpenAI(); - }).toThrow(); + test('with overriden environment variable arguments', () => { + // set options via env var + process.env['OPENAI_API_KEY'] = 'another My API Key'; + const client = new OpenAI({ apiKey: 'My API Key' }); + expect(client.apiKey).toBe('My API Key'); }); }); describe('request building', () => { - const client = new OpenAI({ apiKey: 'my api key' }); + const client = new OpenAI({ apiKey: 'My API Key' }); describe('Content-Length', () => { test('handles multi-byte characters', () => { @@ -200,7 +185,7 @@ describe('retries', () => { return new Response(JSON.stringify({ a: 1 }), { headers: { 'Content-Type': 'application/json' } }); }; - const client = new OpenAI({ apiKey: 'my api key', timeout: 2000, fetch: testFetch }); + const client = new OpenAI({ apiKey: 'My API Key', timeout: 2000, fetch: testFetch }); expect(await client.request({ path: '/foo', method: 'get' })).toEqual({ a: 1 }); expect(count).toEqual(2);