doomiaichat 1.0.0 → 1.0.1
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/package.json +1 -1
- package/src/index.ts +37 -11
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { Configuration, OpenAIApi, CreateChatCompletionResponse } from "openai"
|
|
1
|
+
import { Configuration, OpenAIApi, CreateChatCompletionResponse, ChatCompletionRequestMessage } from "openai"
|
|
2
2
|
import { EventEmitter } from "events";
|
|
3
3
|
import { AxiosRequestConfig, AxiosResponse } from "axios";
|
|
4
|
-
export
|
|
4
|
+
export class AIChat extends EventEmitter {
|
|
5
5
|
private readonly chatRobot: OpenAIApi;
|
|
6
6
|
private readonly chatModel: string;
|
|
7
7
|
private readonly maxtoken: number;
|
|
@@ -23,22 +23,48 @@ export default class AIChat extends EventEmitter {
|
|
|
23
23
|
* 向OpenAI发送一个聊天请求
|
|
24
24
|
* @param {*} chatText
|
|
25
25
|
*/
|
|
26
|
-
public async chatRequest(chatText: string | any
|
|
27
|
-
if (!chatText) return {
|
|
28
|
-
|
|
29
|
-
|
|
26
|
+
public async chatRequest(chatText: string | Array<any>, axiosOption: AxiosRequestConfig): Promise<ChatReponse> {
|
|
27
|
+
if (!chatText) return {successed:false, error:{ errcode: 2, errmsg: '缺失聊天的内容' }};
|
|
28
|
+
if(!this.chatRobot) return {successed: false, error: { errcode: 1, errmsg: '聊天机器人无效' }};
|
|
29
|
+
|
|
30
|
+
let message: Array<ChatCompletionRequestMessage> = typeof(chatText)=='string'?
|
|
31
|
+
[{ role: 'system', content: chatText + '' }] : chatText;
|
|
32
|
+
try {
|
|
30
33
|
const response: AxiosResponse<CreateChatCompletionResponse, any>
|
|
31
34
|
= await this.chatRobot.createChatCompletion({
|
|
32
35
|
model: this.chatModel,
|
|
33
|
-
messages:
|
|
36
|
+
messages: message,
|
|
34
37
|
temperature: this.temperature,
|
|
35
38
|
max_tokens: this.maxtoken
|
|
36
39
|
// n: this.replyCounts
|
|
37
40
|
}, axiosOption);
|
|
38
|
-
return {
|
|
39
|
-
}catch(error){
|
|
40
|
-
return { successed: false,error }
|
|
41
|
+
return {successed:true,message: response.data.choices};
|
|
42
|
+
} catch (error) {
|
|
43
|
+
return { successed: false, error };
|
|
41
44
|
}
|
|
42
|
-
|
|
45
|
+
|
|
43
46
|
}
|
|
44
47
|
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Api封装后的返回结果
|
|
51
|
+
*/
|
|
52
|
+
export interface ChatReponse {
|
|
53
|
+
/**
|
|
54
|
+
* return the result of api called
|
|
55
|
+
* @type {boolean}
|
|
56
|
+
*/
|
|
57
|
+
'successed': boolean
|
|
58
|
+
/**
|
|
59
|
+
* The error info
|
|
60
|
+
* @type {any}
|
|
61
|
+
* @memberof ChatReponse
|
|
62
|
+
*/
|
|
63
|
+
'error'?: any;
|
|
64
|
+
/**
|
|
65
|
+
* The name of the user in a multi-user chat
|
|
66
|
+
* @type {Array<any>}
|
|
67
|
+
* @memberof ChatReponse
|
|
68
|
+
*/
|
|
69
|
+
'message'?: Array<any>;
|
|
70
|
+
}
|