Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(client): support reading the base url from an env variable #547

Merged
merged 1 commit into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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