Skip to content
This repository was archived by the owner on Nov 27, 2025. It is now read-only.
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
7 changes: 7 additions & 0 deletions src/fetch-api.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import ky from 'ky';
import type { Options } from 'ky';
import { OpenAIApiError } from './errors';

const DEFAULT_BASE_URL = 'https://api.openai.com/v1';

export interface FetchOptions extends Options {
credentials?: string;
}

/**
* Create an instance of Ky with options shared by all requests.
*/
export function createApiInstance(opts: {
apiKey: string;
baseUrl?: string;
organizationId?: string;
fetchOptions?: FetchOptions;
}) {
return ky.extend({
prefixUrl: opts.baseUrl || DEFAULT_BASE_URL,
Expand All @@ -21,6 +27,7 @@ export function createApiInstance(opts: {
'OpenAI-Organization': opts.organizationId,
}),
},
...opts.fetchOptions,
hooks: {
beforeError: [
// @ts-ignore
Expand Down
4 changes: 4 additions & 0 deletions src/openai-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {
} from './schemas/completion';
import type { EditParams, EditResponse } from './schemas/edit';
import type { EmbeddingParams, EmbeddingResponse } from './schemas/embedding';
import type { FetchOptions } from './fetch-api';

export type ConfigOpts = {
/**
Expand All @@ -25,6 +26,8 @@ export type ConfigOpts = {
* @see https://beta.openai.com/docs/api-reference/requesting-organization
*/
organizationId?: string;

fetchOptions?: FetchOptions;
};

export class OpenAIClient {
Expand All @@ -39,6 +42,7 @@ export class OpenAIClient {
this.api = createApiInstance({
apiKey,
organizationId: opts.organizationId,
fetchOptions: opts.fetchOptions,
});
}

Expand Down