Merge pull request #511 from zhengxs2018/main

pull/504/head^2
Travis Fischer 2023-04-02 11:16:46 -05:00 zatwierdzone przez GitHub
commit 599e197c09
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
4 zmienionych plików z 24 dodań i 0 usunięć

Wyświetl plik

@ -31,6 +31,7 @@ async function main() {
})
.option('-t, --timeout <timeout>', 'Timeout in milliseconds')
.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)', {
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

Wyświetl plik

@ -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

Wyświetl plik

@ -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

Wyświetl plik

@ -10,6 +10,8 @@ export type ChatGPTAPIOptions = {
/** @defaultValue `'https://api.openai.com'` **/
apiBaseUrl?: string
apiOrg: string
/** @defaultValue `false` **/
debug?: boolean