Skip to content

Commit

Permalink
feat(client): support reading the base url from an env variable (#547)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-bot authored Nov 30, 2023
1 parent 566d290 commit 06fb68d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ export interface ClientOptions {

/**
* Override the default base URL for the API, e.g., "https://api.example.com/v2/"
*
* Defaults to process.env['OPENAI_BASE_URL'].
*/
baseURL?: string;
baseURL?: string | null | undefined;

/**
* The maximum amount of time (in milliseconds) that the client should wait for a response
Expand Down Expand Up @@ -89,9 +91,9 @@ export class OpenAI extends Core.APIClient {
/**
* API Client for interfacing with the OpenAI 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 {string} [opts.apiKey=process.env['OPENAI_API_KEY'] ?? undefined]
* @param {string | null} [opts.organization=process.env['OPENAI_ORG_ID'] ?? null]
* @param {string} [opts.baseURL=process.env['OPENAI_BASE_URL'] ?? https://api.openai.com/v1] - 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.
* @param {Core.Fetch} [opts.fetch] - Specify a custom `fetch` function implementation.
Expand All @@ -101,6 +103,7 @@ export class OpenAI extends Core.APIClient {
* @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.
*/
constructor({
baseURL = Core.readEnv('OPENAI_BASE_URL'),
apiKey = Core.readEnv('OPENAI_API_KEY'),
organization = Core.readEnv('OPENAI_ORG_ID') ?? null,
...opts
Expand All @@ -115,7 +118,7 @@ export class OpenAI extends Core.APIClient {
apiKey,
organization,
...opts,
baseURL: opts.baseURL ?? `https://api.openai.com/v1`,
baseURL: baseURL ?? `https://api.openai.com/v1`,
};

if (!options.dangerouslyAllowBrowser && Core.isRunningInBrowser()) {
Expand Down
15 changes: 15 additions & 0 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,21 @@ describe('instantiate client', () => {
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');
});

afterEach(() => {
process.env['SINK_BASE_URL'] = undefined;
});

test('explicit option', () => {
const client = new OpenAI({ baseURL: 'https://example.com', apiKey: 'My API Key' });
expect(client.baseURL).toEqual('https://example.com');
});

test('env variable', () => {
process.env['OPENAI_BASE_URL'] = 'https://example.com/from_env';
const client = new OpenAI({ apiKey: 'My API Key' });
expect(client.baseURL).toEqual('https://example.com/from_env');
});
});

test('maxRetries option is correctly set', () => {
Expand Down

0 comments on commit 06fb68d

Please sign in to comment.