@leikeduntech/leiai-js 2.2.0 → 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/build/index.js CHANGED
@@ -895,4 +895,3 @@ export {
895
895
  ChatGPTUnofficialProxyAPI,
896
896
  openai
897
897
  };
898
- //# sourceMappingURL=index.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leikeduntech/leiai-js",
3
- "version": "2.2.0",
3
+ "version": "2.3.0",
4
4
  "author": "liuhean",
5
5
  "repository": {
6
6
  "type": "git",
@@ -49,7 +49,7 @@
49
49
  "p-timeout": "^6.1.1",
50
50
  "quick-lru": "^6.1.1",
51
51
  "read-pkg-up": "^9.1.0",
52
- "@leikeduntech/spark-nodejs": "^0.2.0",
52
+ "@leikeduntech/spark-nodejs": "0.3.0",
53
53
  "uuid": "^9.0.0"
54
54
  },
55
55
  "devDependencies": {
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/chatgpt-api.ts","../src/tokenizer.ts","../src/types.ts","../src/fetch.ts","../src/fetch-sse.ts","../src/stream-async-iterable.ts","../src/chatgpt-unofficial-proxy-api.ts","../src/utils.ts"],"sourcesContent":["import Keyv from 'keyv'\nimport pTimeout from 'p-timeout'\nimport QuickLRU from 'quick-lru'\nimport { v4 as uuidv4 } from 'uuid'\n\nimport * as tokenizer from './tokenizer'\nimport * as types from './types'\nimport { fetch as globalFetch } from './fetch'\nimport { fetchSSE } from './fetch-sse'\nimport {Spark} from \"@leikeduntech/spark-nodejs\";\n\nconst CHATGPT_MODEL = 'gpt-3.5-turbo'\n\nconst USER_LABEL_DEFAULT = 'User'\nconst ASSISTANT_LABEL_DEFAULT = 'ChatGPT'\n\nexport class ChatGPTAPI {\n protected _manufacturer: string\n protected _apiKey: string\n protected _apiBaseUrl: string\n protected _apiOrg?: string\n protected _debug: boolean\n\n protected _systemMessage: string\n protected _completionParams: Omit<\n types.openai.CreateChatCompletionRequest,\n 'messages' | 'n'\n >\n protected _maxModelTokens: number\n protected _maxResponseTokens: number\n protected _fetch: types.FetchFn\n\n protected _getMessageById: types.GetMessageByIdFunction\n protected _upsertMessage: types.UpsertMessageFunction\n\n protected _messageStore: Keyv<types.ChatMessage>\n\n /**\n * Creates a new client wrapper around OpenAI's chat completion API, mimicing the official ChatGPT webapp's functionality as closely as possible.\n *\n * @param apiKey - OpenAI API key (required).\n * @param apiOrg - Optional OpenAI API organization (optional).\n * @param apiBaseUrl - Optional override for the OpenAI API base URL.\n * @param debug - Optional enables logging debugging info to stdout.\n * @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.\n * @param maxModelTokens - Optional override for the maximum number of tokens allowed by the model's context. Defaults to 4096.\n * @param maxResponseTokens - Optional override for the minimum number of tokens allowed for the model's response. Defaults to 1000.\n * @param messageStore - Optional [Keyv](https://github.com/jaredwray/keyv) store to persist chat messages to. If not provided, messages will be lost when the process exits.\n * @param getMessageById - Optional function to retrieve a message by its ID. If not provided, the default implementation will be used (using an in-memory `messageStore`).\n * @param upsertMessage - Optional function to insert or update a message. If not provided, the default implementation will be used (using an in-memory `messageStore`).\n * @param fetch - Optional override for the `fetch` implementation to use. Defaults to the global `fetch` function.\n */\n constructor(opts: types.ChatGPTAPIOptions) {\n const {\n manufacturer = 'OpenAI',\n apiKey,\n apiOrg,\n apiBaseUrl = 'https://api.openai.com/v1',\n debug = false,\n messageStore,\n completionParams,\n systemMessage,\n maxModelTokens = 4000,\n maxResponseTokens = 2000,\n getMessageById,\n upsertMessage,\n fetch = globalFetch\n } = opts\n\n this._manufacturer = manufacturer\n this._apiKey = apiKey\n this._apiOrg = apiOrg\n this._apiBaseUrl = apiBaseUrl\n this._debug = !!debug\n this._fetch = fetch\n\n this._completionParams = {\n model: CHATGPT_MODEL,\n temperature: ['baidu','zhipu'].indexOf(manufacturer.toLowerCase()) > -1 ? 0.95 : (['xunfei'].indexOf(manufacturer.toLowerCase()) > -1 ? 0.5: 1),\n ...completionParams\n }\n\n this._systemMessage = systemMessage\n\n if (this._systemMessage === undefined) {\n const currentDate = new Date().toISOString().split('T')[0]\n this._systemMessage = `You are ChatGPT, a large language model trained by ${this._manufacturer}. Answer as concisely as possible.\\nKnowledge cutoff: 2021-09-01\\nCurrent date: ${currentDate}`\n }\n\n this._maxModelTokens = maxModelTokens\n this._maxResponseTokens = maxResponseTokens\n\n this._getMessageById = getMessageById ?? this._defaultGetMessageById\n this._upsertMessage = upsertMessage ?? this._defaultUpsertMessage\n\n if (messageStore) {\n this._messageStore = messageStore\n } else {\n this._messageStore = new Keyv<types.ChatMessage, any>({\n store: new QuickLRU<string, types.ChatMessage>({ maxSize: 10000 })\n })\n }\n\n if (!this._apiKey) {\n throw new Error(`${this._manufacturer} missing required apiKey`)\n }\n\n if (!this._fetch) {\n throw new Error('Invalid environment; fetch is not defined')\n }\n\n if (typeof this._fetch !== 'function') {\n throw new Error('Invalid \"fetch\" is not a function')\n }\n }\n\n /**\n * Sends a message to the OpenAI chat completions endpoint, waits for the response\n * to resolve, and returns the response.\n *\n * If you want your response to have historical context, you must provide a valid `parentMessageId`.\n *\n * If you want to receive a stream of partial responses, use `opts.onProgress`.\n *\n * Set `debug: true` in the `ChatGPTAPI` constructor to log more info on the full prompt sent to the OpenAI chat completions API. You can override the `systemMessage` in `opts` to customize the assistant's instructions.\n *\n * @param message - The prompt message to send\n * @param opts.parentMessageId - Optional ID of the previous message in the conversation (defaults to `undefined`)\n * @param opts.conversationId - Optional ID of the conversation (defaults to `undefined`)\n * @param opts.messageId - Optional ID of the message to send (defaults to a random UUID)\n * @param opts.systemMessage - Optional override for the chat \"system message\" which acts as instructions to the model (defaults to the ChatGPT system message)\n * @param opts.timeoutMs - Optional timeout in milliseconds (defaults to no timeout)\n * @param opts.onProgress - Optional callback which will be invoked every time the partial response is updated\n * @param opts.abortSignal - Optional callback used to abort the underlying `fetch` call using an [AbortController](https://developer.mozilla.org/en-US/docs/Web/API/AbortController)\n * @param completionParams - Optional 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.\n *\n * @returns The response from ChatGPT\n */\n async sendMessage(\n text: string,\n opts: types.SendMessageOptions = {}\n ): Promise<types.ChatMessage> {\n const {\n parentMessageId,\n messageId = uuidv4(),\n timeoutMs,\n onProgress,\n stream = onProgress ? true : false,\n completionParams,\n conversationId\n } = opts\n\n let { abortSignal } = opts\n\n let abortController: AbortController = null\n if (timeoutMs && !abortSignal) {\n abortController = new AbortController()\n abortSignal = abortController.signal\n }\n\n const message: types.ChatMessage = {\n role: 'user',\n id: messageId,\n conversationId,\n parentMessageId,\n text\n }\n\n const latestQuestion = message\n\n const { messages, aliyunMessage, maxTokens, numTokens, errorMessage } = await this._buildMessages(\n text,\n opts\n )\n\n if(this._debug) console.log(`typeof errorMessage ${typeof errorMessage},numTokens:${numTokens}, _maxModelTokens:${this._maxModelTokens},errorMessage: ${errorMessage}`)\n\n // 发起请求之前的校验错误拦截\n if (errorMessage !== '' && errorMessage !== null && errorMessage.length > 0) {\n return new Promise((resolve, reject) => {\n const errMsg = {\n manufacturer: this._manufacturer,\n statusCode: 400,\n message: errorMessage\n }\n return reject(errMsg)\n })\n }\n\n const result: types.ChatMessage = {\n role: 'assistant',\n id: uuidv4(),\n conversationId,\n parentMessageId: messageId,\n text: '',\n result:'',\n detail: {usage: null}\n }\n\n const responseP = new Promise<types.ChatMessage>(\n async (resolve, reject) => {\n let url = `${this._apiBaseUrl}/chat/completions`\n const headers = {\n 'Content-Type': 'application/json',\n Authorization: `Bearer ${this._apiKey}`\n }\n if (this._manufacturer.toLowerCase() === 'baidu') {\n url = this._apiBaseUrl\n delete headers.Authorization\n } else if (this._manufacturer.toLowerCase() === 'aliyun') {\n url = this._apiBaseUrl\n if (stream) headers['X-DashScope-SSE'] = 'enable'\n } else if (this._manufacturer.toLowerCase() === 'zhipu') {\n url = this._apiBaseUrl\n headers['Authorization'] = this._apiKey\n if (stream) headers['accept'] = 'text/event-stream'\n } else if (this._manufacturer.toLowerCase() === 'azure') {\n url = this._apiBaseUrl\n headers['api-key'] = this._apiKey\n delete headers.Authorization\n } else if (this._manufacturer.toLowerCase() === 'xunfei') {\n url = ''\n delete headers.Authorization\n }\n\n let body = {\n max_tokens: maxTokens,\n ...this._completionParams,\n ...completionParams,\n messages,\n stream\n }\n if (this._manufacturer.toLowerCase() === 'aliyun') {\n delete body.messages;\n body = Object.assign(body, {input: aliyunMessage})\n } else if (this._manufacturer.toLowerCase() === 'zhipu') {\n delete body.messages;\n body = Object.assign(body, {prompt: messages})\n }\n\n // Support multiple organizations\n // See https://platform.openai.com/docs/api-reference/authentication\n if (this._apiOrg && this._manufacturer.toLowerCase() === 'openai') {\n headers['OpenAI-Organization'] = this._apiOrg\n }\n\n if (this._debug) {\n console.log(`api url (${url}`)\n console.log(`api header (${JSON.stringify(headers)}`)\n console.log(`sendMessage (${numTokens} tokens) body: `, body)\n console.log(`sendMessage (${numTokens} tokens) message : `, aliyunMessage || messages )\n }\n\n if (this._manufacturer.toLowerCase() === 'xunfei') {\n const self = this;\n const keyList = this._apiKey.split('.'); // appId, apiKey, apiSecret\n const options = {\n secret: keyList[2], key: keyList[1], appid: keyList[0],temperature: body?.temperature, useHistory: !!parentMessageId,chatId: ''\n };\n if (message?.conversationId) options.chatId = message?.conversationId\n if (self._debug) console.log('spark options ', options)\n const sparkIns = new Spark(options)\n const url = sparkIns.chat({\n content: messages && JSON.stringify(messages),\n // onData 表示分段拿到返回结果\n onData({content, start, end, seq}){\n // content 表示分段的内容\n // start 表示是否是第一段\n // end 表示是否是最后一段\n // seq 表示序号\n if (self._debug) console.log('onData',content, start, end, seq);\n result.id = `xunfei-${Math.floor(Math.random() * 10000000000000)}`\n result.delta = content\n if (content) result.text += content\n result.role = 'assistant'\n\n onProgress?.(result)\n },\n onEnd({content, tokens, questionTokens}){\n // content 表示完整的返回\n // token 表示返回回答的token数\n // questionTokens 表示发起对话的token数\n if (self._debug) console.log('onEnd',content, tokens, questionTokens);\n result.detail = {usage: {prompt_tokens: questionTokens, completion_tokens: tokens, total_tokens: questionTokens + tokens}}\n result.text = content.trim()\n return resolve(result)\n }\n });\n } else {\n if (stream) {\n fetchSSE(\n url,\n {\n method: 'POST',\n headers,\n body: JSON.stringify(body),\n signal: abortSignal,\n onMessage: (data: string) => {\n if (this._debug) {\n // console.log(typeof data, 'row data: ',data)\n }\n if (data === '[DONE]') {\n result.text = result.text.trim()\n return resolve(result)\n }\n\n try {\n const response: types.openai.CreateChatCompletionDeltaResponse =\n JSON.parse(data)\n\n if (this._manufacturer.toLowerCase() === 'baidu') {\n if (response?.is_end === true) {\n result.text += response.result.trim()\n result.detail = response\n return resolve(result)\n }\n } else if (this._manufacturer.toLowerCase() === 'azure') {\n if (response.choices[0]?.finish_reason === 'stop') {\n result.text = result.text.trim()\n return resolve(result)\n }\n } else if (this._manufacturer.toLowerCase() === 'aliyun') {\n if (response?.output?.finish_reason === 'stop') {\n result.text = response?.output.text.trim()\n return resolve(result)\n }\n } else if (this._manufacturer.toLowerCase() === 'zhipu') {\n if (response?.event === 'finish') {\n result.text += response?.data.trim()\n return resolve(result)\n }\n }\n\n if (this._manufacturer.toLowerCase() === 'aliyun') {\n if (response?.request_id) {\n result.id = response.request_id\n }\n } else {\n if (response?.id) {\n result.id = response.id\n }\n }\n\n if (response.choices?.length && ['openai','azure'].indexOf(this._manufacturer.toLowerCase()) > -1) {\n const delta = response.choices[0].delta\n result.delta = delta.content\n if (delta?.content) result.text += delta.content\n\n if (delta.role) {\n result.role = delta.role\n }\n\n result.detail = response\n onProgress?.(result)\n } else if (response?.result && this._manufacturer.toLowerCase() === 'baidu') {\n result.delta = response.result\n if (response?.result) result.text += response.result\n result.role = 'assistant'\n\n result.detail = response\n onProgress?.(result)\n } else if (response?.output && this._manufacturer.toLowerCase() === 'aliyun') {\n response.usage = Object.assign(response.usage, {prompt_tokens: response.usage?.input_tokens,completion_tokens: response.usage?.output_tokens, total_tokens: response.usage?.input_tokens + response.usage?.output_tokens})\n result.delta = ''\n if (response?.output?.text) result.text = response?.output?.text\n result.role = 'assistant'\n\n result.detail = response\n onProgress?.(result)\n } else if (response?.data && this._manufacturer.toLowerCase() === 'zhipu') {\n if (response.event === 'finish') {\n if (response?.meta?.usage) {\n response.usage = response?.meta?.usage\n } else {\n response.usage = {prompt_tokens: 1, completion_tokens: 1, total_tokens: 2}\n }\n }\n result.delta = response.data\n if (response?.data) result.text += response?.data\n result.role = 'assistant'\n\n result.detail = response\n onProgress?.(result)\n }\n } catch (err) {\n console.warn(`${this._manufacturer} stream SEE event unexpected error`, err)\n return reject(err)\n }\n }\n },\n this._fetch,\n this._manufacturer\n ).catch(reject)\n } else {\n try {\n const res = await this._fetch(url, {\n method: 'POST',\n headers,\n body: JSON.stringify(body),\n signal: abortSignal\n })\n\n if (!res.ok) {\n const reason = await res.text()\n const msg = `${this._manufacturer} error ${\n res.status || res.statusText\n }: ${reason}`\n const error = new types.ChatGPTError(msg, { cause: res })\n error.statusCode = res.status\n error.statusText = res.statusText\n return reject(error)\n }\n\n const response: types.openai.CreateChatCompletionResponse =\n await res.json()\n if (this._debug) {\n console.log(response)\n }\n\n if (this._manufacturer.toLowerCase() === 'aliyun') {\n if (response?.request_id) {\n result.id = response.request_id\n }\n } else {\n if (response?.id) {\n result.id = response.id\n }\n }\n\n if (response?.choices?.length && ['openai','azure'].indexOf(this._manufacturer.toLowerCase()) > -1) {\n const message = response.choices[0].message\n result.text = message.content\n if (message.role) {\n result.role = message.role\n }\n } else if(response?.result && this._manufacturer.toLowerCase() === 'baidu') {\n result.text = response.result\n result.role = 'assistant'\n } else if(response?.output?.text && this._manufacturer.toLowerCase() === 'aliyun') {\n result.text = response?.output?.text\n result.role = 'assistant'\n } else {\n const res = response as any\n return reject(\n new Error(\n `${this._manufacturer} error: ${\n res?.detail?.message || res?.detail?.error_msg || res?.detail || 'unknown'\n }`\n )\n )\n }\n\n result.detail = response\n\n return resolve(result)\n } catch (err) {\n return reject(err)\n }\n }\n }\n }\n ).then(async (message) => {\n if (message.detail && !message.detail.usage) {\n try {\n const promptTokens = numTokens\n let completionTokens = 0;\n if (['baidu','aliyun'].indexOf(this._manufacturer.toLowerCase()) > -1 ) {\n completionTokens = message.detail?.usage?.total_tokens;\n } else {\n completionTokens = await this._getTokenCount(message.text)\n }\n message.detail.usage = {\n prompt_tokens: promptTokens,\n completion_tokens: completionTokens,\n total_tokens: promptTokens + completionTokens,\n estimated: true\n }\n } catch (err) {\n // TODO: this should really never happen, but if it does,\n // we should handle notify the user gracefully\n }\n }\n\n return Promise.all([\n this._upsertMessage(latestQuestion),\n this._upsertMessage(message)\n ]).then(() => message)\n })\n\n if (timeoutMs) {\n if (abortController) {\n // This will be called when a timeout occurs in order for us to forcibly\n // ensure that the underlying HTTP request is aborted.\n ;(responseP as any).cancel = () => {\n abortController.abort()\n }\n }\n\n return pTimeout(responseP, {\n milliseconds: timeoutMs,\n message: `${this._manufacturer} timed out waiting for response`\n })\n } else {\n return responseP\n }\n }\n\n get manufacturer(): string {\n return this._manufacturer\n }\n\n set manufacturer(manufacturer: string) {\n this._manufacturer = manufacturer\n }\n\n get maxModelTokens(): number {\n return this._maxModelTokens\n }\n\n set maxModelTokens(maxModelTokens: number) {\n this._maxModelTokens = maxModelTokens\n }\n\n get maxResponseTokens(): number {\n return this._maxResponseTokens\n }\n\n set maxResponseTokens(maxResponseTokens: number) {\n this._maxResponseTokens = maxResponseTokens\n }\n\n get apiBaseUrl(): string {\n return this._apiBaseUrl\n }\n\n set apiBaseUrl(apiBaseUrl: string) {\n this._apiBaseUrl = apiBaseUrl\n }\n\n get apiKey(): string {\n return this._apiKey\n }\n\n set apiKey(apiKey: string) {\n this._apiKey = apiKey\n }\n\n get apiOrg(): string {\n return this._apiOrg\n }\n\n set apiOrg(apiOrg: string) {\n this._apiOrg = apiOrg\n }\n\n protected async _buildMessages(text: string, opts: types.SendMessageOptions) {\n const { systemMessage = this._systemMessage } = opts\n let { parentMessageId } = opts\n let errorMessage = ''\n\n const userLabel = USER_LABEL_DEFAULT\n const assistantLabel = ASSISTANT_LABEL_DEFAULT\n\n const maxNumTokens = this._maxModelTokens - this._maxResponseTokens\n let messages: types.openai.ChatCompletionRequestMessage[] = []\n\n if (systemMessage && ['openai','azure'].indexOf(this._manufacturer.toLowerCase()) > -1) {\n messages.push({\n role: 'system',\n content: systemMessage\n })\n }\n\n const systemMessageOffset = messages.length\n const userMessage:any = ['baidu','azure','zhipu', 'xunfei'].indexOf(this._manufacturer.toLowerCase()) > -1 ? [{role: 'user',content: text}] : [{role: 'user',content: text,name: opts.name}]\n let nextMessages = text\n ? messages.concat(userMessage)\n : messages\n let numTokens = 0\n\n do {\n const prompt = nextMessages\n .reduce((prompt, message) => {\n switch (message.role) {\n case 'system':\n return prompt.concat([`Instructions:\\n${message.content}`])\n case 'user':\n return prompt.concat([`${userLabel}:\\n${message.content}`])\n default:\n return prompt.concat([`${assistantLabel}:\\n${message.content}`])\n }\n }, [] as string[])\n .join('\\n\\n')\n\n const nextNumTokensEstimate = await this._getTokenCount(prompt)\n const isValidPrompt = nextNumTokensEstimate <= maxNumTokens\n\n if (prompt && !isValidPrompt && ['openai','azure'].indexOf(this._manufacturer.toLowerCase()) > -1) {\n break\n }\n\n messages = nextMessages\n numTokens = nextNumTokensEstimate\n\n if (!isValidPrompt && ['openai','azure'].indexOf(this._manufacturer.toLowerCase()) > -1) {\n break\n }\n\n if (!parentMessageId) {\n break\n }\n\n const parentMessage = await this._getMessageById(parentMessageId)\n if (!parentMessage) {\n break\n }\n\n const parentMessageRole = parentMessage.role || 'user'\n const parentMessageItem:any = ['baidu','azure', 'zhipu', 'xunfei'].indexOf(this._manufacturer.toLowerCase()) > -1 ? {\n role: parentMessageRole,\n content: parentMessage.text\n } : {\n role: parentMessageRole,\n content: parentMessage.text,\n name: parentMessage.name\n }\n\n nextMessages = nextMessages.slice(0, systemMessageOffset).concat([\n parentMessageItem,\n ...nextMessages.slice(systemMessageOffset)\n ])\n\n parentMessageId = parentMessage.parentMessageId\n } while (true)\n\n // Use up to 4096 tokens (prompt + response), but try to leave 1000 tokens\n // for the response.\n let maxTokens = this._maxModelTokens // 只有openai的接口需要,其他随便设一个值\n if (['openai','azure'].indexOf(this._manufacturer.toLowerCase()) > -1) {\n maxTokens = Math.max(\n 1,\n Math.min(this._maxModelTokens - numTokens, this._maxResponseTokens)\n )\n } else if (numTokens > this._maxModelTokens) {\n maxTokens = this._maxModelTokens\n errorMessage = `${this._manufacturer}:当前提问上下文内容长度${numTokens}tokns超长,该模型最大提问长度为${this._maxModelTokens}tokens,请切换其他增强AI模型或减少字数或者关闭上下文历史提高单次提问长度!`\n }\n\n let aliyunMessage:any;\n if (this._manufacturer.toLowerCase() === 'aliyun') {\n aliyunMessage = {prompt: '', history: []}\n let onceMessage = {user: '', bot: ''}\n messages.forEach((item, index) => {\n if (index < messages.length -1 && index > 1) {\n if (!onceMessage.user || !onceMessage.bot) {\n if (item.role === 'user') onceMessage.user = item.content\n if (item.role === 'assistant') onceMessage.bot = item.content\n }\n if (onceMessage.user && onceMessage.bot) {\n aliyunMessage.history.push(onceMessage)\n onceMessage = {user: '', bot: ''}\n }\n } else {\n aliyunMessage.prompt = item.content\n }\n })\n }\n\n return { messages,aliyunMessage, maxTokens, numTokens, errorMessage }\n }\n\n protected async _getTokenCount(text: string) {\n // TODO: use a better fix in the tokenizer\n text = text.replace(/<\\|endoftext\\|>/g, '')\n\n return tokenizer.encode(text).length\n }\n\n protected async _defaultGetMessageById(\n id: string\n ): Promise<types.ChatMessage> {\n const res = await this._messageStore.get(id)\n return res\n }\n\n protected async _defaultUpsertMessage(\n message: types.ChatMessage\n ): Promise<void> {\n await this._messageStore.set(message.id, message)\n }\n}\n","import { getEncoding } from 'js-tiktoken'\n\n// TODO: make this configurable\nconst tokenizer = getEncoding('cl100k_base')\n\nexport function encode(input: string): Uint32Array {\n return new Uint32Array(tokenizer.encode(input))\n}\n","import Keyv from 'keyv'\n\nexport type Role = 'user' | 'assistant' | 'system'\n\nexport type FetchFn = typeof fetch\n\nexport type ChatGPTAPIOptions = {\n /** 模型厂商公司 **/\n manufacturer: string\n\n apiKey: string\n\n /** @defaultValue `'https://api.openai.com'` **/\n apiBaseUrl?: string\n\n apiOrg?: string\n\n /** @defaultValue `false` **/\n debug?: boolean\n\n completionParams?: Partial<\n Omit<openai.CreateChatCompletionRequest, 'messages' | 'n' | 'stream'>\n >\n\n systemMessage?: string\n\n /** @defaultValue `4096` **/\n maxModelTokens?: number\n\n /** @defaultValue `1000` **/\n maxResponseTokens?: number\n\n messageStore?: Keyv\n getMessageById?: GetMessageByIdFunction\n upsertMessage?: UpsertMessageFunction\n\n fetch?: FetchFn\n}\n\nexport type SendMessageOptions = {\n /** The name of a user in a multi-user chat. */\n name?: string\n parentMessageId?: string\n conversationId?: string\n messageId?: string\n stream?: boolean\n systemMessage?: string\n timeoutMs?: number\n onProgress?: (partialResponse: ChatMessage) => void\n abortSignal?: AbortSignal\n completionParams?: Partial<\n Omit<openai.CreateChatCompletionRequest, 'messages' | 'n' | 'stream'>\n >\n}\n\nexport type MessageActionType = 'next' | 'variant'\n\nexport type SendMessageBrowserOptions = {\n conversationId?: string\n parentMessageId?: string\n messageId?: string\n action?: MessageActionType\n timeoutMs?: number\n onProgress?: (partialResponse: ChatMessage) => void\n abortSignal?: AbortSignal\n}\n\nexport interface ChatMessage {\n id?: string\n text?: string\n role?: Role\n name?: string\n delta?: string\n detail?:\n | openai.CreateChatCompletionResponse\n | CreateChatCompletionStreamResponse\n\n // relevant for both ChatGPTAPI and ChatGPTUnofficialProxyAPI\n parentMessageId?: string\n\n // only relevant for ChatGPTUnofficialProxyAPI (optional for ChatGPTAPI)\n conversationId?: string\n object?: string\n created?: number\n sentence_id?: number\n is_end?: boolean\n is_truncated?: boolean\n result?: string\n need_clear_history?: boolean\n usage?: object\n}\n\nexport class ChatGPTError extends Error {\n statusCode?: number\n statusText?: string\n isFinal?: boolean\n accountId?: string\n}\n\n/** Returns a chat message from a store by it's ID (or null if not found). */\nexport type GetMessageByIdFunction = (id: string) => Promise<ChatMessage>\n\n/** Upserts a chat message to a store. */\nexport type UpsertMessageFunction = (message: ChatMessage) => Promise<void>\n\nexport interface CreateChatCompletionStreamResponse\n extends openai.CreateChatCompletionDeltaResponse {\n usage: CreateCompletionStreamResponseUsage\n}\n\nexport interface CreateCompletionStreamResponseUsage\n extends openai.CreateCompletionResponseUsage {\n estimated: true\n}\n\n/**\n * https://chat.openapi.com/backend-api/conversation\n */\nexport type ConversationJSONBody = {\n /**\n * The action to take\n */\n action: string\n\n /**\n * The ID of the conversation\n */\n conversation_id?: string\n\n /**\n * Prompts to provide\n */\n messages: Prompt[]\n\n /**\n * The model to use\n */\n model: string\n\n /**\n * The parent message ID\n */\n parent_message_id: string\n}\n\nexport type Prompt = {\n /**\n * The content of the prompt\n */\n content: PromptContent\n\n /**\n * The ID of the prompt\n */\n id: string\n\n /**\n * The role played in the prompt\n */\n role: Role\n}\n\nexport type ContentType = 'text'\n\nexport type PromptContent = {\n /**\n * The content type of the prompt\n */\n content_type: ContentType\n\n /**\n * The parts to the prompt\n */\n parts: string[]\n}\n\nexport type ConversationResponseEvent = {\n message?: Message\n conversation_id?: string\n error?: string | null\n}\n\nexport type Message = {\n id: string\n content: MessageContent\n role: Role\n user: string | null\n create_time: string | null\n update_time: string | null\n end_turn: null\n weight: number\n recipient: string\n metadata: MessageMetadata\n}\n\nexport type MessageContent = {\n content_type: string\n parts: string[]\n}\n\nexport type MessageMetadata = any\n\nexport namespace openai {\n export interface CreateChatCompletionDeltaResponse {\n id: string\n object: 'chat.completion.chunk'\n created: number\n model: string\n choices: [\n {\n delta: {\n role: Role\n content?: string\n }\n index: number\n finish_reason: string | null\n }\n ]\n sentence_id?: number\n is_end?: boolean\n is_truncated?: boolean\n result?: string\n need_clear_history?: boolean\n usage?: any\n output?: any\n request_id?: string\n event?: string\n data?: string\n meta?: any\n type?: string\n }\n\n /**\n *\n * @export\n * @interface ChatCompletionRequestMessage\n */\n export interface ChatCompletionRequestMessage {\n /**\n * The role of the author of this message.\n * @type {string}\n * @memberof ChatCompletionRequestMessage\n */\n role?: ChatCompletionRequestMessageRoleEnum\n /**\n * The contents of the message\n * @type {string}\n * @memberof ChatCompletionRequestMessage\n */\n content?: string\n /**\n * The name of the user in a multi-user chat\n * @type {string}\n * @memberof ChatCompletionRequestMessage\n */\n name?: string\n\n /**\n * 阿里云的参数 input = {prompt: '哪个公园距离我更近', history: [\n *\n * {\n *\n * \"user\":\"今天天气好吗?\",\n *\n * \"bot\":\"今天天气不错,要出去玩玩嘛?\"\n *\n * },\n *\n * {\n *\n * \"user\":\"那你有什么地方推荐?\",\n *\n * \"bot\":\"我建议你去公园,春天来了,花朵开了,很美丽。\"\n *\n * }\n *\n * ]}\n */\n input?: object\n }\n export declare const ChatCompletionRequestMessageRoleEnum: {\n readonly System: 'system'\n readonly User: 'user'\n readonly Assistant: 'assistant'\n }\n export declare type ChatCompletionRequestMessageRoleEnum =\n (typeof ChatCompletionRequestMessageRoleEnum)[keyof typeof ChatCompletionRequestMessageRoleEnum]\n /**\n *\n * @export\n * @interface ChatCompletionResponseMessage\n */\n export interface ChatCompletionResponseMessage {\n /**\n * The role of the author of this message.\n * @type {string}\n * @memberof ChatCompletionResponseMessage\n */\n role: ChatCompletionResponseMessageRoleEnum\n /**\n * The contents of the message\n * @type {string}\n * @memberof ChatCompletionResponseMessage\n */\n content: string\n }\n export declare const ChatCompletionResponseMessageRoleEnum: {\n readonly System: 'system'\n readonly User: 'user'\n readonly Assistant: 'assistant'\n }\n export declare type ChatCompletionResponseMessageRoleEnum =\n (typeof ChatCompletionResponseMessageRoleEnum)[keyof typeof ChatCompletionResponseMessageRoleEnum]\n /**\n *\n * @export\n * @interface CreateChatCompletionRequest\n */\n export interface CreateChatCompletionRequest {\n /**\n * ID of the model to use. Currently, only `gpt-3.5-turbo` and `gpt-3.5-turbo-0301` are supported.\n * @type {string}\n * @memberof CreateChatCompletionRequest\n */\n model: string\n /**\n * The messages to generate chat completions for, in the [chat format](/docs/guides/chat/introduction).\n * @type {Array<ChatCompletionRequestMessage>}\n * @memberof CreateChatCompletionRequest\n */\n messages: Array<ChatCompletionRequestMessage>\n /**\n * What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. We generally recommend altering this or `top_p` but not both.\n * @type {number}\n * @memberof CreateChatCompletionRequest\n */\n temperature?: number | null\n /**\n * An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered. We generally recommend altering this or `temperature` but not both.\n * @type {number}\n * @memberof CreateChatCompletionRequest\n */\n top_p?: number | null\n /**\n * How many chat completion choices to generate for each input message.\n * @type {number}\n * @memberof CreateChatCompletionRequest\n */\n n?: number | null\n /**\n * If set, partial message deltas will be sent, like in ChatGPT. Tokens will be sent as data-only [server-sent events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Event_stream_format) as they become available, with the stream terminated by a `data: [DONE]` message.\n * @type {boolean}\n * @memberof CreateChatCompletionRequest\n */\n stream?: boolean | null\n /**\n *\n * @type {CreateChatCompletionRequestStop}\n * @memberof CreateChatCompletionRequest\n */\n stop?: CreateChatCompletionRequestStop\n /**\n * The maximum number of tokens allowed for the generated answer. By default, the number of tokens the model can return will be (4096 - prompt tokens).\n * @type {number}\n * @memberof CreateChatCompletionRequest\n */\n max_tokens?: number\n /**\n * Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far, increasing the model\\'s likelihood to talk about new topics. [See more information about frequency and presence penalties.](/docs/api-reference/parameter-details)\n * @type {number}\n * @memberof CreateChatCompletionRequest\n */\n presence_penalty?: number | null\n /**\n * Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model\\'s likelihood to repeat the same line verbatim. [See more information about frequency and presence penalties.](/docs/api-reference/parameter-details)\n * @type {number}\n * @memberof CreateChatCompletionRequest\n */\n frequency_penalty?: number | null\n /**\n * Modify the likelihood of specified tokens appearing in the completion. Accepts a json object that maps tokens (specified by their token ID in the tokenizer) to an associated bias value from -100 to 100. Mathematically, the bias is added to the logits generated by the model prior to sampling. The exact effect will vary per model, but values between -1 and 1 should decrease or increase likelihood of selection; values like -100 or 100 should result in a ban or exclusive selection of the relevant token.\n * @type {object}\n * @memberof CreateChatCompletionRequest\n */\n logit_bias?: object | null\n /**\n * A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. [Learn more](/docs/guides/safety-best-practices/end-user-ids).\n * @type {string}\n * @memberof CreateChatCompletionRequest\n */\n user?: string\n }\n /**\n * @type CreateChatCompletionRequestStop\n * Up to 4 sequences where the API will stop generating further tokens.\n * @export\n */\n export declare type CreateChatCompletionRequestStop = Array<string> | string\n /**\n *\n * @export\n * @interface CreateChatCompletionResponse\n */\n export interface CreateChatCompletionResponse {\n /**\n *\n * @type {string}\n * @memberof CreateChatCompletionResponse\n */\n id?: string\n /**\n *\n * @type {string}\n * @memberof CreateChatCompletionResponse\n */\n object?: string\n /**\n *\n * @type {number}\n * @memberof CreateChatCompletionResponse\n */\n created?: number\n /**\n *\n * @type {string}\n * @memberof CreateChatCompletionResponse\n */\n model?: string\n /**\n *\n * @type {Array<CreateChatCompletionResponseChoicesInner>}\n * @memberof CreateChatCompletionResponse\n */\n choices?: Array<CreateChatCompletionResponseChoicesInner>\n /**\n *\n * @type {CreateCompletionResponseUsage}\n * @memberof CreateChatCompletionResponse\n */\n usage?: CreateCompletionResponseUsage\n result?: string\n is_truncated?: boolean\n need_clear_history?: boolean\n output?: any\n request_id?: string\n }\n /**\n *\n * @export\n * @interface CreateChatCompletionResponseChoicesInner\n */\n export interface CreateChatCompletionResponseChoicesInner {\n /**\n *\n * @type {number}\n * @memberof CreateChatCompletionResponseChoicesInner\n */\n index?: number\n /**\n *\n * @type {ChatCompletionResponseMessage}\n * @memberof CreateChatCompletionResponseChoicesInner\n */\n message?: ChatCompletionResponseMessage\n /**\n *\n * @type {string}\n * @memberof CreateChatCompletionResponseChoicesInner\n */\n finish_reason?: string\n }\n /**\n *\n * @export\n * @interface CreateCompletionResponseUsage\n */\n export interface CreateCompletionResponseUsage {\n /**\n *\n * @type {number}\n * @memberof CreateCompletionResponseUsage\n */\n prompt_tokens: number\n /**\n *\n * @type {number}\n * @memberof CreateCompletionResponseUsage\n */\n completion_tokens: number\n /**\n *\n * @type {number}\n * @memberof CreateCompletionResponseUsage\n */\n total_tokens: number\n }\n}\n","/// <reference lib=\"dom\" />\n\nconst fetch = globalThis.fetch\n\nexport { fetch }\n","import { createParser } from 'eventsource-parser'\n\nimport * as types from './types'\nimport { fetch as globalFetch } from './fetch'\nimport { streamAsyncIterable } from './stream-async-iterable'\n\nexport async function fetchSSE(\n url: string,\n options: Parameters<typeof fetch>[1] & {\n onMessage: (data: string) => void\n onError?: (error: any) => void\n },\n fetch: types.FetchFn = globalFetch,\n manufacturer: string = 'OpenAI'\n) {\n const { onMessage, onError, ...fetchOptions } = options\n const res = await fetch(url, fetchOptions)\n if (!res.ok) {\n let reason: string\n\n try {\n reason = await res.text()\n } catch (err) {\n reason = res.statusText\n }\n\n const msg = `${manufacturer} error ${res.status}: ${reason}`\n const error = new types.ChatGPTError(msg, { cause: res })\n error.statusCode = res.status\n error.statusText = res.statusText\n throw error\n }\n\n const parser = createParser((event) => {\n if (event.type === 'event') {\n if (manufacturer.toLowerCase() === 'zhipu') {\n onMessage(JSON.stringify(event))\n } else {\n onMessage(event.data)\n }\n }\n })\n\n // handle special response errors\n const feed = (chunk: string) => {\n let response = null\n\n try {\n response = JSON.parse(chunk)\n } catch {\n // ignore\n }\n\n if (response?.detail?.type === 'invalid_request_error') {\n const msg = `${manufacturer} error ${response.detail.message}: ${response.detail.code} (${response.detail.type})`\n const error = new types.ChatGPTError(msg, { cause: response })\n error.statusCode = response.detail.code\n error.statusText = response.detail.message\n\n if (onError) {\n onError(error)\n } else {\n console.error(error)\n }\n\n // don't feed to the event parser\n return\n }\n\n parser.feed(chunk)\n }\n\n if (!res.body.getReader) {\n // Vercel polyfills `fetch` with `node-fetch`, which doesn't conform to\n // web standards, so this is a workaround...\n const body: NodeJS.ReadableStream = res.body as any\n\n if (!body.on || !body.read) {\n throw new types.ChatGPTError('unsupported \"fetch\" implementation')\n }\n\n body.on('readable', () => {\n let chunk: string | Buffer\n while (null !== (chunk = body.read())) {\n feed(chunk.toString())\n }\n })\n } else {\n for await (const chunk of streamAsyncIterable(res.body)) {\n const str = new TextDecoder().decode(chunk)\n feed(str)\n }\n }\n}\n","export async function* streamAsyncIterable<T>(stream: ReadableStream<T>) {\n const reader = stream.getReader()\n try {\n while (true) {\n const { done, value } = await reader.read()\n if (done) {\n return\n }\n yield value\n }\n } finally {\n reader.releaseLock()\n }\n}\n","import pTimeout from 'p-timeout'\nimport { v4 as uuidv4 } from 'uuid'\n\nimport * as types from './types'\nimport { fetch as globalFetch } from './fetch'\nimport { fetchSSE } from './fetch-sse'\nimport { isValidUUIDv4 } from './utils'\n\nexport class ChatGPTUnofficialProxyAPI {\n protected _accessToken: string\n protected _apiReverseProxyUrl: string\n protected _debug: boolean\n protected _model: string\n protected _headers: Record<string, string>\n protected _fetch: types.FetchFn\n\n /**\n * @param fetch - Optional override for the `fetch` implementation to use. Defaults to the global `fetch` function.\n */\n constructor(opts: {\n accessToken: string\n\n /** @defaultValue `https://bypass.duti.tech/api/conversation` **/\n apiReverseProxyUrl?: string\n\n /** @defaultValue `text-davinci-002-render-sha` **/\n model?: string\n\n /** @defaultValue `false` **/\n debug?: boolean\n\n /** @defaultValue `undefined` **/\n headers?: Record<string, string>\n\n fetch?: types.FetchFn\n }) {\n const {\n accessToken,\n apiReverseProxyUrl = 'https://bypass.duti.tech/api/conversation',\n model = 'text-davinci-002-render-sha',\n debug = false,\n headers,\n fetch = globalFetch\n } = opts\n\n this._accessToken = accessToken\n this._apiReverseProxyUrl = apiReverseProxyUrl\n this._debug = !!debug\n this._model = model\n this._fetch = fetch\n this._headers = headers\n\n if (!this._accessToken) {\n throw new Error('ChatGPT invalid accessToken')\n }\n\n if (!this._fetch) {\n throw new Error('Invalid environment; fetch is not defined')\n }\n\n if (typeof this._fetch !== 'function') {\n throw new Error('Invalid \"fetch\" is not a function')\n }\n }\n\n get accessToken(): string {\n return this._accessToken\n }\n\n set accessToken(value: string) {\n this._accessToken = value\n }\n\n /**\n * Sends a message to ChatGPT, waits for the response to resolve, and returns\n * the response.\n *\n * If you want your response to have historical context, you must provide a valid `parentMessageId`.\n *\n * If you want to receive a stream of partial responses, use `opts.onProgress`.\n * If you want to receive the full response, including message and conversation IDs,\n * you can use `opts.onConversationResponse` or use the `ChatGPTAPI.getConversation`\n * helper.\n *\n * Set `debug: true` in the `ChatGPTAPI` constructor to log more info on the full prompt sent to the OpenAI completions API. You can override the `promptPrefix` and `promptSuffix` in `opts` to customize the prompt.\n *\n * @param message - The prompt message to send\n * @param opts.conversationId - Optional ID of a conversation to continue (defaults to a random UUID)\n * @param opts.parentMessageId - Optional ID of the previous message in the conversation (defaults to `undefined`)\n * @param opts.messageId - Optional ID of the message to send (defaults to a random UUID)\n * @param opts.timeoutMs - Optional timeout in milliseconds (defaults to no timeout)\n * @param opts.onProgress - Optional callback which will be invoked every time the partial response is updated\n * @param opts.abortSignal - Optional callback used to abort the underlying `fetch` call using an [AbortController](https://developer.mozilla.org/en-US/docs/Web/API/AbortController)\n *\n * @returns The response from ChatGPT\n */\n async sendMessage(\n text: string,\n opts: types.SendMessageBrowserOptions = {}\n ): Promise<types.ChatMessage> {\n if (!!opts.conversationId !== !!opts.parentMessageId) {\n throw new Error(\n 'ChatGPTUnofficialProxyAPI.sendMessage: conversationId and parentMessageId must both be set or both be undefined'\n )\n }\n\n if (opts.conversationId && !isValidUUIDv4(opts.conversationId)) {\n throw new Error(\n 'ChatGPTUnofficialProxyAPI.sendMessage: conversationId is not a valid v4 UUID'\n )\n }\n\n if (opts.parentMessageId && !isValidUUIDv4(opts.parentMessageId)) {\n throw new Error(\n 'ChatGPTUnofficialProxyAPI.sendMessage: parentMessageId is not a valid v4 UUID'\n )\n }\n\n if (opts.messageId && !isValidUUIDv4(opts.messageId)) {\n throw new Error(\n 'ChatGPTUnofficialProxyAPI.sendMessage: messageId is not a valid v4 UUID'\n )\n }\n\n const {\n conversationId,\n parentMessageId = uuidv4(),\n messageId = uuidv4(),\n action = 'next',\n timeoutMs,\n onProgress\n } = opts\n\n let { abortSignal } = opts\n\n let abortController: AbortController = null\n if (timeoutMs && !abortSignal) {\n abortController = new AbortController()\n abortSignal = abortController.signal\n }\n\n const body: types.ConversationJSONBody = {\n action,\n messages: [\n {\n id: messageId,\n role: 'user',\n content: {\n content_type: 'text',\n parts: [text]\n }\n }\n ],\n model: this._model,\n parent_message_id: parentMessageId\n }\n\n if (conversationId) {\n body.conversation_id = conversationId\n }\n\n const result: types.ChatMessage = {\n role: 'assistant',\n id: uuidv4(),\n parentMessageId: messageId,\n conversationId,\n text: ''\n }\n\n const responseP = new Promise<types.ChatMessage>((resolve, reject) => {\n const url = this._apiReverseProxyUrl\n const headers = {\n ...this._headers,\n Authorization: `Bearer ${this._accessToken}`,\n Accept: 'text/event-stream',\n 'Content-Type': 'application/json'\n }\n\n if (this._debug) {\n console.log('POST', url, { body, headers })\n }\n\n fetchSSE(\n url,\n {\n method: 'POST',\n headers,\n body: JSON.stringify(body),\n signal: abortSignal,\n onMessage: (data: string) => {\n if (data === '[DONE]') {\n return resolve(result)\n }\n\n try {\n const convoResponseEvent: types.ConversationResponseEvent =\n JSON.parse(data)\n if (convoResponseEvent.conversation_id) {\n result.conversationId = convoResponseEvent.conversation_id\n }\n\n if (convoResponseEvent.message?.id) {\n result.id = convoResponseEvent.message.id\n }\n\n const message = convoResponseEvent.message\n // console.log('event', JSON.stringify(convoResponseEvent, null, 2))\n\n if (message) {\n let text = message?.content?.parts?.[0]\n\n if (text) {\n result.text = text\n\n if (onProgress) {\n onProgress(result)\n }\n }\n }\n } catch (err) {\n if (this._debug) {\n console.warn('chatgpt unexpected JSON error', err)\n }\n // reject(err)\n }\n },\n onError: (err) => {\n reject(err)\n }\n },\n this._fetch\n ).catch((err) => {\n const errMessageL = err.toString().toLowerCase()\n\n if (\n result.text &&\n (errMessageL === 'error: typeerror: terminated' ||\n errMessageL === 'typeerror: terminated')\n ) {\n // OpenAI sometimes forcefully terminates the socket from their end before\n // the HTTP request has resolved cleanly. In my testing, these cases tend to\n // happen when OpenAI has already send the last `response`, so we can ignore\n // the `fetch` error in this case.\n return resolve(result)\n } else {\n return reject(err)\n }\n })\n })\n\n if (timeoutMs) {\n if (abortController) {\n // This will be called when a timeout occurs in order for us to forcibly\n // ensure that the underlying HTTP request is aborted.\n ;(responseP as any).cancel = () => {\n abortController.abort()\n }\n }\n\n return pTimeout(responseP, {\n milliseconds: timeoutMs,\n message: 'ChatGPT timed out waiting for response'\n })\n } else {\n return responseP\n }\n }\n}\n","const uuidv4Re =\n /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i\n\nexport function isValidUUIDv4(str: string): boolean {\n return str && uuidv4Re.test(str)\n}\n"],"mappings":";AAAA,OAAO,UAAU;AACjB,OAAO,cAAc;AACrB,OAAO,cAAc;AACrB,SAAS,MAAM,cAAc;;;ACH7B,SAAS,mBAAmB;AAG5B,IAAM,YAAY,YAAY,aAAa;AAEpC,SAAS,OAAO,OAA4B;AACjD,SAAO,IAAI,YAAY,UAAU,OAAO,KAAK,CAAC;AAChD;;;ACqFO,IAAM,eAAN,cAA2B,MAAM;AAKxC;AAyGO,IAAU;AAAA,CAAV,CAAUA,YAAV;AAAA,GAAU;;;ACxMjB,IAAM,QAAQ,WAAW;;;ACFzB,SAAS,oBAAoB;;;ACA7B,gBAAuB,oBAAuB,QAA2B;AACvE,QAAM,SAAS,OAAO,UAAU;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,MAAM;AAAA,kBACnB;AAAA,gBACF;AAAA,cACF;AAAA,YACF,SAAS,KAAP;AACA,kBAAI,KAAK,QAAQ;AACf,wBAAQ,KAAK,iCAAiC,GAAG;AAAA,cACnD;AAAA,YAEF;AAAA,UACF;AAAA,UACA,SAAS,CAAC,QAAQ;AAChB,mBAAO,GAAG;AAAA,UACZ;AAAA,QACF;AAAA,QACA,KAAK;AAAA,MACP,EAAE,MAAM,CAAC,QAAQ;AACf,cAAM,cAAc,IAAI,SAAS,EAAE,YAAY;AAE/C,YACE,OAAO,SACN,gBAAgB,kCACf,gBAAgB,0BAClB;AAKA,iBAAO,QAAQ,MAAM;AAAA,QACvB,OAAO;AACL,iBAAO,OAAO,GAAG;AAAA,QACnB;AAAA,MACF,CAAC;AAAA,IACH,CAAC;AAED,QAAI,WAAW;AACb,UAAI,iBAAiB;AAGnB;AAAC,QAAC,UAAkB,SAAS,MAAM;AACjC,0BAAgB,MAAM;AAAA,QACxB;AAAA,MACF;AAEA,aAAOC,UAAS,WAAW;AAAA,QACzB,cAAc;AAAA,QACd,SAAS;AAAA,MACX,CAAC;AAAA,IACH,OAAO;AACL,aAAO;AAAA,IACT;AAAA,EACF;AACF;","names":["openai","fetch","fetch","url","_a","_b","_c","_d","_e","message","res","prompt","pTimeout","uuidv4","fetch","uuidv4","text","pTimeout"]}