@aidejs/core 0.1.0-alpha.2 → 0.1.0-alpha.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/README.md CHANGED
@@ -1,14 +1,8 @@
1
- <img src="./docs/images/logo.png" />
1
+ <img src="https://NN-Studio.github.io/Aidejs/images/logo.png" />
2
2
 
3
3
  # [Aidejs](https://github.com/NN-Studio/Aidejs)
4
4
  一个用于构建人工智能代理的JavaScript框架,它提供了一组工具和库,使创建智能应用程序变得轻松和高效
5
5
 
6
- ## 功能列表
7
-
8
- 通过类似拼积木的方式,你可以轻松地组合各个模块完成个性化的 AI 应用开发。
9
-
10
- 下面是可用模块:
11
-
12
6
  <p>
13
7
  <a href="https://github.com/NN-Studio/Aidejs/issues">
14
8
  <img src="https://img.shields.io/github/issues/NN-Studio/Aidejs" alt="issue">
@@ -19,14 +13,27 @@
19
13
  <a href="https://github.com/NN-Studio/Aidejs">
20
14
  <img src="https://img.shields.io/github/forks/NN-Studio/Aidejs" alt="forks">
21
15
  </a>
16
+ <a href="https://gitee.com/NN-Studio/Aidejs" target='_blank'>
17
+ <img alt="Gitee repo stars" src="https://gitee.com/NN-Studio/Aidejs/badge/star.svg">
18
+ </a>
19
+ <a href="https://gitee.com/NN-Studio/Aidejs">
20
+ <img src="https://gitee.com/NN-Studio/Aidejs/badge/fork.svg" alt="forks">
21
+ </a>
22
22
  </p>
23
23
 
24
+ ## 功能列表
25
+
26
+ 通过类似拼积木的方式,你可以轻松地组合各个模块完成个性化的 AI 应用开发。
27
+
28
+ 下面是可用模块:
29
+
24
30
  <table>
25
31
  <thead>
26
32
  <tr>
27
33
  <td>名称</td>
28
34
  <td>简介</td>
29
- <td>地址</td>
35
+ <td>下周统计</td>
36
+ <td>最新版本</td>
30
37
  </tr>
31
38
  </thead>
32
39
  <tbody>
@@ -34,14 +41,28 @@
34
41
  <td>@aidejs/core</td>
35
42
  <td>必选项,基础库,提供了最核心的LLM交互功能和插件系统</td>
36
43
  <td>
37
- <a href="https://www.npmjs.com/package/@aidejs/core" target="_blank">访问</a>
44
+ <a href="https://zxl20070701.github.io/toolbox/#/npm-download?packages=@aidejs/core&interval=7">
45
+ <img src="https://img.shields.io/npm/dm/@aidejs/core.svg" alt="downloads">
46
+ </a>
47
+ </td>
48
+ <td>
49
+ <a href="https://www.npmjs.com/package/@aidejs/core">
50
+ <img src="https://img.shields.io/npm/v/@aidejs/core.svg" alt="npm">
51
+ </a>
38
52
  </td>
39
53
  </tr>
40
54
  <tr>
41
55
  <td>@aidejs/tools</td>
42
56
  <td>非必选,工具库,提供了各种实用的工具函数</td>
43
57
  <td>
44
- <a href="https://www.npmjs.com/package/@aidejs/tools" target="_blank">访问</a>
58
+ <a href="https://zxl20070701.github.io/toolbox/#/npm-download?packages=@aidejs/tools&interval=7">
59
+ <img src="https://img.shields.io/npm/dm/@aidejs/tools.svg" alt="downloads">
60
+ </a>
61
+ </td>
62
+ <td>
63
+ <a href="https://www.npmjs.com/package/@aidejs/tools">
64
+ <img src="https://img.shields.io/npm/v/@aidejs/tools.svg" alt="npm">
65
+ </a>
45
66
  </td>
46
67
  </tr>
47
68
  </tbody>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aidejs/core",
3
- "version": "0.1.0-alpha.2",
3
+ "version": "0.1.0-alpha.3",
4
4
  "description": "@aidejs/core",
5
5
  "main": "./src/index.js",
6
6
  "typings": "./types/index.d.ts",
