doomiaichat 1.4.5 → 2.1.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/dist/azureai.d.ts +14 -0
- package/dist/azureai.js +73 -0
- package/dist/declare.d.ts +99 -0
- package/dist/declare.js +2 -0
- package/dist/index.d.ts +1 -162
- package/dist/index.js +24 -360
- package/dist/openai.d.ts +94 -0
- package/dist/openai.js +496 -0
- package/dist/openaiprovider.d.ts +22 -0
- package/dist/openaiprovider.js +39 -0
- package/package.json +6 -3
- package/src/azureai.ts +65 -0
- package/src/declare.ts +105 -0
- package/src/index.ts +1 -429
- package/src/openai.ts +477 -0
- package/src/openaiprovider.ts +32 -0
package/dist/index.js
CHANGED
|
@@ -1,363 +1,27 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
10
24
|
};
|
|
11
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.
|
|
13
|
-
|
|
14
|
-
const events_1 = require("events");
|
|
15
|
-
const SECTION_LENGTH = 256; ///每256个字符分成一组
|
|
16
|
-
const MESSAGE_LENGTH = 8; ///每次送8句话给openai 进行解析,送多了,会报错
|
|
17
|
-
//请将答案放在最后,标记为答案:()
|
|
18
|
-
const QUESTION_TEXT_MAPPING = {
|
|
19
|
-
singlechoice: ',根据以上内容,生成1道单选题,每道题目4个选项,请按照{"question":"","choice":[],"answer":[]}的JSON结构输出,choice中的元素用大写字母ABCD开头,answer数组中包含一个正确答案',
|
|
20
|
-
multiplechoice: ',根据以上内容,请生成1道多选题,提供4个选项,答案至少1个以上选项,请按照{"question":"","choice":[],"answer":[]}的JSON结构输出,choice中的元素用大写字母ABCD开头,answer数组中包含正确答案选项',
|
|
21
|
-
trueorfalse: ',根据以上内容,请生成1道判断题,请按照{"question":"","choice":["A.正确","B.错误"],"answer":[]}的JSON结构输出,answer数组中包含一个元素,"正确"或"错误"',
|
|
22
|
-
completion: ',根据以上内容,请生成1道填空题,每道题目1个填空,请按照{"question":"","answer":[]}的JSON结构输出,answer数组中包含填空答案' //请将答案放在最后,标记为答案:()
|
|
23
|
-
};
|
|
24
|
-
const QUESTION_TYPE = ['singlechoice', 'multiplechoice', 'trueorfalse', 'completion'];
|
|
25
|
-
class AIChat extends events_1.EventEmitter {
|
|
26
|
-
/**
|
|
27
|
-
*
|
|
28
|
-
* @param apiKey 调用OpenAI 的key
|
|
29
|
-
* @param apiOption
|
|
30
|
-
*/
|
|
31
|
-
constructor(apiKey, apiOption = {}) {
|
|
32
|
-
super();
|
|
33
|
-
this.chatRobot = new openai_1.OpenAIApi(new openai_1.Configuration({ apiKey }));
|
|
34
|
-
this.chatModel = apiOption.model || 'gpt-3.5-turbo';
|
|
35
|
-
this.maxtoken = apiOption.maxtoken || 1024;
|
|
36
|
-
this.temperature = apiOption.temperature || 0.9;
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* 向OpenAI发送一个聊天请求
|
|
40
|
-
* @param {*} chatText
|
|
41
|
-
*/
|
|
42
|
-
chatRequest(chatText, callChatOption, axiosOption = {}) {
|
|
43
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
44
|
-
if (!chatText)
|
|
45
|
-
return { successed: false, error: { errcode: 2, errmsg: '缺失聊天的内容' } };
|
|
46
|
-
if (!this.chatRobot)
|
|
47
|
-
return { successed: false, error: { errcode: 1, errmsg: '聊天机器人无效' } };
|
|
48
|
-
let message = typeof (chatText) == 'string' ?
|
|
49
|
-
[{ role: 'user', content: chatText }] : chatText;
|
|
50
|
-
// console.log('message', message)
|
|
51
|
-
try {
|
|
52
|
-
const response = yield this.chatRobot.createChatCompletion({
|
|
53
|
-
model: (callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.model) || this.chatModel,
|
|
54
|
-
messages: message,
|
|
55
|
-
temperature: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.temperature) || this.temperature),
|
|
56
|
-
max_tokens: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.maxtoken) || this.maxtoken),
|
|
57
|
-
n: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.replyCounts) || 1) || 1
|
|
58
|
-
}, axiosOption);
|
|
59
|
-
return { successed: true, message: response.data.choices };
|
|
60
|
-
}
|
|
61
|
-
catch (error) {
|
|
62
|
-
return { successed: false, error };
|
|
63
|
-
}
|
|
64
|
-
});
|
|
65
|
-
}
|
|
66
|
-
/**
|
|
67
|
-
* 判断一句话的表达情绪
|
|
68
|
-
* @param {*} s1
|
|
69
|
-
* @param {*} axiosOption
|
|
70
|
-
*/
|
|
71
|
-
getScentenceEmotional(s1, axiosOption = { timeout: 30000 }) {
|
|
72
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
73
|
-
if (!s1)
|
|
74
|
-
return { successed: false, error: { errcode: 2, errmsg: '缺失参数' } };
|
|
75
|
-
const emotion = ['愤怒', '威胁', '讽刺', '愧疚', '兴奋', '友好', '消极', '生气', '正常'];
|
|
76
|
-
const messages = [
|
|
77
|
-
{ role: 'user', content: s1 },
|
|
78
|
-
{ role: 'user', content: `请分析上述内容的语言情绪,请从"${emotion.join(',')}"这些情绪中对应一个输出` },
|
|
79
|
-
];
|
|
80
|
-
const result = yield this.chatRequest(messages, {}, axiosOption);
|
|
81
|
-
if (result.successed && result.message) {
|
|
82
|
-
let value = result.message[0].message.content.trim();
|
|
83
|
-
for (const word of emotion) {
|
|
84
|
-
if (value.indexOf(word) >= 0)
|
|
85
|
-
return { successed: true, emotion: word };
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
return { successed: true, emotion: '未知' };
|
|
89
|
-
});
|
|
90
|
-
}
|
|
91
|
-
/**
|
|
92
|
-
* 获取两句话的相似度取值
|
|
93
|
-
* @param {*} s1
|
|
94
|
-
* @param {*} s2
|
|
95
|
-
*/
|
|
96
|
-
getScentenseSimilarity(s1, s2, axiosOption = { timeout: 30000 }) {
|
|
97
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
98
|
-
if (!s1 || !s2)
|
|
99
|
-
return { successed: false, error: { errcode: 2, errmsg: '缺失参数' } };
|
|
100
|
-
const messages = [
|
|
101
|
-
{ role: 'user', content: s1 },
|
|
102
|
-
{ role: 'user', content: s2 },
|
|
103
|
-
{ role: 'user', content: '请从语义上对比以上两句话的相似度,请仅输出0至100之间的整数对比结果即可' },
|
|
104
|
-
];
|
|
105
|
-
const result = yield this.chatRequest(messages, { maxtoken: 32 }, axiosOption);
|
|
106
|
-
if (result.successed && result.message) {
|
|
107
|
-
let value = result.message[0].message.content.replace(/[^\d]/g, "");
|
|
108
|
-
if (value > 100)
|
|
109
|
-
value = Math.floor(value / 10);
|
|
110
|
-
return { successed: true, value: Number(value) };
|
|
111
|
-
}
|
|
112
|
-
return { successed: false, error: result.error, value: 0 };
|
|
113
|
-
});
|
|
114
|
-
}
|
|
115
|
-
/**
|
|
116
|
-
* 获得一种内容的相似说法
|
|
117
|
-
* 比如:
|
|
118
|
-
* 你今年多大?
|
|
119
|
-
* 相似问法:您是哪一年出生的
|
|
120
|
-
* 您今年贵庚?
|
|
121
|
-
* @param {*} content
|
|
122
|
-
* @param {需要出来的数量} count
|
|
123
|
-
*/
|
|
124
|
-
getSimilarityContent(content, count = 1, axiosOption = {}) {
|
|
125
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
126
|
-
let chnReg = /([\u4e00-\u9fa5]|[\ufe30-\uffa0])/.test(content); ///检查源话是否含有中文内容
|
|
127
|
-
let engReg = /[a-zA-Z]/.test(content); ///检查源话是否含有英文内容
|
|
128
|
-
///如果源话是全中文,那么结果中不应该出来英文的相似说法,如果源话是全英文,则结果不能出现全中文的说法
|
|
129
|
-
let prefix = (!chnReg && engReg) ? '请用完整的英文表达,' : ((chnReg && !engReg) ? '请用完整的中文表达,' : '');
|
|
130
|
-
const text = `${prefix}生成与下面句子意思相同的内容"${content}"`;
|
|
131
|
-
let result = yield this.chatRequest(text, { replyCounts: count }, axiosOption);
|
|
132
|
-
if (!result.successed || !result.message)
|
|
133
|
-
return result;
|
|
134
|
-
let replys = result.message.map(item => { return item.message.content.trim(); });
|
|
135
|
-
return { successed: true, message: replys };
|
|
136
|
-
});
|
|
137
|
-
}
|
|
138
|
-
/**
|
|
139
|
-
* 提取内容的中心思想摘要
|
|
140
|
-
* @param content
|
|
141
|
-
* @param axiosOption
|
|
142
|
-
*/
|
|
143
|
-
getSummaryOfContent(content, axiosOption = {}) {
|
|
144
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
145
|
-
const arrContent = typeof (content) == 'string' ? this.splitLongText(content) : content;
|
|
146
|
-
let summary = [];
|
|
147
|
-
while (arrContent.length > 0) {
|
|
148
|
-
let subarray = arrContent.slice(0, MESSAGE_LENGTH);
|
|
149
|
-
subarray.push({ role: 'user', content: '根据上述内容精简,提炼摘要内容,并将摘要逐条放入JSON数组结构输出' });
|
|
150
|
-
let result = yield this.chatRequest(subarray, {}, axiosOption);
|
|
151
|
-
if (result.successed && result.message) {
|
|
152
|
-
try {
|
|
153
|
-
let jsonObjItems = JSON.parse(result.message[0].content);
|
|
154
|
-
console.log('result.message[0].content', result.message[0].content);
|
|
155
|
-
if (Array.isArray(jsonObjItems))
|
|
156
|
-
summary = summary.concat(jsonObjItems);
|
|
157
|
-
}
|
|
158
|
-
catch (error) {
|
|
159
|
-
console.log('result.message[0].content', error, result.message[0].content);
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
////删除已经处理的文本
|
|
163
|
-
arrContent.splice(0, MESSAGE_LENGTH);
|
|
164
|
-
}
|
|
165
|
-
return { successed: true, summary };
|
|
166
|
-
});
|
|
167
|
-
}
|
|
168
|
-
/**
|
|
169
|
-
* 从指定的文本内容中生成相关的问答
|
|
170
|
-
* @param {*} content
|
|
171
|
-
* @param {*} count
|
|
172
|
-
* @param {*} axiosOption
|
|
173
|
-
* @returns
|
|
174
|
-
*/ //并在答案末尾处必须给出答案内容中的关键词
|
|
175
|
-
generateQuestionsFromContent(content, count = 1, axiosOption = {}) {
|
|
176
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
177
|
-
let arrContent = this.splitLongText(content);
|
|
178
|
-
///没20句话分为一组,适应大文件内容多次请求组合结果
|
|
179
|
-
///每一句话需要产生的题目
|
|
180
|
-
let questions4EverySentense = count / arrContent.length; //Math.ceil(arrContent.length / 20);
|
|
181
|
-
let faqs = [], gotted = 0;
|
|
182
|
-
while (arrContent.length > 0 && gotted < count) {
|
|
183
|
-
////每次最多送MESSAGE_LENGTH句话给openai
|
|
184
|
-
let subarray = arrContent.slice(0, MESSAGE_LENGTH);
|
|
185
|
-
let itemCount = Math.min(Math.ceil(subarray.length * questions4EverySentense), count - gotted);
|
|
186
|
-
//subarray.push({ role: 'user', content:'请根据上述内容,给出一道提问与答案以及答案关键词,按照先问题内容,再标准答案,再关键词的顺序输出,关键词之间用、分开'})
|
|
187
|
-
subarray.push({ role: 'user', content: '请根据上述内容,给出一道提问与答案以及答案关键词,按照{"question":"","answer":"","keywords":[]}的JSON结构输出,keywords数组中的关键词必须存在于答案内容中' });
|
|
188
|
-
let result = yield this.chatRequest(subarray, { replyCounts: itemCount }, axiosOption);
|
|
189
|
-
if (result.successed && result.message) {
|
|
190
|
-
let msgs = this.pickUpFaqContent(result.message);
|
|
191
|
-
if (msgs.length) {
|
|
192
|
-
///对外发送检出问答题的信号
|
|
193
|
-
this.emit('parseout', { type: 'qa', items: msgs });
|
|
194
|
-
gotted += msgs.length; //result.message.length;
|
|
195
|
-
// console.log('gotted=', gotted)
|
|
196
|
-
faqs = faqs.concat(msgs);
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
////删除已经处理的文本
|
|
200
|
-
arrContent.splice(0, MESSAGE_LENGTH);
|
|
201
|
-
}
|
|
202
|
-
arrContent = []; /// 释放内存
|
|
203
|
-
///发出信号,解析完毕
|
|
204
|
-
this.emit('parseover', { type: 'qa', items: faqs });
|
|
205
|
-
return { successed: true, message: faqs };
|
|
206
|
-
});
|
|
207
|
-
}
|
|
208
|
-
/**
|
|
209
|
-
* 解析Faq返回的问题
|
|
210
|
-
* @param {*} messages
|
|
211
|
-
* @returns
|
|
212
|
-
*/
|
|
213
|
-
pickUpFaqContent(messages) {
|
|
214
|
-
let replys = messages.map(item => {
|
|
215
|
-
let content = item.message.content.trim().replace(/\t|\n|\v|\r|\f/g, '');
|
|
216
|
-
try {
|
|
217
|
-
let jsonObj = JSON.parse(content);
|
|
218
|
-
if (!Array.isArray(jsonObj.keywords)) {
|
|
219
|
-
jsonObj.keywords = (jsonObj.keywords || '').split(',');
|
|
220
|
-
}
|
|
221
|
-
return jsonObj;
|
|
222
|
-
}
|
|
223
|
-
catch (err) {
|
|
224
|
-
console.log('JSON error', content, err);
|
|
225
|
-
return { question: null };
|
|
226
|
-
}
|
|
227
|
-
});
|
|
228
|
-
return replys.filter(n => { return n.question != null; });
|
|
229
|
-
}
|
|
230
|
-
/**
|
|
231
|
-
* 从指定的文本内容中生成一张试卷
|
|
232
|
-
* @param {*} content
|
|
233
|
-
* @param {试卷的参数} paperOption
|
|
234
|
-
* totalscore: 试卷总分,默认100
|
|
235
|
-
* section: {type:[0,1,2,3]为单选、多选、判断、填空题型 count:生成多少道 score:本段分数}
|
|
236
|
-
* @param {*} axiosOption
|
|
237
|
-
* @returns
|
|
238
|
-
*/ //并在答案末尾处必须给出答案内容中的关键词
|
|
239
|
-
generateExaminationPaperFromContent(content, paperOption = {}, axiosOption = {}) {
|
|
240
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
|
|
241
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
242
|
-
let arrContent = this.splitLongText(content);
|
|
243
|
-
let sectionCount = {
|
|
244
|
-
singlechoice: (((_a = paperOption.singlechoice) === null || _a === void 0 ? void 0 : _a.count) || 0) / arrContent.length,
|
|
245
|
-
multiplechoice: (((_b = paperOption.multiplechoice) === null || _b === void 0 ? void 0 : _b.count) || 0) / arrContent.length,
|
|
246
|
-
trueorfalse: (((_c = paperOption.trueorfalse) === null || _c === void 0 ? void 0 : _c.count) || 0) / arrContent.length,
|
|
247
|
-
completion: (((_d = paperOption.completion) === null || _d === void 0 ? void 0 : _d.count) || 0) / arrContent.length
|
|
248
|
-
};
|
|
249
|
-
///剩余待生成的题目数量
|
|
250
|
-
let remainCount = {
|
|
251
|
-
singlechoice: ((_e = paperOption.singlechoice) === null || _e === void 0 ? void 0 : _e.count) || 0,
|
|
252
|
-
multiplechoice: ((_f = paperOption.multiplechoice) === null || _f === void 0 ? void 0 : _f.count) || 0,
|
|
253
|
-
trueorfalse: ((_g = paperOption.trueorfalse) === null || _g === void 0 ? void 0 : _g.count) || 0,
|
|
254
|
-
completion: ((_h = paperOption.completion) === null || _h === void 0 ? void 0 : _h.count) || 0
|
|
255
|
-
};
|
|
256
|
-
///每种类型的题目的分数
|
|
257
|
-
let ITEM_SCORE = {
|
|
258
|
-
singlechoice: ((_j = paperOption.singlechoice) === null || _j === void 0 ? void 0 : _j.score) || 0,
|
|
259
|
-
multiplechoice: ((_k = paperOption.multiplechoice) === null || _k === void 0 ? void 0 : _k.score) || 0,
|
|
260
|
-
trueorfalse: ((_l = paperOption.trueorfalse) === null || _l === void 0 ? void 0 : _l.score) || 0,
|
|
261
|
-
completion: ((_m = paperOption.completion) === null || _m === void 0 ? void 0 : _m.score) || 0
|
|
262
|
-
};
|
|
263
|
-
///最后生成出来的结果
|
|
264
|
-
let paperReturned = {
|
|
265
|
-
singlechoice: [], multiplechoice: [], trueorfalse: [], completion: []
|
|
266
|
-
}, noMoreQuestionRetrive = false, totalscore = 0;
|
|
267
|
-
while (arrContent.length > 0 && !noMoreQuestionRetrive) {
|
|
268
|
-
////每次最多送MESSAGE_LENGTH句话给openai
|
|
269
|
-
let subarray = arrContent.slice(0, MESSAGE_LENGTH);
|
|
270
|
-
/**
|
|
271
|
-
* 每种类型的题目进行遍历
|
|
272
|
-
*/
|
|
273
|
-
noMoreQuestionRetrive = true;
|
|
274
|
-
for (const key of QUESTION_TYPE) {
|
|
275
|
-
///还需要抓取题目
|
|
276
|
-
if (remainCount[key] > 0) {
|
|
277
|
-
noMoreQuestionRetrive = false;
|
|
278
|
-
subarray.push({ role: 'user', content: QUESTION_TEXT_MAPPING[key] });
|
|
279
|
-
let itemCount = Math.min(remainCount[key], Math.ceil(subarray.length * sectionCount[key]));
|
|
280
|
-
console.log(QUESTION_TEXT_MAPPING[key], itemCount);
|
|
281
|
-
let result = yield this.chatRequest(subarray, { replyCounts: itemCount }, axiosOption);
|
|
282
|
-
if (result.successed && result.message) {
|
|
283
|
-
//console.log('paper result', key, result.message.length)
|
|
284
|
-
let pickedQuestions = this.pickUpQuestions(result.message, key, ITEM_SCORE[key]);
|
|
285
|
-
if (pickedQuestions.length) {
|
|
286
|
-
///对外发送检出题目的信号
|
|
287
|
-
this.emit('parseout', { type: 'question', name: key, items: pickedQuestions });
|
|
288
|
-
paperReturned[key] = paperReturned[key].concat(pickedQuestions);
|
|
289
|
-
remainCount[key] = remainCount[key] - pickedQuestions.length;
|
|
290
|
-
totalscore = totalscore + pickedQuestions.length * ITEM_SCORE[key];
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
subarray.splice(subarray.length - 1, 1); ///把最后的问法删除
|
|
294
|
-
}
|
|
295
|
-
}
|
|
296
|
-
////删除已经处理的文本
|
|
297
|
-
arrContent.splice(0, MESSAGE_LENGTH);
|
|
298
|
-
}
|
|
299
|
-
///发出信号,解析完毕
|
|
300
|
-
this.emit('parseover', { type: 'question', items: paperReturned });
|
|
301
|
-
return { successed: true, score: totalscore, paper: paperReturned };
|
|
302
|
-
});
|
|
303
|
-
}
|
|
304
|
-
/**
|
|
305
|
-
* 从答复中得到题目
|
|
306
|
-
* @param {*} result
|
|
307
|
-
*
|
|
308
|
-
*/
|
|
309
|
-
pickUpQuestions(result, questiontype, score = 1) {
|
|
310
|
-
let item = result.map(m => {
|
|
311
|
-
////防止输出的JSON格式不合法
|
|
312
|
-
try {
|
|
313
|
-
let jsonObj = JSON.parse(m.message.content);
|
|
314
|
-
jsonObj.score = score;
|
|
315
|
-
if (jsonObj.choice && Array.isArray(jsonObj.choice) && questiontype != 'completion') {
|
|
316
|
-
jsonObj.fullanswer = (jsonObj.answer + '').replace(/,|[^ABCDE]/g, '');
|
|
317
|
-
jsonObj.choice = jsonObj.choice.map((item, index) => {
|
|
318
|
-
let seqNo = String.fromCharCode(65 + index);
|
|
319
|
-
let correctReg = new RegExp(`${seqNo}.|${seqNo}`, 'ig');
|
|
320
|
-
//let answer = jsonObj.fullanswer
|
|
321
|
-
return {
|
|
322
|
-
id: seqNo,
|
|
323
|
-
content: item.replace(correctReg, '').trim(),
|
|
324
|
-
iscorrect: (jsonObj.fullanswer.indexOf(seqNo) >= 0 || jsonObj.fullanswer.indexOf(m)) >= 0 ? 1 : 0
|
|
325
|
-
};
|
|
326
|
-
});
|
|
327
|
-
}
|
|
328
|
-
switch (questiontype) {
|
|
329
|
-
case 'singlechoice':
|
|
330
|
-
jsonObj.answer = (jsonObj.answer + '').replace(/,|[^ABCDEFG]/g, '').split('').slice(0, 1);
|
|
331
|
-
break;
|
|
332
|
-
case 'multiplechoice':
|
|
333
|
-
jsonObj.answer = (jsonObj.answer + '').replace(/,|[^ABCDEFG]/g, '').split('');
|
|
334
|
-
break;
|
|
335
|
-
case 'trueorfalse':
|
|
336
|
-
jsonObj.answer = [(jsonObj.answer + '').indexOf('正确') >= 0 ? 'A' : 'B'];
|
|
337
|
-
break;
|
|
338
|
-
}
|
|
339
|
-
return jsonObj;
|
|
340
|
-
}
|
|
341
|
-
catch (err) {
|
|
342
|
-
console.log('error happened:', err);
|
|
343
|
-
return null;
|
|
344
|
-
}
|
|
345
|
-
});
|
|
346
|
-
return item.filter(i => { return i != null; });
|
|
347
|
-
}
|
|
348
|
-
/**
|
|
349
|
-
* 将一段很长的文本,按1024长度来划分到多个中
|
|
350
|
-
* @param {*} content
|
|
351
|
-
*/
|
|
352
|
-
splitLongText(content, len = SECTION_LENGTH) {
|
|
353
|
-
let start = 0, message = [], length = content.length;
|
|
354
|
-
while (start < length) {
|
|
355
|
-
const subtext = content.substr(start, len);
|
|
356
|
-
if (subtext)
|
|
357
|
-
message.push({ role: 'user', content: subtext });
|
|
358
|
-
start += len;
|
|
359
|
-
}
|
|
360
|
-
return message;
|
|
361
|
-
}
|
|
362
|
-
}
|
|
363
|
-
exports.AIChat = AIChat;
|
|
26
|
+
exports.AIFactory = void 0;
|
|
27
|
+
exports.AIFactory = __importStar(require("./openaiprovider"));
|
package/dist/openai.d.ts
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { OpenAIApi, ChatCompletionRequestMessage } from "openai";
|
|
3
|
+
import { EventEmitter } from "events";
|
|
4
|
+
import { OpenAIApiParameters, ChatReponse, SummaryReponse, FaqItem, ExaminationPaperResult, EmotionResult, SimilarityResult, QuestionItem } from './declare';
|
|
5
|
+
export default class OpenAIGpt extends EventEmitter {
|
|
6
|
+
protected readonly apiKey: string;
|
|
7
|
+
private aiApi;
|
|
8
|
+
protected readonly chatModel: string;
|
|
9
|
+
protected readonly maxtoken: number;
|
|
10
|
+
protected readonly temperature: number;
|
|
11
|
+
/**
|
|
12
|
+
*
|
|
13
|
+
* @param apiKey 调用OpenAI 的key
|
|
14
|
+
* @param apiOption
|
|
15
|
+
*/
|
|
16
|
+
constructor(apiKey: string, apiOption?: OpenAIApiParameters);
|
|
17
|
+
/**
|
|
18
|
+
* 初始化OpenAI 的聊天对象Api
|
|
19
|
+
*/
|
|
20
|
+
createOpenAI(apiKey: string): OpenAIApi;
|
|
21
|
+
/**
|
|
22
|
+
* 向OpenAI发送一个聊天请求
|
|
23
|
+
* @param {*} chatText
|
|
24
|
+
*/
|
|
25
|
+
chatRequest(chatText: string | Array<any>, callChatOption: OpenAIApiParameters, axiosOption?: any): Promise<ChatReponse>;
|
|
26
|
+
/**
|
|
27
|
+
* 判断一句话的表达情绪
|
|
28
|
+
* @param {*} s1
|
|
29
|
+
* @param {*} axiosOption
|
|
30
|
+
*/
|
|
31
|
+
getScentenceEmotional(s1: string, axiosOption?: any): Promise<EmotionResult>;
|
|
32
|
+
/**
|
|
33
|
+
* 获取两句话的相似度取值
|
|
34
|
+
* @param {*} s1
|
|
35
|
+
* @param {*} s2
|
|
36
|
+
*/
|
|
37
|
+
getScentenseSimilarity(s1: string, s2: string, axiosOption?: any): Promise<SimilarityResult>;
|
|
38
|
+
/**
|
|
39
|
+
* 获得一种内容的相似说法
|
|
40
|
+
* 比如:
|
|
41
|
+
* 你今年多大?
|
|
42
|
+
* 相似问法:您是哪一年出生的
|
|
43
|
+
* 您今年贵庚?
|
|
44
|
+
* @param {*} content
|
|
45
|
+
* @param {需要出来的数量} count
|
|
46
|
+
*/
|
|
47
|
+
getSimilarityContent(content: string, count?: number, axiosOption?: any): Promise<ChatReponse>;
|
|
48
|
+
/**
|
|
49
|
+
* 提取内容的中心思想摘要
|
|
50
|
+
* @param content
|
|
51
|
+
* @param axiosOption
|
|
52
|
+
*/
|
|
53
|
+
getSummaryOfContent(content: string | Array<any>, axiosOption?: any): Promise<SummaryReponse>;
|
|
54
|
+
/**
|
|
55
|
+
* 从指定的文本内容中生成相关的问答
|
|
56
|
+
* @param {*} content
|
|
57
|
+
* @param {*} count
|
|
58
|
+
* @param {*} axiosOption
|
|
59
|
+
* @returns
|
|
60
|
+
*/ generateQuestionsFromContent(content: string, count?: number, axiosOption?: any): Promise<ChatReponse>;
|
|
61
|
+
/**
|
|
62
|
+
* 从指定的文本内容中生成相关的问答
|
|
63
|
+
* @param {*} content
|
|
64
|
+
* @param {*} count
|
|
65
|
+
* @param {*} axiosOption
|
|
66
|
+
* @returns
|
|
67
|
+
*/ generateQuestionsFromContent_Orgin(content: string, count?: number, promotion?: string | null, sliceslength?: number, axiosOption?: any): Promise<ChatReponse>;
|
|
68
|
+
/**
|
|
69
|
+
* 解析Faq返回的问题
|
|
70
|
+
* @param {*} messages
|
|
71
|
+
* @returns
|
|
72
|
+
*/
|
|
73
|
+
protected pickUpFaqContent(messages: Array<any>): Array<FaqItem>;
|
|
74
|
+
/**
|
|
75
|
+
* 从指定的文本内容中生成一张试卷
|
|
76
|
+
* @param {*} content
|
|
77
|
+
* @param {试卷的参数} paperOption
|
|
78
|
+
* totalscore: 试卷总分,默认100
|
|
79
|
+
* section: {type:[0,1,2,3]为单选、多选、判断、填空题型 count:生成多少道 score:本段分数}
|
|
80
|
+
* @param {*} axiosOption
|
|
81
|
+
* @returns
|
|
82
|
+
*/ generateExaminationPaperFromContent(content: string, paperOption?: any, axiosOption?: any): Promise<ExaminationPaperResult>;
|
|
83
|
+
/**
|
|
84
|
+
* 从答复中得到题目
|
|
85
|
+
* @param {*} result
|
|
86
|
+
*
|
|
87
|
+
*/
|
|
88
|
+
protected pickUpQuestions(result: Array<any>, count: number, questiontype: string, score?: number): Array<QuestionItem>;
|
|
89
|
+
/**
|
|
90
|
+
* 将一段很长的文本,按1024长度来划分到多个中
|
|
91
|
+
* @param {*} content
|
|
92
|
+
*/
|
|
93
|
+
protected splitLongText(content: string, len?: number): Array<ChatCompletionRequestMessage>;
|
|
94
|
+
}
|