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