doomiaichat 7.1.8 → 7.1.10
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/corzbot.d.ts +25 -11
- package/dist/corzbot.js +85 -54
- package/dist/gptbase.d.ts +10 -0
- package/dist/gptbase.js +16 -0
- package/package.json +2 -2
- package/src/corzbot.ts +89 -55
- package/src/gptbase.ts +13 -0
package/dist/corzbot.d.ts
CHANGED
|
@@ -4,10 +4,12 @@
|
|
|
4
4
|
import { CozeAPI, EnterMessage } from '@coze/api';
|
|
5
5
|
import GptBase from "./gptbase";
|
|
6
6
|
import { CorzAuthorization } from './corzauthorization';
|
|
7
|
+
import { ApiResult } from './declare';
|
|
7
8
|
export default class CorzBot extends GptBase {
|
|
8
9
|
private authorizationProvider;
|
|
9
10
|
private setting;
|
|
10
11
|
protected botid: string;
|
|
12
|
+
protected workflowid: string;
|
|
11
13
|
private apiKey;
|
|
12
14
|
private lastThinkingMessage;
|
|
13
15
|
/**
|
|
@@ -21,20 +23,32 @@ export default class CorzBot extends GptBase {
|
|
|
21
23
|
* 发起一次会话
|
|
22
24
|
*/
|
|
23
25
|
createCoversation(client?: CozeAPI): Promise<string | null>;
|
|
26
|
+
setVariables(params: any): Promise<ApiResult>;
|
|
24
27
|
/**
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
28
|
+
* 获取设置的变量
|
|
29
|
+
* @returns
|
|
30
|
+
*/
|
|
31
|
+
getVariables(params: any): Promise<any>;
|
|
32
|
+
/**
|
|
33
|
+
* 组装请求参数
|
|
34
|
+
* @param message
|
|
35
|
+
* @param callChatOption
|
|
36
|
+
* @param attach
|
|
37
|
+
* @param _axiosOption
|
|
38
|
+
*/
|
|
31
39
|
getRequestStream<T>(client: CozeAPI, message: EnterMessage[], callChatOption?: any): Promise<T>;
|
|
32
40
|
/**
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
41
|
+
* 同步调用的问答请求需要获取会话详情
|
|
42
|
+
* @param chatid
|
|
43
|
+
* @param conversation_id
|
|
44
|
+
*/
|
|
45
|
+
private getChatDetail;
|
|
46
|
+
/**
|
|
47
|
+
* 非流式传输聊天请求
|
|
48
|
+
* @param chatText
|
|
49
|
+
* @param callChatOption
|
|
50
|
+
* @param _axiosOption
|
|
51
|
+
*/
|
|
38
52
|
chatRequest(message: any[] | string, callChatOption?: any, _axiosOption?: any): Promise<any>;
|
|
39
53
|
/**
|
|
40
54
|
* 解析深度思考的JSON内容
|
package/dist/corzbot.js
CHANGED
|
@@ -54,6 +54,7 @@ class CorzBot extends gptbase_1.default {
|
|
|
54
54
|
this.lastThinkingMessage = { action: '', textposition: 0 };
|
|
55
55
|
////初始化扣子客户端
|
|
56
56
|
this.botid = this.setting['botid'] || this.setting['botID'];
|
|
57
|
+
this.workflowid = this.setting['workflowid'];
|
|
57
58
|
this.apiKey = this.setting['apiKey'];
|
|
58
59
|
}
|
|
59
60
|
createClient() {
|
|
@@ -74,7 +75,8 @@ class CorzBot extends gptbase_1.default {
|
|
|
74
75
|
return __awaiter(this, void 0, void 0, function* () {
|
|
75
76
|
try {
|
|
76
77
|
const czApi = client !== null && client !== void 0 ? client : yield this.createClient();
|
|
77
|
-
const
|
|
78
|
+
const params = this.botid ? { bot_id: this.botid } : {};
|
|
79
|
+
const result = yield czApi.conversations.create(params);
|
|
78
80
|
return result.id;
|
|
79
81
|
}
|
|
80
82
|
catch (error) {
|
|
@@ -83,19 +85,41 @@ class CorzBot extends gptbase_1.default {
|
|
|
83
85
|
}
|
|
84
86
|
});
|
|
85
87
|
}
|
|
88
|
+
setVariables(params) {
|
|
89
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
90
|
+
const client = yield this.createClient();
|
|
91
|
+
const cozeParams = Object.assign({}, this.botid ? { bot_id: this.botid } : {}, params);
|
|
92
|
+
client.variables.update(cozeParams);
|
|
93
|
+
return { successed: true };
|
|
94
|
+
});
|
|
95
|
+
}
|
|
86
96
|
/**
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
97
|
+
* 获取设置的变量
|
|
98
|
+
* @returns
|
|
99
|
+
*/
|
|
100
|
+
getVariables(params) {
|
|
101
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
102
|
+
const client = yield this.createClient();
|
|
103
|
+
const cozeParams = Object.assign({}, { bot_id: this.botid }, params);
|
|
104
|
+
const data = yield client.variables.retrieve(cozeParams);
|
|
105
|
+
if (data)
|
|
106
|
+
return Object.assign({ successed: true }, data);
|
|
107
|
+
return { successed: false, error: 'get variables failed' };
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* 组装请求参数
|
|
112
|
+
* @param message
|
|
113
|
+
* @param callChatOption
|
|
114
|
+
* @param attach
|
|
115
|
+
* @param _axiosOption
|
|
116
|
+
*/
|
|
93
117
|
getRequestStream(client, message, callChatOption = {}) {
|
|
94
|
-
var _a;
|
|
118
|
+
var _a, _b;
|
|
95
119
|
return __awaiter(this, void 0, void 0, function* () {
|
|
96
120
|
//简单的对话请求
|
|
97
121
|
const conversation_id = (_a = callChatOption.session_id) !== null && _a !== void 0 ? _a : yield this.createCoversation(client);
|
|
98
|
-
if (!
|
|
122
|
+
if (!this.workflowid) {
|
|
99
123
|
const req = {
|
|
100
124
|
bot_id: this.botid,
|
|
101
125
|
additional_messages: message,
|
|
@@ -108,19 +132,41 @@ class CorzBot extends gptbase_1.default {
|
|
|
108
132
|
const worflowreq = {
|
|
109
133
|
bot_id: this.botid,
|
|
110
134
|
additional_messages: message,
|
|
111
|
-
|
|
135
|
+
ext: callChatOption.ext,
|
|
136
|
+
workflow_id: this.workflowid,
|
|
112
137
|
conversation_id,
|
|
113
|
-
|
|
138
|
+
is_async: false,
|
|
139
|
+
parameters: Object.assign({ input: (_b = message[0]) === null || _b === void 0 ? void 0 : _b.content }, this.setting.customVariables || {}, callChatOption.customVariables || {}),
|
|
140
|
+
//parameters: { input: message[0]?.content }
|
|
114
141
|
};
|
|
115
142
|
return worflowreq;
|
|
116
143
|
});
|
|
117
144
|
}
|
|
118
145
|
/**
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
146
|
+
* 同步调用的问答请求需要获取会话详情
|
|
147
|
+
* @param chatid
|
|
148
|
+
* @param conversation_id
|
|
149
|
+
*/
|
|
150
|
+
getChatDetail(client, conversation_id, chatid) {
|
|
151
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
152
|
+
const maxWaitTime = 120000; // 最大等待120秒
|
|
153
|
+
const startTime = Date.now();
|
|
154
|
+
while ((Date.now() - startTime) < maxWaitTime) {
|
|
155
|
+
const chatinfo = yield client.chat.retrieve(conversation_id, chatid);
|
|
156
|
+
// 状态已经是完成了,可以去获取对话的内容了
|
|
157
|
+
if (chatinfo.status === api_1.ChatStatus.COMPLETED)
|
|
158
|
+
return { usage: chatinfo.usage, messages: yield client.chat.messages.list(conversation_id, chatid) };
|
|
159
|
+
yield new Promise(resolve => setTimeout(resolve, 1500)); // 等待1500ms
|
|
160
|
+
}
|
|
161
|
+
return null;
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* 非流式传输聊天请求
|
|
166
|
+
* @param chatText
|
|
167
|
+
* @param callChatOption
|
|
168
|
+
* @param _axiosOption
|
|
169
|
+
*/
|
|
124
170
|
chatRequest(message, callChatOption = {}, _axiosOption = {}) {
|
|
125
171
|
return __awaiter(this, void 0, void 0, function* () {
|
|
126
172
|
if (!message)
|
|
@@ -138,31 +184,29 @@ class CorzBot extends gptbase_1.default {
|
|
|
138
184
|
////如果参数中用的是Workflow,则调用对话流来输出结果
|
|
139
185
|
////否则用智能体的对话来输出结果
|
|
140
186
|
const params = yield this.getRequestStream(client, message, callChatOption);
|
|
141
|
-
const response = yield client.chat.
|
|
142
|
-
if (
|
|
143
|
-
const
|
|
144
|
-
|
|
145
|
-
type:
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
187
|
+
const response = !this.workflowid ? yield client.chat.create(params) : yield client.workflows.runs.create(params);
|
|
188
|
+
if (this.workflowid && response.msg === 'Success') {
|
|
189
|
+
const resp = response.data;
|
|
190
|
+
try {
|
|
191
|
+
return { successed: true, message: [{ role: 'assistant', type: 'answer', content: JSON.parse(resp).data }] };
|
|
192
|
+
}
|
|
193
|
+
catch (error) {
|
|
194
|
+
return { successed: true, message: [{ role: 'assistant', type: 'answer', content: resp }] };
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
if (!this.workflowid && response.conversation_id && response.id) {
|
|
198
|
+
const ccd = response;
|
|
199
|
+
const chatData = yield this.getChatDetail(client, ccd.conversation_id, ccd.id);
|
|
200
|
+
if (chatData) {
|
|
201
|
+
const message = chatData.messages.filter(x => x.type === 'answer').map(item => ({
|
|
202
|
+
role: item.role,
|
|
203
|
+
type: item.type,
|
|
204
|
+
content: item.content,
|
|
205
|
+
}));
|
|
206
|
+
return { successed: true, message, usage: chatData.usage, session_id: ccd.conversation_id };
|
|
207
|
+
}
|
|
154
208
|
}
|
|
155
|
-
return { successed: false, error: { message: '聊天未完成'
|
|
156
|
-
// for await (const part of stream) {
|
|
157
|
-
// if (part.event === ChatEventType.ERROR) return { successed: false, error: part.data.msg }
|
|
158
|
-
// ////在流式传输中,提取相关推荐问题
|
|
159
|
-
// if (part.event === ChatEventType.CONVERSATION_MESSAGE_COMPLETED) {
|
|
160
|
-
// const { type, content, conversation_id } = part.data;
|
|
161
|
-
// ///回答全部结束
|
|
162
|
-
// if (type === 'answer') return { successed: true, message: [{ message: { content } }], session_id: conversation_id };
|
|
163
|
-
// }
|
|
164
|
-
// }
|
|
165
|
-
//return { successed: false, error:'unknow' }
|
|
209
|
+
return { successed: false, error: { message: '聊天未完成' } };
|
|
166
210
|
});
|
|
167
211
|
}
|
|
168
212
|
/**
|
|
@@ -255,7 +299,7 @@ class CorzBot extends gptbase_1.default {
|
|
|
255
299
|
////否则用智能体的对话来输出结果
|
|
256
300
|
let requestid = Math.ceil(Math.random() * (new Date().getTime() * Math.random()) / 1000), index = 0;
|
|
257
301
|
const params = yield this.getRequestStream(client, message, callChatOption);
|
|
258
|
-
const stream = !
|
|
302
|
+
const stream = !this.workflowid ? client.chat.stream(params) : client.workflows.runs.stream(params);
|
|
259
303
|
let deltaindex = 0, fullanswer = [], followup = [], cardData = [];
|
|
260
304
|
let deepThinking = '', thinkingStatus = DeepThinkingStatus.None, cardResource = ''; // 是否在深度思考中
|
|
261
305
|
try {
|
|
@@ -300,19 +344,6 @@ class CorzBot extends gptbase_1.default {
|
|
|
300
344
|
this.emit('chatthinking', result.thinking);
|
|
301
345
|
}
|
|
302
346
|
}
|
|
303
|
-
// 如果不是卡片资源,则发送thinking事件,并更新thinkingStatus
|
|
304
|
-
// if(thinkingStatus !== DeepThinkingStatus.CardResource){
|
|
305
|
-
// deepThinking = result.content;
|
|
306
|
-
// this.emit('chatthinking', result.thinking);
|
|
307
|
-
// } else if (thinkingStatus === DeepThinkingStatus.CardResource) {
|
|
308
|
-
// cardResource += result.thinking?.text;
|
|
309
|
-
// // 卡片流结束,解析卡片资源数据
|
|
310
|
-
// if (result.thinking?.completed){
|
|
311
|
-
// const cardData:any[] = cardResource.replace(/[\x00-\x1F\x7F]/g, '').split('|').map((item:string)=>parse(item));
|
|
312
|
-
// // 将卡片资源返回给客户端
|
|
313
|
-
// this.emit('chatcard', cardData);
|
|
314
|
-
// }
|
|
315
|
-
// }
|
|
316
347
|
thinkingStatus = result.status;
|
|
317
348
|
}
|
|
318
349
|
if (thinkingStatus != DeepThinkingStatus.ThinkingOver)
|
package/dist/gptbase.d.ts
CHANGED
|
@@ -23,6 +23,16 @@ export default abstract class GptBase extends EventEmitter {
|
|
|
23
23
|
* @returns
|
|
24
24
|
*/
|
|
25
25
|
createCoversation(_client?: any): Promise<string | null>;
|
|
26
|
+
/**
|
|
27
|
+
* 设置智能体的变量
|
|
28
|
+
* @returns
|
|
29
|
+
*/
|
|
30
|
+
setVariables(_params: any): Promise<ApiResult>;
|
|
31
|
+
/**
|
|
32
|
+
* 获取智能体的变量
|
|
33
|
+
* @returns
|
|
34
|
+
*/
|
|
35
|
+
getVariables(_params: any): Promise<any>;
|
|
26
36
|
/**
|
|
27
37
|
* 流式的聊天模式
|
|
28
38
|
* @param chatText
|
package/dist/gptbase.js
CHANGED
|
@@ -31,6 +31,22 @@ class GptBase extends events_1.EventEmitter {
|
|
|
31
31
|
return null;
|
|
32
32
|
});
|
|
33
33
|
}
|
|
34
|
+
/**
|
|
35
|
+
* 设置智能体的变量
|
|
36
|
+
* @returns
|
|
37
|
+
*/
|
|
38
|
+
setVariables(_params) {
|
|
39
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
40
|
+
return { successed: true };
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* 获取智能体的变量
|
|
45
|
+
* @returns
|
|
46
|
+
*/
|
|
47
|
+
getVariables(_params) {
|
|
48
|
+
return __awaiter(this, void 0, void 0, function* () { return { successed: false }; });
|
|
49
|
+
}
|
|
34
50
|
/**
|
|
35
51
|
* 流式的聊天模式
|
|
36
52
|
* @param chatText
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "doomiaichat",
|
|
3
|
-
"version": "7.1.
|
|
3
|
+
"version": "7.1.10",
|
|
4
4
|
"description": "Doomisoft OpenAI",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
},
|
|
18
18
|
"dependencies": {
|
|
19
19
|
"@azure/openai": "^1.0.0-beta.11",
|
|
20
|
-
"@coze/api": "^1.
|
|
20
|
+
"@coze/api": "^1.3.5",
|
|
21
21
|
"axios": "^1.8.4",
|
|
22
22
|
"openai": "^4.29.0"
|
|
23
23
|
}
|
package/src/corzbot.ts
CHANGED
|
@@ -8,13 +8,21 @@ import {
|
|
|
8
8
|
RoleType,
|
|
9
9
|
StreamChatReq,
|
|
10
10
|
EnterMessage,
|
|
11
|
-
ChatWorkflowReq,
|
|
11
|
+
// ChatWorkflowReq,
|
|
12
|
+
// CreateChatReq,
|
|
13
|
+
// ChatStatus,
|
|
14
|
+
VariableUpdateReq,
|
|
15
|
+
VariableRetrieveReq,
|
|
12
16
|
CreateChatReq,
|
|
17
|
+
RunWorkflowReq,
|
|
18
|
+
RunWorkflowData,
|
|
19
|
+
CreateChatData,
|
|
13
20
|
ChatStatus
|
|
14
21
|
} from '@coze/api';
|
|
15
22
|
import GptBase from "./gptbase"
|
|
16
23
|
import { CorzAuthorization } from './corzauthorization';
|
|
17
24
|
import { parse } from 'querystring'
|
|
25
|
+
import { ApiResult } from './declare';
|
|
18
26
|
|
|
19
27
|
// 定义深度思考的动作
|
|
20
28
|
enum DeepThinkingAction {
|
|
@@ -34,6 +42,7 @@ type CardType = { type: number, tag: string }
|
|
|
34
42
|
type TThinkingMessage = { action: string, textposition: number }
|
|
35
43
|
export default class CorzBot extends GptBase {
|
|
36
44
|
protected botid: string;
|
|
45
|
+
protected workflowid: string;
|
|
37
46
|
private apiKey: string;
|
|
38
47
|
private lastThinkingMessage: TThinkingMessage = { action: '', textposition: 0 };
|
|
39
48
|
// protected client: CozeAPI;
|
|
@@ -46,6 +55,7 @@ export default class CorzBot extends GptBase {
|
|
|
46
55
|
super();
|
|
47
56
|
////初始化扣子客户端
|
|
48
57
|
this.botid = this.setting['botid'] || this.setting['botID'];
|
|
58
|
+
this.workflowid = this.setting['workflowid'];
|
|
49
59
|
this.apiKey = this.setting['apiKey'];
|
|
50
60
|
}
|
|
51
61
|
private async createClient(): Promise<CozeAPI> {
|
|
@@ -61,24 +71,43 @@ export default class CorzBot extends GptBase {
|
|
|
61
71
|
override async createCoversation(client?: CozeAPI): Promise<string | null> {
|
|
62
72
|
try {
|
|
63
73
|
const czApi = client ?? await this.createClient();
|
|
64
|
-
const
|
|
74
|
+
const params = this.botid ? { bot_id: this.botid } : {};
|
|
75
|
+
const result = await czApi.conversations.create(params)
|
|
65
76
|
return result.id;
|
|
66
77
|
} catch (error) {
|
|
67
78
|
console.error('createCoversation error in coze api');
|
|
68
79
|
return null;
|
|
69
80
|
}
|
|
70
81
|
}
|
|
82
|
+
|
|
83
|
+
override async setVariables(params: any): Promise<ApiResult> {
|
|
84
|
+
const client = await this.createClient();
|
|
85
|
+
const cozeParams: VariableUpdateReq = Object.assign({}, this.botid ? { bot_id: this.botid } : {}, params);
|
|
86
|
+
client.variables.update(cozeParams)
|
|
87
|
+
return { successed: true };
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* 获取设置的变量
|
|
91
|
+
* @returns
|
|
92
|
+
*/
|
|
93
|
+
override async getVariables(params: any): Promise<any> {
|
|
94
|
+
const client = await this.createClient();
|
|
95
|
+
const cozeParams: VariableRetrieveReq = Object.assign({}, { bot_id: this.botid }, params);
|
|
96
|
+
const data = await client.variables.retrieve(cozeParams);
|
|
97
|
+
if (data) return { successed: true, ...data }
|
|
98
|
+
return { successed: false, error: 'get variables failed' };
|
|
99
|
+
}
|
|
71
100
|
/**
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
101
|
+
* 组装请求参数
|
|
102
|
+
* @param message
|
|
103
|
+
* @param callChatOption
|
|
104
|
+
* @param attach
|
|
105
|
+
* @param _axiosOption
|
|
106
|
+
*/
|
|
78
107
|
async getRequestStream<T>(client: CozeAPI, message: EnterMessage[], callChatOption: any = {}): Promise<T> {
|
|
79
108
|
//简单的对话请求
|
|
80
109
|
const conversation_id = callChatOption.session_id ?? await this.createCoversation(client);
|
|
81
|
-
if (!
|
|
110
|
+
if (!this.workflowid) {
|
|
82
111
|
const req: StreamChatReq = {
|
|
83
112
|
bot_id: this.botid,
|
|
84
113
|
additional_messages: message,
|
|
@@ -88,21 +117,40 @@ export default class CorzBot extends GptBase {
|
|
|
88
117
|
req.custom_variables = Object.assign({}, this.setting.customVariables || {}, callChatOption.customVariables || {});
|
|
89
118
|
return req as T;
|
|
90
119
|
}
|
|
91
|
-
const worflowreq:
|
|
120
|
+
const worflowreq: any = {
|
|
92
121
|
bot_id: this.botid,
|
|
93
122
|
additional_messages: message,
|
|
94
|
-
|
|
123
|
+
ext: callChatOption.ext,
|
|
124
|
+
workflow_id: this.workflowid,//callChatOption.workflowid,
|
|
95
125
|
conversation_id,
|
|
96
|
-
|
|
126
|
+
is_async:false,
|
|
127
|
+
parameters: Object.assign({ input: message[0]?.content }, this.setting.customVariables || {}, callChatOption.customVariables || {}),
|
|
128
|
+
//parameters: { input: message[0]?.content }
|
|
97
129
|
}
|
|
98
130
|
return worflowreq as T;
|
|
99
131
|
}
|
|
100
132
|
/**
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
133
|
+
* 同步调用的问答请求需要获取会话详情
|
|
134
|
+
* @param chatid
|
|
135
|
+
* @param conversation_id
|
|
136
|
+
*/
|
|
137
|
+
private async getChatDetail(client: CozeAPI, conversation_id: string, chatid: string) {
|
|
138
|
+
const maxWaitTime = 120000; // 最大等待120秒
|
|
139
|
+
const startTime = Date.now();
|
|
140
|
+
while ((Date.now() - startTime) < maxWaitTime) {
|
|
141
|
+
const chatinfo = await client.chat.retrieve(conversation_id, chatid);
|
|
142
|
+
// 状态已经是完成了,可以去获取对话的内容了
|
|
143
|
+
if (chatinfo.status === ChatStatus.COMPLETED) return { usage: chatinfo.usage, messages: await client.chat.messages.list(conversation_id, chatid)};
|
|
144
|
+
await new Promise(resolve => setTimeout(resolve, 1500)); // 等待1500ms
|
|
145
|
+
}
|
|
146
|
+
return null;
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* 非流式传输聊天请求
|
|
150
|
+
* @param chatText
|
|
151
|
+
* @param callChatOption
|
|
152
|
+
* @param _axiosOption
|
|
153
|
+
*/
|
|
106
154
|
public async chatRequest(message: any[] | string, callChatOption: any = {}, _axiosOption: any = {}): Promise<any> {
|
|
107
155
|
if (!message) this.emit('chaterror', { successed: false, error: 'no message in chat' });
|
|
108
156
|
///如果是字符,则组装成API需要的消息
|
|
@@ -114,34 +162,33 @@ export default class CorzBot extends GptBase {
|
|
|
114
162
|
}
|
|
115
163
|
];
|
|
116
164
|
const client = await this.createClient();
|
|
165
|
+
|
|
117
166
|
////如果参数中用的是Workflow,则调用对话流来输出结果
|
|
118
167
|
////否则用智能体的对话来输出结果
|
|
119
168
|
const params = await this.getRequestStream(client, message, callChatOption);
|
|
120
|
-
const response = await client.chat.
|
|
121
|
-
if (
|
|
122
|
-
const
|
|
123
|
-
|
|
124
|
-
type:
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
169
|
+
const response = !this.workflowid ? await client.chat.create(params as CreateChatReq) : await client.workflows.runs.create(params as RunWorkflowReq);
|
|
170
|
+
if (this.workflowid && (response as RunWorkflowData).msg === 'Success') {
|
|
171
|
+
const resp = (response as RunWorkflowData).data;
|
|
172
|
+
try {
|
|
173
|
+
return { successed: true, message: [{ role: 'assistant', type:'answer', content: JSON.parse(resp).data }]};
|
|
174
|
+
} catch (error) {
|
|
175
|
+
return { successed: true, message: [{ role: 'assistant', type: 'answer', content: resp }] };
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
if (!this.workflowid && (response as CreateChatData).conversation_id && (response as CreateChatData).id) {
|
|
179
|
+
const ccd = response as CreateChatData;
|
|
180
|
+
const chatData = await this.getChatDetail(client, ccd.conversation_id, ccd.id);
|
|
181
|
+
if (chatData){
|
|
182
|
+
const message = chatData.messages.filter(x => x.type === 'answer').map(item => ({
|
|
183
|
+
role: item.role,
|
|
184
|
+
type: item.type,
|
|
185
|
+
content: item.content,
|
|
186
|
+
}));
|
|
187
|
+
return { successed: true, message, usage: chatData.usage,session_id:ccd.conversation_id };
|
|
188
|
+
}
|
|
133
189
|
}
|
|
134
|
-
return { successed: false, error: { message: '聊天未完成'
|
|
135
|
-
|
|
136
|
-
// if (part.event === ChatEventType.ERROR) return { successed: false, error: part.data.msg }
|
|
137
|
-
// ////在流式传输中,提取相关推荐问题
|
|
138
|
-
// if (part.event === ChatEventType.CONVERSATION_MESSAGE_COMPLETED) {
|
|
139
|
-
// const { type, content, conversation_id } = part.data;
|
|
140
|
-
// ///回答全部结束
|
|
141
|
-
// if (type === 'answer') return { successed: true, message: [{ message: { content } }], session_id: conversation_id };
|
|
142
|
-
// }
|
|
143
|
-
// }
|
|
144
|
-
//return { successed: false, error:'unknow' }
|
|
190
|
+
return { successed: false, error: { message: '聊天未完成' } };
|
|
191
|
+
|
|
145
192
|
}
|
|
146
193
|
/**
|
|
147
194
|
* 解析深度思考的JSON内容
|
|
@@ -230,7 +277,7 @@ export default class CorzBot extends GptBase {
|
|
|
230
277
|
////否则用智能体的对话来输出结果
|
|
231
278
|
let requestid = Math.ceil(Math.random() * (new Date().getTime() * Math.random()) / 1000), index = 0;
|
|
232
279
|
const params = await this.getRequestStream(client, message, callChatOption);
|
|
233
|
-
const stream = !
|
|
280
|
+
const stream = !this.workflowid ? client.chat.stream(params as StreamChatReq) : client.workflows.runs.stream(params as RunWorkflowReq);
|
|
234
281
|
let deltaindex = 0, fullanswer: string[] = [], followup: string[] = [], cardData: CardType[] = [];
|
|
235
282
|
let deepThinking = '', thinkingStatus = DeepThinkingStatus.None, cardResource = ''; // 是否在深度思考中
|
|
236
283
|
for await (const part of stream) {
|
|
@@ -268,19 +315,6 @@ export default class CorzBot extends GptBase {
|
|
|
268
315
|
this.emit('chatthinking', result.thinking);
|
|
269
316
|
}
|
|
270
317
|
}
|
|
271
|
-
// 如果不是卡片资源,则发送thinking事件,并更新thinkingStatus
|
|
272
|
-
// if(thinkingStatus !== DeepThinkingStatus.CardResource){
|
|
273
|
-
// deepThinking = result.content;
|
|
274
|
-
// this.emit('chatthinking', result.thinking);
|
|
275
|
-
// } else if (thinkingStatus === DeepThinkingStatus.CardResource) {
|
|
276
|
-
// cardResource += result.thinking?.text;
|
|
277
|
-
// // 卡片流结束,解析卡片资源数据
|
|
278
|
-
// if (result.thinking?.completed){
|
|
279
|
-
// const cardData:any[] = cardResource.replace(/[\x00-\x1F\x7F]/g, '').split('|').map((item:string)=>parse(item));
|
|
280
|
-
// // 将卡片资源返回给客户端
|
|
281
|
-
// this.emit('chatcard', cardData);
|
|
282
|
-
// }
|
|
283
|
-
// }
|
|
284
318
|
thinkingStatus = result.status;
|
|
285
319
|
}
|
|
286
320
|
if (thinkingStatus != DeepThinkingStatus.ThinkingOver) continue;
|
package/src/gptbase.ts
CHANGED
|
@@ -28,6 +28,19 @@ export default abstract class GptBase extends EventEmitter {
|
|
|
28
28
|
async createCoversation(_client?:any):Promise<string|null> {
|
|
29
29
|
return null;
|
|
30
30
|
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* 设置智能体的变量
|
|
34
|
+
* @returns
|
|
35
|
+
*/
|
|
36
|
+
async setVariables(_params: any):Promise<ApiResult> {
|
|
37
|
+
return { successed: true };
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* 获取智能体的变量
|
|
41
|
+
* @returns
|
|
42
|
+
*/
|
|
43
|
+
async getVariables(_params: any): Promise<any> { return {successed: false}}
|
|
31
44
|
/**
|
|
32
45
|
* 流式的聊天模式
|
|
33
46
|
* @param chatText
|