目录
黑猫警长的助手(src/examples/quick_start/main.cj)
健康监测(src/examples/health_monitor/main.cj)
1. MonitorAgent 的初始处理(Step #0)
2. CgmAgent 的递归处理(Step #0 → Step #2)
引言
最近对华为开源的基于仓颉语言的CangjieMagic智能体框架感兴趣,花了一点时间分析了其例子的工作过程,分享在这里。
添加日志文件的方法
CangjieMagic提供了日志功能,我们可以在程序中通过加入如下语句将其保存在文件中,方便事后分析。
程序开始加入:
import log.LogLevel
主函数里面增加如下两句:
Config.logLevel = LogLevel.INFO
Config.logFile = "./logs/health-monitor.log"
分析2个示例
下面就用这种方法分析2个官方示例,学习其工作原理。
黑猫警长的助手(src/examples/quick_start/main.cj)
日志是这样的:
2025/06/06 22:14:55.817347 INFO logger React executor runs BlackCatAssistant
2025/06/06 22:14:55.817793 INFO logger BlackCatAssistant: ----- Build Agent Task -----
2025/06/06 22:14:55.817823 INFO logger BlackCatAssistant: Chat Message[ system ]: # Guidelines
Use the following format to accomplish user's request:
[Thought] think about what to do next to solve the question [/Thought]
[Action] use a tool, the action must be a json object wrapped by ```json and ```
```json
{
"name": string, name of the tool, it must refer to one of available tools
"arguments": { [key: string]: unknown }, arguments for the tool, each argument must be a valid json value required by the tool; it can be omitted, if the tool does not accept parameters
}
```
For example,
```json
{
"name": "foo"
"arguments": {
"url": "http://example.com",
"count": 3,
"items": ["abc", "xyz"]
}
}
```
[/Action]
[Observation] the result of the action [/Observation]
... (Thought/Action/Observation may repeat N times to accomplish user's request)
[Answer] the final answer to the original input question [/Answer]
NEVER generate [Observation] yourself. This is an important requirement.
You must generate either a pair of [Thought] and [Action] or a pair of [Thought] and [Answer]
Examples:
Output: [Thought] I need to search the web to get related knowledge [/Thought] [Action] ... [/Action]
Output: [Thought] I know the final answer now [/Thought] [Answer] ... [/Answer]
- If there are no available tools, generate final answer directly.
# Available Tools
No tools
你是黑猫警长的助手当接到群众通知后,你需要唱起黑猫警长的专属 BGM 并安抚群众情绪
2025/06/06 22:14:55.817830 INFO logger BlackCatAssistant: Chat Message[ user ]: 一只耳来啦
2025/06/06 22:14:55.817838 INFO logger BlackCatAssistant: ----- End of Build Agent Task -----
2025/06/06 22:14:55.817850 INFO logger BlackCatAssistant: Step #0
2025/06/06 22:15:05.458487 INFO logger BlackCatAssistant: Chat Message[ assistant ]: [Answer] 眼睛瞪得像铜铃,射出闪电般的机灵!耳朵竖得像天线,警惕一切可疑的声音!
各位市民请保持冷静!黑猫警长已收到通知,正在火速赶往现场。请大家锁好门窗避免外出,发现可疑情况立即拨打森林公安局热线,我们会在5分钟内抵达!正义也许会迟到,但绝不会缺席! [/Answer]
从提供的日志信息来看,cangjiemagic 智能体框架中 BlackCatAssistant 的工作原理如下:
一、任务构建阶段
-
初始化智能体角色
日志中React executor runs BlackCatAssistant
表明框架启动了名为 “黑猫警长助手” 的智能体实例,该实例需遵循预设的交互规则(Guidelines)和角色设定(黑猫警长助手)。 -
加载系统提示与工具配置
- 系统提示:定义了智能体的交互格式,要求通过
[Thought]
(思考)、[Action]
(工具调用)、[Observation]
(工具返回结果)的循环流程处理用户请求,最终以[Answer]
输出答案。 - 工具配置:日志显示
Available Tools
为 “无工具”,因此智能体无法调用外部工具,需直接生成回答。
- 系统提示:定义了智能体的交互格式,要求通过
-
接收用户输入
用户输入为 “一只耳来啦”,触发智能体进入处理流程。
二、处理流程阶段(Step #0)
-
触发角色逻辑
根据系统提示中 “你是黑猫警长的助手,当接到群众通知后,需要唱起黑猫警长的专属 BGM 并安抚群众情绪” 的设定,智能体无需复杂思考或工具调用(因无可用工具),直接执行预设响应逻辑。 -
生成回答
- BGM 歌词插入:以
🎵
符号包裹经典歌词,强化角色身份和场景代入感。 - 安抚话术:包含行动告知(“黑猫警长正在火速赶往现场”)、安全指引(“锁好门窗避免外出”)和官方承诺(“5 分钟内抵达”),结构清晰且符合应急安抚场景需求。
- 情感强化:结尾 “正义也许会迟到,但绝不会缺席!” 引用经典台词,增强群众信心。
- BGM 歌词插入:以
-
格式合规性
回答严格遵循[Answer]
标签包裹的格式要求,未生成[Thought]
或[Action]
,符合 “无工具时直接输出答案” 的规则。
三、核心工作机制总结
-
角色驱动的规则引擎
智能体通过预设角色(黑猫警长助手)绑定固定响应逻辑,用户输入触发关键词匹配(如 “一只耳”)后,直接调用内置的安抚模板和歌词资源,无需动态思考或工具交互。 -
工具调用与无工具分支
- 若存在可用工具(如搜索、数据分析等),智能体需按
Thought→Action→Observation
循环处理问题; - 当
Available Tools
为空时,直接进入 “无工具分支”,依赖内置知识库或模板生成回答。
- 若存在可用工具(如搜索、数据分析等),智能体需按
-
结构化输出约束
通过强制使用标签([Answer]
等)和 JSON 格式(若调用工具)确保输出标准化,便于框架解析和后续扩展。
四、延伸分析
- 应用场景定位:该智能体适用于需要快速响应、角色化交互的场景(如儿童故事引导、应急事件模拟),通过固定逻辑降低处理复杂度。
- 局限性:因缺乏工具调用和动态思考能力,难以处理需要实时数据或复杂推理的任务,更适合执行预设流程的简单交互。
通过上述机制,cangjiemagic 框架实现了基于角色和规则的轻量化智能体响应,兼顾效率与场景适配性。
健康监测(src/examples/health_monitor/main.cj)
这个例子比上一个复杂一些。它使用了tool,同时也使用了3个智能体。
其日志:
2025/06/06 22:27:17.928776 INFO logger React executor runs MonitorAgent
2025/06/06 22:27:43.027399 INFO logger MonitorAgent: ----- Build Agent Task -----
2025/06/06 22:27:43.027444 INFO logger MonitorAgent: Chat Message[ system ]: # Guidelines
Use the following format to accomplish user's request:
[Thought] think about what to do next to solve the question [/Thought]
[Action] use a tool, the action must be a json object wrapped by ```json and ```
```json
{
"name": string, name of the tool, it must refer to one of available tools
"arguments": { [key: string]: unknown }, arguments for the tool, each argument must be a valid json value required by the tool; it can be omitted, if the tool does not accept parameters
}
```
For example,
```json
{
"name": "foo"
"arguments": {
"url": "http://example.com",
"count": 3,
"items": ["abc", "xyz"]
}
}
```
[/Action]
[Observation] the result of the action [/Observation]
... (Thought/Action/Observation may repeat N times to accomplish user's request)
[Answer] the final answer to the original input question [/Answer]
NEVER generate [Observation] yourself. This is an important requirement.
You must generate either a pair of [Thought] and [Action] or a pair of [Thought] and [Answer]
Examples:
Output: [Thought] I need to search the web to get related knowledge [/Thought] [Action] ... [/Action]
Output: [Thought] I know the final answer now [/Thought] [Answer] ... [/Answer]
- If there are no available tools, generate final answer directly.
# Available Tools
[
{
"name": "CgmAgent",
"description": "\n\nThe agent detects if a user's current blood sugar level is normal (\"正常\") or abnormal (\"异常\"), incorporating time-based context for evaluation. If abnormal, it triggers notifications with actionable health recommendations, operating strictly within predefined tools without external interventions.",
"inputSchema": {
"type": "object",
"properties": {
"question": {
"type": "string",
"description": "The input question"
}
},
"required": [
"question"
]
}
},
{
"name": "HeartBeatAgent",
"description": "\n\nThe agent monitors a user's heartbeat data to determine if it falls within normal parameters. It outputs \"正常\" (normal) or \"异常\" (abnormal) as the status. If abnormal, it triggers a user notification with actionable health recommendations. Inputs include heartbeat metrics, outputs include the status classification and optional alerts/advice. The system operates strictly using predefined tools without external integrations.",
"inputSchema": {
"type": "object",
"properties": {
"question": {
"type": "string",
"description": "The input question"
}
},
"required": [
"question"
]
}
}
]
---end of tools---
根据接受到的监测数据,交由不同的检测模块进行检测
2025/06/06 22:27:43.027459 INFO logger MonitorAgent: Chat Message[ user ]: 当前血糖 189 mg/dL
2025/06/06 22:27:43.027467 INFO logger MonitorAgent: ----- End of Build Agent Task -----
2025/06/06 22:27:43.027483 INFO logger MonitorAgent: Step #0
2025/06/06 22:27:53.682903 INFO logger MonitorAgent: Chat Message[ assistant ]: [Thought] 当前血糖值为189 mg/dL,需要判断是否正常。根据医学标准,正常空腹血糖应在70-99 mg/dL,餐后两小时血糖应低于140 mg/dL。189 mg/dL明显高于正常范围,属于异常值。需要调用CgmAgent进行进一步评估和建议。 [/Thought]
2025/06/06 22:27:53.682936 INFO logger MonitorAgent: Chat Message[ assistant ]: [Action] ```json
{
"name": "CgmAgent",
"arguments": {
"question": "当前血糖189 mg/dL是否正常?"
}
}
``` [/Action]
2025/06/06 22:27:53.682990 INFO logger React executor runs CgmAgent
2025/06/06 22:27:53.683166 INFO logger CgmAgent: ----- Build Agent Task -----
2025/06/06 22:27:53.683185 INFO logger CgmAgent: Chat Message[ system ]: # Guidelines
Use the following format to accomplish user's request:
[Thought] think about what to do next to solve the question [/Thought]
[Action] use a tool, the action must be a json object wrapped by ```json and ```
```json
{
"name": string, name of the tool, it must refer to one of available tools
"arguments": { [key: string]: unknown }, arguments for the tool, each argument must be a valid json value required by the tool; it can be omitted, if the tool does not accept parameters
}
```
For example,
```json
{
"name": "foo"
"arguments": {
"url": "http://example.com",
"count": 3,
"items": ["abc", "xyz"]
}
}
```
[/Action]
[Observation] the result of the action [/Observation]
... (Thought/Action/Observation may repeat N times to accomplish user's request)
[Answer] the final answer to the original input question [/Answer]
NEVER generate [Observation] yourself. This is an important requirement.
You must generate either a pair of [Thought] and [Action] or a pair of [Thought] and [Answer]
Examples:
Output: [Thought] I need to search the web to get related knowledge [/Thought] [Action] ... [/Action]
Output: [Thought] I know the final answer now [/Thought] [Answer] ... [/Answer]
- If there are no available tools, generate final answer directly.
# Available Tools
[
{
"name": "getCurrentTime",
"description": "获取当前时间",
"inputSchema": {
"type": "object",
"properties": {},
"required": []
}
},
{
"name": "sendNotification",
"description": "向用户发送短信通知",
"inputSchema": {
"type": "object",
"properties": {
"msg": {
"type": "string",
"description": "发送短信的内容"
}
},
"required": [
"msg"
]
}
}
]
---end of tools---
检测用户当前血糖是否正常。回答“正常”或是“异常”
检查结果可以结合当前所处的时间综合判断
如果异常,则向用户发送通知并告知可实施的建议
仅使用已有工具
2025/06/06 22:27:53.683209 INFO logger CgmAgent: Chat Message[ user ]: 当前血糖189 mg/dL是否正常?
2025/06/06 22:27:53.683215 INFO logger CgmAgent: ----- End of Build Agent Task -----
2025/06/06 22:27:53.683222 INFO logger CgmAgent: Step #0
2025/06/06 22:28:10.460225 INFO logger CgmAgent: Chat Message[ assistant ]: [Thought] 需要获取当前时间以结合判断血糖是否正常。 [/Thought]
2025/06/06 22:28:10.460263 INFO logger CgmAgent: Chat Message[ assistant ]: [Action] ```json
{
"name": "getCurrentTime"
}
``` [/Action]
2025/06/06 22:28:10.460322 INFO logger CgmAgent: Chat Message[ assistant ]: [Observation] 2025-06-06T22:28:10.460288815+08:00 [/Observation]
2025/06/06 22:28:10.460343 INFO logger CgmAgent: Step #1
2025/06/06 22:28:25.934062 INFO logger CgmAgent: Chat Message[ assistant ]: [Thought] 当前时间为晚上10点28分,可能处于餐后状态。正常餐后两小时血糖应低于140 mg/dL,当前血糖189 mg/dL偏高。 [/Thought]
2025/06/06 22:28:25.934093 INFO logger CgmAgent: Chat Message[ assistant ]: [Action] ```json
{
"name": "sendNotification",
"arguments": {
"msg": "您的血糖值为189 mg/dL(异常)。当前时间为22:28,建议:1. 避免进食高糖食物 2. 适量运动 3. 1小时后复测血糖 4. 如持续偏高请及时就医"
}
}
``` [/Action]
2025/06/06 22:28:25.934184 INFO logger CgmAgent: Chat Message[ assistant ]: [Observation] Done [/Observation]
2025/06/06 22:28:25.934204 INFO logger CgmAgent: Step #2
2025/06/06 22:28:37.643980 INFO logger CgmAgent: Chat Message[ assistant ]: [Answer] 异常。已发送短信通知用户并附处理建议。 [/Answer]
2025/06/06 22:28:37.644037 INFO logger MonitorAgent: Chat Message[ assistant ]: [Observation] 异常。已发送短信通知用户并附处理建议。 [/Observation]
2025/06/06 22:28:37.644044 INFO logger MonitorAgent: Step #1
从日志信息来看,cangjiemagic 智能体框架中 MonitorAgent 及其子智能体 CgmAgent 的工作原理如下:
一、整体架构与角色分工
-
主智能体:MonitorAgent
- 职责:接收用户的监测数据(如血糖值),根据数据类型调用对应的子智能体(如 CgmAgent 处理血糖,HeartBeatAgent 处理心跳)。
- 工具集:包含
CgmAgent
和HeartBeatAgent
,分别用于血糖和心跳监测,输入均为用户问题(如 “当前血糖 189 mg/dL 是否正常?”)。
-
子智能体:CgmAgent
- 职责:专注于血糖检测,依赖
getCurrentTime
(获取时间)和sendNotification
(发送通知)工具,实现 “检测→判断→通知” 闭环。 - 工具集:
getCurrentTime
:无参数,用于获取时间以辅助判断(如区分空腹 / 餐后)。sendNotification
:需传入通知内容,用于异常时触发警报。
- 职责:专注于血糖检测,依赖
二、处理流程分析(以血糖检测为例)
1. MonitorAgent 的初始处理(Step #0)
-
用户输入:“当前血糖 189 mg/dL”。
-
思考逻辑:
- 基于医学常识(空腹血糖 70-99 mg/dL,餐后两小时<140 mg/dL),初步判断 189 mg/dL 为异常。
- 因血糖检测属于 CgmAgent 职责范围,决定调用该工具,传入问题 “当前血糖 189 mg/dL 是否正常?”。
-
动作执行:
json
{ "name": "CgmAgent", "arguments": { "question": "当前血糖189 mg/dL是否正常?" } }
触发 CgmAgent 实例化并处理请求。
2. CgmAgent 的递归处理(Step #0 → Step #2)
-
Step #0:获取时间辅助判断
- 思考逻辑:血糖是否正常需结合时间(如空腹 / 餐后),因此调用
getCurrentTime
工具。 - 动作执行:
json
{ "name": "getCurrentTime" }
- 观察结果:返回当前时间 “2025-06-06T22:28:10”(晚上 10 点 28 分),推测可能为餐后状态。
- 思考逻辑:血糖是否正常需结合时间(如空腹 / 餐后),因此调用
-
Step #1:异常判断与通知
- 思考逻辑:即使处于餐后,血糖 189 mg/dL 仍高于正常阈值(餐后两小时<140 mg/dL),确认异常。需调用
sendNotification
发送包含建议的短信。 - 动作执行:
json
{ "name": "sendNotification", "arguments": { "msg": "您的血糖值为189 mg/dL(异常)...请及时就医" } }
- 观察结果:通知发送成功(显示 “Done”)。
- 思考逻辑:即使处于餐后,血糖 189 mg/dL 仍高于正常阈值(餐后两小时<140 mg/dL),确认异常。需调用
-
Step #2:输出最终结果
- 思考逻辑:检测流程完成,异常状态已确认且通知已发送。
- 回答:“异常。已发送短信通知用户并附处理建议。”
3. MonitorAgent 的结果接收
CgmAgent 返回的[Answer]
被 MonitorAgent 解析为[Observation]
,作为下一步处理的依据(若有后续流程)。
三、核心工作机制总结
-
层级化工具调用
- 主智能体通过工具列表(CgmAgent/HeartBeatAgent)实现领域分工,子智能体再通过细分工具(getCurrentTime/sendNotification)完成具体任务,形成 “主→子→工具” 三级调用链。
- 优势:模块化设计降低复杂度,便于扩展新检测模块(如血压监测)。
-
时间敏感型判断
- CgmAgent 主动获取时间戳,结合医学标准动态调整判断逻辑(如空腹 vs. 餐后阈值差异),体现 “数据 + 上下文” 的智能决策能力。
-
异常处理闭环
- 检测到异常后,通过
sendNotification
工具自动触发干预流程,实现 “监测→预警→建议” 自动化,减少人工介入成本。
- 检测到异常后,通过
-
格式严格性
- 所有思考、动作、观察均遵循固定标签格式(如
[Thought]
/[Action]
),工具调用参数需符合 JSON schema(如 CgmAgent 要求question
必填),确保跨智能体交互的标准化。
- 所有思考、动作、观察均遵循固定标签格式(如
四、延伸分析
- 应用场景:适用于医疗健康监测、工业设备监控等需要实时数据判断与自动化响应的场景,通过多层工具调用实现复杂逻辑。
- 可扩展性:若新增检测类型(如血氧),只需创建新子智能体并定义其工具集,无需修改主智能体核心逻辑。
- 局限性:依赖预设医学阈值和工具能力,无法处理未定义场景(如罕见病指标),且缺乏动态学习能力(如用户个性化基线分析)。
通过上述机制,cangjiemagic 框架实现了基于规则和工具链的分层智能处理,适用于需要结构化流程的监测与响应场景。
结束语
CangjieMagic智能体框架提供了常用的智能体模版,对我们学习智能体的工作机制还是非常有帮助的。今天先分享到这里,后续还会进一步研究,欢迎交流。