@whwrm/react-agent 1.1.0 → 1.1.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/README.md +211 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -195,6 +195,217 @@ MIT
|
|
|
195
195
|
|
|
196
196
|
---
|
|
197
197
|
|
|
198
|
+
## 项目结构说明
|
|
199
|
+
|
|
200
|
+
### 📁 文件位置总览
|
|
201
|
+
|
|
202
|
+
配置文件放在**你自己的项目目录**下,不是在 npm 包里:
|
|
203
|
+
|
|
204
|
+
```
|
|
205
|
+
your-project/
|
|
206
|
+
├── config.json # Agent 配置(必需)
|
|
207
|
+
├── tools.json # 工具定义(可选)
|
|
208
|
+
├── skills.json # 技能定义(可选)
|
|
209
|
+
├── tools-server.ts # 工具服务器(生产环境用,可选)
|
|
210
|
+
├── skills-server.ts # 技能服务器(生产环境用,可选)
|
|
211
|
+
└── main.ts # 你的入口文件
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
> ⚠️ **注意**:`example/` 目录下的文件是**示例代码**,不是 npm 包的一部分。你需要在自己的项目中创建这些配置文件。
|
|
215
|
+
|
|
216
|
+
---
|
|
217
|
+
|
|
218
|
+
### 📄 config.json(必需)
|
|
219
|
+
|
|
220
|
+
Agent 的主配置文件,定义 LLM、工具服务器地址等:
|
|
221
|
+
|
|
222
|
+
```json
|
|
223
|
+
{
|
|
224
|
+
"llm": {
|
|
225
|
+
"model": "glm-5",
|
|
226
|
+
"apiKey": "${API_KEY}",
|
|
227
|
+
"baseUrl": "https://coding.dashscope.aliyuncs.com/v1"
|
|
228
|
+
},
|
|
229
|
+
"systemPrompt": "你是一个智能助手",
|
|
230
|
+
"maxIterations": 10,
|
|
231
|
+
"contextWindowTokens": 128000,
|
|
232
|
+
"tools": {
|
|
233
|
+
"url": "http://localhost:3000",
|
|
234
|
+
"definitionsFile": "./tools.json"
|
|
235
|
+
},
|
|
236
|
+
"skills": {
|
|
237
|
+
"url": "http://localhost:3000",
|
|
238
|
+
"definitionsFile": "./skills.json"
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
**使用方式**:
|
|
244
|
+
```typescript
|
|
245
|
+
import { loadConfig } from '@whwrm/react-agent';
|
|
246
|
+
const config = loadConfig('./config.json');
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
---
|
|
250
|
+
|
|
251
|
+
### 📄 tools.json(可选)
|
|
252
|
+
|
|
253
|
+
工具定义文件,定义 Agent 可以调用的工具。**只有在使用远程工具服务器时才需要**。
|
|
254
|
+
|
|
255
|
+
```json
|
|
256
|
+
[
|
|
257
|
+
{
|
|
258
|
+
"name": "get_weather",
|
|
259
|
+
"description": "获取城市天气",
|
|
260
|
+
"parameters": {
|
|
261
|
+
"type": "object",
|
|
262
|
+
"properties": { "city": { "type": "string" } },
|
|
263
|
+
"required": ["city"]
|
|
264
|
+
},
|
|
265
|
+
"api": {
|
|
266
|
+
"url": "https://wttr.in/{city}?format=j1",
|
|
267
|
+
"method": "GET"
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
]
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
**两种使用方式**:
|
|
274
|
+
|
|
275
|
+
| 方式 | 说明 | 适用场景 |
|
|
276
|
+
|------|------|----------|
|
|
277
|
+
| `registerTool()` | 代码中直接注册 | 开发环境、简单场景 |
|
|
278
|
+
| `tools.json` + 工具服务器 | 配置文件定义 | 生产环境、热重载 |
|
|
279
|
+
|
|
280
|
+
---
|
|
281
|
+
|
|
282
|
+
### 📄 skills.json(可选)
|
|
283
|
+
|
|
284
|
+
技能定义文件,定义 Agent 可以使用的技能。**只有在使用远程技能服务器时才需要**。
|
|
285
|
+
|
|
286
|
+
```json
|
|
287
|
+
[
|
|
288
|
+
{
|
|
289
|
+
"name": "summarize",
|
|
290
|
+
"description": "总结文本",
|
|
291
|
+
"parameters": {
|
|
292
|
+
"type": "object",
|
|
293
|
+
"properties": { "content": { "type": "string" } },
|
|
294
|
+
"required": ["content"]
|
|
295
|
+
},
|
|
296
|
+
"prompt": "请总结以下内容:{content}"
|
|
297
|
+
}
|
|
298
|
+
]
|
|
299
|
+
```
|
|
300
|
+
|
|
301
|
+
---
|
|
302
|
+
|
|
303
|
+
### 📄 basic.ts(示例文件)
|
|
304
|
+
|
|
305
|
+
**这是一个示例文件**,展示 Agent 的基础用法。你不需要复制它,只需要参考它的代码:
|
|
306
|
+
|
|
307
|
+
```typescript
|
|
308
|
+
import { ReactAgent, loadConfig } from '@whwrm/react-agent';
|
|
309
|
+
|
|
310
|
+
const config = loadConfig('./config.json');
|
|
311
|
+
const agent = new ReactAgent(config);
|
|
312
|
+
const answer = await agent.run('问题');
|
|
313
|
+
```
|
|
314
|
+
|
|
315
|
+
---
|
|
316
|
+
|
|
317
|
+
### 📄 tools-server.ts 和 skills-server.ts(生产环境)
|
|
318
|
+
|
|
319
|
+
**这是生产环境的独立服务器代码**,用于:
|
|
320
|
+
- 长期运行,不随请求启动
|
|
321
|
+
- 支持热重载配置(修改 JSON 文件后自动生效)
|
|
322
|
+
- 多个 Agent 实例共享同一服务器
|
|
323
|
+
|
|
324
|
+
**开发环境 vs 生产环境**:
|
|
325
|
+
|
|
326
|
+
| 场景 | 方式 | 说明 |
|
|
327
|
+
|------|------|------|
|
|
328
|
+
| 开发/测试 | `createToolServer()` | 自动创建临时服务器,用完关闭 |
|
|
329
|
+
| 生产环境 | 独立部署 tools-server.ts | 长期运行,支持热重载 |
|
|
330
|
+
|
|
331
|
+
**开发环境示例**:
|
|
332
|
+
```typescript
|
|
333
|
+
import { createToolServer, registerTool, getToolDefinitions } from '@whwrm/react-agent';
|
|
334
|
+
|
|
335
|
+
registerTool('get_weather', async (args) => {
|
|
336
|
+
return `${args.city}: 25°C`;
|
|
337
|
+
}, { name: 'get_weather', description: '获取天气', parameters: {...} });
|
|
338
|
+
|
|
339
|
+
const server = await createToolServer({ port: 3000 });
|
|
340
|
+
// 完成后关闭
|
|
341
|
+
server.close();
|
|
342
|
+
```
|
|
343
|
+
|
|
344
|
+
**生产环境**:
|
|
345
|
+
```bash
|
|
346
|
+
# 1. 启动工具服务器(长期运行)
|
|
347
|
+
ts-node tools-server.ts
|
|
348
|
+
|
|
349
|
+
# 2. Agent API 调用远程工具服务器
|
|
350
|
+
```
|
|
351
|
+
|
|
352
|
+
**tools-server.ts 的要求**:
|
|
353
|
+
- 需要安装 `express`
|
|
354
|
+
- 需要创建 `tools.json` 配置文件
|
|
355
|
+
- 支持环境变量(如 `${SERPER_KEY}`)
|
|
356
|
+
|
|
357
|
+
**skills-server.ts 的要求**:
|
|
358
|
+
- 需要安装 `express`
|
|
359
|
+
- 需要创建 `skills.json` 配置文件
|
|
360
|
+
- 需要配置 `LLM_API_KEY` 环境变量(用于执行技能)
|
|
361
|
+
|
|
362
|
+
---
|
|
363
|
+
|
|
364
|
+
### 🔄 完整工作流程
|
|
365
|
+
|
|
366
|
+
**方式一:开发环境(简单)**
|
|
367
|
+
```typescript
|
|
368
|
+
import { ask, createToolServer, registerTool, getToolDefinitions } from '@whwrm/react-agent';
|
|
369
|
+
|
|
370
|
+
// 1. 注册工具(不需要 tools.json)
|
|
371
|
+
registerTool('get_weather', async (args) => { ... });
|
|
372
|
+
|
|
373
|
+
// 2. 启动服务器
|
|
374
|
+
const server = await createToolServer({ port: 3000 });
|
|
375
|
+
|
|
376
|
+
// 3. 调用
|
|
377
|
+
const answer = await ask({
|
|
378
|
+
llm: { model: 'glm-5', apiKey: 'xxx' },
|
|
379
|
+
tools: { url: 'http://localhost:3000', definitions: getToolDefinitions() },
|
|
380
|
+
}, '问题');
|
|
381
|
+
|
|
382
|
+
server.close();
|
|
383
|
+
```
|
|
384
|
+
|
|
385
|
+
**方式二:生产环境(推荐)**
|
|
386
|
+
```bash
|
|
387
|
+
# 终端 1:启动工具服务器
|
|
388
|
+
cd your-project
|
|
389
|
+
ts-node tools-server.ts
|
|
390
|
+
|
|
391
|
+
# 终端 2:启动技能服务器(可选)
|
|
392
|
+
ts-node skills-server.ts
|
|
393
|
+
```
|
|
394
|
+
|
|
395
|
+
```typescript
|
|
396
|
+
// Agent API - 无状态
|
|
397
|
+
import { ask, loadConfig } from '@whwrm/react-agent';
|
|
398
|
+
|
|
399
|
+
const config = loadConfig('./config.json');
|
|
400
|
+
const answer = await ask(config, '问题');
|
|
401
|
+
```
|
|
402
|
+
|
|
403
|
+
## License
|
|
404
|
+
|
|
405
|
+
MIT
|
|
406
|
+
|
|
407
|
+
---
|
|
408
|
+
|
|
198
409
|
# 中文文档
|
|
199
410
|
|
|
200
411
|
一个轻量级的 ReAct (Reasoning + Acting) 框架,让 LLM 能够自主调用工具完成任务。
|