doomiaichat 1.0.1 → 1.0.2
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 +46 -3
- package/dist/index.js +45 -9
- package/package.json +1 -1
- package/src/index.ts +46 -6
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import { EventEmitter } from "events";
|
|
3
3
|
import { AxiosRequestConfig } from "axios";
|
|
4
|
-
export
|
|
4
|
+
export declare class AIChat extends EventEmitter {
|
|
5
5
|
private readonly chatRobot;
|
|
6
6
|
private readonly chatModel;
|
|
7
7
|
private readonly maxtoken;
|
|
@@ -11,10 +11,53 @@ export default class AIChat extends EventEmitter {
|
|
|
11
11
|
* @param apiKey 调用OpenAI 的key
|
|
12
12
|
* @param apiOption
|
|
13
13
|
*/
|
|
14
|
-
constructor(apiKey: string, apiOption?:
|
|
14
|
+
constructor(apiKey: string, apiOption?: CallChatApiOption);
|
|
15
15
|
/**
|
|
16
16
|
* 向OpenAI发送一个聊天请求
|
|
17
17
|
* @param {*} chatText
|
|
18
18
|
*/
|
|
19
|
-
chatRequest(chatText: string | any, axiosOption: AxiosRequestConfig): Promise<
|
|
19
|
+
chatRequest(chatText: string | Array<any>, callChatOption: CallChatApiOption, axiosOption: AxiosRequestConfig): Promise<ChatReponse>;
|
|
20
|
+
/**
|
|
21
|
+
* 判断一句话的表达情绪
|
|
22
|
+
* @param {*} s1
|
|
23
|
+
* @param {*} axiosOption
|
|
24
|
+
*/
|
|
25
|
+
getScentenceEmotional(s1: string, axiosOption?: any): Promise<ChatReponse>;
|
|
26
|
+
/**
|
|
27
|
+
* 获取两句话的相似度取值
|
|
28
|
+
* @param {*} s1
|
|
29
|
+
* @param {*} s2
|
|
30
|
+
*/
|
|
31
|
+
getScentenseSimilarity(s1: string, s2: string, axiosOption?: any): Promise<ChatReponse>;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Api封装后的返回结果
|
|
35
|
+
*/
|
|
36
|
+
export interface ChatReponse {
|
|
37
|
+
/**
|
|
38
|
+
* return the result of api called
|
|
39
|
+
* @type {boolean}
|
|
40
|
+
*/
|
|
41
|
+
'successed': boolean;
|
|
42
|
+
/**
|
|
43
|
+
* The error info
|
|
44
|
+
* @type {any}
|
|
45
|
+
* @memberof ChatReponse
|
|
46
|
+
*/
|
|
47
|
+
'error'?: any;
|
|
48
|
+
/**
|
|
49
|
+
* The name of the user in a multi-user chat
|
|
50
|
+
* @type {Array<any>}
|
|
51
|
+
* @memberof ChatReponse
|
|
52
|
+
*/
|
|
53
|
+
'message'?: Array<any>;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* 调用OpenAI Api的参数约定
|
|
57
|
+
*/
|
|
58
|
+
export interface CallChatApiOption {
|
|
59
|
+
model?: string;
|
|
60
|
+
maxtoken?: number;
|
|
61
|
+
temperature?: number;
|
|
62
|
+
replyCounts?: number;
|
|
20
63
|
}
|
package/dist/index.js
CHANGED
|
@@ -9,7 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
};
|
|
10
10
|
import { Configuration, OpenAIApi } from "openai";
|
|
11
11
|
import { EventEmitter } from "events";
|
|
12
|
-
export
|
|
12
|
+
export class AIChat extends EventEmitter {
|
|
13
13
|
/**
|
|
14
14
|
*
|
|
15
15
|
* @param apiKey 调用OpenAI 的key
|
|
@@ -26,19 +26,21 @@ export default class AIChat extends EventEmitter {
|
|
|
26
26
|
* 向OpenAI发送一个聊天请求
|
|
27
27
|
* @param {*} chatText
|
|
28
28
|
*/
|
|
29
|
-
chatRequest(chatText, axiosOption) {
|
|
29
|
+
chatRequest(chatText, callChatOption, axiosOption) {
|
|
30
30
|
return __awaiter(this, void 0, void 0, function* () {
|
|
31
31
|
if (!chatText)
|
|
32
|
-
return { successed: false, errcode: 2, errmsg: '缺失聊天的内容' };
|
|
32
|
+
return { successed: false, error: { errcode: 2, errmsg: '缺失聊天的内容' } };
|
|
33
33
|
if (!this.chatRobot)
|
|
34
|
-
return { successed: false, errcode: 1, errmsg: '聊天机器人无效' };
|
|
34
|
+
return { successed: false, error: { errcode: 1, errmsg: '聊天机器人无效' } };
|
|
35
|
+
let message = typeof (chatText) == 'string' ?
|
|
36
|
+
[{ role: 'system', content: chatText + '' }] : chatText;
|
|
35
37
|
try {
|
|
36
38
|
const response = yield this.chatRobot.createChatCompletion({
|
|
37
|
-
model: this.chatModel,
|
|
38
|
-
messages:
|
|
39
|
-
temperature: this.temperature,
|
|
40
|
-
max_tokens: this.maxtoken
|
|
41
|
-
|
|
39
|
+
model: callChatOption.model || this.chatModel,
|
|
40
|
+
messages: message,
|
|
41
|
+
temperature: (callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.temperature) || this.temperature,
|
|
42
|
+
max_tokens: (callChatOption === null || callChatOption === void 0 ? void 0 : callChatOption.maxtoken) || this.maxtoken,
|
|
43
|
+
n: callChatOption.replyCounts || 1
|
|
42
44
|
}, axiosOption);
|
|
43
45
|
return { successed: true, message: response.data.choices };
|
|
44
46
|
}
|
|
@@ -47,4 +49,38 @@ export default class AIChat extends EventEmitter {
|
|
|
47
49
|
}
|
|
48
50
|
});
|
|
49
51
|
}
|
|
52
|
+
/**
|
|
53
|
+
* 判断一句话的表达情绪
|
|
54
|
+
* @param {*} s1
|
|
55
|
+
* @param {*} axiosOption
|
|
56
|
+
*/
|
|
57
|
+
getScentenceEmotional(s1, axiosOption = { timeout: 30000 }) {
|
|
58
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
59
|
+
if (!s1)
|
|
60
|
+
return { successed: false, error: { errcode: 2, errmsg: '缺失参数' } };
|
|
61
|
+
const emotion = ['愤怒', '威胁', '讽刺', '愧疚', '兴奋', '友好', '消极', '生气', '正常'];
|
|
62
|
+
const messages = [
|
|
63
|
+
{ role: 'user', content: s1 },
|
|
64
|
+
{ role: 'user', content: `请分析上述内容的语言情绪,请从"${emotion.join(',')}"这些情绪中对应一个输出` },
|
|
65
|
+
];
|
|
66
|
+
return this.chatRequest(messages, {}, axiosOption);
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* 获取两句话的相似度取值
|
|
71
|
+
* @param {*} s1
|
|
72
|
+
* @param {*} s2
|
|
73
|
+
*/
|
|
74
|
+
getScentenseSimilarity(s1, s2, axiosOption = { timeout: 30000 }) {
|
|
75
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
76
|
+
if (!s1 || !s2)
|
|
77
|
+
return { successed: false, error: { errcode: 2, errmsg: '缺失参数' } };
|
|
78
|
+
const messages = [
|
|
79
|
+
{ role: 'user', content: s1 },
|
|
80
|
+
{ role: 'user', content: s2 },
|
|
81
|
+
{ role: 'user', content: '请从语义上对比以上两句话的相似度,请仅输出0至100之间的整数对比结果即可' },
|
|
82
|
+
];
|
|
83
|
+
return this.chatRequest(messages, { maxtoken: 32 }, axiosOption);
|
|
84
|
+
});
|
|
85
|
+
}
|
|
50
86
|
}
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -11,7 +11,7 @@ export class AIChat extends EventEmitter {
|
|
|
11
11
|
* @param apiKey 调用OpenAI 的key
|
|
12
12
|
* @param apiOption
|
|
13
13
|
*/
|
|
14
|
-
constructor(apiKey: string, apiOption:
|
|
14
|
+
constructor(apiKey: string, apiOption: CallChatApiOption = {}) {
|
|
15
15
|
super();
|
|
16
16
|
this.chatRobot = new OpenAIApi(new Configuration({ apiKey }))
|
|
17
17
|
this.chatModel = apiOption.model || 'gpt-3.5-turbo';
|
|
@@ -23,7 +23,7 @@ export class AIChat extends EventEmitter {
|
|
|
23
23
|
* 向OpenAI发送一个聊天请求
|
|
24
24
|
* @param {*} chatText
|
|
25
25
|
*/
|
|
26
|
-
public async chatRequest(chatText: string | Array<any>, axiosOption: AxiosRequestConfig): Promise<ChatReponse> {
|
|
26
|
+
public async chatRequest(chatText: string | Array<any>, callChatOption: CallChatApiOption, axiosOption: AxiosRequestConfig): Promise<ChatReponse> {
|
|
27
27
|
if (!chatText) return {successed:false, error:{ errcode: 2, errmsg: '缺失聊天的内容' }};
|
|
28
28
|
if(!this.chatRobot) return {successed: false, error: { errcode: 1, errmsg: '聊天机器人无效' }};
|
|
29
29
|
|
|
@@ -32,11 +32,11 @@ export class AIChat extends EventEmitter {
|
|
|
32
32
|
try {
|
|
33
33
|
const response: AxiosResponse<CreateChatCompletionResponse, any>
|
|
34
34
|
= await this.chatRobot.createChatCompletion({
|
|
35
|
-
model: this.chatModel,
|
|
35
|
+
model: callChatOption.model || this.chatModel,
|
|
36
36
|
messages: message,
|
|
37
|
-
temperature: this.temperature,
|
|
38
|
-
max_tokens: this.maxtoken
|
|
39
|
-
|
|
37
|
+
temperature: callChatOption?.temperature || this.temperature,
|
|
38
|
+
max_tokens: callChatOption?.maxtoken || this.maxtoken,
|
|
39
|
+
n: callChatOption.replyCounts || 1
|
|
40
40
|
}, axiosOption);
|
|
41
41
|
return {successed:true,message: response.data.choices};
|
|
42
42
|
} catch (error) {
|
|
@@ -44,6 +44,37 @@ export class AIChat extends EventEmitter {
|
|
|
44
44
|
}
|
|
45
45
|
|
|
46
46
|
}
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* 判断一句话的表达情绪
|
|
51
|
+
* @param {*} s1
|
|
52
|
+
* @param {*} axiosOption
|
|
53
|
+
*/
|
|
54
|
+
async getScentenceEmotional(s1:string, axiosOption: any = { timeout: 30000 }):Promise<ChatReponse> {
|
|
55
|
+
if (!s1) return { successed: false, error:{errcode: 2, errmsg: '缺失参数'} }
|
|
56
|
+
const emotion = ['愤怒', '威胁', '讽刺', '愧疚', '兴奋', '友好', '消极', '生气', '正常'];
|
|
57
|
+
const messages = [
|
|
58
|
+
{ role: 'user', content: s1 },
|
|
59
|
+
{ role: 'user', content: `请分析上述内容的语言情绪,请从"${emotion.join(',')}"这些情绪中对应一个输出` },
|
|
60
|
+
]
|
|
61
|
+
return this.chatRequest(messages,{}, axiosOption);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* 获取两句话的相似度取值
|
|
66
|
+
* @param {*} s1
|
|
67
|
+
* @param {*} s2
|
|
68
|
+
*/
|
|
69
|
+
async getScentenseSimilarity(s1: string, s2: string, axiosOption:any = { timeout: 30000 }):Promise<ChatReponse> {
|
|
70
|
+
if (!s1 || !s2) return { successed: false, error: { errcode: 2, errmsg: '缺失参数' } }
|
|
71
|
+
const messages = [
|
|
72
|
+
{ role: 'user', content: s1 },
|
|
73
|
+
{ role: 'user', content: s2 },
|
|
74
|
+
{ role: 'user', content: '请从语义上对比以上两句话的相似度,请仅输出0至100之间的整数对比结果即可' },
|
|
75
|
+
]
|
|
76
|
+
return this.chatRequest(messages, {maxtoken:32}, axiosOption);
|
|
77
|
+
}
|
|
47
78
|
}
|
|
48
79
|
|
|
49
80
|
/**
|
|
@@ -67,4 +98,13 @@ export interface ChatReponse {
|
|
|
67
98
|
* @memberof ChatReponse
|
|
68
99
|
*/
|
|
69
100
|
'message'?: Array<any>;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* 调用OpenAI Api的参数约定
|
|
104
|
+
*/
|
|
105
|
+
export interface CallChatApiOption{
|
|
106
|
+
model?:string, ///模型名称
|
|
107
|
+
maxtoken?:number; ///返回的最大token
|
|
108
|
+
temperature?:number;
|
|
109
|
+
replyCounts?:number; ///返回多少答案
|
|
70
110
|
}
|