doomiaichat 1.2.0 → 1.4.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/index.d.ts +24 -14
- package/dist/index.js +19 -4
- package/package.json +1 -1
- package/src/index.ts +45 -21
package/dist/index.d.ts
CHANGED
|
@@ -22,13 +22,13 @@ export declare class AIChat extends EventEmitter {
|
|
|
22
22
|
* @param {*} s1
|
|
23
23
|
* @param {*} axiosOption
|
|
24
24
|
*/
|
|
25
|
-
getScentenceEmotional(s1: string, axiosOption?: any): Promise<
|
|
25
|
+
getScentenceEmotional(s1: string, axiosOption?: any): Promise<EmotionResult>;
|
|
26
26
|
/**
|
|
27
27
|
* 获取两句话的相似度取值
|
|
28
28
|
* @param {*} s1
|
|
29
29
|
* @param {*} s2
|
|
30
30
|
*/
|
|
31
|
-
getScentenseSimilarity(s1: string, s2: string, axiosOption?: any): Promise<
|
|
31
|
+
getScentenseSimilarity(s1: string, s2: string, axiosOption?: any): Promise<SimilarityResult>;
|
|
32
32
|
/**
|
|
33
33
|
* 获得一种内容的相似说法
|
|
34
34
|
* 比如:
|
|
@@ -73,10 +73,7 @@ export declare class AIChat extends EventEmitter {
|
|
|
73
73
|
*/
|
|
74
74
|
private splitLongText;
|
|
75
75
|
}
|
|
76
|
-
|
|
77
|
-
* Api封装后的返回结果
|
|
78
|
-
*/
|
|
79
|
-
export interface ChatReponse {
|
|
76
|
+
interface ApiResult {
|
|
80
77
|
/**
|
|
81
78
|
* return the result of api called
|
|
82
79
|
* @type {boolean}
|
|
@@ -88,6 +85,11 @@ export interface ChatReponse {
|
|
|
88
85
|
* @memberof ChatReponse
|
|
89
86
|
*/
|
|
90
87
|
'error'?: any;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Api封装后的返回结果
|
|
91
|
+
*/
|
|
92
|
+
export interface ChatReponse extends ApiResult {
|
|
91
93
|
/**
|
|
92
94
|
* The name of the user in a multi-user chat
|
|
93
95
|
* @type {Array<any>}
|
|
@@ -107,7 +109,7 @@ export interface CallChatApiOption {
|
|
|
107
109
|
/**
|
|
108
110
|
* 调用OpenAI Api的参数约定
|
|
109
111
|
*/
|
|
110
|
-
export interface
|
|
112
|
+
export interface FaqItem {
|
|
111
113
|
'question': string;
|
|
112
114
|
'answer'?: string;
|
|
113
115
|
'keywords'?: Array<string>;
|
|
@@ -115,21 +117,29 @@ export interface FaqResultResponse {
|
|
|
115
117
|
/**
|
|
116
118
|
* 调用OpenAI Api的参数约定
|
|
117
119
|
*/
|
|
118
|
-
export interface ExaminationPaperResult {
|
|
119
|
-
/**
|
|
120
|
-
* return the result of api called
|
|
121
|
-
* @type {boolean}
|
|
122
|
-
*/
|
|
123
|
-
'successed': boolean;
|
|
120
|
+
export interface ExaminationPaperResult extends ApiResult {
|
|
124
121
|
'score': number;
|
|
125
122
|
'paper': any;
|
|
126
123
|
}
|
|
127
124
|
/**
|
|
128
125
|
* 调用OpenAI Api的参数约定
|
|
129
126
|
*/
|
|
130
|
-
export interface
|
|
127
|
+
export interface QuestionItem {
|
|
131
128
|
'question': string;
|
|
132
129
|
'answer'?: Array<string>;
|
|
133
130
|
'choice'?: Array<string>;
|
|
134
131
|
'score'?: number;
|
|
135
132
|
}
|
|
133
|
+
/**
|
|
134
|
+
* 调用OpenAI Api的参数约定
|
|
135
|
+
*/
|
|
136
|
+
export interface SimilarityResult extends ApiResult {
|
|
137
|
+
'value'?: number;
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* 调用OpenAI Api的参数约定
|
|
141
|
+
*/
|
|
142
|
+
export interface EmotionResult extends ApiResult {
|
|
143
|
+
'emotion'?: string;
|
|
144
|
+
}
|
|
145
|
+
export {};
|
package/dist/index.js
CHANGED
|
@@ -49,11 +49,11 @@ class AIChat extends events_1.EventEmitter {
|
|
|
49
49
|
[{ role: 'system', content: chatText + '' }] : chatText;
|
|
50
50
|
try {
|
|
51
51
|
const response = yield this.chatRobot.createChatCompletion({
|
|
52
|
-
model: callChatOption.model || this.chatModel,
|
|
52
|
+
model: (callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.model) || this.chatModel,
|
|
53
53
|
messages: message,
|
|
54
54
|
temperature: (callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.temperature) || this.temperature,
|
|
55
55
|
max_tokens: (callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.maxtoken) || this.maxtoken,
|
|
56
|
-
n: callChatOption.replyCounts || 1
|
|
56
|
+
n: (callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.replyCounts) || 1
|
|
57
57
|
}, axiosOption);
|
|
58
58
|
return { successed: true, message: response.data.choices };
|
|
59
59
|
}
|
|
@@ -76,7 +76,15 @@ class AIChat extends events_1.EventEmitter {
|
|
|
76
76
|
{ role: 'user', content: s1 },
|
|
77
77
|
{ role: 'user', content: `请分析上述内容的语言情绪,请从"${emotion.join(',')}"这些情绪中对应一个输出` },
|
|
78
78
|
];
|
|
79
|
-
|
|
79
|
+
const result = yield this.chatRequest(messages, {}, axiosOption);
|
|
80
|
+
if (result.successed && result.message) {
|
|
81
|
+
let value = result.message[0].message.content.trim();
|
|
82
|
+
for (const word of emotion) {
|
|
83
|
+
if (value.indexOf(word) >= 0)
|
|
84
|
+
return { successed: true, emotion: word };
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return { successed: true, emotion: '未知' };
|
|
80
88
|
});
|
|
81
89
|
}
|
|
82
90
|
/**
|
|
@@ -93,7 +101,14 @@ class AIChat extends events_1.EventEmitter {
|
|
|
93
101
|
{ role: 'user', content: s2 },
|
|
94
102
|
{ role: 'user', content: '请从语义上对比以上两句话的相似度,请仅输出0至100之间的整数对比结果即可' },
|
|
95
103
|
];
|
|
96
|
-
|
|
104
|
+
const result = yield this.chatRequest(messages, { maxtoken: 32 }, axiosOption);
|
|
105
|
+
if (result.successed && result.message) {
|
|
106
|
+
let value = result.message[0].message.content.replace(/[^\d]/g, "");
|
|
107
|
+
if (value > 100)
|
|
108
|
+
value = Math.floor(value / 10);
|
|
109
|
+
return { successed: true, value: Number(value) };
|
|
110
|
+
}
|
|
111
|
+
return { successed: false, error: result.error, value: 0 };
|
|
97
112
|
});
|
|
98
113
|
}
|
|
99
114
|
/**
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -43,11 +43,11 @@ export class AIChat extends EventEmitter {
|
|
|
43
43
|
try {
|
|
44
44
|
const response: AxiosResponse<CreateChatCompletionResponse, any>
|
|
45
45
|
= await this.chatRobot.createChatCompletion({
|
|
46
|
-
model: callChatOption
|
|
46
|
+
model: callChatOption?.model || this.chatModel,
|
|
47
47
|
messages: message,
|
|
48
48
|
temperature: callChatOption?.temperature || this.temperature,
|
|
49
49
|
max_tokens: callChatOption?.maxtoken || this.maxtoken,
|
|
50
|
-
n: callChatOption
|
|
50
|
+
n: callChatOption?.replyCounts || 1
|
|
51
51
|
}, axiosOption);
|
|
52
52
|
return {successed:true,message: response.data.choices};
|
|
53
53
|
} catch (error) {
|
|
@@ -62,14 +62,21 @@ export class AIChat extends EventEmitter {
|
|
|
62
62
|
* @param {*} s1
|
|
63
63
|
* @param {*} axiosOption
|
|
64
64
|
*/
|
|
65
|
-
async getScentenceEmotional(s1:string, axiosOption: any = { timeout: 30000 }):Promise<
|
|
65
|
+
async getScentenceEmotional(s1:string, axiosOption: any = { timeout: 30000 }):Promise<EmotionResult> {
|
|
66
66
|
if (!s1) return { successed: false, error:{errcode: 2, errmsg: '缺失参数'} }
|
|
67
67
|
const emotion = ['愤怒', '威胁', '讽刺', '愧疚', '兴奋', '友好', '消极', '生气', '正常'];
|
|
68
68
|
const messages = [
|
|
69
69
|
{ role: 'user', content: s1 },
|
|
70
70
|
{ role: 'user', content: `请分析上述内容的语言情绪,请从"${emotion.join(',')}"这些情绪中对应一个输出` },
|
|
71
71
|
]
|
|
72
|
-
|
|
72
|
+
const result = await this.chatRequest(messages,{}, axiosOption);
|
|
73
|
+
if (result.successed && result.message){
|
|
74
|
+
let value = result.message[0].message.content.trim();
|
|
75
|
+
for (const word of emotion) {
|
|
76
|
+
if (value.indexOf(word) >= 0) return { successed: true, emotion: word };
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return { successed: true, emotion: '未知' };
|
|
73
80
|
}
|
|
74
81
|
|
|
75
82
|
/**
|
|
@@ -77,14 +84,21 @@ export class AIChat extends EventEmitter {
|
|
|
77
84
|
* @param {*} s1
|
|
78
85
|
* @param {*} s2
|
|
79
86
|
*/
|
|
80
|
-
async getScentenseSimilarity(s1: string, s2: string, axiosOption:any = { timeout: 30000 }):Promise<
|
|
87
|
+
async getScentenseSimilarity(s1: string, s2: string, axiosOption: any = { timeout: 30000 }): Promise<SimilarityResult> {
|
|
81
88
|
if (!s1 || !s2) return { successed: false, error: { errcode: 2, errmsg: '缺失参数' } }
|
|
82
89
|
const messages = [
|
|
83
90
|
{ role: 'user', content: s1 },
|
|
84
91
|
{ role: 'user', content: s2 },
|
|
85
92
|
{ role: 'user', content: '请从语义上对比以上两句话的相似度,请仅输出0至100之间的整数对比结果即可' },
|
|
86
93
|
]
|
|
87
|
-
|
|
94
|
+
const result = await this.chatRequest(messages, {maxtoken:32}, axiosOption);
|
|
95
|
+
if (result.successed && result.message){
|
|
96
|
+
let value = result.message[0].message.content.replace(/[^\d]/g, "")
|
|
97
|
+
if (value > 100) value = Math.floor(value / 10);
|
|
98
|
+
return { successed: true, value:Number(value) };
|
|
99
|
+
}
|
|
100
|
+
return { successed: false, error:result.error,value:0 };
|
|
101
|
+
|
|
88
102
|
}
|
|
89
103
|
|
|
90
104
|
|
|
@@ -121,7 +135,7 @@ export class AIChat extends EventEmitter {
|
|
|
121
135
|
///没20句话分为一组,适应大文件内容多次请求组合结果
|
|
122
136
|
///每一句话需要产生的题目
|
|
123
137
|
let questions4EverySentense:number = count / arrContent.length; //Math.ceil(arrContent.length / 20);
|
|
124
|
-
let faqs:Array<
|
|
138
|
+
let faqs:Array<FaqItem> = [], gotted:number = 0;
|
|
125
139
|
while (arrContent.length > 0 && gotted < count) {
|
|
126
140
|
////每次最多送MESSAGE_LENGTH句话给openai
|
|
127
141
|
let subarray = arrContent.slice(0, MESSAGE_LENGTH);
|
|
@@ -154,7 +168,7 @@ export class AIChat extends EventEmitter {
|
|
|
154
168
|
* @param {*} messages
|
|
155
169
|
* @returns
|
|
156
170
|
*/
|
|
157
|
-
private pickUpFaqContent(messages: Array<any>): Array<
|
|
171
|
+
private pickUpFaqContent(messages: Array<any>): Array<FaqItem> {
|
|
158
172
|
let replys = messages.map(item => {
|
|
159
173
|
let content = item.message.content.trim().replace(/\t|\n|\v|\r|\f/g, '');
|
|
160
174
|
try {
|
|
@@ -249,7 +263,7 @@ export class AIChat extends EventEmitter {
|
|
|
249
263
|
* @param {*} result
|
|
250
264
|
*
|
|
251
265
|
*/
|
|
252
|
-
private pickUpQuestions(result: Array<any>, questiontype: string, score: number = 1): Array<
|
|
266
|
+
private pickUpQuestions(result: Array<any>, questiontype: string, score: number = 1): Array<QuestionItem> {
|
|
253
267
|
let item = result.map(m => {
|
|
254
268
|
////防止输出的JSON格式不合法
|
|
255
269
|
try {
|
|
@@ -302,10 +316,7 @@ export class AIChat extends EventEmitter {
|
|
|
302
316
|
}
|
|
303
317
|
}
|
|
304
318
|
|
|
305
|
-
|
|
306
|
-
* Api封装后的返回结果
|
|
307
|
-
*/
|
|
308
|
-
export interface ChatReponse {
|
|
319
|
+
interface ApiResult {
|
|
309
320
|
/**
|
|
310
321
|
* return the result of api called
|
|
311
322
|
* @type {boolean}
|
|
@@ -317,6 +328,12 @@ export interface ChatReponse {
|
|
|
317
328
|
* @memberof ChatReponse
|
|
318
329
|
*/
|
|
319
330
|
'error'?: any;
|
|
331
|
+
}
|
|
332
|
+
/**
|
|
333
|
+
* Api封装后的返回结果
|
|
334
|
+
*/
|
|
335
|
+
export interface ChatReponse extends ApiResult {
|
|
336
|
+
|
|
320
337
|
/**
|
|
321
338
|
* The name of the user in a multi-user chat
|
|
322
339
|
* @type {Array<any>}
|
|
@@ -337,7 +354,7 @@ export interface CallChatApiOption{
|
|
|
337
354
|
/**
|
|
338
355
|
* 调用OpenAI Api的参数约定
|
|
339
356
|
*/
|
|
340
|
-
export interface
|
|
357
|
+
export interface FaqItem {
|
|
341
358
|
'question': string, ///模型名称
|
|
342
359
|
'answer'?: string; ///返回的最大token
|
|
343
360
|
'keywords'?: Array<string>;
|
|
@@ -346,22 +363,29 @@ export interface FaqResultResponse {
|
|
|
346
363
|
/**
|
|
347
364
|
* 调用OpenAI Api的参数约定
|
|
348
365
|
*/
|
|
349
|
-
export interface ExaminationPaperResult {
|
|
350
|
-
/**
|
|
351
|
-
* return the result of api called
|
|
352
|
-
* @type {boolean}
|
|
353
|
-
*/
|
|
354
|
-
'successed': boolean,
|
|
366
|
+
export interface ExaminationPaperResult extends ApiResult {
|
|
355
367
|
'score': number, ///卷面总分
|
|
356
368
|
'paper': any
|
|
357
369
|
}
|
|
358
370
|
/**
|
|
359
371
|
* 调用OpenAI Api的参数约定
|
|
360
372
|
*/
|
|
361
|
-
export interface
|
|
373
|
+
export interface QuestionItem {
|
|
362
374
|
'question': string, ///模型名称
|
|
363
375
|
'answer'?: Array<string>; ///返回的最大token
|
|
364
376
|
'choice'?: Array<string>;
|
|
365
377
|
'score'?:number;
|
|
366
378
|
}
|
|
379
|
+
/**
|
|
380
|
+
* 调用OpenAI Api的参数约定
|
|
381
|
+
*/
|
|
382
|
+
export interface SimilarityResult extends ApiResult {
|
|
383
|
+
'value'?:number; ///相识度的值
|
|
384
|
+
}
|
|
367
385
|
|
|
386
|
+
/**
|
|
387
|
+
* 调用OpenAI Api的参数约定
|
|
388
|
+
*/
|
|
389
|
+
export interface EmotionResult extends ApiResult {
|
|
390
|
+
'emotion'?: string; ///情绪
|
|
391
|
+
}
|