UNPKG

@mi-gpt/openai

Version:
85 lines (82 loc) 2.59 kB
import { kDefaultOpenAIConfig } from './chunk-OKPRVUMK.js'; import { deepMerge } from '@mi-gpt/utils'; import OpenAIClient from 'openai'; import { ProxyAgent } from 'proxy-agent'; var _OpenAI = class { _client; _abortCallbacks = {}; config = {}; init(config) { var _a; this.config = deepMerge(kDefaultOpenAIConfig, config); this._client ??= new OpenAIClient({ baseURL: this.config.baseURL, apiKey: this.config.apiKey, httpAgent: this.config.enableProxy ? new ProxyAgent() : void 0, ...(_a = this.config.extra) == null ? void 0 : _a.clientOptions }); } dispose() { this._client = null; this._abortCallbacks = {}; } cancel(requestId) { if (requestId && this._abortCallbacks[requestId]) { this._abortCallbacks[requestId](); delete this._abortCallbacks[requestId]; } } async chat(options) { var _a, _b, _c, _d, _e, _f, _g; const { requestId, onStream, requestOptions, createParams, onError } = options; let signal; if (requestId) { const controller = new AbortController(); this._abortCallbacks[requestId] = () => controller.abort(); signal = controller.signal; } const params = deepMerge( { model: this.config.model, ...(_a = this.config.extra) == null ? void 0 : _a.createParams }, createParams ); const res = await this._client.chat.completions.create( params, deepMerge( { ...(_b = this.config.extra) == null ? void 0 : _b.requestOptions }, { ...requestOptions, signal } ) ).catch(async (e) => { console.error("\u274C LLM \u54CD\u5E94\u5F02\u5E38", e); await (onError == null ? void 0 : onError(e)); return null; }); let result = ""; if (params.stream) { for await (const chunk of res ?? []) { const text = ((_d = (_c = chunk.choices[0]) == null ? void 0 : _c.delta) == null ? void 0 : _d.content) || ""; const aborted = requestId && !Object.keys(this._abortCallbacks).includes(requestId); if (aborted) { result = ""; break; } if (text) { result += text; onStream == null ? void 0 : onStream(text); } } } else { result = ((_g = (_f = (_e = res == null ? void 0 : res.choices) == null ? void 0 : _e[0]) == null ? void 0 : _f.message) == null ? void 0 : _g.content) ?? ""; } if (requestId) { delete this._abortCallbacks[requestId]; } return result; } }; var OpenAI = new _OpenAI(); export { OpenAI };