doomiaichat 7.1.8 → 7.1.9
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 +81 -52
- package/dist/gptbase.d.ts +10 -0
- package/dist/gptbase.js +16 -0
- package/package.json +2 -2
- package/src/corzbot.ts +84 -52
- 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
118
|
var _a;
|
|
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,7 +132,8 @@ 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
|
parameters: Object.assign({}, this.setting.customVariables || {}, callChatOption.customVariables || {}),
|
|
114
139
|
};
|
|
@@ -116,11 +141,30 @@ class CorzBot extends gptbase_1.default {
|
|
|
116
141
|
});
|
|
117
142
|
}
|
|
118
143
|
/**
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
144
|
+
* 同步调用的问答请求需要获取会话详情
|
|
145
|
+
* @param chatid
|
|
146
|
+
* @param conversation_id
|
|
147
|
+
*/
|
|
148
|
+
getChatDetail(client, conversation_id, chatid) {
|
|
149
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
150
|
+
const maxWaitTime = 120000; // 最大等待120秒
|
|
151
|
+
const startTime = Date.now();
|
|
152
|
+
while ((Date.now() - startTime) < maxWaitTime) {
|
|
153
|
+
const chatinfo = yield client.chat.retrieve(conversation_id, chatid);
|
|
154
|
+
// 状态已经是完成了,可以去获取对话的内容了
|
|
155
|
+
if (chatinfo.status === api_1.ChatStatus.COMPLETED)
|
|
156
|
+
return { usage: chatinfo.usage, messages: yield client.chat.messages.list(conversation_id, chatid) };
|
|
157
|
+
yield new Promise(resolve => setTimeout(resolve, 1500)); // 等待1500ms
|
|
158
|
+
}
|
|
159
|
+
return null;
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* 非流式传输聊天请求
|
|
164
|
+
* @param chatText
|
|
165
|
+
* @param callChatOption
|
|
166
|
+
* @param _axiosOption
|
|
167
|
+
*/
|
|
124
168
|
chatRequest(message, callChatOption = {}, _axiosOption = {}) {
|
|
125
169
|
return __awaiter(this, void 0, void 0, function* () {
|
|
126
170
|
if (!message)
|
|
@@ -138,31 +182,29 @@ class CorzBot extends gptbase_1.default {
|
|
|
138
182
|
////如果参数中用的是Workflow,则调用对话流来输出结果
|
|
139
183
|
////否则用智能体的对话来输出结果
|
|
140
184
|
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
|
-
|
|
185
|
+
const response = !this.workflowid ? yield client.chat.create(params) : yield client.workflows.runs.create(params);
|
|
186
|
+
if (this.workflowid && response.msg === 'Success') {
|
|
187
|
+
const resp = response.data;
|
|
188
|
+
try {
|
|
189
|
+
return { successed: true, message: [{ role: 'assistant', type: 'answer', content: JSON.parse(resp).data }] };
|
|
190
|
+
}
|
|
191
|
+
catch (error) {
|
|
192
|
+
return { successed: true, message: [{ role: 'assistant', type: 'answer', content: resp }] };
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
if (!this.workflowid && response.conversation_id && response.id) {
|
|
196
|
+
const ccd = response;
|
|
197
|
+
const chatData = yield this.getChatDetail(client, ccd.conversation_id, ccd.id);
|
|
198
|
+
if (chatData) {
|
|
199
|
+
const message = chatData.messages.filter(x => x.type === 'answer').map(item => ({
|
|
200
|
+
role: item.role,
|
|
201
|
+
type: item.type,
|
|
202
|
+
content: item.content,
|
|
203
|
+
}));
|
|
204
|
+
return { successed: true, message, usage: chatData.usage, session_id: ccd.conversation_id };
|
|
205
|
+
}
|
|
154
206
|
}
|
|
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' }
|
|
207
|
+
return { successed: false, error: { message: '聊天未完成' } };
|
|
166
208
|
});
|
|
167
209
|
}
|
|
168
210
|
/**
|
|
@@ -255,7 +297,7 @@ class CorzBot extends gptbase_1.default {
|
|
|
255
297
|
////否则用智能体的对话来输出结果
|
|
256
298
|
let requestid = Math.ceil(Math.random() * (new Date().getTime() * Math.random()) / 1000), index = 0;
|
|
257
299
|
const params = yield this.getRequestStream(client, message, callChatOption);
|
|
258
|
-
const stream = !
|
|
300
|
+
const stream = !this.workflowid ? client.chat.stream(params) : client.workflows.runs.stream(params);
|
|
259
301
|
let deltaindex = 0, fullanswer = [], followup = [], cardData = [];
|
|
260
302
|
let deepThinking = '', thinkingStatus = DeepThinkingStatus.None, cardResource = ''; // 是否在深度思考中
|
|
261
303
|
try {
|
|
@@ -300,19 +342,6 @@ class CorzBot extends gptbase_1.default {
|
|
|
300
342
|
this.emit('chatthinking', result.thinking);
|
|
301
343
|
}
|
|
302
344
|
}
|
|
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
345
|
thinkingStatus = result.status;
|
|
317
346
|
}
|
|
318
347
|
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.9",
|
|
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
|
@@ -9,12 +9,20 @@ import {
|
|
|
9
9
|
StreamChatReq,
|
|
10
10
|
EnterMessage,
|
|
11
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,
|
|
@@ -91,18 +120,35 @@ export default class CorzBot extends GptBase {
|
|
|
91
120
|
const worflowreq: ChatWorkflowReq = {
|
|
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
|
parameters: Object.assign({}, this.setting.customVariables || {}, callChatOption.customVariables || {}),
|
|
97
127
|
}
|
|
98
128
|
return worflowreq as T;
|
|
99
129
|
}
|
|
100
130
|
/**
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
131
|
+
* 同步调用的问答请求需要获取会话详情
|
|
132
|
+
* @param chatid
|
|
133
|
+
* @param conversation_id
|
|
134
|
+
*/
|
|
135
|
+
private async getChatDetail(client: CozeAPI, conversation_id: string, chatid: string) {
|
|
136
|
+
const maxWaitTime = 120000; // 最大等待120秒
|
|
137
|
+
const startTime = Date.now();
|
|
138
|
+
while ((Date.now() - startTime) < maxWaitTime) {
|
|
139
|
+
const chatinfo = await client.chat.retrieve(conversation_id, chatid);
|
|
140
|
+
// 状态已经是完成了,可以去获取对话的内容了
|
|
141
|
+
if (chatinfo.status === ChatStatus.COMPLETED) return { usage: chatinfo.usage, messages: await client.chat.messages.list(conversation_id, chatid)};
|
|
142
|
+
await new Promise(resolve => setTimeout(resolve, 1500)); // 等待1500ms
|
|
143
|
+
}
|
|
144
|
+
return null;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* 非流式传输聊天请求
|
|
148
|
+
* @param chatText
|
|
149
|
+
* @param callChatOption
|
|
150
|
+
* @param _axiosOption
|
|
151
|
+
*/
|
|
106
152
|
public async chatRequest(message: any[] | string, callChatOption: any = {}, _axiosOption: any = {}): Promise<any> {
|
|
107
153
|
if (!message) this.emit('chaterror', { successed: false, error: 'no message in chat' });
|
|
108
154
|
///如果是字符,则组装成API需要的消息
|
|
@@ -114,34 +160,33 @@ export default class CorzBot extends GptBase {
|
|
|
114
160
|
}
|
|
115
161
|
];
|
|
116
162
|
const client = await this.createClient();
|
|
163
|
+
|
|
117
164
|
////如果参数中用的是Workflow,则调用对话流来输出结果
|
|
118
165
|
////否则用智能体的对话来输出结果
|
|
119
166
|
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
|
-
|
|
167
|
+
const response = !this.workflowid ? await client.chat.create(params as CreateChatReq) : await client.workflows.runs.create(params as RunWorkflowReq);
|
|
168
|
+
if (this.workflowid && (response as RunWorkflowData).msg === 'Success') {
|
|
169
|
+
const resp = (response as RunWorkflowData).data;
|
|
170
|
+
try {
|
|
171
|
+
return { successed: true, message: [{ role: 'assistant', type:'answer', content: JSON.parse(resp).data }]};
|
|
172
|
+
} catch (error) {
|
|
173
|
+
return { successed: true, message: [{ role: 'assistant', type: 'answer', content: resp }] };
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
if (!this.workflowid && (response as CreateChatData).conversation_id && (response as CreateChatData).id) {
|
|
177
|
+
const ccd = response as CreateChatData;
|
|
178
|
+
const chatData = await this.getChatDetail(client, ccd.conversation_id, ccd.id);
|
|
179
|
+
if (chatData){
|
|
180
|
+
const message = chatData.messages.filter(x => x.type === 'answer').map(item => ({
|
|
181
|
+
role: item.role,
|
|
182
|
+
type: item.type,
|
|
183
|
+
content: item.content,
|
|
184
|
+
}));
|
|
185
|
+
return { successed: true, message, usage: chatData.usage,session_id:ccd.conversation_id };
|
|
186
|
+
}
|
|
133
187
|
}
|
|
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' }
|
|
188
|
+
return { successed: false, error: { message: '聊天未完成' } };
|
|
189
|
+
|
|
145
190
|
}
|
|
146
191
|
/**
|
|
147
192
|
* 解析深度思考的JSON内容
|
|
@@ -230,7 +275,7 @@ export default class CorzBot extends GptBase {
|
|
|
230
275
|
////否则用智能体的对话来输出结果
|
|
231
276
|
let requestid = Math.ceil(Math.random() * (new Date().getTime() * Math.random()) / 1000), index = 0;
|
|
232
277
|
const params = await this.getRequestStream(client, message, callChatOption);
|
|
233
|
-
const stream = !
|
|
278
|
+
const stream = !this.workflowid ? client.chat.stream(params as StreamChatReq) : client.workflows.runs.stream(params as RunWorkflowReq);
|
|
234
279
|
let deltaindex = 0, fullanswer: string[] = [], followup: string[] = [], cardData: CardType[] = [];
|
|
235
280
|
let deepThinking = '', thinkingStatus = DeepThinkingStatus.None, cardResource = ''; // 是否在深度思考中
|
|
236
281
|
for await (const part of stream) {
|
|
@@ -268,19 +313,6 @@ export default class CorzBot extends GptBase {
|
|
|
268
313
|
this.emit('chatthinking', result.thinking);
|
|
269
314
|
}
|
|
270
315
|
}
|
|
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
316
|
thinkingStatus = result.status;
|
|
285
317
|
}
|
|
286
318
|
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
|