@auipage/agent 0.1.0-alpha.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/.mailmap +1 -0
- package/AUTHORS.txt +6 -0
- package/CHANGELOG +4 -0
- package/README.md +134 -0
- package/package.json +31 -0
- package/src/callFuns.js +35 -0
- package/src/index.js +22 -0
- package/src/runConversation.js +59 -0
- package/types/index.d.ts +0 -0
package/.mailmap
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
zxl20070701 <1904314465@qq.com>
|
package/AUTHORS.txt
ADDED
package/CHANGELOG
ADDED
package/README.md
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
# [@auipage/agent](https://github.com/handless-ui/auipage/packages/agent/README.md)
|
|
2
|
+
智能体快速开发框架,用于快速开发智能体网页应用。
|
|
3
|
+
|
|
4
|
+
<p>
|
|
5
|
+
<a href="https://zxl20070701.github.io/toolbox/#/npm-download?packages=@auipage/agent&interval=7">
|
|
6
|
+
<img src="https://img.shields.io/npm/dm/@auipage/agent.svg" alt="downloads">
|
|
7
|
+
</a>
|
|
8
|
+
<a href="https://www.npmjs.com/package/@auipage/agent">
|
|
9
|
+
<img src="https://img.shields.io/npm/v/@auipage/agent.svg" alt="npm">
|
|
10
|
+
</a>
|
|
11
|
+
<a href="https://github.com/handless-ui/auipage" target='_blank'>
|
|
12
|
+
<img alt="GitHub repo stars" src="https://img.shields.io/github/stars/handless-ui/auipage?style=social">
|
|
13
|
+
</a>
|
|
14
|
+
</p>
|
|
15
|
+
|
|
16
|
+
<img src="https://nodei.co/npm/@auipage/agent.png?downloads=true&downloadRank=true&stars=true" alt="NPM">
|
|
17
|
+
|
|
18
|
+
## 如何使用?
|
|
19
|
+
|
|
20
|
+
```
|
|
21
|
+
npm install --save @auipage/agent
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
安装后,即可在项目中使用。
|
|
25
|
+
|
|
26
|
+
### 快速上手
|
|
27
|
+
|
|
28
|
+
`@auipage/agent` 暴露一个 `Agent` 构造器。实例化时需要传入一个 `Model` 配置和可选的工具列表,然后调用 `generate(text)` 即可让模型基于系统提示与用户输入返回最终文本。
|
|
29
|
+
|
|
30
|
+
```javascript
|
|
31
|
+
import { Agent } from "@auipage/agent";
|
|
32
|
+
|
|
33
|
+
const agent = new Agent({
|
|
34
|
+
model: {
|
|
35
|
+
model: "gpt-4o-mini", // 模型名称
|
|
36
|
+
apiKey: "your-api-key", // 模型服务提供的 API Key
|
|
37
|
+
baseURL: "https://api.openai.com/v1" // 模型服务的 baseURL
|
|
38
|
+
},
|
|
39
|
+
systemPrompt: "你是一个简洁、乐于助人的助手。"
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
const answer = await agent.generate("用一句话介绍 JavaScript");
|
|
43
|
+
console.log(answer);
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
`generate(text)` 返回的是一个 `Promise<string>`,表示模型最终给出的回复。
|
|
47
|
+
|
|
48
|
+
### 注册工具(Tool Calling)
|
|
49
|
+
|
|
50
|
+
如果模型在回复过程中判定需要调用工具,Agent 会按工具的名称在 `tools` 列表中找到对应的工具并执行,把执行结果作为 `tool` 消息再交给模型继续生成,直至拿到最终回复。
|
|
51
|
+
|
|
52
|
+
每个工具由两部分组成:
|
|
53
|
+
|
|
54
|
+
- `description`:符合模型 `tools` 描述规范的描述对象,用于告诉模型该工具何时被调用以及参数结构。
|
|
55
|
+
- `valueOf(args)`:工具真正执行的函数,参数 `args` 是模型解析后的对象,返回值可以是任意可序列化值,也可以是 `Promise`。
|
|
56
|
+
|
|
57
|
+
```javascript
|
|
58
|
+
import { Agent } from "@auipage/agent";
|
|
59
|
+
|
|
60
|
+
const getWeather = {
|
|
61
|
+
description: {
|
|
62
|
+
type: "function",
|
|
63
|
+
function: {
|
|
64
|
+
name: "getWeather",
|
|
65
|
+
description: "查询指定城市的天气",
|
|
66
|
+
parameters: {
|
|
67
|
+
type: "object",
|
|
68
|
+
properties: {
|
|
69
|
+
city: { type: "string", description: "城市名称,例如:南京" }
|
|
70
|
+
},
|
|
71
|
+
required: ["city"]
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
},
|
|
75
|
+
valueOf({ city }) {
|
|
76
|
+
// 这里访问真实的天气接口
|
|
77
|
+
return `${city} 今天晴,气温 25°C`;
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
const agent = new Agent({
|
|
82
|
+
model: {
|
|
83
|
+
model: "gpt-4o-mini",
|
|
84
|
+
apiKey: "your-api-key",
|
|
85
|
+
baseURL: "https://api.openai.com/v1"
|
|
86
|
+
},
|
|
87
|
+
systemPrompt: "你可以使用 getWeather 工具查询天气。",
|
|
88
|
+
tools: [getWeather]
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
const answer = await agent.generate("北京今天天气怎么样?");
|
|
92
|
+
console.log(answer);
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
`valueOf` 也可以返回 `Promise`:
|
|
96
|
+
|
|
97
|
+
```javascript
|
|
98
|
+
const searchWeb = {
|
|
99
|
+
description: {
|
|
100
|
+
type: "function",
|
|
101
|
+
function: {
|
|
102
|
+
name: "searchWeb",
|
|
103
|
+
description: "在网络上搜索关键词",
|
|
104
|
+
parameters: {
|
|
105
|
+
type: "object",
|
|
106
|
+
properties: {
|
|
107
|
+
query: { type: "string", description: "搜索关键词" }
|
|
108
|
+
},
|
|
109
|
+
required: ["query"]
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
},
|
|
113
|
+
valueOf({ query }) {
|
|
114
|
+
return fetch(`/api/search?q=${encodeURIComponent(query)}`).then(r => r.text());
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
### 配置项一览
|
|
120
|
+
|
|
121
|
+
| 字段 | 类型 | 必填 | 说明 |
|
|
122
|
+
| --- | --- | --- | --- |
|
|
123
|
+
| `model` | `object` | 是 | 透传给 `@auipage/model` 的模型配置,包含 `model`、`apiKey`、`baseURL` |
|
|
124
|
+
| `model.model` | `string` | 是 | 模型名称 |
|
|
125
|
+
| `model.apiKey` | `string` | 是 | 模型服务的 API Key |
|
|
126
|
+
| `model.baseURL` | `string` | 是 | 模型服务的 baseURL |
|
|
127
|
+
| `systemPrompt` | `string` | 否 | 系统提示词,会作为首条 `system` 消息发送给模型 |
|
|
128
|
+
| `tools` | `Tool[]` | 否 | 工具列表,每项需提供 `description` 和 `valueOf` |
|
|
129
|
+
|
|
130
|
+
## 版权
|
|
131
|
+
|
|
132
|
+
MIT License
|
|
133
|
+
|
|
134
|
+
Copyright (c) [zxl20070701](https://zxl20070701.github.io/notebook/home.html) 走一步,再走一步
|
package/package.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@auipage/agent",
|
|
3
|
+
"version": "0.1.0-alpha.0",
|
|
4
|
+
"description": "@auipage/agent",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./src/index.js",
|
|
7
|
+
"typings": "types/index.d.ts",
|
|
8
|
+
"sideEffects": false,
|
|
9
|
+
"scripts": {
|
|
10
|
+
"prepublishOnly": "node ../../swap-pkg.js --mode=publish",
|
|
11
|
+
"postpublish": "node ../../swap-pkg.js --mode=restore"
|
|
12
|
+
},
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/handless-ui/auipage.git",
|
|
16
|
+
"directory": "packages/agent"
|
|
17
|
+
},
|
|
18
|
+
"author": {
|
|
19
|
+
"name": "zxl20070701",
|
|
20
|
+
"url": "https://zxl20070701.github.io/notebook/home.html"
|
|
21
|
+
},
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"bugs": {
|
|
24
|
+
"url": "https://github.com/handless-ui/auipage/issues"
|
|
25
|
+
},
|
|
26
|
+
"homepage": "https://github.com/handless-ui/auipage#readme",
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"@auipage/types": "^0.1.0-alpha.0",
|
|
29
|
+
"@auipage/model": "^0.1.0-alpha.0"
|
|
30
|
+
}
|
|
31
|
+
}
|
package/src/callFuns.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 根据模型返回的 tool_calls,依次在 _this.tools 中查找匹配的工具并执行,
|
|
3
|
+
* 返回与 tool_calls 等长的结果数组(同步值/异步值都会被规范化)。
|
|
4
|
+
*
|
|
5
|
+
* @param {Object} _this Agent 实例
|
|
6
|
+
* @param {Array} tool_calls 模型返回的工具调用列表
|
|
7
|
+
* @returns {Promise<Array>} 每个工具的执行结果,未匹配到的槽位会被填入错误说明字符串
|
|
8
|
+
*/
|
|
9
|
+
export default function callFuns(_this, tool_calls) {
|
|
10
|
+
|
|
11
|
+
const tasks = tool_calls.map(call => {
|
|
12
|
+
const tool = _this.tools.find(
|
|
13
|
+
t => t.description.function.name === call.function.name
|
|
14
|
+
);
|
|
15
|
+
|
|
16
|
+
if (!tool) {
|
|
17
|
+
return Promise.resolve("工具" + call.function.name + "不存在");
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
let result;
|
|
21
|
+
try {
|
|
22
|
+
result = tool.valueOf(JSON.parse(call.function.arguments));
|
|
23
|
+
} catch (e) {
|
|
24
|
+
return Promise.resolve("参数解析失败:" + e);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if (result instanceof Promise) {
|
|
28
|
+
// 工具内部报错也不要阻断对话,转成字符串继续往下走
|
|
29
|
+
return result.catch(e => "运行出错:" + e);
|
|
30
|
+
}
|
|
31
|
+
return Promise.resolve(result);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
return Promise.all(tasks);
|
|
35
|
+
};
|
package/src/index.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Model } from "@auipage/model";
|
|
2
|
+
import runConversation from "./runConversation.js";
|
|
3
|
+
|
|
4
|
+
function Agent(option) {
|
|
5
|
+
this.model = new Model(option.model);
|
|
6
|
+
this.tools = option.tools || [];
|
|
7
|
+
this.systemPrompt = option.systemPrompt || "";
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
Agent.prototype.generate = function (text) {
|
|
11
|
+
const messages = [{
|
|
12
|
+
role: "system",
|
|
13
|
+
content: this.systemPrompt
|
|
14
|
+
}, {
|
|
15
|
+
role: "user",
|
|
16
|
+
content: text
|
|
17
|
+
}];
|
|
18
|
+
|
|
19
|
+
return runConversation(this, messages);
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export { Agent };
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import callFuns from "./callFuns.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 把一次工具调用的执行结果转成 tool 消息。
|
|
5
|
+
*/
|
|
6
|
+
function toToolMessage(call, value) {
|
|
7
|
+
return {
|
|
8
|
+
role: "tool",
|
|
9
|
+
tool_call_id: call.id,
|
|
10
|
+
tool_name: call.function.name,
|
|
11
|
+
content: typeof value === "string" ? value : (JSON.stringify(value) + "")
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* 把模型响应按 role 追加到 messages,返回是否还有工具调用需要下一轮处理。
|
|
17
|
+
*/
|
|
18
|
+
function appendAssistantMessage(messages, result) {
|
|
19
|
+
if (result.content) {
|
|
20
|
+
messages.push({ role: "assistant", content: result.content });
|
|
21
|
+
}
|
|
22
|
+
if (result.tool_calls && result.tool_calls.length > 0) {
|
|
23
|
+
messages.push({ role: "assistant", tool_calls: result.tool_calls });
|
|
24
|
+
return true;
|
|
25
|
+
}
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* 执行模型对话循环:
|
|
31
|
+
* 1. 调用模型获取响应
|
|
32
|
+
* 2. 若返回工具调用,依次执行工具并将结果追加到 messages,继续循环
|
|
33
|
+
* 3. 否则将最终内容 resolve 给调用方
|
|
34
|
+
*
|
|
35
|
+
* @param {Object} _this Agent 实例
|
|
36
|
+
* @param {Array} messages 当前对话消息列表(会被原地修改)
|
|
37
|
+
* @returns {Promise<string>} 最终 assistant 的文本内容
|
|
38
|
+
*/
|
|
39
|
+
export default async function runConversation(_this, messages) {
|
|
40
|
+
|
|
41
|
+
while (true) {
|
|
42
|
+
const result = await _this.model.completions({
|
|
43
|
+
messages,
|
|
44
|
+
tools: _this.tools,
|
|
45
|
+
stream: false,
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
const hasToolCalls = appendAssistantMessage(messages, result);
|
|
49
|
+
|
|
50
|
+
if (!hasToolCalls) {
|
|
51
|
+
return result.content;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const toolResults = await callFuns(_this, result.tool_calls);
|
|
55
|
+
result.tool_calls.forEach((call, i) => {
|
|
56
|
+
messages.push(toToolMessage(call, toolResults[i]));
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
}
|
package/types/index.d.ts
ADDED
|
File without changes
|