doomiaichat 4.7.0 → 4.8.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/openai.d.ts +2 -2
- package/dist/openai.js +142 -95
- package/package.json +1 -1
- package/src/openai.ts +53 -7
package/dist/openai.d.ts
CHANGED
|
@@ -82,7 +82,7 @@ export default class OpenAIGpt extends GptBase {
|
|
|
82
82
|
* @param {*} messages
|
|
83
83
|
* @returns
|
|
84
84
|
*/
|
|
85
|
-
protected pickUpFaqContent(messages: Array<any>): Array<FaqItem
|
|
85
|
+
protected pickUpFaqContent(messages: Array<any>): Promise<Array<FaqItem>>;
|
|
86
86
|
/**
|
|
87
87
|
* 从指定的文本内容中生成一张试卷
|
|
88
88
|
* @param {*} content
|
|
@@ -97,7 +97,7 @@ export default class OpenAIGpt extends GptBase {
|
|
|
97
97
|
* @param {*} result
|
|
98
98
|
*
|
|
99
99
|
*/
|
|
100
|
-
protected pickUpQuestions(result: Array<any>, count: number, questiontype: string, score?: number): Array<QuestionItem
|
|
100
|
+
protected pickUpQuestions(result: Array<any>, count: number, questiontype: string, score?: number): Promise<Array<QuestionItem>>;
|
|
101
101
|
/**
|
|
102
102
|
* 验证JSON字符串是否是真正可转换为JSON的合法格式
|
|
103
103
|
* 这里只能做一个最简单的处理,就是用两端的符号
|
package/dist/openai.js
CHANGED
|
@@ -69,6 +69,15 @@ const QUESTION_PROMPT = {
|
|
|
69
69
|
trueorfalse: '根据以下内容,请生成@ITEMCOUNT@道判断题,每道题正确和错误两个选项,输出结果必须是JSON数组并按照[{"question":"","choice":["A.正确","B.错误"],"answer":[]}]的结构输出',
|
|
70
70
|
completion: '根据以下内容,请生成@ITEMCOUNT@道填空题和对应答案,输出结果必须是JSON数组并按照[{"question":"","answer":["填空答案1","填空答案2"]}]的结构输出'
|
|
71
71
|
};
|
|
72
|
+
/**
|
|
73
|
+
* 问题生成的Prompt
|
|
74
|
+
*/
|
|
75
|
+
const QUESTION_PROMPT_FIXED = {
|
|
76
|
+
singlechoice: '[{"question":"","choice":[],"answer":[]}]',
|
|
77
|
+
multiplechoice: '[{"question":"","choice":[],"answer":[]}]',
|
|
78
|
+
trueorfalse: '[{"question":"","choice":["A.正确","B.错误"],"answer":[]}]',
|
|
79
|
+
completion: '[{"question":"","answer":["填空答案1","填空答案2"]}]'
|
|
80
|
+
};
|
|
72
81
|
const QUESTION_TYPE = ['singlechoice', 'multiplechoice', 'trueorfalse', 'completion'];
|
|
73
82
|
class OpenAIGpt extends gptbase_1.default {
|
|
74
83
|
/**
|
|
@@ -137,6 +146,12 @@ class OpenAIGpt extends gptbase_1.default {
|
|
|
137
146
|
max_tokens: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.maxtoken) || this.maxtoken),
|
|
138
147
|
n: Number((callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.replyCounts) || 1) || 1
|
|
139
148
|
}, axiosOption);
|
|
149
|
+
// console.log('finish_reason==>', response.data.choices)
|
|
150
|
+
////输出的内容不合规
|
|
151
|
+
if (response.data.choices[0].finish_reason === 'content_filter') {
|
|
152
|
+
console.log('content_filter');
|
|
153
|
+
return { successed: false, error: 'content filter' };
|
|
154
|
+
}
|
|
140
155
|
return { successed: true, message: response.data.choices, usage: response.data.usage };
|
|
141
156
|
}
|
|
142
157
|
catch (error) {
|
|
@@ -388,6 +403,7 @@ class OpenAIGpt extends gptbase_1.default {
|
|
|
388
403
|
{ role: 'user', content: arrContent.slice(0, 1)[0]
|
|
389
404
|
}
|
|
390
405
|
];
|
|
406
|
+
console.log('Faq Question Pick Prompt:', subarray);
|
|
391
407
|
//subarray.push({ role: 'user', content:'请根据上述内容,给出一道提问与答案以及答案关键词,按照先问题内容,再标准答案,再关键词的顺序输出,关键词之间用、分开'})
|
|
392
408
|
//subarray.unshift({ role: 'system', content: `你是一位专业培训师,从以下内容中提取${itemCount}条提问及答案,并从答案内容提取出至少2个关键词,最终结果按照[{"question":"提问内容","answer":"答案内容","keywords":["关键词1","关键词2"]}]的JSON数组结构输出。` })
|
|
393
409
|
// subarray.unshift({role: 'system', content: FAQ_ROLE_DEFINE});
|
|
@@ -396,7 +412,7 @@ class OpenAIGpt extends gptbase_1.default {
|
|
|
396
412
|
let result = yield this.chatRequest(subarray, { replyCounts: 1 }, axiosOption);
|
|
397
413
|
if (result.successed && result.message) {
|
|
398
414
|
// console.log('result is ', result.message[0].message.content)
|
|
399
|
-
let msgs = this.pickUpFaqContent(result.message);
|
|
415
|
+
let msgs = yield this.pickUpFaqContent(result.message);
|
|
400
416
|
if (msgs.length) {
|
|
401
417
|
///对外发送检出问答题的信号
|
|
402
418
|
this.emit('parseout', { type: 'qa', items: msgs });
|
|
@@ -420,32 +436,47 @@ class OpenAIGpt extends gptbase_1.default {
|
|
|
420
436
|
*/
|
|
421
437
|
pickUpFaqContent(messages) {
|
|
422
438
|
var _a, _b;
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
let
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
440
|
+
if (!((_b = (_a = messages[0]) === null || _a === void 0 ? void 0 : _a.message) === null || _b === void 0 ? void 0 : _b.content))
|
|
441
|
+
return [];
|
|
442
|
+
let answerString = messages[0].message.content.trim().replace(/\t|\n|\v|\r|\f/g, '');
|
|
443
|
+
let jsonObj = this.fixedJsonString(answerString);
|
|
444
|
+
if (!jsonObj.length) {
|
|
445
|
+
let fixedAsk = [
|
|
446
|
+
{ role: 'system', content: '角色扮演:假设你是一位高级JSON数据分析师' },
|
|
447
|
+
{ role: 'user', content: `请分析以下内容,严格按照[{"question":"提问内容","answer":"答案内容","keywords":["关键词1","关键词2"]}]的标准JSON数组结构输出。` },
|
|
448
|
+
{ role: 'user', content: answerString },
|
|
449
|
+
];
|
|
450
|
+
console.log('pickUpFaqContent fixedAsk', fixedAsk);
|
|
451
|
+
let fixedJsonResult = yield this.chatRequest(fixedAsk, { replyCounts: 1 }, {});
|
|
452
|
+
if (fixedJsonResult.successed) {
|
|
453
|
+
answerString = fixedJsonResult.message[0].message.content.trim().replace(/\t|\n|\v|\r|\f/g, '');
|
|
454
|
+
jsonObj = this.fixedJsonString(answerString);
|
|
439
455
|
}
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
}
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
456
|
+
if (!jsonObj.length)
|
|
457
|
+
return [];
|
|
458
|
+
}
|
|
459
|
+
try {
|
|
460
|
+
//let jsonObj = JSON.parse(answerString);
|
|
461
|
+
//let jsonObj = eval(answerString);
|
|
462
|
+
jsonObj.map((item) => {
|
|
463
|
+
let realKeyword = [];
|
|
464
|
+
let keywords = (item.keywords + '').split(',');
|
|
465
|
+
let answer = item.answer || '';
|
|
466
|
+
for (const k of keywords) {
|
|
467
|
+
if (k && answer.indexOf(k) >= 0)
|
|
468
|
+
realKeyword.push(k);
|
|
469
|
+
}
|
|
470
|
+
item.keywords = realKeyword;
|
|
471
|
+
return item;
|
|
472
|
+
});
|
|
473
|
+
return jsonObj;
|
|
474
|
+
}
|
|
475
|
+
catch (err) {
|
|
476
|
+
console.log('JSON error', err);
|
|
477
|
+
return [];
|
|
478
|
+
}
|
|
479
|
+
});
|
|
449
480
|
}
|
|
450
481
|
/**
|
|
451
482
|
* 从指定的文本内容中生成一张试卷
|
|
@@ -508,7 +539,7 @@ class OpenAIGpt extends gptbase_1.default {
|
|
|
508
539
|
console.log('subarray returned', result.successed);
|
|
509
540
|
if (result.successed && result.message) {
|
|
510
541
|
//console.log('paper result', key, result.message.length)
|
|
511
|
-
let pickedQuestions = this.pickUpQuestions(result.message, itemCount, key, ITEM_SCORE[key]);
|
|
542
|
+
let pickedQuestions = yield this.pickUpQuestions(result.message, itemCount, key, ITEM_SCORE[key]);
|
|
512
543
|
if (pickedQuestions.length) {
|
|
513
544
|
///对外发送检出题目的信号
|
|
514
545
|
this.emit('parseout', { type: 'question', name: key, items: pickedQuestions });
|
|
@@ -537,76 +568,92 @@ class OpenAIGpt extends gptbase_1.default {
|
|
|
537
568
|
*/
|
|
538
569
|
pickUpQuestions(result, count, questiontype, score = 1) {
|
|
539
570
|
var _a, _b;
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
571
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
572
|
+
if (!((_b = (_a = result[0]) === null || _a === void 0 ? void 0 : _a.message) === null || _b === void 0 ? void 0 : _b.content))
|
|
573
|
+
return [];
|
|
574
|
+
let answerString = result[0].message.content.trim().replace(/\t|\n|\v|\r|\f/g, '');
|
|
575
|
+
let jsonObj = this.fixedJsonString(answerString);
|
|
576
|
+
////修复的结果无法用程序修复,请求GPT来分析修复一下这个结果
|
|
577
|
+
if (!jsonObj.length) {
|
|
578
|
+
let fixedAsk = [
|
|
579
|
+
{ role: 'system', content: '角色扮演:假设你是一位高级JSON数据分析师' },
|
|
580
|
+
{ role: 'user', content: `请分析以下内容,严格按照${QUESTION_PROMPT_FIXED[questiontype]}的标准JSON数组结构输出。` },
|
|
581
|
+
{ role: 'user', content: answerString },
|
|
582
|
+
];
|
|
583
|
+
console.log('fixedAsk', fixedAsk);
|
|
584
|
+
let fixedJsonResult = yield this.chatRequest(fixedAsk, { replyCounts: 1 }, {});
|
|
585
|
+
if (fixedJsonResult.successed) {
|
|
586
|
+
answerString = fixedJsonResult.message[0].message.content.trim().replace(/\t|\n|\v|\r|\f/g, '');
|
|
587
|
+
jsonObj = this.fixedJsonString(answerString);
|
|
588
|
+
}
|
|
589
|
+
if (!jsonObj.length)
|
|
590
|
+
return [];
|
|
591
|
+
}
|
|
592
|
+
let returnItems = [];
|
|
593
|
+
try {
|
|
594
|
+
// let jsonObj = JSON.parse(answerString);
|
|
595
|
+
returnItems = jsonObj.map((questionitem) => {
|
|
596
|
+
console.log('answer item from jsonObj', questionitem);
|
|
597
|
+
if (questionitem.choice && Array.isArray(questionitem.choice) && questiontype != 'completion') {
|
|
598
|
+
questionitem.fullanswer = (questionitem.answer + '').replace(/,|[^ABCDE]/g, '');
|
|
599
|
+
questionitem.score = score;
|
|
600
|
+
if (questionitem.choice) {
|
|
601
|
+
questionitem.choice = questionitem.choice.map((item, index) => {
|
|
602
|
+
let seqNo = 'ABCDEFG'[index]; //String.fromCharCode(65 + index);
|
|
603
|
+
let correctReg = new RegExp(`${seqNo}.|${seqNo}`, 'ig');
|
|
604
|
+
// console.log('itemitemitem', item)
|
|
605
|
+
//let answer = jsonObj.fullanswer
|
|
606
|
+
return {
|
|
607
|
+
id: seqNo,
|
|
608
|
+
content: (item + '').replace(correctReg, '').trim(),
|
|
609
|
+
iscorrect: (questionitem.fullanswer || '').indexOf(seqNo) >= 0 ? 1 : 0
|
|
610
|
+
//|| jsonObj.fullanswer.indexOf(m))
|
|
611
|
+
};
|
|
612
|
+
});
|
|
613
|
+
}
|
|
614
|
+
///如果是非判断题,题目的选项数量小于2 ,则无效
|
|
615
|
+
///如果是判断题,题目的选项必须=2
|
|
616
|
+
if (!questionitem.choice || (questiontype != 'trueorfalse' && questionitem.choice.length < 3) || (questiontype == 'trueorfalse' && questionitem.choice.length != 2)) {
|
|
617
|
+
return null;
|
|
618
|
+
}
|
|
567
619
|
}
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
620
|
+
switch (questiontype) {
|
|
621
|
+
case 'singlechoice':
|
|
622
|
+
questionitem.answer = (questionitem.answer + '').replace(/,|[^ABCDEFG]/g, '').split('').slice(0, 1);
|
|
623
|
+
break;
|
|
624
|
+
case 'multiplechoice':
|
|
625
|
+
questionitem.answer = (questionitem.answer + '').replace(/,|[^ABCDEFG]/g, '').split('');
|
|
626
|
+
break;
|
|
627
|
+
case 'trueorfalse':
|
|
628
|
+
let rightItem = questionitem.choice.find((x) => { return x.iscorrect == 1; });
|
|
629
|
+
questionitem.answer = [(rightItem === null || rightItem === void 0 ? void 0 : rightItem.id) || 'Z']; //[(questionitem.answer + '').indexOf('正确') >= 0 ? 'A' : 'B']
|
|
630
|
+
break;
|
|
572
631
|
}
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
let rightAnswer = questionitem.choice ? questionitem.choice.filter((item) => { return item.iscorrect === 1; }) : [];
|
|
599
|
-
///单选题的正确选项大于了1个
|
|
600
|
-
if (rightAnswer.length === 0 || !questionitem.answer || questionitem.answer.length === 0)
|
|
601
|
-
return null;
|
|
602
|
-
}
|
|
603
|
-
return questionitem;
|
|
604
|
-
});
|
|
605
|
-
}
|
|
606
|
-
catch (err) {
|
|
607
|
-
console.log('error happened:', err);
|
|
608
|
-
}
|
|
609
|
-
return returnItems.filter(i => { return i != null; }).slice(0, count);
|
|
632
|
+
///单选题验证
|
|
633
|
+
if (questiontype == 'singlechoice') {
|
|
634
|
+
let rightAnswer = questionitem.choice ? questionitem.choice.filter((item) => { return item.iscorrect === 1; }) : [];
|
|
635
|
+
///单选题的正确选项大于了1个
|
|
636
|
+
if (rightAnswer.length != 1 || !questionitem.answer || questionitem.answer.length !== 1)
|
|
637
|
+
return null;
|
|
638
|
+
///正确选项和答案不一致
|
|
639
|
+
if (rightAnswer[0].id.toUpperCase() != (questionitem.answer[0] || '').toUpperCase())
|
|
640
|
+
return null;
|
|
641
|
+
}
|
|
642
|
+
///多选题验证
|
|
643
|
+
if (questiontype == 'multiplechoice') {
|
|
644
|
+
let rightAnswer = questionitem.choice ? questionitem.choice.filter((item) => { return item.iscorrect === 1; }) : [];
|
|
645
|
+
///单选题的正确选项大于了1个
|
|
646
|
+
if (rightAnswer.length === 0 || !questionitem.answer || questionitem.answer.length === 0)
|
|
647
|
+
return null;
|
|
648
|
+
}
|
|
649
|
+
return questionitem;
|
|
650
|
+
});
|
|
651
|
+
}
|
|
652
|
+
catch (err) {
|
|
653
|
+
console.log('error happened:', err);
|
|
654
|
+
}
|
|
655
|
+
return returnItems.filter(i => { return i != null; }).slice(0, count);
|
|
656
|
+
});
|
|
610
657
|
}
|
|
611
658
|
/**
|
|
612
659
|
* 验证JSON字符串是否是真正可转换为JSON的合法格式
|
package/package.json
CHANGED
package/src/openai.ts
CHANGED
|
@@ -57,6 +57,16 @@ const QUESTION_PROMPT: any ={
|
|
|
57
57
|
trueorfalse: '根据以下内容,请生成@ITEMCOUNT@道判断题,每道题正确和错误两个选项,输出结果必须是JSON数组并按照[{"question":"","choice":["A.正确","B.错误"],"answer":[]}]的结构输出',
|
|
58
58
|
completion: '根据以下内容,请生成@ITEMCOUNT@道填空题和对应答案,输出结果必须是JSON数组并按照[{"question":"","answer":["填空答案1","填空答案2"]}]的结构输出'
|
|
59
59
|
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* 问题生成的Prompt
|
|
63
|
+
*/
|
|
64
|
+
const QUESTION_PROMPT_FIXED: any = {
|
|
65
|
+
singlechoice: '[{"question":"","choice":[],"answer":[]}]',
|
|
66
|
+
multiplechoice: '[{"question":"","choice":[],"answer":[]}]',
|
|
67
|
+
trueorfalse: '[{"question":"","choice":["A.正确","B.错误"],"answer":[]}]',
|
|
68
|
+
completion: '[{"question":"","answer":["填空答案1","填空答案2"]}]'
|
|
69
|
+
}
|
|
60
70
|
const QUESTION_TYPE: string[] = ['singlechoice', 'multiplechoice', 'trueorfalse', 'completion']
|
|
61
71
|
|
|
62
72
|
export default class OpenAIGpt extends GptBase {
|
|
@@ -129,6 +139,12 @@ export default class OpenAIGpt extends GptBase {
|
|
|
129
139
|
max_tokens: Number(callChatOption?.maxtoken || this.maxtoken),
|
|
130
140
|
n: Number(callChatOption?.replyCounts || 1) || 1
|
|
131
141
|
}, axiosOption);
|
|
142
|
+
// console.log('finish_reason==>', response.data.choices)
|
|
143
|
+
////输出的内容不合规
|
|
144
|
+
if (response.data.choices[0].finish_reason ==='content_filter') {
|
|
145
|
+
console.log('content_filter')
|
|
146
|
+
return {successed:false,error:'content filter'}
|
|
147
|
+
}
|
|
132
148
|
return { successed: true, message: response.data.choices, usage: response.data.usage };
|
|
133
149
|
} catch (error) {
|
|
134
150
|
console.log('result is error ', error)
|
|
@@ -355,7 +371,8 @@ export default class OpenAIGpt extends GptBase {
|
|
|
355
371
|
{ role: 'user', content: `从以下内容中提取${itemCount}条提问及答案,并从答案内容提取出至少2个关键词,最终结果按照[{"question":"提问内容","answer":"答案内容","keywords":["关键词1","关键词2"]}]的JSON数组结构输出。`},
|
|
356
372
|
{ role: 'user', content: arrContent.slice(0, 1)[0]
|
|
357
373
|
}
|
|
358
|
-
|
|
374
|
+
]
|
|
375
|
+
console.log('Faq Question Pick Prompt:', subarray)
|
|
359
376
|
//subarray.push({ role: 'user', content:'请根据上述内容,给出一道提问与答案以及答案关键词,按照先问题内容,再标准答案,再关键词的顺序输出,关键词之间用、分开'})
|
|
360
377
|
//subarray.unshift({ role: 'system', content: `你是一位专业培训师,从以下内容中提取${itemCount}条提问及答案,并从答案内容提取出至少2个关键词,最终结果按照[{"question":"提问内容","answer":"答案内容","keywords":["关键词1","关键词2"]}]的JSON数组结构输出。` })
|
|
361
378
|
// subarray.unshift({role: 'system', content: FAQ_ROLE_DEFINE});
|
|
@@ -364,7 +381,7 @@ export default class OpenAIGpt extends GptBase {
|
|
|
364
381
|
let result = await this.chatRequest(subarray, { replyCounts: 1 }, axiosOption);
|
|
365
382
|
if (result.successed && result.message) {
|
|
366
383
|
// console.log('result is ', result.message[0].message.content)
|
|
367
|
-
let msgs = this.pickUpFaqContent(result.message);
|
|
384
|
+
let msgs = await this.pickUpFaqContent(result.message);
|
|
368
385
|
if (msgs.length) {
|
|
369
386
|
///对外发送检出问答题的信号
|
|
370
387
|
this.emit('parseout', { type: 'qa', items: msgs })
|
|
@@ -388,11 +405,26 @@ export default class OpenAIGpt extends GptBase {
|
|
|
388
405
|
* @param {*} messages
|
|
389
406
|
* @returns
|
|
390
407
|
*/
|
|
391
|
-
protected pickUpFaqContent(messages: Array<any>): Array<FaqItem
|
|
408
|
+
protected async pickUpFaqContent(messages: Array<any>): Promise<Array<FaqItem>> {
|
|
392
409
|
if (!messages[0]?.message?.content) return [];
|
|
393
410
|
let answerString = messages[0].message.content.trim().replace(/\t|\n|\v|\r|\f/g, '');
|
|
394
411
|
let jsonObj = this.fixedJsonString(answerString);
|
|
395
|
-
if (!jsonObj.length)
|
|
412
|
+
if (!jsonObj.length){
|
|
413
|
+
let fixedAsk = [
|
|
414
|
+
{ role: 'system', content: '角色扮演:假设你是一位高级JSON数据分析师' },
|
|
415
|
+
{ role: 'user', content: `请分析以下内容,严格按照[{"question":"提问内容","answer":"答案内容","keywords":["关键词1","关键词2"]}]的标准JSON数组结构输出。` },
|
|
416
|
+
{ role: 'user', content: answerString },
|
|
417
|
+
]
|
|
418
|
+
console.log('pickUpFaqContent fixedAsk', fixedAsk)
|
|
419
|
+
let fixedJsonResult: any = await this.chatRequest(fixedAsk, { replyCounts: 1 }, {})
|
|
420
|
+
if (fixedJsonResult.successed) {
|
|
421
|
+
answerString = fixedJsonResult.message[0].message.content.trim().replace(/\t|\n|\v|\r|\f/g, '');
|
|
422
|
+
jsonObj = this.fixedJsonString(answerString);
|
|
423
|
+
}
|
|
424
|
+
if (!jsonObj.length) return []
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
|
|
396
428
|
try {
|
|
397
429
|
//let jsonObj = JSON.parse(answerString);
|
|
398
430
|
//let jsonObj = eval(answerString);
|
|
@@ -475,7 +507,7 @@ export default class OpenAIGpt extends GptBase {
|
|
|
475
507
|
console.log('subarray returned', result.successed)
|
|
476
508
|
if (result.successed && result.message) {
|
|
477
509
|
//console.log('paper result', key, result.message.length)
|
|
478
|
-
let pickedQuestions = this.pickUpQuestions(result.message, itemCount, key, ITEM_SCORE[key]);
|
|
510
|
+
let pickedQuestions = await this.pickUpQuestions(result.message, itemCount, key, ITEM_SCORE[key]);
|
|
479
511
|
if (pickedQuestions.length) {
|
|
480
512
|
///对外发送检出题目的信号
|
|
481
513
|
this.emit('parseout', { type: 'question', name: key, items: pickedQuestions })
|
|
@@ -502,11 +534,25 @@ export default class OpenAIGpt extends GptBase {
|
|
|
502
534
|
* @param {*} result
|
|
503
535
|
*
|
|
504
536
|
*/
|
|
505
|
-
protected pickUpQuestions(result: Array<any>, count: number, questiontype: string, score: number = 1): Array<QuestionItem
|
|
537
|
+
protected async pickUpQuestions(result: Array<any>, count: number, questiontype: string, score: number = 1): Promise<Array<QuestionItem>> {
|
|
506
538
|
if (!result[0]?.message?.content) return [];
|
|
507
539
|
let answerString = result[0].message.content.trim().replace(/\t|\n|\v|\r|\f/g, '');
|
|
508
540
|
let jsonObj = this.fixedJsonString(answerString);
|
|
509
|
-
|
|
541
|
+
////修复的结果无法用程序修复,请求GPT来分析修复一下这个结果
|
|
542
|
+
if (!jsonObj.length){
|
|
543
|
+
let fixedAsk = [
|
|
544
|
+
{role:'system',content:'角色扮演:假设你是一位高级JSON数据分析师'},
|
|
545
|
+
{ role: 'user', content: `请分析以下内容,严格按照${QUESTION_PROMPT_FIXED[questiontype]}的标准JSON数组结构输出。` },
|
|
546
|
+
{ role: 'user', content: answerString },
|
|
547
|
+
]
|
|
548
|
+
console.log('fixedAsk', fixedAsk)
|
|
549
|
+
let fixedJsonResult:any =await this.chatRequest(fixedAsk,{replyCounts:1},{})
|
|
550
|
+
if (fixedJsonResult.successed){
|
|
551
|
+
answerString = fixedJsonResult.message[0].message.content.trim().replace(/\t|\n|\v|\r|\f/g, '');
|
|
552
|
+
jsonObj = this.fixedJsonString(answerString);
|
|
553
|
+
}
|
|
554
|
+
if (!jsonObj.length) return []
|
|
555
|
+
}
|
|
510
556
|
let returnItems: QuestionItem[] = [];
|
|
511
557
|
try {
|
|
512
558
|
// let jsonObj = JSON.parse(answerString);
|