package/src/Tool.js CHANGED
@@ -1,3 +1,5 @@
1
+ const debug = require("./utils/debug");
2
+
1
3
  module.exports = class Tool {
2
4
 
3
5
  constructor() {
@@ -26,7 +28,13 @@ module.exports = class Tool {
26
28
  if (!this.tools[name]) {
27
29
  throw new Error(`工具${name}不存在`);
28
30
  }
29
- return this.tools[name].valueOf.call(aidejs, args);
31
+ let value = this.tools[name].valueOf.call(aidejs, args);
32
+ if (aidejs.options.debug) {
33
+ debug("工具调用", {
34
+ name, args, value
35
+ });
36
+ }
37
+ return value;
30
38
  }
31
39
 
32
40
  };
package/src/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  const { initOption } = require("oipage/nodejs/option/index");
2
2
  const OpenAI = require("./utils/OpenAI");
3
- const Tool = require("./Tool.js");
3
+ const Tool = require("./Tool");
4
+ const debug = require("./utils/debug");
4
5
 
5
6
  module.exports = class Aidejs {
6
7
 
@@ -9,9 +10,11 @@ module.exports = class Aidejs {
9
10
 
10
11
  constructor(options = {}) {
11
12
  this.options = initOption(options, {
13
+ debug: false,
12
14
  model: "",
13
15
  url: "http://localhost:11434/v1",
14
- apiKey: ""
16
+ apiKey: "",
17
+ messages: []
15
18
  });
16
19
 
17
20
  if (!this.options.model) {
@@ -35,7 +38,7 @@ module.exports = class Aidejs {
35
38
  task(message, logback, inputback, thinkback) {
36
39
  let _this = this;
37
40
 
38
- let messages = [], lastIndex = -1, toolCalls = false;
41
+ let messages = [..._this.options.messages], lastIndex = -1, toolCalls = false;
39
42
 
40
43
  let resetLastIndex = () => {
41
44
  messages.push({
@@ -62,15 +65,80 @@ module.exports = class Aidejs {
62
65
  };
63
66
 
64
67
  let runChat = () => {
68
+ let tools = Aidejs.tool.valueOf();
69
+
70
+ if (this.options.debug) {
71
+ debug("OpenAI messages", messages);
72
+ debug("OpenAI tools", tools.map(function (funItem) {
73
+ return funItem.function.name + " " + funItem.function.description;
74
+ }));
75
+ }
76
+
65
77
  OpenAI(this.options.url + "/chat/completions", {
66
78
  header: {
67
79
  Authorization: `Bearer ${this.options.apiKey}`
68
80
  },
69
81
  params: {
82
+
83
+ // 必需,模型ID,也就是你选择的具体哪个模型
70
84
  model: this.options.model,
85
+
86
+ // 必需,提供的array类型的消息列表,包含从头到尾的对话历史
87
+ // 比如值是一个数组,每个条目可以是下列类型之一:
88
+ // (所有的类型其实都应该是一个json对象,下面列出的都是其包含的字段)
89
+ // 1、System message
90
+ // content:必须提供的string类型,表示system的消息内容
91
+ // role:必须提供的string类型,表示消息作者的角色,对于system message应该是"system"
92
+ // name:可选的string类型,表示对话参与者的名称
93
+ // 2、User message
94
+ // content:必须提供的string或array类型,二选一,表示user的消息内容
95
+ // 为string类型时,表示消息的文本内容
96
+ // 为array类型时,用来包含多个内容部分的数组,而此数组的每个条目都是一个json对象,可以分别是如下类型之一
97
+ // 1)文本
98
+ // type:必须提供的string类型,表示内容部分的类型,一般是“text”
99
+ // text:必须提供的string类型,文字内容
100
+ // 2)图片
101
+ // type:必须提供的string类型,表示内容部分的类型,一般是"image_url"
102
+ // image_url:必须提供的string类型,图像的 URL 或 Base64 编码的图像数据
103
+ // role:必须提供的string类型,表示消息作者的角色,对于user message应该是"user"
104
+ // name:可选的string类型,表示对话参与者的名称
105
+ // 3、Assistant message(实际代码就是下面的choice.delta的值)
106
+ // content:必须提供(指定 tool_calls 时除外)的string类型,表示助手消息的内容
107
+ // role:必须提供的string类型,表示消息作者的角色,对于assistant message应该是"assistant"
108
+ // name:可选的string类型,表示对话参与者的名称
109
+ // tool_calls:可选的array类型,大模型生成的工具调用,例如函数调用,用这里代码打印的例子:
110
+ // [
111
+ // {
112
+ // id: 'call_graj8v5e', // 必须提供,表示函数调用的id
113
+ // index: 0,
114
+ // type: 'function', // 必须提供,表示工具调用的类型(目前仅支持“function”类型)。
115
+ // function: {
116
+ // name: 'aidejs_disk_readPlain', // 必须提供,要调用的函数的名称
117
+ // arguments: '{"filepath":"./.mailmap"}' // 必须提供,表示调用函数所用的参数
118
+ // }
119
+ // }
120
+ // ]
121
+ // 4、Tool message
122
+ // 一个json对象,在用户根据assistant的tool_calls内容调用了某个函数后,
123
+ // 用户可能还需要再把函数调用结果反馈给大模型,让大模型根据函数调用结果给出最终的总结性的答复
124
+ // content:必须提供的string类型,表示工具消息的内容,一般是把函数调用的结果描述在这里
125
+ // role:必须提供的string类型,表示消息作者的角色,对于tool message应该是"tool"
126
+ // tool_call_id:必须提供的string类型,表示本次消息是对哪个函数调用的结果反馈
71
127
  messages,
72
- tools: Aidejs.tool.valueOf(),
73
- stream: true
128
+
129
+ // 用户可选的一个字段,是array类型,表示可供模型选择的一个工具列表
130
+ // 列表中最多支持 128 个tool
131
+ // 具体说明可以查看 /packages/tools/src/disk/readPlain.js
132
+ tools,
133
+
134
+ // 可选的bool值,如果设置了true,将会流式的返回消息
135
+ stream: true,
136
+
137
+ // softmax 函数中的除数,温度越低,生成的结果越确定,温度越高,生成的结果越随机
138
+ // temperature: 0.7,
139
+
140
+ // 类似的有这些策略:TopP、TopK、MinP,分别表示前若干个累积积累和、前K名、最低概率
141
+ // top_p: 0.8,
74
142
  }
75
143
  }, function (chunk) {
76
144
  try {
@@ -88,6 +156,7 @@ module.exports = class Aidejs {
88
156
  }
89
157
 
90
158
  if (choice.delta.tool_calls) {
159
+ if (_this.options.debug) debug("tool_calls", choice.delta.tool_calls);
91
160
 
92
161
  // 执行一次工具
93
162
  let runTool = (toolIndex) => {
@@ -101,6 +170,7 @@ module.exports = class Aidejs {
101
170
  let toolback = function (value) {
102
171
  messages.push({
103
172
  role: "tool",
173
+ tool_call_id: tool_call.id,
104
174
  tool_name: tool_call.function.name,
105
175
  content: typeof value === "string" ? value : (JSON.stringify(value) + "")
106
176
  });
@@ -128,7 +198,7 @@ module.exports = class Aidejs {
128
198
  };
129
199
  runChoice(0);
130
200
  } catch (e) {
131
- console.error("处理智能体响应时发生错误:", e);
201
+ debug("处理智能体响应时发生错误", e);
132
202
  }
133
203
  }, function () {
134
204
  if (toolCalls) {
@@ -138,7 +208,7 @@ module.exports = class Aidejs {
138
208
  runInputback();
139
209
  }
140
210
  }, function (e) {
141
- console.error("对接LLM解答问题时发生错误:", e);
211
+ debug("对接LLM解答问题时发生错误", e);
142
212
  toolCalls = false;
143
213
  runInputback();
144
214
  });
@@ -0,0 +1,5 @@
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
+ }
package/types/index.d.ts CHANGED
@@ -4,9 +4,11 @@
4
4
  export default class Aidejs {
5
5
 
6
6
  constructor(options: {
7
- model: string,
8
- url?: string,
7
+ debug?: boolean
8
+ model: string
9
+ url?: string
9
10
  apiKey?: string
11
+ messages?: Array<any>
10
12
  })
11
13
 
12
14
  /**
@@ -26,6 +28,6 @@ export default class Aidejs {
26
28
  * 执行任务
27
29
  * @param message
28
30
  */
29
- task(message: string, logback: (message: string) => void, inputback: () => Promise<string>): this
31
+ task(message?: string, logback?: (message: string) => void, inputback?: () => Promise<string>, thinkback?: (think: string) => void): this
30
32
 
31
33
  }