kopia lustrzana https://github.com/transitive-bullshit/chatgpt-api
feat: exports init options for external use
rodzic
e7f24b8c3d
commit
1a0b570dc9
|
@ -7,6 +7,7 @@ import * as tokenizer from './tokenizer'
|
||||||
import * as types from './types'
|
import * as types from './types'
|
||||||
import { fetch as globalFetch } from './fetch'
|
import { fetch as globalFetch } from './fetch'
|
||||||
import { fetchSSE } from './fetch-sse'
|
import { fetchSSE } from './fetch-sse'
|
||||||
|
import { ChatGPTAPIOptions } from './types'
|
||||||
|
|
||||||
const CHATGPT_MODEL = 'gpt-3.5-turbo'
|
const CHATGPT_MODEL = 'gpt-3.5-turbo'
|
||||||
|
|
||||||
|
@ -46,47 +47,19 @@ export class ChatGPTAPI {
|
||||||
* @param upsertMessage - Optional function to insert or update a message. If not provided, the default implementation will be used (using an in-memory `messageStore`).
|
* @param upsertMessage - Optional function to insert or update a message. If not provided, the default implementation will be used (using an in-memory `messageStore`).
|
||||||
* @param fetch - Optional override for the `fetch` implementation to use. Defaults to the global `fetch` function.
|
* @param fetch - Optional override for the `fetch` implementation to use. Defaults to the global `fetch` function.
|
||||||
*/
|
*/
|
||||||
constructor(opts: {
|
constructor({
|
||||||
apiKey: string
|
apiKey,
|
||||||
|
apiBaseUrl = 'https://api.openai.com',
|
||||||
/** @defaultValue `'https://api.openai.com'` **/
|
debug = false,
|
||||||
apiBaseUrl?: string
|
messageStore,
|
||||||
|
completionParams,
|
||||||
/** @defaultValue `false` **/
|
systemMessage,
|
||||||
debug?: boolean
|
maxModelTokens = 4096,
|
||||||
|
maxResponseTokens = 1000,
|
||||||
completionParams?: Partial<
|
getMessageById,
|
||||||
Omit<types.openai.CreateChatCompletionRequest, 'messages' | 'n'>
|
upsertMessage,
|
||||||
>
|
fetch = globalFetch
|
||||||
|
}: ChatGPTAPIOptions) {
|
||||||
systemMessage?: string
|
|
||||||
|
|
||||||
/** @defaultValue `4096` **/
|
|
||||||
maxModelTokens?: number
|
|
||||||
|
|
||||||
/** @defaultValue `1000` **/
|
|
||||||
maxResponseTokens?: number
|
|
||||||
|
|
||||||
messageStore?: Keyv
|
|
||||||
getMessageById?: types.GetMessageByIdFunction
|
|
||||||
upsertMessage?: types.UpsertMessageFunction
|
|
||||||
|
|
||||||
fetch?: types.FetchFn
|
|
||||||
}) {
|
|
||||||
const {
|
|
||||||
apiKey,
|
|
||||||
apiBaseUrl = 'https://api.openai.com',
|
|
||||||
debug = false,
|
|
||||||
messageStore,
|
|
||||||
completionParams,
|
|
||||||
systemMessage,
|
|
||||||
maxModelTokens = 4096,
|
|
||||||
maxResponseTokens = 1000,
|
|
||||||
getMessageById = this._defaultGetMessageById,
|
|
||||||
upsertMessage = this._defaultUpsertMessage,
|
|
||||||
fetch = globalFetch
|
|
||||||
} = opts
|
|
||||||
|
|
||||||
this._apiKey = apiKey
|
this._apiKey = apiKey
|
||||||
this._apiBaseUrl = apiBaseUrl
|
this._apiBaseUrl = apiBaseUrl
|
||||||
this._debug = !!debug
|
this._debug = !!debug
|
||||||
|
@ -110,8 +83,8 @@ export class ChatGPTAPI {
|
||||||
this._maxModelTokens = maxModelTokens
|
this._maxModelTokens = maxModelTokens
|
||||||
this._maxResponseTokens = maxResponseTokens
|
this._maxResponseTokens = maxResponseTokens
|
||||||
|
|
||||||
this._getMessageById = getMessageById
|
this._getMessageById = getMessageById ?? this._defaultGetMessageById
|
||||||
this._upsertMessage = upsertMessage
|
this._upsertMessage = upsertMessage ?? this._defaultUpsertMessage
|
||||||
|
|
||||||
if (messageStore) {
|
if (messageStore) {
|
||||||
this._messageStore = messageStore
|
this._messageStore = messageStore
|
||||||
|
|
30
src/types.ts
30
src/types.ts
|
@ -1,7 +1,37 @@
|
||||||
|
import Keyv from 'keyv'
|
||||||
|
|
||||||
export type Role = 'user' | 'assistant' | 'system'
|
export type Role = 'user' | 'assistant' | 'system'
|
||||||
|
|
||||||
export type FetchFn = typeof fetch
|
export type FetchFn = typeof fetch
|
||||||
|
|
||||||
|
export type ChatGPTAPIOptions = {
|
||||||
|
apiKey: string
|
||||||
|
|
||||||
|
/** @defaultValue `'https://api.openai.com'` **/
|
||||||
|
apiBaseUrl?: string
|
||||||
|
|
||||||
|
/** @defaultValue `false` **/
|
||||||
|
debug?: boolean
|
||||||
|
|
||||||
|
completionParams?: Partial<
|
||||||
|
Omit<openai.CreateChatCompletionRequest, 'messages' | 'n'>
|
||||||
|
>
|
||||||
|
|
||||||
|
systemMessage?: string
|
||||||
|
|
||||||
|
/** @defaultValue `4096` **/
|
||||||
|
maxModelTokens?: number
|
||||||
|
|
||||||
|
/** @defaultValue `1000` **/
|
||||||
|
maxResponseTokens?: number
|
||||||
|
|
||||||
|
messageStore?: Keyv
|
||||||
|
getMessageById?: GetMessageByIdFunction
|
||||||
|
upsertMessage?: UpsertMessageFunction
|
||||||
|
|
||||||
|
fetch?: FetchFn
|
||||||
|
}
|
||||||
|
|
||||||
export type SendMessageOptions = {
|
export type SendMessageOptions = {
|
||||||
/** The name of a user in a multi-user chat. */
|
/** The name of a user in a multi-user chat. */
|
||||||
name?: string
|
name?: string
|
||||||
|
|
Ładowanie…
Reference in New Issue