kopia lustrzana https://github.com/transitive-bullshit/chatgpt-api
Merge pull request #511 from zhengxs2018/main
commit
599e197c09
|
@ -31,6 +31,7 @@ async function main() {
|
||||||
})
|
})
|
||||||
.option('-t, --timeout <timeout>', 'Timeout in milliseconds')
|
.option('-t, --timeout <timeout>', 'Timeout in milliseconds')
|
||||||
.option('-k, --apiKey <apiKey>', 'OpenAI API key')
|
.option('-k, --apiKey <apiKey>', 'OpenAI API key')
|
||||||
|
.option('-o, --apiOrg <apiOrg>', 'OpenAI API key')
|
||||||
.option('-m, --model <model>', 'Model (gpt-3.5-turbo, gpt-4)', {
|
.option('-m, --model <model>', 'Model (gpt-3.5-turbo, gpt-4)', {
|
||||||
default: 'gpt-3.5-turbo'
|
default: 'gpt-3.5-turbo'
|
||||||
})
|
})
|
||||||
|
@ -39,6 +40,7 @@ async function main() {
|
||||||
'Unique name for the conversation'
|
'Unique name for the conversation'
|
||||||
)
|
)
|
||||||
.action(async (prompt, options) => {
|
.action(async (prompt, options) => {
|
||||||
|
const apiOrg = options.apiOrg || process.env.OPENAI_API_ORG
|
||||||
const apiKey = options.apiKey || process.env.OPENAI_API_KEY
|
const apiKey = options.apiKey || process.env.OPENAI_API_KEY
|
||||||
if (!apiKey) {
|
if (!apiKey) {
|
||||||
console.error('error: either set OPENAI_API_KEY or use --apiKey\n')
|
console.error('error: either set OPENAI_API_KEY or use --apiKey\n')
|
||||||
|
@ -71,6 +73,7 @@ async function main() {
|
||||||
|
|
||||||
const api = new ChatGPTAPI({
|
const api = new ChatGPTAPI({
|
||||||
apiKey,
|
apiKey,
|
||||||
|
apiOrg,
|
||||||
debug: options.debug,
|
debug: options.debug,
|
||||||
completionParams: {
|
completionParams: {
|
||||||
model
|
model
|
||||||
|
|
|
@ -150,6 +150,7 @@ Options:
|
||||||
-s, --store Enables the local message cache (default: true)
|
-s, --store Enables the local message cache (default: true)
|
||||||
-t, --timeout Timeout in milliseconds
|
-t, --timeout Timeout in milliseconds
|
||||||
-k, --apiKey OpenAI API key
|
-k, --apiKey OpenAI API key
|
||||||
|
-o, --apiOrg OpenAI API organization
|
||||||
-n, --conversationName Unique name for the conversation
|
-n, --conversationName Unique name for the conversation
|
||||||
-h, --help Display this message
|
-h, --help Display this message
|
||||||
-v, --version Display version number
|
-v, --version Display version number
|
||||||
|
|
|
@ -16,6 +16,7 @@ const ASSISTANT_LABEL_DEFAULT = 'ChatGPT'
|
||||||
export class ChatGPTAPI {
|
export class ChatGPTAPI {
|
||||||
protected _apiKey: string
|
protected _apiKey: string
|
||||||
protected _apiBaseUrl: string
|
protected _apiBaseUrl: string
|
||||||
|
protected _apiOrg?: string
|
||||||
protected _debug: boolean
|
protected _debug: boolean
|
||||||
|
|
||||||
protected _systemMessage: string
|
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.
|
* 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 apiKey - OpenAI API key (required).
|
||||||
|
* @param apiOrg - OpenAI API organization (optional).
|
||||||
* @param apiBaseUrl - Optional override for the OpenAI API base URL.
|
* @param apiBaseUrl - Optional override for the OpenAI API base URL.
|
||||||
* @param debug - Optional enables logging debugging info to stdout.
|
* @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.
|
* @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) {
|
constructor(opts: types.ChatGPTAPIOptions) {
|
||||||
const {
|
const {
|
||||||
apiKey,
|
apiKey,
|
||||||
|
apiOrg,
|
||||||
apiBaseUrl = 'https://api.openai.com/v1',
|
apiBaseUrl = 'https://api.openai.com/v1',
|
||||||
debug = false,
|
debug = false,
|
||||||
messageStore,
|
messageStore,
|
||||||
|
@ -62,6 +65,7 @@ export class ChatGPTAPI {
|
||||||
} = opts
|
} = opts
|
||||||
|
|
||||||
this._apiKey = apiKey
|
this._apiKey = apiKey
|
||||||
|
this._apiOrg = apiOrg
|
||||||
this._apiBaseUrl = apiBaseUrl
|
this._apiBaseUrl = apiBaseUrl
|
||||||
this._debug = !!debug
|
this._debug = !!debug
|
||||||
this._fetch = fetch
|
this._fetch = fetch
|
||||||
|
@ -185,6 +189,12 @@ export class ChatGPTAPI {
|
||||||
stream
|
stream
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Support multiple organizations
|
||||||
|
// See https://platform.openai.com/docs/api-reference/authentication
|
||||||
|
if (this._apiOrg) {
|
||||||
|
headers['OpenAI-Organization'] = this._apiOrg
|
||||||
|
}
|
||||||
|
|
||||||
if (this._debug) {
|
if (this._debug) {
|
||||||
console.log(`sendMessage (${numTokens} tokens)`, body)
|
console.log(`sendMessage (${numTokens} tokens)`, body)
|
||||||
}
|
}
|
||||||
|
@ -316,6 +326,14 @@ export class ChatGPTAPI {
|
||||||
this._apiKey = apiKey
|
this._apiKey = apiKey
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get apiOrg(): string {
|
||||||
|
return this._apiOrg
|
||||||
|
}
|
||||||
|
|
||||||
|
set apiOrg(apiOrg: string) {
|
||||||
|
this._apiOrg = apiOrg
|
||||||
|
}
|
||||||
|
|
||||||
protected async _buildMessages(text: string, opts: types.SendMessageOptions) {
|
protected async _buildMessages(text: string, opts: types.SendMessageOptions) {
|
||||||
const { systemMessage = this._systemMessage } = opts
|
const { systemMessage = this._systemMessage } = opts
|
||||||
let { parentMessageId } = opts
|
let { parentMessageId } = opts
|
||||||
|
|
|
@ -10,6 +10,8 @@ export type ChatGPTAPIOptions = {
|
||||||
/** @defaultValue `'https://api.openai.com'` **/
|
/** @defaultValue `'https://api.openai.com'` **/
|
||||||
apiBaseUrl?: string
|
apiBaseUrl?: string
|
||||||
|
|
||||||
|
apiOrg: string
|
||||||
|
|
||||||
/** @defaultValue `false` **/
|
/** @defaultValue `false` **/
|
||||||
debug?: boolean
|
debug?: boolean
|
||||||
|
|
||||||
|
|
Ładowanie…
Reference in New Issue