@aidejs/core 0.1.0-alpha.3 → 0.1.0-beta.0

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/README.md CHANGED
@@ -39,7 +39,7 @@
39
39
  <tbody>
40
40
  <tr>
41
41
  <td>@aidejs/core</td>
42
- <td>必选项,基础库,提供了最核心的LLM交互功能和插件系统</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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aidejs/core",
3
- "version": "0.1.0-alpha.3",
3
+ "version": "0.1.0-beta.0",
4
4
  "description": "@aidejs/core",
5
5
  "main": "./src/index.js",
6
6
  "typings": "./types/index.d.ts",
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
+ }
@@ -1,6 +1,6 @@
1
1
  const https = require('https');
2
2
  const http = require('http');
3
- const toJSON = require("./toJSON");
3
+ const { toJSON } = require("./utils");
4
4
 
5
5
  module.exports = function (url, options = {}, databack, callback, errorback) {
6
6
 
package/src/Tool.js CHANGED
@@ -1,4 +1,4 @@
1
- const debug = require("./utils/debug");
1
+ const { debug } = require("./utils");
2
2
 
3
3
  module.exports = class Tool {
4
4
 
package/src/index.js CHANGED
@@ -1,12 +1,13 @@
1
1
  const { initOption } = require("oipage/nodejs/option/index");
2
- const OpenAI = require("./utils/OpenAI");
2
+ const OpenAI = require("./OpenAI");
3
3
  const Tool = require("./Tool");
4
- const debug = require("./utils/debug");
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
+ static messages = [];
10
11
 
11
12
  constructor(options = {}) {
12
13
  this.options = initOption(options, {
@@ -36,22 +37,15 @@ module.exports = class Aidejs {
36
37
 
37
38
  // 执行任务
38
39
  task(message, logback, inputback, thinkback) {
39
- let _this = this;
40
+ let _this = this, toolCalls = false;
40
41
 
41
- let messages = [..._this.options.messages], lastIndex = -1, toolCalls = false;
42
-
43
- let resetLastIndex = () => {
44
- messages.push({
45
- role: "assistant",
46
- content: ""
47
- });
48
- lastIndex = messages.length - 1;
49
- };
42
+ // 记忆管理对象
43
+ let memory = new Memory([...Aidejs.messages, ..._this.options.messages]);
50
44
 
51
45
  let runInputback = () => {
52
46
  if (inputback) {
53
47
  inputback().then(message => {
54
- messages.push({
48
+ memory.push({
55
49
  role: "user",
56
50
  content: message
57
51
  });
@@ -67,21 +61,21 @@ module.exports = class Aidejs {
67
61
  let runChat = () => {
68
62
  let tools = Aidejs.tool.valueOf();
69
63
 
70
- if (this.options.debug) {
71
- debug("OpenAI messages", messages);
64
+ if (_this.options.debug) {
65
+ debug("OpenAI messages", memory.valueOf());
72
66
  debug("OpenAI tools", tools.map(function (funItem) {
73
67
  return funItem.function.name + " " + funItem.function.description;
74
68
  }));
75
69
  }
76
70
 
77
- OpenAI(this.options.url + "/chat/completions", {
71
+ OpenAI(_this.options.url + "/chat/completions", {
78
72
  header: {
79
- Authorization: `Bearer ${this.options.apiKey}`
73
+ Authorization: `Bearer ${_this.options.apiKey}`
80
74
  },
81
75
  params: {
82
76
 
83
77
  // 必需,模型ID,也就是你选择的具体哪个模型
84
- model: this.options.model,
78
+ model: _this.options.model,
85
79
 
86
80
  // 必需,提供的array类型的消息列表,包含从头到尾的对话历史
87
81
  // 比如值是一个数组,每个条目可以是下列类型之一:
@@ -124,7 +118,7 @@ module.exports = class Aidejs {
124
118
  // content:必须提供的string类型,表示工具消息的内容,一般是把函数调用的结果描述在这里
125
119
  // role:必须提供的string类型,表示消息作者的角色,对于tool message应该是"tool"
126
120
  // tool_call_id:必须提供的string类型,表示本次消息是对哪个函数调用的结果反馈
127
- messages,
121
+ messages: memory.valueOf(),
128
122
 
129
123
  // 用户可选的一个字段,是array类型,表示可供模型选择的一个工具列表
130
124
  // 列表中最多支持 128 个tool
@@ -151,11 +145,14 @@ module.exports = class Aidejs {
151
145
 
152
146
  if (choice.delta.content) {
153
147
  if (logback) logback(choice.delta.content);
148
+ memory.assistant(choice.delta.content);
154
149
  } else if (choice.delta.reasoning) {
155
150
  if (thinkback) thinkback(choice.delta.reasoning);
156
151
  }
157
152
 
158
153
  if (choice.delta.tool_calls) {
154
+ memory.push(choice.delta);
155
+
159
156
  if (_this.options.debug) debug("tool_calls", choice.delta.tool_calls);
160
157
 
161
158
  // 执行一次工具
@@ -168,7 +165,7 @@ module.exports = class Aidejs {
168
165
  let result = Aidejs.tool.invoke(_this, tool_call.function.name, JSON.parse(tool_call.function.arguments));
169
166
 
170
167
  let toolback = function (value) {
171
- messages.push({
168
+ memory.push({
172
169
  role: "tool",
173
170
  tool_call_id: tool_call.id,
174
171
  tool_name: tool_call.function.name,
@@ -181,7 +178,7 @@ module.exports = class Aidejs {
181
178
  result.then(function (value) {
182
179
  toolback(value);
183
180
  }).catch(function (error) {
184
- toolback(error + "");
181
+ toolback(error || "失败");
185
182
  });
186
183
  } else {
187
184
  toolback(result);
@@ -190,8 +187,6 @@ module.exports = class Aidejs {
190
187
  };
191
188
  runTool(0);
192
189
  } else {
193
- if (messages[lastIndex].role !== "assistant") resetLastIndex();
194
- messages[lastIndex].content += choice.delta.content || "";
195
190
  runChoice(choiceIndex + 1);
196
191
  }
197
192
 
@@ -212,12 +207,10 @@ module.exports = class Aidejs {
212
207
  toolCalls = false;
213
208
  runInputback();
214
209
  });
215
-
216
- resetLastIndex();
217
210
  };
218
211
 
219
212
  if (message) {
220
- messages.push({
213
+ memory.push({
221
214
  role: "user",
222
215
  content: message
223
216
  });
@@ -225,8 +218,6 @@ module.exports = class Aidejs {
225
218
  } else {
226
219
  runInputback();
227
220
  }
228
-
229
- return this;
230
221
  }
231
222
 
232
223
  };
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): void
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): this
31
+ task(message?: string, logback?: (message: string) => void, inputback?: () => Promise<string>, thinkback?: (think: string) => void): void
32
32
 
33
33
  }
@@ -1,5 +0,0 @@
1
- module.exports = function (name, content) {
2
- console.log("\n\x1b[33m【" + name + "】 " + new Date().toString() + "\x1b[0m");
3
- console.log(content);
4
- console.log("\x1b[33m---------------------------------------------------------------------\x1b[0m\n");
5
- }
@@ -1,8 +0,0 @@
1
- module.exports = function (value) {
2
- try {
3
- value = value.trim().replace(/data: \[DONE\]$/, "");
4
- return JSON.parse(value);
5
- } catch (e) {
6
- return value;
7
- }
8
- }