doomiaichat 7.0.1 → 7.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/aimp.d.ts +2 -3
- package/dist/aimp.js +42 -14
- package/package.json +1 -1
- package/src/aimp.ts +39 -13
package/dist/aimp.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import GptBase from "./gptbase";
|
|
2
|
-
import { ChatReponse } from './declare';
|
|
3
2
|
export default class AIMiddlePlatform extends GptBase {
|
|
4
3
|
protected apikey: string;
|
|
5
4
|
protected agent: {
|
|
@@ -21,7 +20,7 @@ export default class AIMiddlePlatform extends GptBase {
|
|
|
21
20
|
* @param callChatOption
|
|
22
21
|
* @param axiosOption
|
|
23
22
|
*/
|
|
24
|
-
chatRequest(chatText: string, callChatOption: any, axiosOption?: any): Promise<
|
|
23
|
+
chatRequest(chatText: string | any, callChatOption: any, axiosOption?: any): Promise<any>;
|
|
25
24
|
/**
|
|
26
25
|
* 流式传输聊天请求
|
|
27
26
|
* @param chatText
|
|
@@ -30,5 +29,5 @@ export default class AIMiddlePlatform extends GptBase {
|
|
|
30
29
|
* @param axiosOption
|
|
31
30
|
* @returns
|
|
32
31
|
*/
|
|
33
|
-
chatRequestInStream(chatText: string |
|
|
32
|
+
chatRequestInStream(chatText: string | any, callChatOption: any, attach?: any, axiosOption?: any): Promise<any>;
|
|
34
33
|
}
|
package/dist/aimp.js
CHANGED
|
@@ -26,6 +26,7 @@ const axios_1 = __importDefault(require("axios"));
|
|
|
26
26
|
const declare_1 = require("./declare");
|
|
27
27
|
const gptbase_1 = __importDefault(require("./gptbase"));
|
|
28
28
|
const stream_1 = require("stream");
|
|
29
|
+
// import { ChatReponse } from './declare';
|
|
29
30
|
class AIMiddlePlatform extends gptbase_1.default {
|
|
30
31
|
/**
|
|
31
32
|
*
|
|
@@ -44,22 +45,30 @@ class AIMiddlePlatform extends gptbase_1.default {
|
|
|
44
45
|
* @param axiosOption
|
|
45
46
|
*/
|
|
46
47
|
chatRequest(chatText, callChatOption, axiosOption = {}) {
|
|
48
|
+
var _a;
|
|
47
49
|
return __awaiter(this, void 0, void 0, function* () {
|
|
48
|
-
if (!chatText)
|
|
50
|
+
if (!chatText) {
|
|
49
51
|
this.emit('chaterror', { successed: false, error: 'no text in chat' });
|
|
52
|
+
return { successed: false, error: 'no text in chat' };
|
|
53
|
+
}
|
|
54
|
+
const question = typeof chatText === 'object' ? (_a = chatText.text) !== null && _a !== void 0 ? _a : chatText.content : chatText;
|
|
50
55
|
axiosOption = Object.assign({}, axiosOption || { timeout: 60000 });
|
|
51
56
|
const opts = Object.assign({ headers: {
|
|
52
57
|
'Content-Type': 'application/json',
|
|
53
58
|
'authorization': `Bearer ${this.apikey}`
|
|
54
59
|
}, method: 'post', url: `${this.agent.endpoint}/api/v1/agents/${this.agent.agentid}/completions`, data: {
|
|
55
|
-
question
|
|
60
|
+
question,
|
|
56
61
|
session_id: callChatOption.session_id,
|
|
57
62
|
stream: false
|
|
58
63
|
} }, axiosOption);
|
|
64
|
+
console.log('Question===>', question);
|
|
59
65
|
const response = yield (0, declare_1.request)(opts);
|
|
60
66
|
if (!response.successed)
|
|
61
67
|
return { successed: false, error: 'failed' };
|
|
62
|
-
|
|
68
|
+
console.log('reponse===>', response.data);
|
|
69
|
+
const { answer: message, session_id } = response.data.data;
|
|
70
|
+
this.emit('chatdone', { successed: true, segment: message, text: message, finish_reason: 'stop', index: 0, session_id });
|
|
71
|
+
return { successed: true, message, session_id };
|
|
63
72
|
});
|
|
64
73
|
}
|
|
65
74
|
/**
|
|
@@ -72,9 +81,12 @@ class AIMiddlePlatform extends gptbase_1.default {
|
|
|
72
81
|
*/
|
|
73
82
|
chatRequestInStream(chatText, callChatOption, attach, axiosOption) {
|
|
74
83
|
var _a, e_1, _b, _c;
|
|
84
|
+
var _d;
|
|
75
85
|
return __awaiter(this, void 0, void 0, function* () {
|
|
76
86
|
if (!chatText)
|
|
77
87
|
this.emit('chaterror', { successed: false, error: 'no text in chat' });
|
|
88
|
+
// console.log('Question===>', chatText)
|
|
89
|
+
const question = typeof chatText === 'object' ? (_d = chatText.text) !== null && _d !== void 0 ? _d : chatText.content : chatText;
|
|
78
90
|
axiosOption = Object.assign({}, axiosOption || { timeout: 60000 });
|
|
79
91
|
let requestid = Math.ceil(Math.random() * (new Date().getTime() * Math.random()) / 1000);
|
|
80
92
|
try {
|
|
@@ -82,50 +94,66 @@ class AIMiddlePlatform extends gptbase_1.default {
|
|
|
82
94
|
'Content-Type': 'application/json',
|
|
83
95
|
'authorization': `Bearer ${this.apikey}`
|
|
84
96
|
}, method: 'post', url: `${this.agent.endpoint}/api/v1/agents/${this.agent.agentid}/completions`, data: {
|
|
85
|
-
question
|
|
97
|
+
question,
|
|
86
98
|
session_id: callChatOption.session_id,
|
|
87
99
|
stream: true,
|
|
88
100
|
optional: callChatOption.optional,
|
|
89
101
|
}, responseType: 'stream' }, axiosOption);
|
|
90
|
-
// console.log('opts', opts)
|
|
91
102
|
const response = yield (0, axios_1.default)(opts);
|
|
92
103
|
const readableStream = stream_1.Readable.from(response.data);
|
|
93
|
-
let index = 0, session_id;
|
|
104
|
+
let index = 0, session_id, fullanswer = '', errorKeeped = [];
|
|
94
105
|
try {
|
|
95
|
-
for (var
|
|
106
|
+
for (var _e = true, readableStream_1 = __asyncValues(readableStream), readableStream_1_1; readableStream_1_1 = yield readableStream_1.next(), _a = readableStream_1_1.done, !_a;) {
|
|
96
107
|
_c = readableStream_1_1.value;
|
|
97
|
-
|
|
108
|
+
_e = false;
|
|
98
109
|
try {
|
|
99
110
|
const chunk = _c;
|
|
100
|
-
|
|
111
|
+
///可能接收到的数据不完整,导致JSON.parse失败
|
|
112
|
+
let answerData = null, jsonStr = '';
|
|
113
|
+
try {
|
|
114
|
+
jsonStr = chunk.toString().split('data:');
|
|
115
|
+
if (jsonStr.length)
|
|
116
|
+
jsonStr = jsonStr[jsonStr.length - 1] + '';
|
|
117
|
+
answerData = JSON.parse(errorKeeped.join('') + jsonStr);
|
|
118
|
+
}
|
|
119
|
+
catch (e) {
|
|
120
|
+
////如果发生了JSON解析错误,则当前的数据不完整,留着拼凑下一回数据
|
|
121
|
+
// console.log('json parse error===>', errorKeeped.join('') + jsonStr)
|
|
122
|
+
errorKeeped.push(jsonStr);
|
|
123
|
+
// console.log('After Push===>', errorKeeped.join('') )
|
|
124
|
+
continue;
|
|
125
|
+
}
|
|
126
|
+
errorKeeped = [];
|
|
101
127
|
const { answer, running_status } = answerData.data;
|
|
102
128
|
if (running_status === true)
|
|
103
129
|
continue;
|
|
130
|
+
const segment = answer ? answer.replace(fullanswer, '') : '';
|
|
131
|
+
fullanswer = answer !== null && answer !== void 0 ? answer : fullanswer;
|
|
104
132
|
if (!session_id)
|
|
105
133
|
session_id = answerData.data.session_id;
|
|
106
134
|
const finished = answerData.data === true;
|
|
107
|
-
let output = { successed: true, requestid, segment:
|
|
135
|
+
let output = { successed: true, requestid, segment: segment, text: fullanswer, finish_reason: finished ? 'stop' : null, index: index++, session_id };
|
|
108
136
|
if (attach)
|
|
109
137
|
output = Object.assign({}, output, attach);
|
|
110
138
|
this.emit(finished ? 'chatdone' : 'chattext', output);
|
|
111
139
|
}
|
|
112
140
|
finally {
|
|
113
|
-
|
|
141
|
+
_e = true;
|
|
114
142
|
}
|
|
115
143
|
}
|
|
116
144
|
}
|
|
117
145
|
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
118
146
|
finally {
|
|
119
147
|
try {
|
|
120
|
-
if (!
|
|
148
|
+
if (!_e && !_a && (_b = readableStream_1.return)) yield _b.call(readableStream_1);
|
|
121
149
|
}
|
|
122
150
|
finally { if (e_1) throw e_1.error; }
|
|
123
151
|
}
|
|
124
152
|
return { successed: true, requestid };
|
|
125
153
|
}
|
|
126
154
|
catch (error) {
|
|
127
|
-
this.emit('requesterror', { successed: false, requestid, error: 'call axios faied ' + error });
|
|
128
|
-
return { successed: false, requestid }
|
|
155
|
+
// this.emit('requesterror', { successed: false, requestid, error: 'call axios faied ' + error });
|
|
156
|
+
// return { successed: false, requestid }
|
|
129
157
|
}
|
|
130
158
|
});
|
|
131
159
|
}
|
package/package.json
CHANGED
package/src/aimp.ts
CHANGED
|
@@ -5,7 +5,7 @@ import axios from 'axios';
|
|
|
5
5
|
import { request } from "./declare";
|
|
6
6
|
import GptBase from "./gptbase"
|
|
7
7
|
import { Readable } from 'stream';
|
|
8
|
-
import { ChatReponse } from './declare';
|
|
8
|
+
// import { ChatReponse } from './declare';
|
|
9
9
|
export default class AIMiddlePlatform extends GptBase {
|
|
10
10
|
|
|
11
11
|
protected apikey: string;
|
|
@@ -27,8 +27,12 @@ export default class AIMiddlePlatform extends GptBase {
|
|
|
27
27
|
* @param callChatOption
|
|
28
28
|
* @param axiosOption
|
|
29
29
|
*/
|
|
30
|
-
public async chatRequest(chatText: string, callChatOption: any, axiosOption: any = {}): Promise<
|
|
31
|
-
if (!chatText)
|
|
30
|
+
public async chatRequest(chatText: string | any, callChatOption: any, axiosOption: any = {}): Promise<any> {
|
|
31
|
+
if (!chatText) {
|
|
32
|
+
this.emit('chaterror', { successed: false, error: 'no text in chat' });
|
|
33
|
+
return { successed: false, error: 'no text in chat' };
|
|
34
|
+
}
|
|
35
|
+
const question = typeof chatText === 'object' ? chatText.text ?? chatText.content : chatText;
|
|
32
36
|
axiosOption = Object.assign({}, axiosOption || { timeout: 60000 })
|
|
33
37
|
const opts: any = {
|
|
34
38
|
headers: {
|
|
@@ -38,15 +42,20 @@ export default class AIMiddlePlatform extends GptBase {
|
|
|
38
42
|
method: 'post',
|
|
39
43
|
url: `${this.agent.endpoint}/api/v1/agents/${this.agent.agentid}/completions`,
|
|
40
44
|
data: {
|
|
41
|
-
question
|
|
45
|
+
question,
|
|
42
46
|
session_id: callChatOption.session_id,
|
|
43
47
|
stream: false
|
|
44
48
|
},
|
|
45
49
|
...axiosOption
|
|
46
50
|
}
|
|
51
|
+
console.log('Question===>', question)
|
|
47
52
|
const response = await request(opts);
|
|
53
|
+
|
|
48
54
|
if (!response.successed) return { successed: false, error: 'failed' };
|
|
49
|
-
|
|
55
|
+
console.log('reponse===>', response.data)
|
|
56
|
+
const {answer:message,session_id} = response.data.data;
|
|
57
|
+
this.emit('chatdone', { successed: true, segment: message, text: message, finish_reason: 'stop', index: 0, session_id })
|
|
58
|
+
return { successed: true, message,session_id };
|
|
50
59
|
}
|
|
51
60
|
/**
|
|
52
61
|
* 流式传输聊天请求
|
|
@@ -56,8 +65,10 @@ export default class AIMiddlePlatform extends GptBase {
|
|
|
56
65
|
* @param axiosOption
|
|
57
66
|
* @returns
|
|
58
67
|
*/
|
|
59
|
-
override async chatRequestInStream(chatText: string |
|
|
68
|
+
override async chatRequestInStream(chatText: string | any, callChatOption: any, attach?: any, axiosOption?: any): Promise<any> {
|
|
60
69
|
if (!chatText) this.emit('chaterror', { successed: false, error: 'no text in chat' });
|
|
70
|
+
// console.log('Question===>', chatText)
|
|
71
|
+
const question = typeof chatText === 'object' ? chatText.text??chatText.content : chatText;
|
|
61
72
|
axiosOption = Object.assign({}, axiosOption || { timeout: 60000 })
|
|
62
73
|
let requestid = Math.ceil(Math.random() * (new Date().getTime() * Math.random()) / 1000);
|
|
63
74
|
try {
|
|
@@ -69,7 +80,7 @@ export default class AIMiddlePlatform extends GptBase {
|
|
|
69
80
|
method: 'post',
|
|
70
81
|
url: `${this.agent.endpoint}/api/v1/agents/${this.agent.agentid}/completions`,
|
|
71
82
|
data: {
|
|
72
|
-
question
|
|
83
|
+
question,
|
|
73
84
|
session_id: callChatOption.session_id,
|
|
74
85
|
stream: true,
|
|
75
86
|
optional: callChatOption.optional,
|
|
@@ -77,24 +88,39 @@ export default class AIMiddlePlatform extends GptBase {
|
|
|
77
88
|
responseType: 'stream',
|
|
78
89
|
...axiosOption
|
|
79
90
|
}
|
|
80
|
-
// console.log('opts', opts)
|
|
81
91
|
const response = await axios(opts);
|
|
82
92
|
const readableStream = Readable.from(response.data);
|
|
83
|
-
let index = 0, session_id;
|
|
93
|
+
let index = 0, session_id,fullanswer='',errorKeeped=[];
|
|
84
94
|
for await (const chunk of readableStream) {
|
|
85
|
-
|
|
95
|
+
///可能接收到的数据不完整,导致JSON.parse失败
|
|
96
|
+
let answerData = null, jsonStr = '';
|
|
97
|
+
try{
|
|
98
|
+
jsonStr = chunk.toString().split('data:')
|
|
99
|
+
if (jsonStr.length) jsonStr = jsonStr[jsonStr.length-1]+''
|
|
100
|
+
answerData = JSON.parse(errorKeeped.join('') + jsonStr);
|
|
101
|
+
}catch(e){
|
|
102
|
+
////如果发生了JSON解析错误,则当前的数据不完整,留着拼凑下一回数据
|
|
103
|
+
// console.log('json parse error===>', errorKeeped.join('') + jsonStr)
|
|
104
|
+
errorKeeped.push(jsonStr)
|
|
105
|
+
// console.log('After Push===>', errorKeeped.join('') )
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
errorKeeped = [];
|
|
86
109
|
const { answer, running_status } = answerData.data;
|
|
87
110
|
if (running_status === true) continue;
|
|
111
|
+
const segment = answer ? answer.replace(fullanswer,''):''
|
|
112
|
+
fullanswer = answer ?? fullanswer;
|
|
88
113
|
if (!session_id) session_id = answerData.data.session_id;
|
|
89
114
|
const finished = answerData.data === true;
|
|
90
|
-
let output = { successed: true, requestid, segment:
|
|
115
|
+
let output = { successed: true, requestid, segment: segment, text: fullanswer, finish_reason: finished ? 'stop' : null, index: index++, session_id };
|
|
91
116
|
if (attach) output = Object.assign({}, output, attach);
|
|
92
117
|
this.emit(finished ? 'chatdone' : 'chattext', output)
|
|
93
118
|
}
|
|
94
119
|
return { successed: true, requestid }
|
|
95
120
|
} catch (error) {
|
|
96
|
-
|
|
97
|
-
|
|
121
|
+
|
|
122
|
+
// this.emit('requesterror', { successed: false, requestid, error: 'call axios faied ' + error });
|
|
123
|
+
// return { successed: false, requestid }
|
|
98
124
|
}
|
|
99
125
|
}
|
|
100
126
|
}
|