ARTICLE DETAIL

资讯详情

深耕网站建设与运营推广的一线实战洞察。

CAMEL HumanToolkit 实战:为 Agent 构建人机交互(Human-in-the-Loop)双向通信通道

CAMEL HumanToolkit 实战:为 Agent 构建人机交互(Human-in-the-Loop)双向通信通道 CAMEL HumanToolkit 实战为 Agent 构建人机交互Human-in-the-Loop双向通信通道【免费下载链接】camel CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org项目地址: https://gitcode.com/GitHub_Trending/ca/camel导读本文围绕 CAMEL 项目中负责人机交互的HumanToolkit展开讲解其两个核心工具——ask_human_via_console双向问答阻塞等待用户回复与send_message_to_user单向通知不要求回复的设计意图、源码实现与调用方式并结合仓库内的可运行示例与 Workforce 中的 HITLHuman-in-the-Loop集成场景说明如何让 Agent 在执行任务中途澄清需求、请求缺失信息、汇报进度或请求决策。读完本文你将掌握在 CAMEL 的ChatAgent中挂载人机交互工具并在多智能体协作中启用人工干预的标准做法。一、HumanToolkit 是什么Agent 与人类之间的通信工具集HumanToolkit是 CAMEL 框架中面向人机交互的工具包官方 API 文档将其定位为 A class representing a toolkit for human interaction并在类的注释中特别强调This toolkit should be called to send a tidy message to the user to keep them informed.在 camel/toolkits/human_toolkit.py 的源码中该类定义为class HumanToolkit(BaseToolkit): rA class representing a toolkit for human interaction. Note: This toolkit should be called to send a tidy message to the user to keep them informed. 从源码结构看HumanToolkit直接继承自BaseToolkitcamel/toolkits/base.py。BaseToolkit是所有 CAMEL 工具包的基类它负责两件关键事情统一超时管理通过__init_subclass__在子类定义时自动为所有可调用方法套上with_timeout包装timeout默认取Constants.TIMEOUT_THRESHOLD如果方法签名中自带timeout参数则跳过自动包装定义抽象接口get_tools()是必须实现的方法返回List[FunctionTool]同时基类还提供了run_mcp_server()方法可将整个工具包以 MCP Server 方式对外暴露。在 camel/toolkits/init.py 中HumanToolkit已被顶层导出因此可以直接通过from camel.toolkits import HumanToolkit导入。官方模块总览 docs/key_modules/tools.md 将它的职责概括为A toolkit for facilitating human-in-the-loop interactions and feedback in AI systems.整个工具包只包含两个可被 Agent 调用的函数工具外加get_tools本身职责非常聚焦一个问一个说。这种极简设计让 LLM 在工具选择时几乎不会产生歧义。二、ask_human_via_console双向问答阻塞等待人类回复2.1 方法签名与语义ask_human_via_console是HumanToolkit中最重要的方法其签名与完整 docstring 如下camel/toolkits/human_toolkit.pydef ask_human_via_console(self, question: str) - str: rUse this tool to ask a question to the user when you are stuck, need clarification, or require a decision to be made. This is a two-way communication channel that will wait for the users response. You should use it to: - Clarify ambiguous instructions or requirements. - Request missing information that you cannot find (e.g., login credentials, file paths). - Ask for a decision when there are multiple viable options. - Seek help when you encounter an error you cannot resolve on your own. Args: question (str): The question to ask the user. Returns: str: The users response to the question. print(fQuestion: {question}) logger.info(fQuestion: {question}) reply input(Your reply: ) logger.info(fUser reply: {reply}) return reply2.2 参数说明参数类型是否必填说明questionstr是要向用户提出的问题工具会自动以Question: question的格式打印到控制台并同步写入日志返回值str即用户在控制台输入的原样回复文本。该返回值会作为函数调用的结果回传给 AgentAgent 据此继续后续推理与行动。2.3 底层实现原理从源码可以看到该方法是一个典型的阻塞式双通道通信print(fQuestion: {question})—— 把问题打印到标准输出控制台用户直接看到问题logger.info(...)—— 问题同时写入 CAMEL 日志默认 logger便于事后审计Agent 问了什么reply input(Your reply: )——阻塞等待用户在控制台输入在用户按下回车之前整个 Agent 的执行流程都会暂停在这里用户的回复再写入日志并作为返回值交还给 Agent。关键在于第 3 步的input()这是进程级的标准输入读取意味着使用该工具时必须运行在具备交互式控制台的环境中如终端、Jupyter/Notebook 内核进程且执行期间不能被自动化编排脚本吞掉标准输入。这也是该方法在 API 文档中被标注为two-way communication channel that will wait for the users response的原因——它是阻塞式blocking的与下面的send_message_to_user形成鲜明对比。2.4 文档推荐的使用场景API 参考文档 docs/reference/camel.toolkits.human_toolkit.md 为 LLM 定义了清晰的使用边界Agent 应在以下四种情形调用该工具澄清歧义用户指令或需求存在多种解读时先问清楚再动手请求缺失信息Agent 无法自行找到的信息例如登录凭证、文件路径直接向用户索要请求决策存在多个可行方案需要拍板时把选项抛给用户求助解围遇到自身无法解决的错误时向用户寻求帮助。三、send_message_to_user单向通知不需要用户回复3.1 方法签名与语义send_message_to_user是HumanToolkit的第二个工具用于向用户发送一句简洁、信息量足的消息camel/toolkits/human_toolkit.pydef send_message_to_user(self, message: str) - str: rUse this tool to send a tidy message to the user in one short sentence. This one-way tool keeps the user informed about your progress, decisions, or actions. It does not require a response. You should use it to: - Announce what you are about to do (e.g., I will now search for papers on GUI Agents.). - Report the result of an action (e.g., I have found 15 relevant papers.). - State a decision (e.g., I will now analyze the top 10 papers.). - Give a status update during a long-running task. Args: message (str): The tidy and informative message for the user. Returns: str: Confirmation that the message was successfully sent. print(f\nAgent Message:\n{message}) logger.info(f\nAgent Message:\n{message}) return fMessage successfully sent to user: {message}3.2 参数说明参数类型是否必填说明messagestr是要发送给用户的一句话消息要求简洁且有信息量tidy and informative返回值str即确认字符串Message successfully sent to user: message表示消息已成功送达。3.3 与 ask_human_via_console 的区别维度ask_human_via_consolesend_message_to_user通信方向双向Agent 问 → 用户答 → 回传 Agent单向Agent 通知 → 用户阅读是否阻塞是等待input()否打印即返回是否要求回复是否返回值用户输入的原始文本固定的送达确认字符串典型用途澄清、要信息、要决策、求助播报进度、汇报结果、陈述决策docstring 中给出的四类典型用法非常具体可以直接作为 system prompt 的参考示例预告行动I will now search for papers on GUI Agents.汇报结果I have found 15 relevant papers.陈述决策I will now analyze the top 10 papers.长任务进度更新长时间运行任务中定期同步状态。值得注意的是send_message_to_user同样通过print输出到控制台并写日志因此它也依赖控制台环境只是不需要用户输入不会阻塞 Agent 执行。四、get_tools把方法暴露为 LLM 可调用的 FunctionTool4.1 实现def get_tools(self) - List[FunctionTool]: rReturns a list of FunctionTool objects representing the functions in the toolkit. Returns: List[FunctionTool]: A list of FunctionTool objects representing the functions in the toolkit. return [ FunctionTool(self.ask_human_via_console), FunctionTool(self.send_message_to_user), ]4.2 FunctionTool 机制FunctionTool是 CAMEL 对LLM 可调用函数的抽象camel/toolkits/function_tool.py。默认情况下它会从函数的签名与 docstring 自动解析出 OpenAI 工具 schema对应get_openai_tool_schema(func)这意味着question: str会被解析为必填的字符串参数并带上 docstring 中的中文/英文描述帮助 LLM 理解何时调用因此HumanToolkit的 docstring 写得如此详细并非冗余——它直接决定了 LLM 在工具选择阶段的调用质量是提示工程 工具工程的结合点。FunctionTool还支持传入自定义openai_tool_schema覆盖自动解析结果也支持synthesize_schema借助辅助模型自动合成 schema等高级选项在HumanToolkit中我们使用默认行为即可。五、实战在 ChatAgent 中挂载 HumanToolkit仓库提供了开箱即用的完整示例 examples/toolkits/human_toolkit.py下面拆解其核心用法。5.1 示例 1通过 Agent 向用户提问ask_human_via_consolefrom camel.agents import ChatAgent from camel.configs import ChatGPTConfig from camel.models import ModelFactory from camel.toolkits import HumanToolkit from camel.types import ModelPlatformType, ModelType human_toolkit HumanToolkit() model ModelFactory.create( model_platformModelPlatformType.OPENAI, model_typeModelType.GPT_4O_MINI, model_config_dictChatGPTConfig( temperature0.0, ).as_dict(), ) agent ChatAgent( system_messageYou are a helpful assistant., modelmodel, tools[*human_toolkit.get_tools()], ) response agent.step( Test me on the capital of some country, and comment on my answer. )tools[*human_toolkit.get_tools()]将两个函数工具展开注册到ChatAgent。当用户要求考考我某个国家的首都Agent 会调用ask_human_via_console控制台会出现一段交互Question: What is the capital of France? Your reply: Paris Thats correct! Paris is indeed the capital of France. Would you like to try another one? Your reply: yes What is the capital of Japan? Your reply: Tokyo Thats correct! Tokyo is the capital of Japan. Would you like to continue with another question? Your reply: no可以看到Agent 的提问、用户的回答、Agent 的点评形成了一个完整的多轮对话闭环——这正是双向通信通道在真实运行时的效果。注意示例中将temperature0.0以保证行为确定性同时模型平台、模型类型可以根据你的环境替换例如改用本地 Ollama 或 DeepSeek 等对应 camel/configs 目录下的各类配置类。5.2 示例 2Agent 发送通知消息send_message_to_useragent_with_message ChatAgent( system_messageYou are an assistant that can send messages to the user., modelmodel, tools[*human_toolkit.get_tools()], ) response agent_with_message.step( Send me a notification about an upcoming meeting. )运行结果会在控制台打印Agent Message: Reminder: You have an upcoming meeting scheduled. Please check your calendar for details! Ive sent you a notification about your upcoming meeting. Please check your calendar for details!第一段是send_message_to_user的print输出第二段是 Agent 在拿到工具返回值送达确认后对用户的自然语言总结。两条路径配合正好演示了单向通知 结果汇报的完整链路。六、进阶在 Workforce 多智能体协作中启用人工干预HumanToolkit并不局限于单 Agent 场景。官方文档 docs/key_modules/workforce.md 给出了在多智能体协作Workforce中启用 HITL 的标准姿势给需要人工帮助的 Agent 挂上工具在 coordinator协调者、task agent 或任意 worker 的初始化参数中传入human_toolkit调用时机由 LLM 自主决定任何 Agent 在执行过程中都可以调用ask_human_via_console来澄清需求、请求审批、解除错误阻塞无需额外线程处理文档明确指出 No special threading is required. Agents prompt the user when they call aHumanToolkittool.——因为input()是同步阻塞的Agent 会自然停下等用户不需要复杂的并发协调。这一设计让人类可以随时插话进多智能体流水线在需要审批、存在歧义或 Worker 卡住时及时介入是实现人在环上human-in-the-loop工作流的最轻量方案。在 camel/utils/context_utils.py 中官方还将其作为任务中途与人类交互的推荐手段写入上下文构建逻辑说明 CAMEL 框架层面对人机协作路径有整体支持。七、使用注意事项与适用前提必须是交互式控制台环境两个工具分别依赖input()和print在无 TTY 的批处理/后台进程或非交互式 CI 中无法正常工作ask_human_via_console会因读不到标准输入而阻塞或报错阻塞语义要写进提示词由于ask_human_via_console会暂停 Agent 执行建议在 system prompt 中约束 Agent 只在确实需要澄清/决策/求助时才调用避免频繁打断自动化流程消息要简洁send_message_to_user的设计哲学是one short sentence应引导 Agent 输出干净、单句、信息完整的中文或英文消息便于用户快速阅读继承自 BaseToolkit 的通用能力HumanToolkit()构造时同样支持timeout参数须为正数否则BaseToolkit.__init__会抛出ValueError且所有方法都会自动获得超时保护如需以 MCP 方式对外暴露整个人机交互工具包可直接调用继承自基类的run_mcp_server()。结语HumanToolkit虽然只有两个方法却是 CAMEL 将人类接入 Agent 执行回路的关键桥梁ask_human_via_console提供了双向、阻塞、等待回复的提问通道send_message_to_user提供了单向、即时的通知通道二者通过get_tools()自动转换为 LLM 可调用的 FunctionTool。结合 examples/toolkits/human_toolkit.py 的单 Agent 示例与 docs/key_modules/workforce.md 的 Workforce HITL 方案你可以快速构建出遇到歧义先问人、长任务定时汇报、多 Agent 协作随时人工介入的可靠人机协同系统。更多工具包的整体能力地图可参考 docs/key_modules/tools.md其 API 参考文档见 docs/reference/camel.toolkits.human_toolkit.md。【免费下载链接】camel CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org项目地址: https://gitcode.com/GitHub_Trending/ca/camel创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表