diff --git a/bin/cli.js b/bin/cli.js index 6589a7e..565818f 100755 --- a/bin/cli.js +++ b/bin/cli.js @@ -31,6 +31,7 @@ async function main() { }) .option('-t, --timeout ', 'Timeout in milliseconds') .option('-k, --apiKey ', 'OpenAI API key') + .option('-o, --apiOrg ', 'OpenAI API key') .option('-m, --model ', 'Model (gpt-3.5-turbo, gpt-4)', { default: 'gpt-3.5-turbo' }) @@ -39,6 +40,7 @@ async function main() { 'Unique name for the conversation' ) .action(async (prompt, options) => { + const apiOrg = options.apiOrg || process.env.OPENAI_API_ORG const apiKey = options.apiKey || process.env.OPENAI_API_KEY if (!apiKey) { console.error('error: either set OPENAI_API_KEY or use --apiKey\n') @@ -71,6 +73,7 @@ async function main() { const api = new ChatGPTAPI({ apiKey, + apiOrg, debug: options.debug, completionParams: { model diff --git a/readme.md b/readme.md index 655c317..1e840c1 100644 --- a/readme.md +++ b/readme.md @@ -150,6 +150,7 @@ Options: -s, --store Enables the local message cache (default: true) -t, --timeout Timeout in milliseconds -k, --apiKey OpenAI API key + -o, --apiOrg OpenAI API organization -n, --conversationName Unique name for the conversation -h, --help Display this message -v, --version Display version number diff --git a/src/chatgpt-api.ts b/src/chatgpt-api.ts index b44cce2..3ba1619 100644 --- a/src/chatgpt-api.ts +++ b/src/chatgpt-api.ts @@ -16,6 +16,7 @@ const ASSISTANT_LABEL_DEFAULT = 'ChatGPT' export class ChatGPTAPI { protected _apiKey: string protected _apiBaseUrl: string + protected _apiOrg?: string protected _debug: boolean protected _systemMessage: string @@ -36,6 +37,7 @@ export class ChatGPTAPI { * Creates a new client wrapper around OpenAI's chat completion API, mimicing the official ChatGPT webapp's functionality as closely as possible. * * @param apiKey - OpenAI API key (required). + * @param apiOrg - OpenAI API organization (optional). * @param apiBaseUrl - Optional override for the OpenAI API base URL. * @param debug - Optional enables logging debugging info to stdout. * @param completionParams - Param overrides to send to the [OpenAI chat completion API](https://platform.openai.com/docs/api-reference/chat/create). Options like `temperature` and `presence_penalty` can be tweaked to change the personality of the assistant. @@ -49,6 +51,7 @@ export class ChatGPTAPI { constructor(opts: types.ChatGPTAPIOptions) { const { apiKey, + apiOrg, apiBaseUrl = 'https://api.openai.com/v1', debug = false, messageStore, @@ -62,6 +65,7 @@ export class ChatGPTAPI { } = opts this._apiKey = apiKey + this._apiOrg = apiOrg this._apiBaseUrl = apiBaseUrl this._debug = !!debug this._fetch = fetch @@ -185,6 +189,12 @@ export class ChatGPTAPI { stream } + // Support multiple organizations + // See https://platform.openai.com/docs/api-reference/authentication + if (this._apiOrg) { + headers['OpenAI-Organization'] = this._apiOrg + } + if (this._debug) { console.log(`sendMessage (${numTokens} tokens)`, body) } @@ -316,6 +326,14 @@ export class ChatGPTAPI { this._apiKey = apiKey } + get apiOrg(): string { + return this._apiOrg + } + + set apiOrg(apiOrg: string) { + this._apiOrg = apiOrg + } + protected async _buildMessages(text: string, opts: types.SendMessageOptions) { const { systemMessage = this._systemMessage } = opts let { parentMessageId } = opts diff --git a/src/types.ts b/src/types.ts index fd23cf7..84fdeea 100644 --- a/src/types.ts +++ b/src/types.ts @@ -10,6 +10,8 @@ export type ChatGPTAPIOptions = { /** @defaultValue `'https://api.openai.com'` **/ apiBaseUrl?: string + apiOrg: string + /** @defaultValue `false` **/ debug?: boolean