doomiaichat 2.0.0 → 2.2.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 +67 -0
- package/dist/baiduai.d.ts +34 -0
- package/dist/baiduai.js +110 -0
- package/dist/declare.d.ts +34 -2
- package/dist/declare.js +33 -0
- package/dist/gptbase.d.ts +56 -0
- package/dist/gptbase.js +12 -0
- package/dist/gptprovider.d.ts +20 -0
- package/dist/gptprovider.js +44 -0
- package/dist/index.d.ts +2 -179
- package/dist/index.js +25 -506
- package/dist/openai.d.ts +8 -10
- package/dist/openai.js +8 -40
- package/dist/openaiprovider.d.ts +20 -0
- package/dist/openaiprovider.js +43 -0
- package/package.json +2 -1
- package/src/azureai.ts +14 -10
- package/src/baiduai.ts +103 -0
- package/src/declare.ts +49 -1
- package/src/gptbase.ts +58 -0
- package/src/gptprovider.ts +38 -0
- package/src/index.ts +2 -1
- package/src/openai.ts +14 -51
- package/tsconfig.json +1 -1
- package/src/openaiprovider.ts +0 -32
package/dist/openai.js
CHANGED
|
@@ -8,9 +8,13 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
11
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
15
|
const openai_1 = require("openai");
|
|
13
|
-
|
|
16
|
+
// import { EventEmitter } from "events";
|
|
17
|
+
const gptbase_1 = __importDefault(require("./gptbase"));
|
|
14
18
|
const SECTION_LENGTH = 1600; ///每2400个字符分成一组
|
|
15
19
|
const MESSAGE_LENGTH = 1; ///每次送8句话给openai 进行解析,送多了,会报错
|
|
16
20
|
//请将答案放在最后,标记为答案:()
|
|
@@ -21,7 +25,7 @@ const QUESTION_TEXT_MAPPING = {
|
|
|
21
25
|
completion: '你是一名专业的出题老师,根据以下内容,请生成@ITEMCOUNT@道填空题和对应答案,输出结果必须是JSON数组并按照[{"question":"","answer":["填空答案1","填空答案2"]}]的结构输出' //请将答案放在最后,标记为答案:()
|
|
22
26
|
};
|
|
23
27
|
const QUESTION_TYPE = ['singlechoice', 'multiplechoice', 'trueorfalse', 'completion'];
|
|
24
|
-
class OpenAIGpt extends
|
|
28
|
+
class OpenAIGpt extends gptbase_1.default {
|
|
25
29
|
/**
|
|
26
30
|
*
|
|
27
31
|
* @param apiKey 调用OpenAI 的key
|
|
@@ -195,7 +199,7 @@ class OpenAIGpt extends events_1.EventEmitter {
|
|
|
195
199
|
//subarray.push({ role: 'user', content:'请根据上述内容,给出一道提问与答案以及答案关键词,按照先问题内容,再标准答案,再关键词的顺序输出,关键词之间用、分开'})
|
|
196
200
|
subarray.unshift({ role: 'system', content: `你是一位专业培训师,从以下内容中提取${itemCount}条提问及答案,并从答案内容提取出至少2个关键词,最终结果按照[{"question":"提问内容","answer":"答案内容","keywords":["关键词1","关键词2"]}]的JSON数组结构输出。` });
|
|
197
201
|
//subarray.unshift({ role: 'system', content: `你是一位专业程序开发工程师,根据以下内容,按照[{"question":"问题内容","answer":"答案内容","keywords":["关键词1","关键词2"]}]JSON数组结构,给出${itemCount}条提问问题及答案以及答案关键词` })
|
|
198
|
-
console.log('subarray', subarray)
|
|
202
|
+
// console.log('subarray', subarray)
|
|
199
203
|
let result = yield this.chatRequest(subarray, { replyCounts: 1 }, axiosOption);
|
|
200
204
|
if (result.successed && result.message) {
|
|
201
205
|
// console.log('result is ', result.message[0].message.content)
|
|
@@ -213,7 +217,7 @@ class OpenAIGpt extends events_1.EventEmitter {
|
|
|
213
217
|
arrContent = []; /// 释放内存
|
|
214
218
|
///发出信号,解析完毕
|
|
215
219
|
this.emit('parseover', { type: 'qa', items: faqs });
|
|
216
|
-
return { successed: true, message: faqs };
|
|
220
|
+
return { successed: true, message: faqs.slice(0, count) };
|
|
217
221
|
});
|
|
218
222
|
}
|
|
219
223
|
/**
|
|
@@ -433,42 +437,6 @@ class OpenAIGpt extends events_1.EventEmitter {
|
|
|
433
437
|
console.log('error happened:', err);
|
|
434
438
|
}
|
|
435
439
|
return returnItems.filter(i => { return i != null; }).slice(0, count);
|
|
436
|
-
// let item = result.map(m => {
|
|
437
|
-
// ////防止输出的JSON格式不合法
|
|
438
|
-
// try {
|
|
439
|
-
// let jsonObj = JSON.parse(m.message.content)
|
|
440
|
-
// jsonObj.score = score;
|
|
441
|
-
// if (jsonObj.choice && Array.isArray(jsonObj.choice) && questiontype != 'completion') {
|
|
442
|
-
// jsonObj.fullanswer = (jsonObj.answer + '').replace(/,|[^ABCDE]/g, '');
|
|
443
|
-
// jsonObj.choice = jsonObj.choice.map((item:string, index:number) => {
|
|
444
|
-
// let seqNo = String.fromCharCode(65 + index);
|
|
445
|
-
// let correctReg = new RegExp(`${seqNo}.|${seqNo}`, 'ig')
|
|
446
|
-
// //let answer = jsonObj.fullanswer
|
|
447
|
-
// return {
|
|
448
|
-
// id: seqNo,
|
|
449
|
-
// content: item.replace(correctReg, '').trim(),
|
|
450
|
-
// iscorrect: (jsonObj.fullanswer.indexOf(seqNo) >= 0 || jsonObj.fullanswer.indexOf(m)) >= 0 ? 1 : 0
|
|
451
|
-
// }
|
|
452
|
-
// })
|
|
453
|
-
// }
|
|
454
|
-
// switch (questiontype) {
|
|
455
|
-
// case 'singlechoice':
|
|
456
|
-
// jsonObj.answer = (jsonObj.answer + '').replace(/,|[^ABCDEFG]/g, '').split('').slice(0, 1);
|
|
457
|
-
// break;
|
|
458
|
-
// case 'multiplechoice':
|
|
459
|
-
// jsonObj.answer = (jsonObj.answer + '').replace(/,|[^ABCDEFG]/g, '').split('');
|
|
460
|
-
// break;
|
|
461
|
-
// case 'trueorfalse':
|
|
462
|
-
// jsonObj.answer = [(jsonObj.answer + '').indexOf('正确') >= 0 ? 'A' : 'B']
|
|
463
|
-
// break;
|
|
464
|
-
// }
|
|
465
|
-
// return jsonObj;
|
|
466
|
-
// } catch (err) {
|
|
467
|
-
// console.log('error happened:', err);
|
|
468
|
-
// return null;
|
|
469
|
-
// }
|
|
470
|
-
// })
|
|
471
|
-
// return item.filter(i => { return i != null; });
|
|
472
440
|
}
|
|
473
441
|
/**
|
|
474
442
|
* 将一段很长的文本,按1024长度来划分到多个中
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { ApiCredential } from './baiduai';
|
|
2
|
+
import GptBase from './gptbase';
|
|
3
|
+
/**
|
|
4
|
+
* OpenAI/NLP 的服务提供商 OpenAI,微软,百度文心(待接入),google(待接入)
|
|
5
|
+
*/
|
|
6
|
+
export declare const AIProviderEnum: {
|
|
7
|
+
readonly OPENAI: "openai";
|
|
8
|
+
readonly MICROSOFT: "microsoft";
|
|
9
|
+
readonly BAIDU: "baidu";
|
|
10
|
+
readonly GOOGLE: "google";
|
|
11
|
+
};
|
|
12
|
+
export type AIProviderEnum = typeof AIProviderEnum[keyof typeof AIProviderEnum];
|
|
13
|
+
/**
|
|
14
|
+
* 根据类型创建不同的TTS引擎对象
|
|
15
|
+
* @param {*} provider
|
|
16
|
+
* @param {*} apikey
|
|
17
|
+
* @param {*} setting
|
|
18
|
+
* @returns
|
|
19
|
+
*/
|
|
20
|
+
export declare function createAIInstance(provider: AIProviderEnum, apikey: string | ApiCredential, setting: any): GptBase | null;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.createAIInstance = exports.AIProviderEnum = void 0;
|
|
7
|
+
/**
|
|
8
|
+
* 语音转文字服务商工厂
|
|
9
|
+
*/
|
|
10
|
+
const openai_1 = __importDefault(require("./openai"));
|
|
11
|
+
const azureai_1 = __importDefault(require("./azureai"));
|
|
12
|
+
const baiduai_1 = __importDefault(require("./baiduai"));
|
|
13
|
+
/**
|
|
14
|
+
* OpenAI/NLP 的服务提供商 OpenAI,微软,百度文心(待接入),google(待接入)
|
|
15
|
+
*/
|
|
16
|
+
exports.AIProviderEnum = {
|
|
17
|
+
OPENAI: 'openai',
|
|
18
|
+
MICROSOFT: 'microsoft',
|
|
19
|
+
BAIDU: 'baidu',
|
|
20
|
+
GOOGLE: 'google'
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* 根据类型创建不同的TTS引擎对象
|
|
24
|
+
* @param {*} provider
|
|
25
|
+
* @param {*} apikey
|
|
26
|
+
* @param {*} setting
|
|
27
|
+
* @returns
|
|
28
|
+
*/
|
|
29
|
+
function createAIInstance(provider, apikey, setting) {
|
|
30
|
+
let { model, maxtoken, temperature, endpoint, engine, version } = setting || {};
|
|
31
|
+
switch (provider) {
|
|
32
|
+
case exports.AIProviderEnum.OPENAI:
|
|
33
|
+
return new openai_1.default(apikey + '', { model, maxtoken, temperature });
|
|
34
|
+
case exports.AIProviderEnum.MICROSOFT:
|
|
35
|
+
return new azureai_1.default(apikey + '', { endpoint, engine, version }, { model, maxtoken, temperature });
|
|
36
|
+
case exports.AIProviderEnum.BAIDU:
|
|
37
|
+
let cred = typeof (apikey) === 'string' ? { apikey, securitykey: apikey } : apikey;
|
|
38
|
+
return new baiduai_1.default(cred);
|
|
39
|
+
default: return null;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
exports.createAIInstance = createAIInstance;
|
|
43
|
+
;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "doomiaichat",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.2.0",
|
|
4
4
|
"description": "Doomisoft OpenAI",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
"typescript": "^4.9.5"
|
|
17
17
|
},
|
|
18
18
|
"dependencies": {
|
|
19
|
+
"axios": "^1.3.4",
|
|
19
20
|
"openai": "^3.2.1"
|
|
20
21
|
}
|
|
21
22
|
}
|
package/src/azureai.ts
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
|
-
import { AzureOpenAIPatameters, ChatReponse, OpenAIApiParameters } from "./declare";
|
|
2
|
-
import { AxiosRequestConfig, AxiosResponse, Axios } from "axios";
|
|
1
|
+
import { AzureOpenAIPatameters, ChatReponse, OpenAIApiParameters, request } from "./declare";
|
|
3
2
|
import OpenAIGpt from "./openai"
|
|
4
3
|
import { ChatCompletionRequestMessage } from "openai";
|
|
5
4
|
export default class AzureAI extends OpenAIGpt {
|
|
6
|
-
protected readonly apiKey: string;
|
|
7
5
|
protected readonly azureSetting: AzureOpenAIPatameters;
|
|
8
6
|
constructor(apiKey: string, azureOption:AzureOpenAIPatameters, apiOption: OpenAIApiParameters = {}) {
|
|
9
7
|
super(apiKey, apiOption);
|
|
@@ -22,7 +20,7 @@ export default class AzureAI extends OpenAIGpt {
|
|
|
22
20
|
/**
|
|
23
21
|
* 请求GPT接口
|
|
24
22
|
*/
|
|
25
|
-
public async chatRequest(chatText: string | Array<any>, paramOption: OpenAIApiParameters, axiosOption:
|
|
23
|
+
public override async chatRequest(chatText: string | Array<any>, paramOption: OpenAIApiParameters, axiosOption: any = {}): Promise<ChatReponse> {
|
|
26
24
|
if (!chatText) return { successed: false, error: { errcode: 2, errmsg: '缺失聊天的内容' } };
|
|
27
25
|
if (!axiosOption.headers )
|
|
28
26
|
axiosOption.headers = { 'api-key': this.apiKey,'Content-Type':'application/json'};
|
|
@@ -33,13 +31,19 @@ export default class AzureAI extends OpenAIGpt {
|
|
|
33
31
|
|
|
34
32
|
let messages: Array<ChatCompletionRequestMessage> = typeof (chatText) == 'string' ?
|
|
35
33
|
[{ role: 'user', content: chatText }] : chatText;
|
|
36
|
-
let axios = new Axios(axiosOption)
|
|
37
34
|
try{
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
35
|
+
let param = {
|
|
36
|
+
...axiosOption,
|
|
37
|
+
method: "post",
|
|
38
|
+
data: {
|
|
39
|
+
messages,
|
|
40
|
+
temperature: Number(paramOption?.temperature || this.temperature),
|
|
41
|
+
max_tokens: Number(paramOption?.maxtoken || this.maxtoken),
|
|
42
|
+
},
|
|
43
|
+
url: this.BaseUrl
|
|
44
|
+
};
|
|
45
|
+
// console.log('axiosOption', param)
|
|
46
|
+
const response =await request(param)
|
|
43
47
|
if (response.data.choices){
|
|
44
48
|
return { successed: true, message: response.data.choices };
|
|
45
49
|
}
|
package/src/baiduai.ts
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { EmotionResult, SimilarityResult, ChatReponse, SummaryReponse, ExaminationPaperResult, ApiResult, CacheProvider, request } from "./declare";
|
|
2
|
+
import GptBase from "./gptbase"
|
|
3
|
+
const TOKEN_CACHE_KEY = "key:_doomisoft:baiduwenxin:"
|
|
4
|
+
export default class BaiduWenXinAI extends GptBase {
|
|
5
|
+
protected credential: ApiCredential;
|
|
6
|
+
private Cacher: CacheProvider|undefined;
|
|
7
|
+
/**
|
|
8
|
+
*
|
|
9
|
+
* @param credential 调用OpenAI 的key
|
|
10
|
+
* @param cacher 用作accesstoken的缓存
|
|
11
|
+
* @param apiOption 用作accesstoken的缓存
|
|
12
|
+
*/
|
|
13
|
+
constructor(credential: ApiCredential, cacher?:CacheProvider) {
|
|
14
|
+
super();
|
|
15
|
+
this.credential = credential;
|
|
16
|
+
this.Cacher = cacher;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* 获取调用Api的Token
|
|
20
|
+
*/
|
|
21
|
+
async getAccessToken():Promise<AccessTokenResult>{
|
|
22
|
+
////如果当前应用是一个委托到第三方的
|
|
23
|
+
///公众号或小程序,则需要用关联的第三方平台来获取api调用的accesstoken
|
|
24
|
+
////如果是需要缓存token,则首先看缓存中是否有AccessToken
|
|
25
|
+
const accesstoken = this.Cacher ? (await this.Cacher.get(TOKEN_CACHE_KEY+this.credential.apikey)):null;
|
|
26
|
+
if (accesstoken) return { successed: true, access_token: accesstoken };
|
|
27
|
+
const option = {
|
|
28
|
+
url: `https://wenxin.baidu.com/moduleApi/portal/api/oauth/token?grant_type=client_credentials&client_id=${this.credential.apikey}&client_secret=${this.credential.securitykey}`,
|
|
29
|
+
method:"post",
|
|
30
|
+
};
|
|
31
|
+
const tokenData = await request(option);
|
|
32
|
+
if (tokenData.successed == true && !tokenData.data.code) {
|
|
33
|
+
///把api accesstoken缓存起来
|
|
34
|
+
if (this.Cacher){
|
|
35
|
+
this.Cacher.set(TOKEN_CACHE_KEY + this.credential.apikey, tokenData.data.data,3600*20);
|
|
36
|
+
}
|
|
37
|
+
return { successed: true, access_token: tokenData.data.data };
|
|
38
|
+
}
|
|
39
|
+
return { successed: false, error: tokenData.data.msg }
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* 请求GPT接口
|
|
43
|
+
*/
|
|
44
|
+
public async chatRequest(chatText: string | Array<any>, _paramOption: any, axiosOption: any = {}): Promise<ApiResult> {
|
|
45
|
+
console.log('call baidu')
|
|
46
|
+
if (!chatText) return { successed: false, error: { errcode: 2, errmsg: '缺失聊天的内容' } };
|
|
47
|
+
const accessToken = await this.getAccessToken();
|
|
48
|
+
if (!accessToken.successed || !accessToken.access_token) return {successed:false,error:'accesstoken get failed'}
|
|
49
|
+
try {
|
|
50
|
+
let param = {
|
|
51
|
+
...axiosOption,
|
|
52
|
+
method: "post",
|
|
53
|
+
data: {
|
|
54
|
+
text: `问题:${chatText}\n回答:`,
|
|
55
|
+
seq_len: 512,
|
|
56
|
+
topp: 0.5,
|
|
57
|
+
penalty_score: 1.2,
|
|
58
|
+
min_dec_len: 12,
|
|
59
|
+
min_dec_penalty_text: "。?:![<S>]",
|
|
60
|
+
task_prompt: "qa",
|
|
61
|
+
mask_type: "paragraph"
|
|
62
|
+
},
|
|
63
|
+
url:`https://wenxin.baidu.com/moduleApi/portal/api/rest/1.0/ernie/3.0.25/zeus?access_token=${accessToken.access_token}`
|
|
64
|
+
};
|
|
65
|
+
console.log('param', param)
|
|
66
|
+
const response = await request(param)
|
|
67
|
+
if (response.successed && !response.data.code) {
|
|
68
|
+
return { successed: true, ...response.data };
|
|
69
|
+
}
|
|
70
|
+
return { successed: false, ...response.data };
|
|
71
|
+
} catch (error) {
|
|
72
|
+
console.log('result is error ', error)
|
|
73
|
+
return { successed: false, error };
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
}
|
|
77
|
+
getScentenceEmotional(_s1: string, _axiosOption: any): Promise<EmotionResult> {
|
|
78
|
+
throw new Error("Method not implemented.");
|
|
79
|
+
}
|
|
80
|
+
getScentenseSimilarity(_s1: string, _s2: string, _axiosOption: any): Promise<SimilarityResult> {
|
|
81
|
+
throw new Error("Method not implemented.");
|
|
82
|
+
}
|
|
83
|
+
getSimilarityContent(_content: string, _count: number, _axiosOption: any): Promise<ChatReponse> {
|
|
84
|
+
throw new Error("Method not implemented.");
|
|
85
|
+
}
|
|
86
|
+
getSummaryOfContent(_content: string | any[], _axiosOption: any): Promise<SummaryReponse> {
|
|
87
|
+
throw new Error("Method not implemented.");
|
|
88
|
+
}
|
|
89
|
+
generateQuestionsFromContent(_content: string, _count: number, _axiosOption: any): Promise<ChatReponse> {
|
|
90
|
+
throw new Error("Method not implemented.");
|
|
91
|
+
}
|
|
92
|
+
generateExaminationPaperFromContent(_content: string, _paperOption: any, _axiosOption: any): Promise<ExaminationPaperResult> {
|
|
93
|
+
throw new Error("Method not implemented.");
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
}
|
|
97
|
+
export interface AccessTokenResult extends ApiResult {
|
|
98
|
+
'access_token'?: string
|
|
99
|
+
}
|
|
100
|
+
export interface ApiCredential{
|
|
101
|
+
'apikey':string,
|
|
102
|
+
'securitykey': string
|
|
103
|
+
}
|
package/src/declare.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
|
|
2
|
-
|
|
2
|
+
import axios from 'axios';
|
|
3
|
+
export interface ApiResult {
|
|
3
4
|
/**
|
|
4
5
|
* return the result of api called
|
|
5
6
|
* @type {boolean}
|
|
@@ -102,4 +103,51 @@ export interface SimilarityResult extends ApiResult {
|
|
|
102
103
|
*/
|
|
103
104
|
export interface EmotionResult extends ApiResult {
|
|
104
105
|
'emotion'?: string; ///情绪
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* 远程请求的返回
|
|
109
|
+
*/
|
|
110
|
+
export interface RpcResult extends ApiResult {
|
|
111
|
+
'data'?: any;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Axios远程请求封装
|
|
116
|
+
* @param opts
|
|
117
|
+
* @returns
|
|
118
|
+
*/
|
|
119
|
+
export async function request(opts: any = {}): Promise<RpcResult> {
|
|
120
|
+
if (!opts.data) opts.data = opts.body;
|
|
121
|
+
try {
|
|
122
|
+
let result = await axios(opts);
|
|
123
|
+
return { successed: true, data: result.data }
|
|
124
|
+
} catch (err) {
|
|
125
|
+
return { successed: false, error: err }
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* 数据缓存提供者接口
|
|
132
|
+
*/
|
|
133
|
+
export interface CacheProvider {
|
|
134
|
+
/**
|
|
135
|
+
* 缓存数据
|
|
136
|
+
* @param key 数据的键名称
|
|
137
|
+
* @param value 数据的键值
|
|
138
|
+
*/
|
|
139
|
+
set(key: string, value: string | object, exp?: number): void;
|
|
140
|
+
/**
|
|
141
|
+
* 从缓存中读取数据
|
|
142
|
+
* @param key 数据的键名称
|
|
143
|
+
*/
|
|
144
|
+
get(key: string): Promise<string | null>;
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* 删除缓存信息
|
|
149
|
+
* @param key 数据的键名称
|
|
150
|
+
*/
|
|
151
|
+
delete(key: string): void;
|
|
152
|
+
|
|
105
153
|
}
|
package/src/gptbase.ts
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { EventEmitter } from "events"
|
|
2
|
+
import { ChatReponse, SummaryReponse, ExaminationPaperResult, EmotionResult, SimilarityResult, ApiResult } from './declare'
|
|
3
|
+
|
|
4
|
+
export default abstract class GptBase extends EventEmitter {
|
|
5
|
+
/**
|
|
6
|
+
* 构造函数
|
|
7
|
+
*/
|
|
8
|
+
constructor() {
|
|
9
|
+
super();
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* 自由聊天模式
|
|
13
|
+
* @param chatText
|
|
14
|
+
* @param _paramOption
|
|
15
|
+
* @param axiosOption
|
|
16
|
+
*/
|
|
17
|
+
abstract chatRequest(chatText: string | Array<any>, _paramOption: any, axiosOption: any): Promise<ApiResult>;
|
|
18
|
+
/**
|
|
19
|
+
* 获取句子的情感
|
|
20
|
+
* @param s1
|
|
21
|
+
* @param axiosOption
|
|
22
|
+
*/
|
|
23
|
+
abstract getScentenceEmotional(s1: string, axiosOption: any ): Promise<EmotionResult> ;
|
|
24
|
+
/**
|
|
25
|
+
* 获取两段文本的相似度
|
|
26
|
+
* @param s1
|
|
27
|
+
* @param s2
|
|
28
|
+
* @param axiosOption
|
|
29
|
+
*/
|
|
30
|
+
abstract getScentenseSimilarity(s1: string, s2: string, axiosOption: any): Promise<SimilarityResult>;
|
|
31
|
+
/**
|
|
32
|
+
* 获取一段文本的相似内容
|
|
33
|
+
* @param content
|
|
34
|
+
* @param count
|
|
35
|
+
* @param axiosOption
|
|
36
|
+
*/
|
|
37
|
+
abstract getSimilarityContent(content: string, count: number, axiosOption: any): Promise<ChatReponse>;
|
|
38
|
+
/**
|
|
39
|
+
* 获取内容的摘要
|
|
40
|
+
* @param content
|
|
41
|
+
* @param axiosOption
|
|
42
|
+
*/
|
|
43
|
+
abstract getSummaryOfContent(content: string | Array<any>, axiosOption: any): Promise<SummaryReponse>;
|
|
44
|
+
/**
|
|
45
|
+
* 从内容中提取问题和答案及管件子
|
|
46
|
+
* @param content
|
|
47
|
+
* @param count
|
|
48
|
+
* @param axiosOption
|
|
49
|
+
*/
|
|
50
|
+
abstract generateQuestionsFromContent(content: string, count: number, axiosOption: any): Promise<ChatReponse>;
|
|
51
|
+
/**
|
|
52
|
+
* 从内容中提取单选多选判断填空题
|
|
53
|
+
* @param content
|
|
54
|
+
* @param paperOption
|
|
55
|
+
* @param axiosOption
|
|
56
|
+
*/
|
|
57
|
+
abstract generateExaminationPaperFromContent(content: string, paperOption: any, axiosOption: any): Promise<ExaminationPaperResult>;
|
|
58
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
//ts check
|
|
2
|
+
/**
|
|
3
|
+
* 语音转文字服务商工厂
|
|
4
|
+
*/
|
|
5
|
+
import OpenAIGpt from './openai';
|
|
6
|
+
import AzureAI from './azureai'
|
|
7
|
+
import BaiduWenXinAI, { ApiCredential } from './baiduai'
|
|
8
|
+
import GptBase from './gptbase';
|
|
9
|
+
/**
|
|
10
|
+
* OpenAI/NLP 的服务提供商 OpenAI,微软,百度文心(待接入),google(待接入)
|
|
11
|
+
*/
|
|
12
|
+
export const GptProviderEnum = {
|
|
13
|
+
OPENAI: 'openai',
|
|
14
|
+
MICROSOFT: 'microsoft',
|
|
15
|
+
BAIDU: 'baidu',
|
|
16
|
+
GOOGLE:'google'
|
|
17
|
+
} as const;
|
|
18
|
+
export type GptProviderEnum = typeof GptProviderEnum[keyof typeof GptProviderEnum];
|
|
19
|
+
/**
|
|
20
|
+
* 根据类型创建不同的TTS引擎对象
|
|
21
|
+
* @param {*} provider
|
|
22
|
+
* @param {*} apikey
|
|
23
|
+
* @param {*} setting
|
|
24
|
+
* @returns
|
|
25
|
+
*/
|
|
26
|
+
export function createGpt(provider: GptProviderEnum, apikey: string|ApiCredential, setting: any): GptBase | null {
|
|
27
|
+
let { model, maxtoken, temperature,endpoint,engine,version } = setting || {};
|
|
28
|
+
switch (provider) {
|
|
29
|
+
case GptProviderEnum.OPENAI:
|
|
30
|
+
return new OpenAIGpt(apikey+'', { model, maxtoken, temperature });
|
|
31
|
+
case GptProviderEnum.MICROSOFT:
|
|
32
|
+
return new AzureAI(apikey+'', { endpoint, engine, version }, { model, maxtoken, temperature }, );
|
|
33
|
+
case GptProviderEnum.BAIDU:
|
|
34
|
+
let cred: ApiCredential = typeof (apikey) === 'string' ? { apikey, securitykey: apikey } : apikey
|
|
35
|
+
return new BaiduWenXinAI(cred);
|
|
36
|
+
default: return null;
|
|
37
|
+
}
|
|
38
|
+
};
|
package/src/index.ts
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
export * as
|
|
1
|
+
export * as GptFactory from "./gptprovider";
|
|
2
|
+
export * as GptBase from "./gptbase";
|
package/src/openai.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { Configuration, OpenAIApi,
|
|
2
|
-
import { EventEmitter } from "events";
|
|
3
|
-
import
|
|
1
|
+
import { Configuration, OpenAIApi, ChatCompletionRequestMessage } from "openai"
|
|
2
|
+
// import { EventEmitter } from "events";
|
|
3
|
+
import GptBase from "./gptbase"
|
|
4
4
|
import { OpenAIApiParameters, ChatReponse, OutlineSummaryItem, SummaryReponse, FaqItem, ExaminationPaperResult, EmotionResult, SimilarityResult, QuestionItem } from './declare'
|
|
5
5
|
const SECTION_LENGTH = 1600; ///每2400个字符分成一组
|
|
6
6
|
const MESSAGE_LENGTH = 1; ///每次送8句话给openai 进行解析,送多了,会报错
|
|
@@ -14,7 +14,7 @@ const QUESTION_TEXT_MAPPING: any = {
|
|
|
14
14
|
|
|
15
15
|
const QUESTION_TYPE: string[] = ['singlechoice', 'multiplechoice', 'trueorfalse', 'completion']
|
|
16
16
|
|
|
17
|
-
export default class OpenAIGpt extends
|
|
17
|
+
export default class OpenAIGpt extends GptBase {
|
|
18
18
|
protected readonly apiKey: string;
|
|
19
19
|
private aiApi: OpenAIApi | undefined;
|
|
20
20
|
protected readonly chatModel: string;
|
|
@@ -42,7 +42,7 @@ export default class OpenAIGpt extends EventEmitter {
|
|
|
42
42
|
* 向OpenAI发送一个聊天请求
|
|
43
43
|
* @param {*} chatText
|
|
44
44
|
*/
|
|
45
|
-
public async chatRequest(chatText: string | Array<any>, callChatOption: OpenAIApiParameters, axiosOption:
|
|
45
|
+
public async chatRequest(chatText: string | Array<any>, callChatOption: OpenAIApiParameters, axiosOption: any = {}): Promise<ChatReponse> {
|
|
46
46
|
if (!chatText) return { successed: false, error: { errcode: 2, errmsg: '缺失聊天的内容' } };
|
|
47
47
|
if (!this.aiApi){
|
|
48
48
|
this.aiApi = this.createOpenAI(this.apiKey);
|
|
@@ -53,8 +53,7 @@ export default class OpenAIGpt extends EventEmitter {
|
|
|
53
53
|
[{ role: 'user', content: chatText }] : chatText;
|
|
54
54
|
// console.log('message', message)
|
|
55
55
|
try {
|
|
56
|
-
const response
|
|
57
|
-
= await this.aiApi.createChatCompletion({
|
|
56
|
+
const response= await this.aiApi.createChatCompletion({
|
|
58
57
|
model: callChatOption?.model || this.chatModel,
|
|
59
58
|
messages: message,
|
|
60
59
|
temperature: Number(callChatOption?.temperature || this.temperature),
|
|
@@ -124,7 +123,7 @@ export default class OpenAIGpt extends EventEmitter {
|
|
|
124
123
|
* @param {*} content
|
|
125
124
|
* @param {需要出来的数量} count
|
|
126
125
|
*/
|
|
127
|
-
async getSimilarityContent(content: string, count: number = 1, axiosOption:
|
|
126
|
+
async getSimilarityContent(content: string, count: number = 1, axiosOption: any = {}): Promise<ChatReponse> {
|
|
128
127
|
let chnReg: boolean = /([\u4e00-\u9fa5]|[\ufe30-\uffa0])/.test(content) ///检查源话是否含有中文内容
|
|
129
128
|
let engReg: boolean = /[a-zA-Z]/.test(content) ///检查源话是否含有英文内容
|
|
130
129
|
///如果源话是全中文,那么结果中不应该出来英文的相似说法,如果源话是全英文,则结果不能出现全中文的说法
|
|
@@ -140,7 +139,7 @@ export default class OpenAIGpt extends EventEmitter {
|
|
|
140
139
|
* @param content
|
|
141
140
|
* @param axiosOption
|
|
142
141
|
*/
|
|
143
|
-
async getSummaryOfContent(content: string | Array<any>, axiosOption:
|
|
142
|
+
async getSummaryOfContent(content: string | Array<any>, axiosOption: any = {}): Promise<SummaryReponse> {
|
|
144
143
|
const arrContent = typeof (content) == 'string' ? this.splitLongText(content) : content;
|
|
145
144
|
let summary: Array<OutlineSummaryItem> = [];
|
|
146
145
|
while (arrContent.length > 0) {
|
|
@@ -168,12 +167,12 @@ export default class OpenAIGpt extends EventEmitter {
|
|
|
168
167
|
* @param {*} axiosOption
|
|
169
168
|
* @returns
|
|
170
169
|
*///并在答案末尾处必须给出答案内容中的关键词
|
|
171
|
-
async generateQuestionsFromContent(content: string, count: number = 1, axiosOption:
|
|
170
|
+
async generateQuestionsFromContent(content: string, count: number = 1, axiosOption: any = {}): Promise<ChatReponse> {
|
|
172
171
|
let arrContent = this.splitLongText(content, 300);
|
|
173
172
|
///没20句话分为一组,适应大文件内容多次请求组合结果
|
|
174
173
|
///每一句话需要产生的题目
|
|
175
174
|
let questions4EverySentense: number = count / arrContent.length; //Math.ceil(arrContent.length / 20);
|
|
176
|
-
let faqs:
|
|
175
|
+
let faqs: FaqItem[] = [], gotted: number = 0;
|
|
177
176
|
while (arrContent.length > 0 && gotted < count) {
|
|
178
177
|
questions4EverySentense = (count - gotted) / arrContent.length
|
|
179
178
|
////每次最多送MESSAGE_LENGTH句话给openai
|
|
@@ -182,7 +181,7 @@ export default class OpenAIGpt extends EventEmitter {
|
|
|
182
181
|
//subarray.push({ role: 'user', content:'请根据上述内容,给出一道提问与答案以及答案关键词,按照先问题内容,再标准答案,再关键词的顺序输出,关键词之间用、分开'})
|
|
183
182
|
subarray.unshift({ role: 'system', content: `你是一位专业培训师,从以下内容中提取${itemCount}条提问及答案,并从答案内容提取出至少2个关键词,最终结果按照[{"question":"提问内容","answer":"答案内容","keywords":["关键词1","关键词2"]}]的JSON数组结构输出。` })
|
|
184
183
|
//subarray.unshift({ role: 'system', content: `你是一位专业程序开发工程师,根据以下内容,按照[{"question":"问题内容","answer":"答案内容","keywords":["关键词1","关键词2"]}]JSON数组结构,给出${itemCount}条提问问题及答案以及答案关键词` })
|
|
185
|
-
console.log('subarray', subarray)
|
|
184
|
+
// console.log('subarray', subarray)
|
|
186
185
|
let result = await this.chatRequest(subarray, { replyCounts: 1 }, axiosOption);
|
|
187
186
|
if (result.successed && result.message) {
|
|
188
187
|
// console.log('result is ', result.message[0].message.content)
|
|
@@ -202,7 +201,7 @@ export default class OpenAIGpt extends EventEmitter {
|
|
|
202
201
|
arrContent = []; /// 释放内存
|
|
203
202
|
///发出信号,解析完毕
|
|
204
203
|
this.emit('parseover', { type: 'qa', items: faqs })
|
|
205
|
-
return { successed: true, message: faqs };
|
|
204
|
+
return { successed: true, message: faqs.slice(0, count) };
|
|
206
205
|
}
|
|
207
206
|
|
|
208
207
|
/**
|
|
@@ -212,7 +211,7 @@ export default class OpenAIGpt extends EventEmitter {
|
|
|
212
211
|
* @param {*} axiosOption
|
|
213
212
|
* @returns
|
|
214
213
|
*///并在答案末尾处必须给出答案内容中的关键词
|
|
215
|
-
async generateQuestionsFromContent_Orgin(content: string, count: number = 1, promotion: string | null = null, sliceslength: number = SECTION_LENGTH, axiosOption:
|
|
214
|
+
async generateQuestionsFromContent_Orgin(content: string, count: number = 1, promotion: string | null = null, sliceslength: number = SECTION_LENGTH, axiosOption: any = {}): Promise<ChatReponse> {
|
|
216
215
|
let arrContent = this.splitLongText(content, sliceslength || 300);
|
|
217
216
|
///没20句话分为一组,适应大文件内容多次请求组合结果
|
|
218
217
|
///每一句话需要产生的题目
|
|
@@ -278,7 +277,7 @@ export default class OpenAIGpt extends EventEmitter {
|
|
|
278
277
|
* @param {*} axiosOption
|
|
279
278
|
* @returns
|
|
280
279
|
*///并在答案末尾处必须给出答案内容中的关键词
|
|
281
|
-
async generateExaminationPaperFromContent(content: string, paperOption: any = {}, axiosOption:
|
|
280
|
+
async generateExaminationPaperFromContent(content: string, paperOption: any = {}, axiosOption: any = {}): Promise<ExaminationPaperResult> {
|
|
282
281
|
let arrContent = this.splitLongText(content, 1024);
|
|
283
282
|
let sectionCount: any = {
|
|
284
283
|
singlechoice: (paperOption.singlechoice?.count || 0) / arrContent.length,
|
|
@@ -417,42 +416,6 @@ export default class OpenAIGpt extends EventEmitter {
|
|
|
417
416
|
console.log('error happened:', err);
|
|
418
417
|
}
|
|
419
418
|
return returnItems.filter(i => { return i != null; }).slice(0, count);
|
|
420
|
-
// let item = result.map(m => {
|
|
421
|
-
// ////防止输出的JSON格式不合法
|
|
422
|
-
// try {
|
|
423
|
-
// let jsonObj = JSON.parse(m.message.content)
|
|
424
|
-
// jsonObj.score = score;
|
|
425
|
-
// if (jsonObj.choice && Array.isArray(jsonObj.choice) && questiontype != 'completion') {
|
|
426
|
-
// jsonObj.fullanswer = (jsonObj.answer + '').replace(/,|[^ABCDE]/g, '');
|
|
427
|
-
// jsonObj.choice = jsonObj.choice.map((item:string, index:number) => {
|
|
428
|
-
// let seqNo = String.fromCharCode(65 + index);
|
|
429
|
-
// let correctReg = new RegExp(`${seqNo}.|${seqNo}`, 'ig')
|
|
430
|
-
// //let answer = jsonObj.fullanswer
|
|
431
|
-
// return {
|
|
432
|
-
// id: seqNo,
|
|
433
|
-
// content: item.replace(correctReg, '').trim(),
|
|
434
|
-
// iscorrect: (jsonObj.fullanswer.indexOf(seqNo) >= 0 || jsonObj.fullanswer.indexOf(m)) >= 0 ? 1 : 0
|
|
435
|
-
// }
|
|
436
|
-
// })
|
|
437
|
-
// }
|
|
438
|
-
// switch (questiontype) {
|
|
439
|
-
// case 'singlechoice':
|
|
440
|
-
// jsonObj.answer = (jsonObj.answer + '').replace(/,|[^ABCDEFG]/g, '').split('').slice(0, 1);
|
|
441
|
-
// break;
|
|
442
|
-
// case 'multiplechoice':
|
|
443
|
-
// jsonObj.answer = (jsonObj.answer + '').replace(/,|[^ABCDEFG]/g, '').split('');
|
|
444
|
-
// break;
|
|
445
|
-
// case 'trueorfalse':
|
|
446
|
-
// jsonObj.answer = [(jsonObj.answer + '').indexOf('正确') >= 0 ? 'A' : 'B']
|
|
447
|
-
// break;
|
|
448
|
-
// }
|
|
449
|
-
// return jsonObj;
|
|
450
|
-
// } catch (err) {
|
|
451
|
-
// console.log('error happened:', err);
|
|
452
|
-
// return null;
|
|
453
|
-
// }
|
|
454
|
-
// })
|
|
455
|
-
// return item.filter(i => { return i != null; });
|
|
456
419
|
}
|
|
457
420
|
/**
|
|
458
421
|
* 将一段很长的文本,按1024长度来划分到多个中
|