@aidejs/core 0.1.0-alpha.3 → 0.1.0-beta.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/CHANGELOG +6 -2
- package/README.md +1 -1
- package/package.json +1 -1
- package/src/Memory.js +41 -0
- package/src/{utils/OpenAI.js → OpenAI.js} +1 -1
- package/src/Tool.js +1 -1
- package/src/index.js +20 -30
- package/src/utils.js +17 -0
- package/types/index.d.ts +2 -2
- package/src/utils/debug.js +0 -5
- package/src/utils/toJSON.js +0 -8
package/CHANGELOG
CHANGED
package/README.md
CHANGED
|
@@ -39,7 +39,7 @@
|
|
|
39
39
|
<tbody>
|
|
40
40
|
<tr>
|
|
41
41
|
<td>@aidejs/core</td>
|
|
42
|
-
<td
|
|
42
|
+
<td>必选项,核心库,提供了最核心的LLM交互功能和插件系统</td>
|
|
43
43
|
<td>
|
|
44
44
|
<a href="https://zxl20070701.github.io/toolbox/#/npm-download?packages=@aidejs/core&interval=7">
|
|
45
45
|
<img src="https://img.shields.io/npm/dm/@aidejs/core.svg" alt="downloads">
|
package/package.json
CHANGED
package/src/Memory.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
module.exports = class Memory {
|
|
2
|
+
|
|
3
|
+
constructor(messages = []) {
|
|
4
|
+
this.messages = messages;
|
|
5
|
+
this.message_value = ""; // 当前流缓存值
|
|
6
|
+
this.message_type = ""; // 当前流类型
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
// 直接添加一次新信息
|
|
10
|
+
// 前置的流会关闭,开始一次新的待整理流
|
|
11
|
+
push(value) {
|
|
12
|
+
this.generate("");
|
|
13
|
+
this.messages.push(value);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// 完结或整理一次流信息
|
|
17
|
+
generate(newType) {
|
|
18
|
+
if (this.message_value) {
|
|
19
|
+
this.messages.push({
|
|
20
|
+
role: this.message_type,
|
|
21
|
+
content: this.message_value
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
this.message_type = newType;
|
|
26
|
+
this.message_value = "";
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// 输入一个词元的助手信息
|
|
30
|
+
assistant(lexeme) {
|
|
31
|
+
if (this.message_type !== "assistant") this.generate("assistant");
|
|
32
|
+
this.message_value += lexeme;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// 获取记忆内容
|
|
36
|
+
valueOf() {
|
|
37
|
+
this.generate("");
|
|
38
|
+
return this.messages;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
}
|
package/src/Tool.js
CHANGED
package/src/index.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
const { initOption } = require("oipage/nodejs/option/index");
|
|
2
|
-
const OpenAI = require("./
|
|
2
|
+
const OpenAI = require("./OpenAI");
|
|
3
3
|
const Tool = require("./Tool");
|
|
4
|
-
const
|
|
4
|
+
const Memory = require("./Memory");
|
|
5
|
+
const { debug } = require("./utils");
|
|
5
6
|
|
|
6
7
|
module.exports = class Aidejs {
|
|
7
8
|
|
|
8
|
-
// 工具管理对象
|
|
9
|
-
static tool = new Tool();
|
|
9
|
+
static tool = new Tool(); // 工具管理对象
|
|
10
10
|
|
|
11
11
|
constructor(options = {}) {
|
|
12
12
|
this.options = initOption(options, {
|
|
@@ -36,22 +36,15 @@ module.exports = class Aidejs {
|
|
|
36
36
|
|
|
37
37
|
// 执行任务
|
|
38
38
|
task(message, logback, inputback, thinkback) {
|
|
39
|
-
let _this = this;
|
|
39
|
+
let _this = this, toolCalls = false;
|
|
40
40
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
let resetLastIndex = () => {
|
|
44
|
-
messages.push({
|
|
45
|
-
role: "assistant",
|
|
46
|
-
content: ""
|
|
47
|
-
});
|
|
48
|
-
lastIndex = messages.length - 1;
|
|
49
|
-
};
|
|
41
|
+
// 记忆管理对象
|
|
42
|
+
let memory = new Memory([..._this.options.messages]);
|
|
50
43
|
|
|
51
44
|
let runInputback = () => {
|
|
52
45
|
if (inputback) {
|
|
53
46
|
inputback().then(message => {
|
|
54
|
-
|
|
47
|
+
memory.push({
|
|
55
48
|
role: "user",
|
|
56
49
|
content: message
|
|
57
50
|
});
|
|
@@ -67,21 +60,21 @@ module.exports = class Aidejs {
|
|
|
67
60
|
let runChat = () => {
|
|
68
61
|
let tools = Aidejs.tool.valueOf();
|
|
69
62
|
|
|
70
|
-
if (
|
|
71
|
-
debug("OpenAI messages",
|
|
63
|
+
if (_this.options.debug) {
|
|
64
|
+
debug("OpenAI messages", memory.valueOf());
|
|
72
65
|
debug("OpenAI tools", tools.map(function (funItem) {
|
|
73
66
|
return funItem.function.name + " " + funItem.function.description;
|
|
74
67
|
}));
|
|
75
68
|
}
|
|
76
69
|
|
|
77
|
-
OpenAI(
|
|
70
|
+
OpenAI(_this.options.url + "/chat/completions", {
|
|
78
71
|
header: {
|
|
79
|
-
Authorization: `Bearer ${
|
|
72
|
+
Authorization: `Bearer ${_this.options.apiKey}`
|
|
80
73
|
},
|
|
81
74
|
params: {
|
|
82
75
|
|
|
83
76
|
// 必需,模型ID,也就是你选择的具体哪个模型
|
|
84
|
-
model:
|
|
77
|
+
model: _this.options.model,
|
|
85
78
|
|
|
86
79
|
// 必需,提供的array类型的消息列表,包含从头到尾的对话历史
|
|
87
80
|
// 比如值是一个数组,每个条目可以是下列类型之一:
|
|
@@ -124,7 +117,7 @@ module.exports = class Aidejs {
|
|
|
124
117
|
// content:必须提供的string类型,表示工具消息的内容,一般是把函数调用的结果描述在这里
|
|
125
118
|
// role:必须提供的string类型,表示消息作者的角色,对于tool message应该是"tool"
|
|
126
119
|
// tool_call_id:必须提供的string类型,表示本次消息是对哪个函数调用的结果反馈
|
|
127
|
-
messages,
|
|
120
|
+
messages: memory.valueOf(),
|
|
128
121
|
|
|
129
122
|
// 用户可选的一个字段,是array类型,表示可供模型选择的一个工具列表
|
|
130
123
|
// 列表中最多支持 128 个tool
|
|
@@ -151,11 +144,14 @@ module.exports = class Aidejs {
|
|
|
151
144
|
|
|
152
145
|
if (choice.delta.content) {
|
|
153
146
|
if (logback) logback(choice.delta.content);
|
|
147
|
+
memory.assistant(choice.delta.content);
|
|
154
148
|
} else if (choice.delta.reasoning) {
|
|
155
149
|
if (thinkback) thinkback(choice.delta.reasoning);
|
|
156
150
|
}
|
|
157
151
|
|
|
158
152
|
if (choice.delta.tool_calls) {
|
|
153
|
+
memory.push(choice.delta);
|
|
154
|
+
|
|
159
155
|
if (_this.options.debug) debug("tool_calls", choice.delta.tool_calls);
|
|
160
156
|
|
|
161
157
|
// 执行一次工具
|
|
@@ -168,7 +164,7 @@ module.exports = class Aidejs {
|
|
|
168
164
|
let result = Aidejs.tool.invoke(_this, tool_call.function.name, JSON.parse(tool_call.function.arguments));
|
|
169
165
|
|
|
170
166
|
let toolback = function (value) {
|
|
171
|
-
|
|
167
|
+
memory.push({
|
|
172
168
|
role: "tool",
|
|
173
169
|
tool_call_id: tool_call.id,
|
|
174
170
|
tool_name: tool_call.function.name,
|
|
@@ -181,7 +177,7 @@ module.exports = class Aidejs {
|
|
|
181
177
|
result.then(function (value) {
|
|
182
178
|
toolback(value);
|
|
183
179
|
}).catch(function (error) {
|
|
184
|
-
toolback(error
|
|
180
|
+
toolback(error || "失败");
|
|
185
181
|
});
|
|
186
182
|
} else {
|
|
187
183
|
toolback(result);
|
|
@@ -190,8 +186,6 @@ module.exports = class Aidejs {
|
|
|
190
186
|
};
|
|
191
187
|
runTool(0);
|
|
192
188
|
} else {
|
|
193
|
-
if (messages[lastIndex].role !== "assistant") resetLastIndex();
|
|
194
|
-
messages[lastIndex].content += choice.delta.content || "";
|
|
195
189
|
runChoice(choiceIndex + 1);
|
|
196
190
|
}
|
|
197
191
|
|
|
@@ -212,12 +206,10 @@ module.exports = class Aidejs {
|
|
|
212
206
|
toolCalls = false;
|
|
213
207
|
runInputback();
|
|
214
208
|
});
|
|
215
|
-
|
|
216
|
-
resetLastIndex();
|
|
217
209
|
};
|
|
218
210
|
|
|
219
211
|
if (message) {
|
|
220
|
-
|
|
212
|
+
memory.push({
|
|
221
213
|
role: "user",
|
|
222
214
|
content: message
|
|
223
215
|
});
|
|
@@ -225,8 +217,6 @@ module.exports = class Aidejs {
|
|
|
225
217
|
} else {
|
|
226
218
|
runInputback();
|
|
227
219
|
}
|
|
228
|
-
|
|
229
|
-
return this;
|
|
230
220
|
}
|
|
231
221
|
|
|
232
222
|
};
|
package/src/utils.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// 打印调试语句
|
|
2
|
+
exports.debug = function (name, content) {
|
|
3
|
+
console.log("\n\x1b[33m【" + name + "】 " + new Date().toString() + "\x1b[0m");
|
|
4
|
+
console.log(content);
|
|
5
|
+
console.log("\x1b[33m[DONE]\x1b[0m\n");
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
// 字符串转JSON
|
|
9
|
+
// 适配LLM返回数据格式
|
|
10
|
+
exports.toJSON = function (value) {
|
|
11
|
+
try {
|
|
12
|
+
value = value.trim().replace(/data: \[DONE\]$/, "");
|
|
13
|
+
return JSON.parse(value);
|
|
14
|
+
} catch (e) {
|
|
15
|
+
return value;
|
|
16
|
+
}
|
|
17
|
+
};
|
package/types/index.d.ts
CHANGED
|
@@ -16,7 +16,7 @@ export default class Aidejs {
|
|
|
16
16
|
* @param name
|
|
17
17
|
* @param value
|
|
18
18
|
*/
|
|
19
|
-
static registerTool(name: string, value: any):
|
|
19
|
+
static registerTool(name: string, value: any): Aidejs
|
|
20
20
|
|
|
21
21
|
/**
|
|
22
22
|
* 安装插件
|
|
@@ -28,6 +28,6 @@ export default class Aidejs {
|
|
|
28
28
|
* 执行任务
|
|
29
29
|
* @param message
|
|
30
30
|
*/
|
|
31
|
-
task(message?: string, logback?: (message: string) => void, inputback?: () => Promise<string>, thinkback?: (think: string) => void):
|
|
31
|
+
task(message?: string, logback?: (message: string) => void, inputback?: () => Promise<string>, thinkback?: (think: string) => void): void
|
|
32
32
|
|
|
33
33
|
}
|
package/src/utils/debug.js
DELETED