mirror of
https://github.com/ikechan8370/chatgpt-plugin.git
synced 2025-12-16 21:37:11 +00:00
fix: refactor qwen mode
This commit is contained in:
parent
32af7b9a74
commit
b221098c37
6 changed files with 1505 additions and 1301 deletions
1084
apps/chat.js
1084
apps/chat.js
File diff suppressed because it is too large
Load diff
1127
model/core.js
Normal file
1127
model/core.js
Normal file
File diff suppressed because it is too large
Load diff
|
|
@ -63,7 +63,7 @@ import * as types from './types.js';
|
||||||
import globalFetch from 'node-fetch';
|
import globalFetch from 'node-fetch';
|
||||||
var CHATGPT_MODEL = 'qwen-turbo'; // qwen-plus
|
var CHATGPT_MODEL = 'qwen-turbo'; // qwen-plus
|
||||||
var USER_LABEL_DEFAULT = 'User';
|
var USER_LABEL_DEFAULT = 'User';
|
||||||
var ASSISTANT_LABEL_DEFAULT = '同义千问';
|
var ASSISTANT_LABEL_DEFAULT = '通义千问';
|
||||||
var QwenApi = /** @class */ (function () {
|
var QwenApi = /** @class */ (function () {
|
||||||
/**
|
/**
|
||||||
* Creates a new client wrapper around Qwen's chat completion API, mimicing the official ChatGPT webapp's functionality as closely as possible.
|
* Creates a new client wrapper around Qwen's chat completion API, mimicing the official ChatGPT webapp's functionality as closely as possible.
|
||||||
|
|
@ -76,11 +76,11 @@ var QwenApi = /** @class */ (function () {
|
||||||
this._apiBaseUrl = apiBaseUrl;
|
this._apiBaseUrl = apiBaseUrl;
|
||||||
this._debug = !!debug;
|
this._debug = !!debug;
|
||||||
this._fetch = fetch;
|
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: "text", incremental_output: false }, parameters) }, completionParams);
|
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;
|
this._systemMessage = systemMessage;
|
||||||
if (this._systemMessage === undefined) {
|
if (this._systemMessage === undefined) {
|
||||||
var currentDate = new Date().toISOString().split('T')[0];
|
var currentDate = new Date().toISOString().split('T')[0];
|
||||||
this._systemMessage = "You are ChatGPT, a large language model trained by Qwen. Answer as concisely as possible.\nKnowledge cutoff: 2021-09-01\nCurrent date: ".concat(currentDate);
|
this._systemMessage = "You are Qwen, a large language model trained by Alibaba Cloud. Answer as concisely as possible.\nCurrent date: ".concat(currentDate);
|
||||||
}
|
}
|
||||||
this._getMessageById = getMessageById !== null && getMessageById !== void 0 ? getMessageById : this._defaultGetMessageById;
|
this._getMessageById = getMessageById !== null && getMessageById !== void 0 ? getMessageById : this._defaultGetMessageById;
|
||||||
this._upsertMessage = upsertMessage !== null && upsertMessage !== void 0 ? upsertMessage : this._defaultUpsertMessage;
|
this._upsertMessage = upsertMessage !== null && upsertMessage !== void 0 ? upsertMessage : this._defaultUpsertMessage;
|
||||||
|
|
@ -120,7 +120,7 @@ var QwenApi = /** @class */ (function () {
|
||||||
* @param opts.timeoutMs - Optional timeout in milliseconds (defaults to no timeout)
|
* @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.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.abortSignal - Optional callback used to abort the underlying `fetch` call using an [AbortController](https://developer.mozilla.org/en-US/docs/Web/API/AbortController)
|
||||||
* @param 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.
|
* @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
|
* @returns The response from ChatGPT
|
||||||
*/
|
*/
|
||||||
|
|
@ -128,7 +128,7 @@ var QwenApi = /** @class */ (function () {
|
||||||
if (opts === void 0) { opts = {}; }
|
if (opts === void 0) { opts = {}; }
|
||||||
if (role === void 0) { role = 'user'; }
|
if (role === void 0) { role = 'user'; }
|
||||||
return __awaiter(this, void 0, void 0, function () {
|
return __awaiter(this, void 0, void 0, function () {
|
||||||
var parentMessageId, _a, messageId, timeoutMs, completionParams, conversationId, abortSignal, abortController, message, latestQuestion, _b, messages, maxTokens, numTokens, result, responseP;
|
var parentMessageId, _a, messageId, timeoutMs, completionParams, conversationId, abortSignal, abortController, message, latestQuestion, parameters, _b, messages, maxTokens, numTokens, result, responseP;
|
||||||
var _this = this;
|
var _this = this;
|
||||||
return __generator(this, function (_c) {
|
return __generator(this, function (_c) {
|
||||||
switch (_c.label) {
|
switch (_c.label) {
|
||||||
|
|
@ -148,6 +148,9 @@ var QwenApi = /** @class */ (function () {
|
||||||
text: text,
|
text: text,
|
||||||
};
|
};
|
||||||
latestQuestion = message;
|
latestQuestion = message;
|
||||||
|
parameters = Object.assign(this._completionParams.parameters, completionParams.parameters);
|
||||||
|
completionParams = Object.assign(this._completionParams, completionParams);
|
||||||
|
completionParams.parameters = parameters;
|
||||||
return [4 /*yield*/, this._buildMessages(text, role, opts, completionParams)];
|
return [4 /*yield*/, this._buildMessages(text, role, opts, completionParams)];
|
||||||
case 1:
|
case 1:
|
||||||
_b = _c.sent(), messages = _b.messages, maxTokens = _b.maxTokens, numTokens = _b.numTokens;
|
_b = _c.sent(), messages = _b.messages, maxTokens = _b.maxTokens, numTokens = _b.numTokens;
|
||||||
|
|
@ -158,28 +161,31 @@ var QwenApi = /** @class */ (function () {
|
||||||
conversationId: conversationId,
|
conversationId: conversationId,
|
||||||
parentMessageId: messageId,
|
parentMessageId: messageId,
|
||||||
text: undefined,
|
text: undefined,
|
||||||
|
functionCall: undefined,
|
||||||
|
conversation: []
|
||||||
};
|
};
|
||||||
this._completionParams.input = { messages: messages };
|
completionParams.input = { messages: messages };
|
||||||
responseP = new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
|
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 url, headers, body, res, reason, msg, error, response, err_1;
|
||||||
return __generator(this, function (_a) {
|
var _a, _b, _c, _d, _e;
|
||||||
switch (_a.label) {
|
return __generator(this, function (_f) {
|
||||||
|
switch (_f.label) {
|
||||||
case 0:
|
case 0:
|
||||||
url = "".concat(this._apiBaseUrl, "/services/aigc/text-generation/generation");
|
url = "".concat(this._apiBaseUrl, "/services/aigc/text-generation/generation");
|
||||||
headers = {
|
headers = {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
Authorization: "Bearer ".concat(this._apiKey)
|
Authorization: "Bearer ".concat(this._apiKey)
|
||||||
};
|
};
|
||||||
body = __assign(__assign({}, this._completionParams), completionParams);
|
body = completionParams;
|
||||||
if (this._debug) {
|
if (this._debug) {
|
||||||
console.log(JSON.stringify(body));
|
console.log(JSON.stringify(body));
|
||||||
}
|
}
|
||||||
if (this._debug) {
|
if (this._debug) {
|
||||||
console.log("sendMessage (".concat(numTokens, " tokens)"), body);
|
console.log("sendMessage (".concat(numTokens, " tokens)"), body);
|
||||||
}
|
}
|
||||||
_a.label = 1;
|
_f.label = 1;
|
||||||
case 1:
|
case 1:
|
||||||
_a.trys.push([1, 6, , 7]);
|
_f.trys.push([1, 6, , 7]);
|
||||||
return [4 /*yield*/, this._fetch(url, {
|
return [4 /*yield*/, this._fetch(url, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: headers,
|
headers: headers,
|
||||||
|
|
@ -187,11 +193,11 @@ var QwenApi = /** @class */ (function () {
|
||||||
signal: abortSignal
|
signal: abortSignal
|
||||||
})];
|
})];
|
||||||
case 2:
|
case 2:
|
||||||
res = _a.sent();
|
res = _f.sent();
|
||||||
if (!!res.ok) return [3 /*break*/, 4];
|
if (!!res.ok) return [3 /*break*/, 4];
|
||||||
return [4 /*yield*/, res.text()];
|
return [4 /*yield*/, res.text()];
|
||||||
case 3:
|
case 3:
|
||||||
reason = _a.sent();
|
reason = _f.sent();
|
||||||
msg = "Qwen error ".concat(res.status || res.statusText, ": ").concat(reason);
|
msg = "Qwen error ".concat(res.status || res.statusText, ": ").concat(reason);
|
||||||
error = new types.ChatGPTError(msg, { cause: res });
|
error = new types.ChatGPTError(msg, { cause: res });
|
||||||
error.statusCode = res.status;
|
error.statusCode = res.status;
|
||||||
|
|
@ -199,18 +205,22 @@ var QwenApi = /** @class */ (function () {
|
||||||
return [2 /*return*/, reject(error)];
|
return [2 /*return*/, reject(error)];
|
||||||
case 4: return [4 /*yield*/, res.json()];
|
case 4: return [4 /*yield*/, res.json()];
|
||||||
case 5:
|
case 5:
|
||||||
response = _a.sent();
|
response = _f.sent();
|
||||||
if (this._debug) {
|
if (this._debug) {
|
||||||
console.log(response);
|
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;
|
||||||
|
}
|
||||||
if (response === null || response === void 0 ? void 0 : response.request_id) {
|
if (response === null || response === void 0 ? void 0 : response.request_id) {
|
||||||
result.id = response.request_id;
|
result.id = response.request_id;
|
||||||
}
|
}
|
||||||
result.detail = response;
|
result.detail = response;
|
||||||
result.text = response.output.text;
|
result.text = response.output.choices[0].message.content;
|
||||||
return [2 /*return*/, resolve(result)];
|
return [2 /*return*/, resolve(result)];
|
||||||
case 6:
|
case 6:
|
||||||
err_1 = _a.sent();
|
err_1 = _f.sent();
|
||||||
return [2 /*return*/, reject(err_1)];
|
return [2 /*return*/, reject(err_1)];
|
||||||
case 7: return [2 /*return*/];
|
case 7: return [2 /*return*/];
|
||||||
}
|
}
|
||||||
|
|
@ -278,7 +288,8 @@ var QwenApi = /** @class */ (function () {
|
||||||
? messages.concat([
|
? messages.concat([
|
||||||
{
|
{
|
||||||
role: role,
|
role: role,
|
||||||
content: text
|
content: text,
|
||||||
|
name: role === 'tool' ? opts.name : undefined
|
||||||
}
|
}
|
||||||
])
|
])
|
||||||
: messages;
|
: messages;
|
||||||
|
|
@ -337,7 +348,8 @@ var QwenApi = /** @class */ (function () {
|
||||||
nextMessages = nextMessages.slice(0, systemMessageOffset).concat(__spreadArray([
|
nextMessages = nextMessages.slice(0, systemMessageOffset).concat(__spreadArray([
|
||||||
{
|
{
|
||||||
role: parentMessageRole,
|
role: parentMessageRole,
|
||||||
content: parentMessage.text
|
content: parentMessage.functionCall ? parentMessage.functionCall.arguments : parentMessage.text,
|
||||||
|
name: parentMessage.functionCall ? parentMessage.functionCall.name : undefined
|
||||||
}
|
}
|
||||||
], nextMessages.slice(systemMessageOffset), true));
|
], nextMessages.slice(systemMessageOffset), true));
|
||||||
parentMessageId = parentMessage.parentMessageId;
|
parentMessageId = parentMessage.parentMessageId;
|
||||||
|
|
|
||||||
|
|
@ -7,11 +7,12 @@ import * as tokenizer from './tokenizer'
|
||||||
import * as types from './types'
|
import * as types from './types'
|
||||||
import globalFetch from 'node-fetch'
|
import globalFetch from 'node-fetch'
|
||||||
import {qwen, Role} from "./types";
|
import {qwen, Role} from "./types";
|
||||||
|
import {openai} from "../openai/types";
|
||||||
|
|
||||||
const CHATGPT_MODEL = 'qwen-turbo' // qwen-plus
|
const CHATGPT_MODEL = 'qwen-turbo' // qwen-plus
|
||||||
|
|
||||||
const USER_LABEL_DEFAULT = 'User'
|
const USER_LABEL_DEFAULT = 'User'
|
||||||
const ASSISTANT_LABEL_DEFAULT = '同义千问'
|
const ASSISTANT_LABEL_DEFAULT = '通义千问'
|
||||||
|
|
||||||
export class QwenApi {
|
export class QwenApi {
|
||||||
protected _apiKey: string
|
protected _apiKey: string
|
||||||
|
|
@ -64,7 +65,7 @@ export class QwenApi {
|
||||||
temperature: 1.0,
|
temperature: 1.0,
|
||||||
seed: 114514,
|
seed: 114514,
|
||||||
enable_search: true,
|
enable_search: true,
|
||||||
result_format: "text",
|
result_format: "message",
|
||||||
incremental_output: false,
|
incremental_output: false,
|
||||||
...parameters
|
...parameters
|
||||||
},
|
},
|
||||||
|
|
@ -75,7 +76,7 @@ export class QwenApi {
|
||||||
|
|
||||||
if (this._systemMessage === undefined) {
|
if (this._systemMessage === undefined) {
|
||||||
const currentDate = new Date().toISOString().split('T')[0]
|
const currentDate = new Date().toISOString().split('T')[0]
|
||||||
this._systemMessage = `You are ChatGPT, a large language model trained by Qwen. Answer as concisely as possible.\nKnowledge cutoff: 2021-09-01\nCurrent date: ${currentDate}`
|
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._getMessageById = getMessageById ?? this._defaultGetMessageById
|
||||||
|
|
@ -120,7 +121,7 @@ export class QwenApi {
|
||||||
* @param opts.timeoutMs - Optional timeout in milliseconds (defaults to no timeout)
|
* @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.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.abortSignal - Optional callback used to abort the underlying `fetch` call using an [AbortController](https://developer.mozilla.org/en-US/docs/Web/API/AbortController)
|
||||||
* @param 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.
|
* @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
|
* @returns The response from ChatGPT
|
||||||
*/
|
*/
|
||||||
|
|
@ -129,7 +130,7 @@ export class QwenApi {
|
||||||
opts: types.SendMessageOptions = {},
|
opts: types.SendMessageOptions = {},
|
||||||
role: Role = 'user',
|
role: Role = 'user',
|
||||||
): Promise<types.ChatMessage> {
|
): Promise<types.ChatMessage> {
|
||||||
const {
|
let {
|
||||||
parentMessageId,
|
parentMessageId,
|
||||||
messageId = uuidv4(),
|
messageId = uuidv4(),
|
||||||
timeoutMs,
|
timeoutMs,
|
||||||
|
|
@ -155,21 +156,30 @@ export class QwenApi {
|
||||||
|
|
||||||
const latestQuestion = message
|
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(
|
const { messages, maxTokens, numTokens } = await this._buildMessages(
|
||||||
text,
|
text,
|
||||||
role,
|
role,
|
||||||
opts,
|
opts,
|
||||||
completionParams
|
completionParams
|
||||||
)
|
)
|
||||||
|
|
||||||
console.log(`maxTokens: ${maxTokens}, numTokens: ${numTokens}`)
|
console.log(`maxTokens: ${maxTokens}, numTokens: ${numTokens}`)
|
||||||
const result: types.ChatMessage = {
|
const result: types.ChatMessage & { conversation: qwen.ChatCompletionRequestMessage[] } = {
|
||||||
role: 'assistant',
|
role: 'assistant',
|
||||||
id: uuidv4(),
|
id: uuidv4(),
|
||||||
conversationId,
|
conversationId,
|
||||||
parentMessageId: messageId,
|
parentMessageId: messageId,
|
||||||
text: undefined,
|
text: undefined,
|
||||||
|
functionCall: undefined,
|
||||||
|
conversation: []
|
||||||
}
|
}
|
||||||
this._completionParams.input = { messages }
|
completionParams.input = { messages }
|
||||||
const responseP = new Promise<types.ChatMessage>(
|
const responseP = new Promise<types.ChatMessage>(
|
||||||
async (resolve, reject) => {
|
async (resolve, reject) => {
|
||||||
const url = `${this._apiBaseUrl}/services/aigc/text-generation/generation`
|
const url = `${this._apiBaseUrl}/services/aigc/text-generation/generation`
|
||||||
|
|
@ -177,10 +187,7 @@ export class QwenApi {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
Authorization: `Bearer ${this._apiKey}`
|
Authorization: `Bearer ${this._apiKey}`
|
||||||
}
|
}
|
||||||
const body = {
|
const body = completionParams
|
||||||
...this._completionParams,
|
|
||||||
...completionParams
|
|
||||||
}
|
|
||||||
if (this._debug) {
|
if (this._debug) {
|
||||||
console.log(JSON.stringify(body))
|
console.log(JSON.stringify(body))
|
||||||
}
|
}
|
||||||
|
|
@ -212,12 +219,15 @@ export class QwenApi {
|
||||||
if (this._debug) {
|
if (this._debug) {
|
||||||
console.log(response)
|
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) {
|
if (response?.request_id) {
|
||||||
result.id = response.request_id
|
result.id = response.request_id
|
||||||
}
|
}
|
||||||
result.detail = response
|
result.detail = response
|
||||||
result.text = response.output.text
|
result.text = response.output.choices[0].message.content
|
||||||
return resolve(result)
|
return resolve(result)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return reject(err)
|
return reject(err)
|
||||||
|
|
@ -283,7 +293,8 @@ export class QwenApi {
|
||||||
? messages.concat([
|
? messages.concat([
|
||||||
{
|
{
|
||||||
role,
|
role,
|
||||||
content: text
|
content: text,
|
||||||
|
name: role === 'tool' ? opts.name : undefined
|
||||||
}
|
}
|
||||||
])
|
])
|
||||||
: messages
|
: messages
|
||||||
|
|
@ -338,7 +349,8 @@ export class QwenApi {
|
||||||
nextMessages = nextMessages.slice(0, systemMessageOffset).concat([
|
nextMessages = nextMessages.slice(0, systemMessageOffset).concat([
|
||||||
{
|
{
|
||||||
role: parentMessageRole,
|
role: parentMessageRole,
|
||||||
content: parentMessage.text
|
content: parentMessage.functionCall ? parentMessage.functionCall.arguments : parentMessage.text,
|
||||||
|
name: parentMessage.functionCall ? parentMessage.functionCall.name : undefined
|
||||||
},
|
},
|
||||||
...nextMessages.slice(systemMessageOffset)
|
...nextMessages.slice(systemMessageOffset)
|
||||||
])
|
])
|
||||||
|
|
|
||||||
|
|
@ -1,80 +1,82 @@
|
||||||
import Keyv from 'keyv'
|
import Keyv from 'keyv'
|
||||||
|
import {openai} from "../openai/types";
|
||||||
|
|
||||||
export type Role = 'user' | 'assistant' | 'system'
|
export type Role = 'user' | 'assistant' | 'system' | 'tool'
|
||||||
|
|
||||||
export type FetchFn = typeof fetch
|
export type FetchFn = typeof fetch
|
||||||
|
|
||||||
export type QWenAPIOptions = {
|
export type QWenAPIOptions = {
|
||||||
apiKey: string
|
apiKey: string
|
||||||
|
|
||||||
/** @defaultValue `'https://dashscope.aliyuncs.com/api/v1'` **/
|
/** @defaultValue `'https://dashscope.aliyuncs.com/api/v1'` **/
|
||||||
apiBaseUrl?: string
|
apiBaseUrl?: string
|
||||||
|
|
||||||
apiOrg?: string
|
apiOrg?: string
|
||||||
|
|
||||||
/** @defaultValue `false` **/
|
/** @defaultValue `false` **/
|
||||||
debug?: boolean
|
debug?: boolean
|
||||||
|
|
||||||
completionParams?: Partial<
|
completionParams?: Partial<
|
||||||
Omit<qwen.CreateChatCompletionRequest, 'messages' | 'n' | 'stream'>
|
Omit<qwen.CreateChatCompletionRequest, 'messages' | 'n' | 'stream'>
|
||||||
>
|
>
|
||||||
parameters?: qwen.QWenParameters,
|
parameters?: qwen.QWenParameters,
|
||||||
|
|
||||||
systemMessage?: string
|
systemMessage?: string
|
||||||
|
|
||||||
messageStore?: Keyv
|
messageStore?: Keyv
|
||||||
getMessageById?: GetMessageByIdFunction
|
getMessageById?: GetMessageByIdFunction
|
||||||
upsertMessage?: UpsertMessageFunction
|
upsertMessage?: UpsertMessageFunction
|
||||||
|
|
||||||
fetch?: FetchFn
|
fetch?: FetchFn
|
||||||
}
|
}
|
||||||
|
|
||||||
export type SendMessageOptions = {
|
export type SendMessageOptions = {
|
||||||
/**
|
/**
|
||||||
* function role name
|
* function role name
|
||||||
*/
|
*/
|
||||||
name?: string
|
name?: string
|
||||||
messageId?: string
|
messageId?: string
|
||||||
stream?: boolean
|
stream?: boolean
|
||||||
systemMessage?: string
|
systemMessage?: string
|
||||||
parentMessageId?: string
|
parentMessageId?: string
|
||||||
conversationId?: string
|
conversationId?: string
|
||||||
timeoutMs?: number
|
timeoutMs?: number
|
||||||
onProgress?: (partialResponse: ChatMessage) => void
|
onProgress?: (partialResponse: ChatMessage) => void
|
||||||
abortSignal?: AbortSignal
|
abortSignal?: AbortSignal
|
||||||
completionParams?: Partial<
|
completionParams?: Partial<
|
||||||
Omit<qwen.CreateChatCompletionRequest, 'messages' | 'n' | 'stream'>
|
Omit<qwen.CreateChatCompletionRequest, 'messages' | 'n' | 'stream'>
|
||||||
>
|
>
|
||||||
}
|
}
|
||||||
|
|
||||||
export type MessageActionType = 'next' | 'variant'
|
export type MessageActionType = 'next' | 'variant'
|
||||||
|
|
||||||
export type SendMessageBrowserOptions = {
|
export type SendMessageBrowserOptions = {
|
||||||
conversationId?: string
|
conversationId?: string
|
||||||
parentMessageId?: string
|
parentMessageId?: string
|
||||||
messageId?: string
|
messageId?: string
|
||||||
action?: MessageActionType
|
action?: MessageActionType
|
||||||
timeoutMs?: number
|
timeoutMs?: number
|
||||||
onProgress?: (partialResponse: ChatMessage) => void
|
onProgress?: (partialResponse: ChatMessage) => void
|
||||||
abortSignal?: AbortSignal
|
abortSignal?: AbortSignal
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ChatMessage {
|
export interface ChatMessage {
|
||||||
id: string
|
id: string
|
||||||
text: string
|
text: string
|
||||||
role: Role
|
role: Role
|
||||||
parentMessageId?: string
|
parentMessageId?: string
|
||||||
conversationId?: string
|
conversationId?: string
|
||||||
detail?:
|
detail?:
|
||||||
| qwen.CreateChatCompletionResponse
|
| qwen.CreateChatCompletionResponse
|
||||||
| CreateChatCompletionStreamResponse
|
| CreateChatCompletionStreamResponse
|
||||||
|
functionCall?: qwen.FunctionCall
|
||||||
}
|
}
|
||||||
|
|
||||||
export class ChatGPTError extends Error {
|
export class ChatGPTError extends Error {
|
||||||
statusCode?: number
|
statusCode?: number
|
||||||
statusText?: string
|
statusText?: string
|
||||||
isFinal?: boolean
|
isFinal?: boolean
|
||||||
accountId?: string
|
accountId?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Returns a chat message from a store by it's ID (or null if not found). */
|
/** Returns a chat message from a store by it's ID (or null if not found). */
|
||||||
|
|
@ -84,230 +86,289 @@ export type GetMessageByIdFunction = (id: string) => Promise<ChatMessage>
|
||||||
export type UpsertMessageFunction = (message: ChatMessage) => Promise<void>
|
export type UpsertMessageFunction = (message: ChatMessage) => Promise<void>
|
||||||
|
|
||||||
export interface CreateChatCompletionStreamResponse
|
export interface CreateChatCompletionStreamResponse
|
||||||
extends openai.CreateChatCompletionDeltaResponse {
|
extends openai.CreateChatCompletionDeltaResponse {
|
||||||
usage: CreateCompletionStreamResponseUsage
|
usage: CreateCompletionStreamResponseUsage
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface CreateCompletionStreamResponseUsage
|
export interface CreateCompletionStreamResponseUsage
|
||||||
extends openai.CreateCompletionResponseUsage {
|
extends openai.CreateCompletionResponseUsage {
|
||||||
estimated: true
|
estimated: true
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* https://chat.openapi.com/backend-api/conversation
|
* https://chat.openapi.com/backend-api/conversation
|
||||||
*/
|
*/
|
||||||
export type ConversationJSONBody = {
|
export type ConversationJSONBody = {
|
||||||
/**
|
/**
|
||||||
* The action to take
|
* The action to take
|
||||||
*/
|
*/
|
||||||
action: string
|
action: string
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The ID of the conversation
|
* The ID of the conversation
|
||||||
*/
|
*/
|
||||||
conversation_id?: string
|
conversation_id?: string
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Prompts to provide
|
* Prompts to provide
|
||||||
*/
|
*/
|
||||||
messages: Prompt[]
|
messages: Prompt[]
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The model to use
|
* The model to use
|
||||||
*/
|
*/
|
||||||
model: string
|
model: string
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The parent message ID
|
* The parent message ID
|
||||||
*/
|
*/
|
||||||
parent_message_id: string
|
parent_message_id: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export type Prompt = {
|
export type Prompt = {
|
||||||
/**
|
/**
|
||||||
* The content of the prompt
|
* The content of the prompt
|
||||||
*/
|
*/
|
||||||
content: PromptContent
|
content: PromptContent
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The ID of the prompt
|
* The ID of the prompt
|
||||||
*/
|
*/
|
||||||
id: string
|
id: string
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The role played in the prompt
|
* The role played in the prompt
|
||||||
*/
|
*/
|
||||||
role: Role
|
role: Role
|
||||||
}
|
}
|
||||||
|
|
||||||
export type ContentType = 'text'
|
export type ContentType = 'text'
|
||||||
|
|
||||||
export type PromptContent = {
|
export type PromptContent = {
|
||||||
/**
|
/**
|
||||||
* The content type of the prompt
|
* The content type of the prompt
|
||||||
*/
|
*/
|
||||||
content_type: ContentType
|
content_type: ContentType
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The parts to the prompt
|
* The parts to the prompt
|
||||||
*/
|
*/
|
||||||
parts: string[]
|
parts: string[]
|
||||||
}
|
}
|
||||||
|
|
||||||
export type ConversationResponseEvent = {
|
export type ConversationResponseEvent = {
|
||||||
message?: Message
|
message?: Message
|
||||||
conversation_id?: string
|
conversation_id?: string
|
||||||
error?: string | null
|
error?: string | null
|
||||||
}
|
}
|
||||||
|
|
||||||
export type Message = {
|
export type Message = {
|
||||||
id: string
|
id: string
|
||||||
content: MessageContent
|
content: MessageContent
|
||||||
role: Role
|
role: Role
|
||||||
user: string | null
|
user: string | null
|
||||||
create_time: string | null
|
create_time: string | null
|
||||||
update_time: string | null
|
update_time: string | null
|
||||||
end_turn: null
|
end_turn: null
|
||||||
weight: number
|
weight: number
|
||||||
recipient: string
|
recipient: string
|
||||||
metadata: MessageMetadata
|
metadata: MessageMetadata
|
||||||
}
|
}
|
||||||
|
|
||||||
export type MessageContent = {
|
export type MessageContent = {
|
||||||
content_type: string
|
content_type: string
|
||||||
parts: string[]
|
parts: string[]
|
||||||
}
|
}
|
||||||
|
|
||||||
export type MessageMetadata = any
|
export type MessageMetadata = any
|
||||||
|
|
||||||
export namespace qwen {
|
export namespace qwen {
|
||||||
export interface CreateChatCompletionDeltaResponse {
|
export interface CreateChatCompletionDeltaResponse {
|
||||||
id: string
|
id: string
|
||||||
object: 'chat.completion.chunk'
|
object: 'chat.completion.chunk'
|
||||||
created: number
|
created: number
|
||||||
model: string
|
model: string
|
||||||
choices: [
|
choices: [
|
||||||
{
|
{
|
||||||
delta: {
|
delta: {
|
||||||
role: Role
|
role: Role
|
||||||
content?: string,
|
content?: string,
|
||||||
function_call?: {name: string, arguments: string}
|
function_call?: { name: string, arguments: string }
|
||||||
}
|
}
|
||||||
index: number
|
index: number
|
||||||
finish_reason: string | null
|
finish_reason: string | null
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @export
|
||||||
|
* @interface ChatCompletionRequestMessage
|
||||||
|
*/
|
||||||
|
export interface ChatCompletionRequestMessage {
|
||||||
|
/**
|
||||||
|
* The role of the author of this message.
|
||||||
|
* @type {string}
|
||||||
|
* @memberof ChatCompletionRequestMessage
|
||||||
|
*/
|
||||||
|
role: ChatCompletionRequestMessageRoleEnum
|
||||||
|
/**
|
||||||
|
* The contents of the message
|
||||||
|
* @type {string}
|
||||||
|
* @memberof ChatCompletionRequestMessage
|
||||||
|
*/
|
||||||
|
content: string
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* role为tool表示当前message为function_call的调用结果,name是function的名称,需要和上轮response中的tool_calls[i].function.name参数保持一致,content为function的输出。
|
||||||
* @export
|
|
||||||
* @interface ChatCompletionRequestMessage
|
|
||||||
*/
|
*/
|
||||||
export interface ChatCompletionRequestMessage {
|
name?: string
|
||||||
/**
|
}
|
||||||
* The role of the author of this message.
|
|
||||||
* @type {string}
|
|
||||||
* @memberof ChatCompletionRequestMessage
|
|
||||||
*/
|
|
||||||
role: ChatCompletionRequestMessageRoleEnum
|
|
||||||
/**
|
|
||||||
* The contents of the message
|
|
||||||
* @type {string}
|
|
||||||
* @memberof ChatCompletionRequestMessage
|
|
||||||
*/
|
|
||||||
content: string
|
|
||||||
}
|
|
||||||
|
|
||||||
export declare const ChatCompletionRequestMessageRoleEnum: {
|
export interface FunctionCall {
|
||||||
readonly System: 'system'
|
name: string
|
||||||
readonly User: 'user'
|
arguments: string
|
||||||
readonly Assistant: 'assistant'
|
}
|
||||||
}
|
|
||||||
export declare type ChatCompletionRequestMessageRoleEnum =
|
export declare const ChatCompletionRequestMessageRoleEnum: {
|
||||||
(typeof ChatCompletionRequestMessageRoleEnum)[keyof typeof ChatCompletionRequestMessageRoleEnum]
|
readonly System: 'system'
|
||||||
|
readonly User: 'user'
|
||||||
|
readonly Assistant: 'assistant'
|
||||||
|
readonly Tool: 'tool'
|
||||||
|
}
|
||||||
|
export declare type ChatCompletionRequestMessageRoleEnum =
|
||||||
|
(typeof ChatCompletionRequestMessageRoleEnum)[keyof typeof ChatCompletionRequestMessageRoleEnum]
|
||||||
|
|
||||||
|
|
||||||
export interface QWenInput {
|
export interface QWenInput {
|
||||||
messages: Array<ChatCompletionRequestMessage>
|
messages: Array<ChatCompletionRequestMessage>
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface QWenParameters {
|
export interface QWenParameters {
|
||||||
result_format: string
|
result_format: "text" | "message"
|
||||||
top_p: number
|
top_p: number
|
||||||
top_k: number
|
top_k: number
|
||||||
seed: number
|
seed: number
|
||||||
temperature: number
|
temperature: number
|
||||||
enable_search: boolean
|
enable_search: boolean
|
||||||
incremental_output: boolean
|
incremental_output: boolean
|
||||||
}
|
tools: Tools[]
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface Tools {
|
||||||
|
type: "function"
|
||||||
|
function: QwenFunction
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface QwenFunction {
|
||||||
|
name: string
|
||||||
|
description: string
|
||||||
|
parameters: QwenFunctionParameters
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface QwenFunctionParameters {
|
||||||
|
type: "object"
|
||||||
|
properties: Properties;
|
||||||
|
required?: string[]
|
||||||
|
}
|
||||||
|
|
||||||
|
interface Properties {
|
||||||
|
[key: string]: Property;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface Property {
|
||||||
|
type: string;
|
||||||
|
description?: string;
|
||||||
|
enum?: string[];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @export
|
||||||
|
* @interface CreateChatCompletionRequest
|
||||||
|
*/
|
||||||
|
export interface CreateChatCompletionRequest {
|
||||||
|
/**
|
||||||
|
* ID of the model to use. Currently, only `gpt-3.5-turbo` and `gpt-3.5-turbo-0301` are supported.
|
||||||
|
* @type {string}
|
||||||
|
* @memberof CreateChatCompletionRequest
|
||||||
|
*/
|
||||||
|
model: string
|
||||||
|
/**
|
||||||
|
* The messages to generate chat completions for, in the [chat format](/docs/guides/chat/introduction).
|
||||||
|
* @type {Array<ChatCompletionRequestMessage>}
|
||||||
|
* @memberof CreateChatCompletionRequest
|
||||||
|
*/
|
||||||
|
input?: QWenInput
|
||||||
|
|
||||||
|
parameters: QWenParameters
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @export
|
||||||
|
* @interface CreateChatCompletionResponse
|
||||||
|
*/
|
||||||
|
export interface CreateChatCompletionResponse {
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @export
|
* @type {string}
|
||||||
* @interface CreateChatCompletionRequest
|
* @memberof CreateChatCompletionResponse
|
||||||
*/
|
*/
|
||||||
export interface CreateChatCompletionRequest {
|
request_id: string
|
||||||
/**
|
/**
|
||||||
* ID of the model to use. Currently, only `gpt-3.5-turbo` and `gpt-3.5-turbo-0301` are supported.
|
*
|
||||||
* @type {string}
|
* @type {QWenOutput}
|
||||||
* @memberof CreateChatCompletionRequest
|
* @memberof CreateChatCompletionResponse
|
||||||
*/
|
*/
|
||||||
model: string
|
output: QWenOutput
|
||||||
/**
|
/**
|
||||||
* The messages to generate chat completions for, in the [chat format](/docs/guides/chat/introduction).
|
*
|
||||||
* @type {Array<ChatCompletionRequestMessage>}
|
* @type {CreateCompletionResponseUsage}
|
||||||
* @memberof CreateChatCompletionRequest
|
* @memberof CreateChatCompletionResponse
|
||||||
*/
|
*/
|
||||||
input?: QWenInput
|
usage?: CreateCompletionResponseUsage
|
||||||
|
}
|
||||||
|
|
||||||
parameters: QWenParameters
|
export interface QWenOutput {
|
||||||
}
|
finish_reason: string
|
||||||
|
text?: string
|
||||||
|
choices?: Choice[]
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface Choice {
|
||||||
|
finish_reason: string
|
||||||
|
message: ResponseMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ResponseMessage {
|
||||||
|
role: Role
|
||||||
|
content: string
|
||||||
|
tool_calls: ToolCall[]
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ToolCall {
|
||||||
|
function: FunctionCall
|
||||||
|
type: "function"
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @export
|
||||||
|
* @interface CreateCompletionResponseUsage
|
||||||
|
*/
|
||||||
|
export interface CreateCompletionResponseUsage {
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @export
|
* @type {number}
|
||||||
* @interface CreateChatCompletionResponse
|
* @memberof CreateCompletionResponseUsage
|
||||||
*/
|
*/
|
||||||
export interface CreateChatCompletionResponse {
|
input_tokens: number
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @type {string}
|
|
||||||
* @memberof CreateChatCompletionResponse
|
|
||||||
*/
|
|
||||||
request_id: string
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @type {QWenOutput}
|
|
||||||
* @memberof CreateChatCompletionResponse
|
|
||||||
*/
|
|
||||||
output: QWenOutput
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @type {CreateCompletionResponseUsage}
|
|
||||||
* @memberof CreateChatCompletionResponse
|
|
||||||
*/
|
|
||||||
usage?: CreateCompletionResponseUsage
|
|
||||||
}
|
|
||||||
export interface QWenOutput {
|
|
||||||
finish_reason: string
|
|
||||||
text: string
|
|
||||||
}
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @export
|
* @type {number}
|
||||||
* @interface CreateCompletionResponseUsage
|
* @memberof CreateCompletionResponseUsage
|
||||||
*/
|
*/
|
||||||
export interface CreateCompletionResponseUsage {
|
output_tokens: number
|
||||||
/**
|
}
|
||||||
*
|
|
||||||
* @type {number}
|
|
||||||
* @memberof CreateCompletionResponseUsage
|
|
||||||
*/
|
|
||||||
input_tokens: number
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @type {number}
|
|
||||||
* @memberof CreateCompletionResponseUsage
|
|
||||||
*/
|
|
||||||
output_tokens: number
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -176,7 +176,7 @@ export class ChatGPTAPI {
|
||||||
completionParams
|
completionParams
|
||||||
)
|
)
|
||||||
console.log(`maxTokens: ${maxTokens}, numTokens: ${numTokens}`)
|
console.log(`maxTokens: ${maxTokens}, numTokens: ${numTokens}`)
|
||||||
const result: types.ChatMessage & { conversation: openai.ChatCompletionRequestMessage[] }= {
|
const result: types.ChatMessage & { conversation: openai.ChatCompletionRequestMessage[] } = {
|
||||||
role: 'assistant',
|
role: 'assistant',
|
||||||
id: uuidv4(),
|
id: uuidv4(),
|
||||||
conversationId,
|
conversationId,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue