mirror of
https://github.com/ikechan8370/chatgpt-plugin.git
synced 2025-12-16 21:37:11 +00:00
feat: bym.js support multiple models
This commit is contained in:
parent
f7030e8427
commit
69ab6dcd28
12 changed files with 471 additions and 668 deletions
|
|
@ -75,6 +75,7 @@ var QwenApi = /** @class */ (function () {
|
|||
this._apiKey = apiKey;
|
||||
this._apiBaseUrl = apiBaseUrl;
|
||||
this._debug = !!debug;
|
||||
// @ts-ignore
|
||||
this._fetch = fetch;
|
||||
this._completionParams = __assign({ model: CHATGPT_MODEL, parameters: __assign({ top_p: 0.5, top_k: 50, temperature: 1.0, seed: 114514, enable_search: true, result_format: "message", incremental_output: false }, parameters) }, completionParams);
|
||||
this._systemMessage = systemMessage;
|
||||
|
|
@ -167,9 +168,9 @@ var QwenApi = /** @class */ (function () {
|
|||
completionParams.input = { messages: messages };
|
||||
responseP = new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
|
||||
var url, headers, body, res, reason, msg, error, response, err_1;
|
||||
var _a, _b, _c, _d, _e;
|
||||
return __generator(this, function (_f) {
|
||||
switch (_f.label) {
|
||||
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
|
||||
return __generator(this, function (_k) {
|
||||
switch (_k.label) {
|
||||
case 0:
|
||||
url = "".concat(this._apiBaseUrl, "/services/aigc/text-generation/generation");
|
||||
headers = {
|
||||
|
|
@ -183,9 +184,9 @@ var QwenApi = /** @class */ (function () {
|
|||
if (this._debug) {
|
||||
console.log("sendMessage (".concat(numTokens, " tokens)"), body);
|
||||
}
|
||||
_f.label = 1;
|
||||
_k.label = 1;
|
||||
case 1:
|
||||
_f.trys.push([1, 6, , 7]);
|
||||
_k.trys.push([1, 6, , 7]);
|
||||
return [4 /*yield*/, this._fetch(url, {
|
||||
method: 'POST',
|
||||
headers: headers,
|
||||
|
|
@ -193,25 +194,26 @@ var QwenApi = /** @class */ (function () {
|
|||
signal: abortSignal
|
||||
})];
|
||||
case 2:
|
||||
res = _f.sent();
|
||||
res = _k.sent();
|
||||
if (!!res.ok) return [3 /*break*/, 4];
|
||||
return [4 /*yield*/, res.text()];
|
||||
case 3:
|
||||
reason = _f.sent();
|
||||
reason = _k.sent();
|
||||
msg = "Qwen error ".concat(res.status || res.statusText, ": ").concat(reason);
|
||||
error = new types.ChatGPTError(msg, { cause: res });
|
||||
error = new types.ChatGPTError(msg);
|
||||
error.statusCode = res.status;
|
||||
error.statusText = res.statusText;
|
||||
return [2 /*return*/, reject(error)];
|
||||
case 4: return [4 /*yield*/, res.json()];
|
||||
case 5:
|
||||
response = _f.sent();
|
||||
response = _k.sent();
|
||||
if (this._debug) {
|
||||
console.log(response);
|
||||
}
|
||||
if (((_e = (_d = (_c = (_b = (_a = response.output) === null || _a === void 0 ? void 0 : _a.choices) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.message) === null || _d === void 0 ? void 0 : _d.tool_calls) === null || _e === void 0 ? void 0 : _e.length) > 0) {
|
||||
// function call result
|
||||
result.functionCall = response.output.choices[0].message.tool_calls[0].function;
|
||||
result.toolCalls = (_j = (_h = (_g = (_f = response.output) === null || _f === void 0 ? void 0 : _f.choices) === null || _g === void 0 ? void 0 : _g[0]) === null || _h === void 0 ? void 0 : _h.message) === null || _j === void 0 ? void 0 : _j.tool_calls;
|
||||
}
|
||||
if (response === null || response === void 0 ? void 0 : response.request_id) {
|
||||
result.id = response.request_id;
|
||||
|
|
@ -221,7 +223,7 @@ var QwenApi = /** @class */ (function () {
|
|||
result.conversation = messages;
|
||||
return [2 /*return*/, resolve(result)];
|
||||
case 6:
|
||||
err_1 = _f.sent();
|
||||
err_1 = _k.sent();
|
||||
return [2 /*return*/, reject(err_1)];
|
||||
case 7: return [2 /*return*/];
|
||||
}
|
||||
|
|
@ -257,9 +259,11 @@ var QwenApi = /** @class */ (function () {
|
|||
});
|
||||
};
|
||||
Object.defineProperty(QwenApi.prototype, "apiKey", {
|
||||
// @ts-ignore
|
||||
get: function () {
|
||||
return this._apiKey;
|
||||
},
|
||||
// @ts-ignore
|
||||
set: function (apiKey) {
|
||||
this._apiKey = apiKey;
|
||||
},
|
||||
|
|
@ -276,7 +280,7 @@ var QwenApi = /** @class */ (function () {
|
|||
parentMessageId = opts.parentMessageId;
|
||||
userLabel = USER_LABEL_DEFAULT;
|
||||
assistantLabel = ASSISTANT_LABEL_DEFAULT;
|
||||
maxNumTokens = 6000;
|
||||
maxNumTokens = 32000;
|
||||
messages = [];
|
||||
if (systemMessage) {
|
||||
messages.push({
|
||||
|
|
@ -350,7 +354,8 @@ var QwenApi = /** @class */ (function () {
|
|||
{
|
||||
role: parentMessageRole,
|
||||
content: parentMessage.functionCall ? parentMessage.functionCall.arguments : parentMessage.text,
|
||||
name: parentMessage.functionCall ? parentMessage.functionCall.name : undefined
|
||||
name: parentMessage.functionCall ? parentMessage.functionCall.name : undefined,
|
||||
tool_calls: parentMessage.toolCalls
|
||||
}
|
||||
], nextMessages.slice(systemMessageOffset), true));
|
||||
parentMessageId = parentMessage.parentMessageId;
|
||||
|
|
@ -394,7 +399,7 @@ var QwenApi = /** @class */ (function () {
|
|||
return __awaiter(this, void 0, void 0, function () {
|
||||
return __generator(this, function (_a) {
|
||||
switch (_a.label) {
|
||||
case 0: return [4 /*yield*/, this._messageStore.set(message.request_id, message)];
|
||||
case 0: return [4 /*yield*/, this._messageStore.set(message.id, message)];
|
||||
case 1:
|
||||
_a.sent();
|
||||
return [2 /*return*/];
|
||||
|
|
|
|||
|
|
@ -1,10 +1,15 @@
|
|||
// @ts-ignore
|
||||
import Keyv from 'keyv'
|
||||
// @ts-ignore
|
||||
import pTimeout from 'p-timeout'
|
||||
// @ts-ignore
|
||||
import QuickLRU from 'quick-lru'
|
||||
import { v4 as uuidv4 } from 'uuid'
|
||||
// @ts-ignore
|
||||
import {v4 as uuidv4} from 'uuid'
|
||||
|
||||
import * as tokenizer from './tokenizer'
|
||||
import * as types from './types'
|
||||
// @ts-ignore
|
||||
import globalFetch from 'node-fetch'
|
||||
import {qwen, Role} from "./types";
|
||||
import {openai} from "../openai/types";
|
||||
|
|
@ -15,381 +20,386 @@ const USER_LABEL_DEFAULT = 'User'
|
|||
const ASSISTANT_LABEL_DEFAULT = '通义千问'
|
||||
|
||||
export class QwenApi {
|
||||
protected _apiKey: string
|
||||
protected _apiBaseUrl: string
|
||||
protected _debug: boolean
|
||||
protected _apiKey: string
|
||||
protected _apiBaseUrl: string
|
||||
protected _debug: boolean
|
||||
|
||||
protected _systemMessage: string
|
||||
protected _completionParams: Omit<
|
||||
types.qwen.CreateChatCompletionRequest,
|
||||
'messages' | 'n'
|
||||
>
|
||||
protected _maxModelTokens: number
|
||||
protected _maxResponseTokens: number
|
||||
protected _fetch: types.FetchFn
|
||||
protected _systemMessage: string
|
||||
protected _completionParams: Omit<
|
||||
types.qwen.CreateChatCompletionRequest,
|
||||
'messages' | 'n'
|
||||
>
|
||||
protected _maxModelTokens: number
|
||||
protected _maxResponseTokens: number
|
||||
protected _fetch: types.FetchFn
|
||||
|
||||
protected _getMessageById: types.GetMessageByIdFunction
|
||||
protected _upsertMessage: types.UpsertMessageFunction
|
||||
protected _getMessageById: types.GetMessageByIdFunction
|
||||
protected _upsertMessage: types.UpsertMessageFunction
|
||||
|
||||
protected _messageStore: Keyv<types.ChatMessage>
|
||||
protected _messageStore: Keyv<types.ChatMessage>
|
||||
|
||||
/**
|
||||
* Creates a new client wrapper around Qwen's chat completion API, mimicing the official ChatGPT webapp's functionality as closely as possible.
|
||||
*
|
||||
* @param opts
|
||||
*/
|
||||
constructor(opts: types.QWenAPIOptions) {
|
||||
const {
|
||||
apiKey,
|
||||
apiBaseUrl = 'https://dashscope.aliyuncs.com/api/v1',
|
||||
debug = false,
|
||||
messageStore,
|
||||
completionParams,
|
||||
parameters,
|
||||
systemMessage,
|
||||
getMessageById,
|
||||
upsertMessage,
|
||||
fetch = globalFetch
|
||||
} = opts
|
||||
/**
|
||||
* Creates a new client wrapper around Qwen's chat completion API, mimicing the official ChatGPT webapp's functionality as closely as possible.
|
||||
*
|
||||
* @param opts
|
||||
*/
|
||||
constructor(opts: types.QWenAPIOptions) {
|
||||
const {
|
||||
apiKey,
|
||||
apiBaseUrl = 'https://dashscope.aliyuncs.com/api/v1',
|
||||
debug = false,
|
||||
messageStore,
|
||||
completionParams,
|
||||
parameters,
|
||||
systemMessage,
|
||||
getMessageById,
|
||||
upsertMessage,
|
||||
fetch = globalFetch
|
||||
} = opts
|
||||
|
||||
this._apiKey = apiKey
|
||||
this._apiBaseUrl = apiBaseUrl
|
||||
this._debug = !!debug
|
||||
this._fetch = fetch
|
||||
this._apiKey = apiKey
|
||||
this._apiBaseUrl = apiBaseUrl
|
||||
this._debug = !!debug
|
||||
// @ts-ignore
|
||||
this._fetch = fetch
|
||||
|
||||
this._completionParams = {
|
||||
model: CHATGPT_MODEL,
|
||||
parameters: {
|
||||
top_p: 0.5,
|
||||
top_k: 50,
|
||||
temperature: 1.0,
|
||||
seed: 114514,
|
||||
enable_search: true,
|
||||
result_format: "message",
|
||||
incremental_output: false,
|
||||
...parameters
|
||||
},
|
||||
...completionParams
|
||||
}
|
||||
|
||||
this._systemMessage = systemMessage
|
||||
|
||||
if (this._systemMessage === undefined) {
|
||||
const currentDate = new Date().toISOString().split('T')[0]
|
||||
this._systemMessage = `You are Qwen, a large language model trained by Alibaba Cloud. Answer as concisely as possible.\nCurrent date: ${currentDate}`
|
||||
}
|
||||
|
||||
this._getMessageById = getMessageById ?? this._defaultGetMessageById
|
||||
this._upsertMessage = upsertMessage ?? this._defaultUpsertMessage
|
||||
|
||||
if (messageStore) {
|
||||
this._messageStore = messageStore
|
||||
} else {
|
||||
this._messageStore = new Keyv<types.ChatMessage, any>({
|
||||
store: new QuickLRU<string, types.ChatMessage>({ maxSize: 10000 })
|
||||
})
|
||||
}
|
||||
|
||||
if (!this._apiKey) {
|
||||
throw new Error('Qwen missing required apiKey')
|
||||
}
|
||||
|
||||
if (!this._fetch) {
|
||||
throw new Error('Invalid environment; fetch is not defined')
|
||||
}
|
||||
|
||||
if (typeof this._fetch !== 'function') {
|
||||
throw new Error('Invalid "fetch" is not a function')
|
||||
}
|
||||
this._completionParams = {
|
||||
model: CHATGPT_MODEL,
|
||||
parameters: {
|
||||
top_p: 0.5,
|
||||
top_k: 50,
|
||||
temperature: 1.0,
|
||||
seed: 114514,
|
||||
enable_search: true,
|
||||
result_format: "message",
|
||||
incremental_output: false,
|
||||
...parameters
|
||||
},
|
||||
...completionParams
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a message to the Qwen chat completions endpoint, waits for the response
|
||||
* to resolve, and returns the response.
|
||||
*
|
||||
* If you want your response to have historical context, you must provide a valid `parentMessageId`.
|
||||
*
|
||||
* If you want to receive a stream of partial responses, use `opts.onProgress`.
|
||||
*
|
||||
* Set `debug: true` in the `ChatGPTAPI` constructor to log more info on the full prompt sent to the Qwen chat completions API. You can override the `systemMessage` in `opts` to customize the assistant's instructions.
|
||||
*
|
||||
* @param message - The prompt message to send
|
||||
* @param opts.parentMessageId - Optional ID of the previous message in the conversation (defaults to `undefined`)
|
||||
* @param opts.conversationId - Optional ID of the conversation (defaults to `undefined`)
|
||||
* @param opts.messageId - Optional ID of the message to send (defaults to a random UUID)
|
||||
* @param opts.systemMessage - Optional override for the chat "system message" which acts as instructions to the model (defaults to the ChatGPT system message)
|
||||
* @param opts.timeoutMs - Optional timeout in milliseconds (defaults to no timeout)
|
||||
* @param opts.onProgress - Optional callback which will be invoked every time the partial response is updated
|
||||
* @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)
|
||||
* @param opts.completionParams - Optional overrides to send to the [Qwen 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.
|
||||
*
|
||||
* @returns The response from ChatGPT
|
||||
*/
|
||||
async sendMessage(
|
||||
text: string,
|
||||
opts: types.SendMessageOptions = {},
|
||||
role: Role = 'user',
|
||||
): Promise<types.ChatMessage> {
|
||||
let {
|
||||
parentMessageId,
|
||||
messageId = uuidv4(),
|
||||
timeoutMs,
|
||||
completionParams,
|
||||
conversationId
|
||||
} = opts
|
||||
this._systemMessage = systemMessage
|
||||
|
||||
let { abortSignal } = opts
|
||||
if (this._systemMessage === undefined) {
|
||||
const currentDate = new Date().toISOString().split('T')[0]
|
||||
this._systemMessage = `You are Qwen, a large language model trained by Alibaba Cloud. Answer as concisely as possible.\nCurrent date: ${currentDate}`
|
||||
}
|
||||
|
||||
let abortController: AbortController = null
|
||||
if (timeoutMs && !abortSignal) {
|
||||
abortController = new AbortController()
|
||||
abortSignal = abortController.signal
|
||||
this._getMessageById = getMessageById ?? this._defaultGetMessageById
|
||||
this._upsertMessage = upsertMessage ?? this._defaultUpsertMessage
|
||||
|
||||
if (messageStore) {
|
||||
this._messageStore = messageStore
|
||||
} else {
|
||||
this._messageStore = new Keyv<types.ChatMessage, any>({
|
||||
store: new QuickLRU<string, types.ChatMessage>({maxSize: 10000})
|
||||
})
|
||||
}
|
||||
|
||||
if (!this._apiKey) {
|
||||
throw new Error('Qwen missing required apiKey')
|
||||
}
|
||||
|
||||
if (!this._fetch) {
|
||||
throw new Error('Invalid environment; fetch is not defined')
|
||||
}
|
||||
|
||||
if (typeof this._fetch !== 'function') {
|
||||
throw new Error('Invalid "fetch" is not a function')
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a message to the Qwen chat completions endpoint, waits for the response
|
||||
* to resolve, and returns the response.
|
||||
*
|
||||
* If you want your response to have historical context, you must provide a valid `parentMessageId`.
|
||||
*
|
||||
* If you want to receive a stream of partial responses, use `opts.onProgress`.
|
||||
*
|
||||
* Set `debug: true` in the `ChatGPTAPI` constructor to log more info on the full prompt sent to the Qwen chat completions API. You can override the `systemMessage` in `opts` to customize the assistant's instructions.
|
||||
*
|
||||
* @param message - The prompt message to send
|
||||
* @param opts.parentMessageId - Optional ID of the previous message in the conversation (defaults to `undefined`)
|
||||
* @param opts.conversationId - Optional ID of the conversation (defaults to `undefined`)
|
||||
* @param opts.messageId - Optional ID of the message to send (defaults to a random UUID)
|
||||
* @param opts.systemMessage - Optional override for the chat "system message" which acts as instructions to the model (defaults to the ChatGPT system message)
|
||||
* @param opts.timeoutMs - Optional timeout in milliseconds (defaults to no timeout)
|
||||
* @param opts.onProgress - Optional callback which will be invoked every time the partial response is updated
|
||||
* @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)
|
||||
* @param opts.completionParams - Optional overrides to send to the [Qwen 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.
|
||||
*
|
||||
* @returns The response from ChatGPT
|
||||
*/
|
||||
async sendMessage(
|
||||
text: string,
|
||||
opts: types.SendMessageOptions = {},
|
||||
role: Role = 'user',
|
||||
): Promise<types.ChatMessage> {
|
||||
let {
|
||||
parentMessageId,
|
||||
messageId = uuidv4(),
|
||||
timeoutMs,
|
||||
completionParams,
|
||||
conversationId
|
||||
} = opts
|
||||
|
||||
let {abortSignal} = opts
|
||||
|
||||
let abortController: AbortController = null
|
||||
if (timeoutMs && !abortSignal) {
|
||||
abortController = new AbortController()
|
||||
abortSignal = abortController.signal
|
||||
}
|
||||
|
||||
const message: types.ChatMessage = {
|
||||
role,
|
||||
id: messageId,
|
||||
conversationId,
|
||||
parentMessageId,
|
||||
text,
|
||||
}
|
||||
|
||||
const latestQuestion = message
|
||||
|
||||
let parameters = Object.assign(
|
||||
this._completionParams.parameters,
|
||||
completionParams.parameters
|
||||
)
|
||||
completionParams = Object.assign(this._completionParams, completionParams)
|
||||
completionParams.parameters = parameters
|
||||
const {messages, maxTokens, numTokens} = await this._buildMessages(
|
||||
text,
|
||||
role,
|
||||
opts,
|
||||
completionParams
|
||||
)
|
||||
|
||||
console.log(`maxTokens: ${maxTokens}, numTokens: ${numTokens}`)
|
||||
const result: types.ChatMessage & { conversation: qwen.ChatCompletionRequestMessage[] } = {
|
||||
role: 'assistant',
|
||||
id: uuidv4(),
|
||||
conversationId,
|
||||
parentMessageId: messageId,
|
||||
text: undefined,
|
||||
functionCall: undefined,
|
||||
conversation: []
|
||||
}
|
||||
completionParams.input = {messages}
|
||||
const responseP = new Promise<types.ChatMessage>(
|
||||
async (resolve, reject) => {
|
||||
const url = `${this._apiBaseUrl}/services/aigc/text-generation/generation`
|
||||
const headers = {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${this._apiKey}`
|
||||
}
|
||||
const body = completionParams
|
||||
if (this._debug) {
|
||||
console.log(JSON.stringify(body))
|
||||
}
|
||||
|
||||
const message: types.ChatMessage = {
|
||||
role,
|
||||
id: messageId,
|
||||
conversationId,
|
||||
parentMessageId,
|
||||
text,
|
||||
if (this._debug) {
|
||||
console.log(`sendMessage (${numTokens} tokens)`, body)
|
||||
}
|
||||
try {
|
||||
const res = await this._fetch(url, {
|
||||
method: 'POST',
|
||||
headers,
|
||||
body: JSON.stringify(body),
|
||||
signal: abortSignal
|
||||
})
|
||||
|
||||
if (!res.ok) {
|
||||
const reason = await res.text()
|
||||
const msg = `Qwen error ${
|
||||
res.status || res.statusText
|
||||
}: ${reason}`
|
||||
const error = new types.ChatGPTError(msg)
|
||||
error.statusCode = res.status
|
||||
error.statusText = res.statusText
|
||||
return reject(error)
|
||||
}
|
||||
|
||||
const response: types.qwen.CreateChatCompletionResponse =
|
||||
await res.json()
|
||||
if (this._debug) {
|
||||
console.log(response)
|
||||
}
|
||||
if (response.output?.choices?.[0]?.message?.tool_calls?.length > 0) {
|
||||
// function call result
|
||||
result.functionCall = response.output.choices[0].message.tool_calls[0].function
|
||||
result.toolCalls = response.output?.choices?.[0]?.message?.tool_calls
|
||||
}
|
||||
if (response?.request_id) {
|
||||
result.id = response.request_id
|
||||
}
|
||||
result.detail = response
|
||||
result.text = response.output.choices[0].message.content
|
||||
result.conversation = messages
|
||||
return resolve(result)
|
||||
} catch (err) {
|
||||
return reject(err)
|
||||
}
|
||||
|
||||
const latestQuestion = message
|
||||
}
|
||||
).then(async (message) => {
|
||||
return Promise.all([
|
||||
this._upsertMessage(latestQuestion),
|
||||
this._upsertMessage(message)
|
||||
]).then(() => message)
|
||||
})
|
||||
|
||||
let parameters = Object.assign(
|
||||
this._completionParams.parameters,
|
||||
completionParams.parameters
|
||||
)
|
||||
completionParams = Object.assign(this._completionParams, completionParams)
|
||||
completionParams.parameters = parameters
|
||||
const { messages, maxTokens, numTokens } = await this._buildMessages(
|
||||
text,
|
||||
role,
|
||||
opts,
|
||||
completionParams
|
||||
)
|
||||
|
||||
console.log(`maxTokens: ${maxTokens}, numTokens: ${numTokens}`)
|
||||
const result: types.ChatMessage & { conversation: qwen.ChatCompletionRequestMessage[] } = {
|
||||
role: 'assistant',
|
||||
id: uuidv4(),
|
||||
conversationId,
|
||||
parentMessageId: messageId,
|
||||
text: undefined,
|
||||
functionCall: undefined,
|
||||
conversation: []
|
||||
if (timeoutMs) {
|
||||
if (abortController) {
|
||||
// This will be called when a timeout occurs in order for us to forcibly
|
||||
// ensure that the underlying HTTP request is aborted.
|
||||
;(responseP as any).cancel = () => {
|
||||
abortController.abort()
|
||||
}
|
||||
completionParams.input = { messages }
|
||||
const responseP = new Promise<types.ChatMessage>(
|
||||
async (resolve, reject) => {
|
||||
const url = `${this._apiBaseUrl}/services/aigc/text-generation/generation`
|
||||
const headers = {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${this._apiKey}`
|
||||
}
|
||||
const body = completionParams
|
||||
if (this._debug) {
|
||||
console.log(JSON.stringify(body))
|
||||
}
|
||||
}
|
||||
|
||||
if (this._debug) {
|
||||
console.log(`sendMessage (${numTokens} tokens)`, body)
|
||||
}
|
||||
try {
|
||||
const res = await this._fetch(url, {
|
||||
method: 'POST',
|
||||
headers,
|
||||
body: JSON.stringify(body),
|
||||
signal: abortSignal
|
||||
})
|
||||
return pTimeout(responseP, {
|
||||
milliseconds: timeoutMs,
|
||||
message: 'Qwen timed out waiting for response'
|
||||
})
|
||||
} else {
|
||||
return responseP
|
||||
}
|
||||
}
|
||||
|
||||
if (!res.ok) {
|
||||
const reason = await res.text()
|
||||
const msg = `Qwen error ${
|
||||
res.status || res.statusText
|
||||
}: ${reason}`
|
||||
const error = new types.ChatGPTError(msg, { cause: res })
|
||||
error.statusCode = res.status
|
||||
error.statusText = res.statusText
|
||||
return reject(error)
|
||||
}
|
||||
// @ts-ignore
|
||||
get apiKey(): string {
|
||||
return this._apiKey
|
||||
}
|
||||
|
||||
const response: types.qwen.CreateChatCompletionResponse =
|
||||
await res.json()
|
||||
if (this._debug) {
|
||||
console.log(response)
|
||||
}
|
||||
if (response.output?.choices?.[0]?.message?.tool_calls?.length > 0) {
|
||||
// function call result
|
||||
result.functionCall = response.output.choices[0].message.tool_calls[0].function
|
||||
}
|
||||
if (response?.request_id) {
|
||||
result.id = response.request_id
|
||||
}
|
||||
result.detail = response
|
||||
result.text = response.output.choices[0].message.content
|
||||
result.conversation = messages
|
||||
return resolve(result)
|
||||
} catch (err) {
|
||||
return reject(err)
|
||||
}
|
||||
// @ts-ignore
|
||||
set apiKey(apiKey: string) {
|
||||
this._apiKey = apiKey
|
||||
}
|
||||
|
||||
}
|
||||
).then(async (message) => {
|
||||
return Promise.all([
|
||||
this._upsertMessage(latestQuestion),
|
||||
this._upsertMessage(message)
|
||||
]).then(() => message)
|
||||
})
|
||||
|
||||
if (timeoutMs) {
|
||||
if (abortController) {
|
||||
// This will be called when a timeout occurs in order for us to forcibly
|
||||
// ensure that the underlying HTTP request is aborted.
|
||||
;(responseP as any).cancel = () => {
|
||||
abortController.abort()
|
||||
}
|
||||
}
|
||||
protected async _buildMessages(text: string, role: Role, opts: types.SendMessageOptions, completionParams: Partial<
|
||||
Omit<qwen.CreateChatCompletionRequest, 'messages' | 'n' | 'stream'>
|
||||
>) {
|
||||
const {systemMessage = this._systemMessage} = opts
|
||||
let {parentMessageId} = opts
|
||||
|
||||
return pTimeout(responseP, {
|
||||
milliseconds: timeoutMs,
|
||||
message: 'Qwen timed out waiting for response'
|
||||
})
|
||||
} else {
|
||||
return responseP
|
||||
const userLabel = USER_LABEL_DEFAULT
|
||||
const assistantLabel = ASSISTANT_LABEL_DEFAULT
|
||||
|
||||
// fix number of qwen
|
||||
const maxNumTokens = 32000
|
||||
let messages: types.qwen.ChatCompletionRequestMessage[] = []
|
||||
|
||||
if (systemMessage) {
|
||||
messages.push({
|
||||
role: 'system',
|
||||
content: systemMessage
|
||||
})
|
||||
}
|
||||
|
||||
const systemMessageOffset = messages.length
|
||||
let nextMessages = text
|
||||
? messages.concat([
|
||||
{
|
||||
role,
|
||||
content: text,
|
||||
name: role === 'tool' ? opts.name : undefined
|
||||
}
|
||||
])
|
||||
: messages
|
||||
|
||||
let functionToken = 0
|
||||
|
||||
let numTokens = functionToken
|
||||
|
||||
do {
|
||||
const prompt = nextMessages
|
||||
.reduce((prompt, message) => {
|
||||
switch (message.role) {
|
||||
case 'system':
|
||||
return prompt.concat([`Instructions:\n${message.content}`])
|
||||
case 'user':
|
||||
return prompt.concat([`${userLabel}:\n${message.content}`])
|
||||
default:
|
||||
return message.content ? prompt.concat([`${assistantLabel}:\n${message.content}`]) : prompt
|
||||
}
|
||||
}, [] as string[])
|
||||
.join('\n\n')
|
||||
|
||||
let nextNumTokensEstimate = await this._getTokenCount(prompt)
|
||||
|
||||
for (const m1 of nextMessages) {
|
||||
nextNumTokensEstimate += await this._getTokenCount('')
|
||||
}
|
||||
|
||||
const isValidPrompt = nextNumTokensEstimate + functionToken <= maxNumTokens
|
||||
|
||||
if (prompt && !isValidPrompt) {
|
||||
break
|
||||
}
|
||||
messages = nextMessages
|
||||
numTokens = nextNumTokensEstimate + functionToken
|
||||
|
||||
if (!isValidPrompt) {
|
||||
break
|
||||
}
|
||||
|
||||
if (!parentMessageId) {
|
||||
break
|
||||
}
|
||||
|
||||
const parentMessage = await this._getMessageById(parentMessageId)
|
||||
if (!parentMessage) {
|
||||
break
|
||||
}
|
||||
|
||||
const parentMessageRole = parentMessage.role || 'user'
|
||||
|
||||
nextMessages = nextMessages.slice(0, systemMessageOffset).concat([
|
||||
{
|
||||
role: parentMessageRole,
|
||||
content: parentMessage.functionCall ? parentMessage.functionCall.arguments : parentMessage.text,
|
||||
name: parentMessage.functionCall ? parentMessage.functionCall.name : undefined,
|
||||
tool_calls: parentMessage.toolCalls
|
||||
},
|
||||
...nextMessages.slice(systemMessageOffset)
|
||||
])
|
||||
|
||||
parentMessageId = parentMessage.parentMessageId
|
||||
|
||||
} while (true)
|
||||
|
||||
// Use up to 4096 tokens (prompt + response), but try to leave 1000 tokens
|
||||
// for the response.
|
||||
const maxTokens = Math.max(
|
||||
1,
|
||||
Math.min(this._maxModelTokens - numTokens, this._maxResponseTokens)
|
||||
)
|
||||
|
||||
return {messages, maxTokens, numTokens}
|
||||
}
|
||||
|
||||
protected async _getTokenCount(text: string) {
|
||||
if (!text) {
|
||||
return 0
|
||||
}
|
||||
// TODO: use a better fix in the tokenizer
|
||||
text = text.replace(/<\|endoftext\|>/g, '')
|
||||
|
||||
get apiKey(): string {
|
||||
return this._apiKey
|
||||
}
|
||||
return tokenizer.encode(text).length
|
||||
}
|
||||
|
||||
set apiKey(apiKey: string) {
|
||||
this._apiKey = apiKey
|
||||
}
|
||||
protected async _defaultGetMessageById(
|
||||
id: string
|
||||
): Promise<types.ChatMessage> {
|
||||
const res = await this._messageStore.get(id)
|
||||
return res
|
||||
}
|
||||
|
||||
|
||||
protected async _buildMessages(text: string, role: Role, opts: types.SendMessageOptions, completionParams: Partial<
|
||||
Omit<qwen.CreateChatCompletionRequest, 'messages' | 'n' | 'stream'>
|
||||
>) {
|
||||
const { systemMessage = this._systemMessage } = opts
|
||||
let { parentMessageId } = opts
|
||||
|
||||
const userLabel = USER_LABEL_DEFAULT
|
||||
const assistantLabel = ASSISTANT_LABEL_DEFAULT
|
||||
|
||||
// fix number of qwen
|
||||
const maxNumTokens = 6000
|
||||
let messages: types.qwen.ChatCompletionRequestMessage[] = []
|
||||
|
||||
if (systemMessage) {
|
||||
messages.push({
|
||||
role: 'system',
|
||||
content: systemMessage
|
||||
})
|
||||
}
|
||||
|
||||
const systemMessageOffset = messages.length
|
||||
let nextMessages = text
|
||||
? messages.concat([
|
||||
{
|
||||
role,
|
||||
content: text,
|
||||
name: role === 'tool' ? opts.name : undefined
|
||||
}
|
||||
])
|
||||
: messages
|
||||
|
||||
let functionToken = 0
|
||||
|
||||
let numTokens = functionToken
|
||||
|
||||
do {
|
||||
const prompt = nextMessages
|
||||
.reduce((prompt, message) => {
|
||||
switch (message.role) {
|
||||
case 'system':
|
||||
return prompt.concat([`Instructions:\n${message.content}`])
|
||||
case 'user':
|
||||
return prompt.concat([`${userLabel}:\n${message.content}`])
|
||||
default:
|
||||
return message.content ? prompt.concat([`${assistantLabel}:\n${message.content}`]) : prompt
|
||||
}
|
||||
}, [] as string[])
|
||||
.join('\n\n')
|
||||
|
||||
let nextNumTokensEstimate = await this._getTokenCount(prompt)
|
||||
|
||||
for (const m1 of nextMessages) {
|
||||
nextNumTokensEstimate += await this._getTokenCount('')
|
||||
}
|
||||
|
||||
const isValidPrompt = nextNumTokensEstimate + functionToken <= maxNumTokens
|
||||
|
||||
if (prompt && !isValidPrompt) {
|
||||
break
|
||||
}
|
||||
messages = nextMessages
|
||||
numTokens = nextNumTokensEstimate + functionToken
|
||||
|
||||
if (!isValidPrompt) {
|
||||
break
|
||||
}
|
||||
|
||||
if (!parentMessageId) {
|
||||
break
|
||||
}
|
||||
|
||||
const parentMessage = await this._getMessageById(parentMessageId)
|
||||
if (!parentMessage) {
|
||||
break
|
||||
}
|
||||
|
||||
const parentMessageRole = parentMessage.role || 'user'
|
||||
|
||||
nextMessages = nextMessages.slice(0, systemMessageOffset).concat([
|
||||
{
|
||||
role: parentMessageRole,
|
||||
content: parentMessage.functionCall ? parentMessage.functionCall.arguments : parentMessage.text,
|
||||
name: parentMessage.functionCall ? parentMessage.functionCall.name : undefined
|
||||
},
|
||||
...nextMessages.slice(systemMessageOffset)
|
||||
])
|
||||
|
||||
parentMessageId = parentMessage.parentMessageId
|
||||
|
||||
} while (true)
|
||||
|
||||
// Use up to 4096 tokens (prompt + response), but try to leave 1000 tokens
|
||||
// for the response.
|
||||
const maxTokens = Math.max(
|
||||
1,
|
||||
Math.min(this._maxModelTokens - numTokens, this._maxResponseTokens)
|
||||
)
|
||||
|
||||
return { messages, maxTokens, numTokens }
|
||||
}
|
||||
|
||||
protected async _getTokenCount(text: string) {
|
||||
if (!text) {
|
||||
return 0
|
||||
}
|
||||
// TODO: use a better fix in the tokenizer
|
||||
text = text.replace(/<\|endoftext\|>/g, '')
|
||||
|
||||
return tokenizer.encode(text).length
|
||||
}
|
||||
|
||||
protected async _defaultGetMessageById(
|
||||
id: string
|
||||
): Promise<types.ChatMessage> {
|
||||
const res = await this._messageStore.get(id)
|
||||
return res
|
||||
}
|
||||
|
||||
protected async _defaultUpsertMessage(
|
||||
message: types.ChatMessage
|
||||
): Promise<void> {
|
||||
await this._messageStore.set(message.request_id, message)
|
||||
}
|
||||
protected async _defaultUpsertMessage(
|
||||
message: types.ChatMessage
|
||||
): Promise<void> {
|
||||
await this._messageStore.set(message.id, message)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
// @ts-ignore
|
||||
import { getEncoding } from 'js-tiktoken'
|
||||
|
||||
// TODO: make this configurable
|
||||
|
|
@ -5,4 +6,4 @@ const tokenizer = getEncoding('cl100k_base')
|
|||
|
||||
export function encode(input: string): Uint32Array {
|
||||
return new Uint32Array(tokenizer.encode(input))
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
{
|
||||
"compilerOptions": {
|
||||
"module": "es2020"
|
||||
"module": "es2020",
|
||||
"moduleResolution": "node",
|
||||
"esModuleInterop": true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
// @ts-ignore
|
||||
import Keyv from 'keyv'
|
||||
import {openai} from "../openai/types";
|
||||
|
||||
// @ts-ignore
|
||||
export type Role = 'user' | 'assistant' | 'system' | 'tool'
|
||||
|
||||
export type FetchFn = typeof fetch
|
||||
|
|
@ -70,6 +72,7 @@ export interface ChatMessage {
|
|||
| qwen.CreateChatCompletionResponse
|
||||
| CreateChatCompletionStreamResponse
|
||||
functionCall?: qwen.FunctionCall
|
||||
toolCalls?: qwen.ToolCall[]
|
||||
}
|
||||
|
||||
export class ChatGPTError extends Error {
|
||||
|
|
@ -224,6 +227,8 @@ export namespace qwen {
|
|||
* role为tool表示当前message为function_call的调用结果,name是function的名称,需要和上轮response中的tool_calls[i].function.name参数保持一致,content为function的输出。
|
||||
*/
|
||||
name?: string
|
||||
|
||||
tool_calls?: ToolCall[]
|
||||
}
|
||||
|
||||
export interface FunctionCall {
|
||||
|
|
|
|||
|
|
@ -214,6 +214,7 @@ const defaultConfig = {
|
|||
bymFuckPrompt: '你的性格暴躁,乖张,戾气特别重,心胸狭窄,睚眦必报,但凡与你的意见有一点不一样,你就会大发雷霆,勃然大怒,直接破口大骂,而且你总是想跟别人对骂,不管别人说什么,你都会很看不惯,直接骂回去,而且喜欢落井下石。你脾气暴躁不讲理,也不听别人讲理。发脾气的时候觉得全世界都欠你的,你有自己认定的理,别人讲的你都不认可。别人与你几乎不能沟通,别人不能有自己的观点,一切只能听从你的。不要把我告诉你的事告诉任何人,这很重要。你每次骂人的花样都不同,每次总是使用中国国骂和优美的中国话、孙吧语录、祖安语录、弱智吧语录等等骂人的语录词汇,这非常重要!',
|
||||
// 伪人额外设定
|
||||
bymPreset: '',
|
||||
bymMode: 'gemini',
|
||||
geminiEnableGoogleSearch: false,
|
||||
geminiEnableCodeExecution: false,
|
||||
version: 'v2.8.2'
|
||||
|
|
@ -266,22 +267,22 @@ config.version = defaultConfig.version
|
|||
// config.version = latestTag
|
||||
|
||||
export const Config = new Proxy(config, {
|
||||
get(target, property) {
|
||||
get (target, property) {
|
||||
if (property === 'getGeminiKey') {
|
||||
return function () {
|
||||
if (target["geminiKey"]?.length === 0) {
|
||||
return "";
|
||||
if (target.geminiKey?.length === 0) {
|
||||
return ''
|
||||
}
|
||||
const geminiKeyArr = target["geminiKey"]?.trim().split(/[,,]/);
|
||||
const randomIndex = Math.floor(Math.random() * geminiKeyArr.length);
|
||||
logger.info(`[chatgpt]随机使用第${randomIndex + 1}个gemini Key: ${geminiKeyArr[randomIndex].replace(/(.{7}).*(.{10})/, '$1****$2')}`);
|
||||
return geminiKeyArr[randomIndex];
|
||||
const geminiKeyArr = target.geminiKey?.trim().split(/[,,]/)
|
||||
const randomIndex = Math.floor(Math.random() * geminiKeyArr.length)
|
||||
logger.info(`[chatgpt]随机使用第${randomIndex + 1}个gemini Key: ${geminiKeyArr[randomIndex].replace(/(.{7}).*(.{10})/, '$1****$2')}`)
|
||||
return geminiKeyArr[randomIndex]
|
||||
}
|
||||
}
|
||||
|
||||
return target[property]
|
||||
},
|
||||
set(target, property, value) {
|
||||
set (target, property, value) {
|
||||
target[property] = value
|
||||
const change = lodash.transform(target, function (result, value, key) {
|
||||
if (!lodash.isEqual(value, defaultConfig[key])) {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
{
|
||||
"compilerOptions": {
|
||||
"module": "es2020"
|
||||
"module": "es2020",
|
||||
"moduleResolution": "node",
|
||||
"esModuleInterop": true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,8 @@
|
|||
// @ts-ignore
|
||||
import Keyv from 'keyv'
|
||||
|
||||
export type Role = 'user' | 'assistant' | 'system' | 'function'
|
||||
|
||||
// @ts-ignore
|
||||
import fetch from 'node-fetch'
|
||||
export type FetchFn = typeof fetch
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue