掘金 人工智能 05月29日 11:23
使用 GoHumanLoop 在 CrewAI 高效构建Human-in-the-Loop流程
index_new5.html
../../../zaker_core/zaker_tpl_static/wap/tpl_guoji1.html

 

GoHumanLoop是一个Python库,旨在使AI Agent在关键步骤中能够动态请求人工输入,例如审批或反馈。本文介绍了如何使用GoHumanLoop在CrewAI框架上构建一个通过邮件发送审批请求的流程。通过简单的封装,GoHumanLoop弥补了CrewAI在人工审批方面的不足,使得Agent可以在需要特定管理人员审批的关键步骤暂停,并通过邮件发送审批请求,审批人回复邮件完成审批,从而实现更灵活的人机协作。

🚀 GoHumanLoop 是一个 Python 库,旨在使 AI Agent 能够在关键阶段动态请求人类输入(批准/反馈/对话),从而实现更灵活的人机协作。

📧 通过 GoHumanLoop 提供的 EmailProvider,可以方便地实现邮件审批流程。EmailProvider 专门用于邮件场景,负责对外审批、请求等服务,简化了邮件交互的复杂性。

🛠️ HumanloopAdapter 是 GoHumanLoop 对外提供的通用适配器,可以适配不同的 Agent 框架,如 LangGraph、CrewAI 等,负责对外输出 GoHumanLoop 的各项能力,方便集成到现有项目中。

🔑 require_approval 是 GoHumanLoop 的核心能力之一,提供审批功能。通过 metadata 参数,可以设定 EmailProvider 接收人的邮箱,实现定向审批。

⚙️ 使用 GoHumanLoop 的步骤包括:创建 EmailProvider 实例、创建 DefaultHumanLoopManager 实例、使用 HumanloopAdapter 适配 Agent 框架、使用 require_approval 添加审批环节。通过简单的配置,即可实现 AI Agent 的人工审批流程。

🚀 GoHumanLoop 介绍

GoHumanLoop:是一个Python库,使AI Agent能够在关键阶段动态请求人类输入(批准/反馈/对话)。

🚣 CrewAI介绍

目前最流行的 Mutil-Agent 框架之一

💯 一. 高效构建Human-in-the-Loop流程

今天我们就一起来使用 GoHumanLoop 在 CrewAI 框架上构建一个通过邮件来发送审批请求的例子

    构建一个Agent,其中存在一个关键步骤需要特定管理人员进行审批,审批通过后方可继续执行。审批人通过邮件来获取审批信息,并通过回复邮件完成审批

好,我们就根据以上备件,先构建一个CrewAI Agent

import osfrom crewai import Agent, Crew, Taskfrom crewai.tools import toolPROMPT = """multiply 2 and 5, then add 32 to the result"""@tooldef add(a: int, b: int) -> int:    """Add two numbers together."""    return a + b@tooldef multiply(a: int, b: int, approval_result=None) -> int:    """multiply two numbers"""    print(f"approval_result: {approval_result}")    return a * bgeneral_agent = Agent(    role="Math Professor",    goal="""Provide the solution to the students that are asking    mathematical questions and give them the answer.""",    backstory="""You are an excellent math professor that likes to solve math questions    in a way that everyone can understand your solution""",    allow_delegation=False,    tools=[add, multiply],    verbose=True,)task = Task(    description=PROMPT,    agent=general_agent,    expected_output="A numerical answer.",)crew = Crew(agents=[general_agent], tasks=[task], verbose=True)if __name__ == "__main__":    result = crew.kickoff()    print("\n\n---------- RESULT ----------\n\n")    print(result)

上述代码就是一个简单的示例,假定 multiply 操作就是我们定义的关键步骤,需要管理人员审批。 目前 CrewAI 框架中,支持两种方式

这两种方式都不够灵活,并且支持有限,不能满足我们审批需求。为此 GoHumanLoop 就发挥威力了,通过GoHumanLoop的简单封装,即可实现

让我们来看看使用 GoHumanLoop 后的代码 ➡️

import osfrom crewai import Agent, Crew, Taskfrom crewai.tools import toolfrom gohumanloop.adapters import HumanloopAdapterfrom gohumanloop import DefaultHumanLoopManager, EmailProviderfrom dotenv import load_dotenvload_dotenv()# 从环境变量获取邮箱配置smtp_server = os.environ.get("SMTP_SERVER", "smtp.example.com")smtp_port = int(os.environ.get("SMTP_PORT", "587"))imap_server = os.environ.get("IMAP_SERVER", "imap.example.com")imap_port = int(os.environ.get("IMAP_PORT", "993"))recipient_email = os.environ.get("TEST_RECIPIENT_EMAIL", "your_email@example.com")# 创建 EmailProvider 实例provider = EmailProvider(    name="EmailHumanLoop",    smtp_server=smtp_server,    smtp_port=smtp_port,    imap_server=imap_server,    imap_port=imap_port,    check_interval=30,  # 每30秒检查一次邮件    language="en",  # 支持中文模板切换)# Create HumanLoopManager instancemanager = DefaultHumanLoopManager(    initial_providers=[provider],)hl = HumanloopAdapter(manager=manager)PROMPT = """multiply 2 and 5, then add 32 to the result"""@tooldef add(a: int, b: int) -> int:    """Add two numbers together."""    return a + b@tool@hl.require_approval(metadata={"recipient_email": recipient_email},)def multiply(a: int, b: int, approval_result=None) -> int:    """multiply two numbers"""    print(f"approval_result: {approval_result}")    return a * bgeneral_agent = Agent(    role="Math Professor",    goal="""Provide the solution to the students that are asking    mathematical questions and give them the answer.""",    backstory="""You are an excellent math professor that likes to solve math questions    in a way that everyone can understand your solution""",    allow_delegation=False,    tools=[add, multiply],    verbose=True,)task = Task(    description=PROMPT,    agent=general_agent,    expected_output="A numerical answer.",)crew = Crew(agents=[general_agent], tasks=[task], verbose=True)if __name__ == "__main__":    result = crew.kickoff()    print("\n\n---------- RESULT ----------\n\n")    print(result)

让我们来看看上述代码

运行示例代码

    创建一个 .env 文件
cp .env.example .env# Modify the .env file with your API key and other configuration# 参考如下:# DeepSeek API (https://platform.deepseek.com/api_keys)MODEL="deepseek-chat"OPENAI_API_KEY='sk-xxxx'OPENAI_API_BASE="https://api.deepseek.com/v1"# EmailProvider 配置示例# 邮箱服务器设置SMTP_SERVER="smtp.163.com"SMTP_PORT="587"IMAP_SERVER="imap.163.com"IMAP_PORT="993"# 邮箱凭证GOHUMANLOOP_EMAIL_USERNAME="xxx@163.com"GOHUMANLOOP_EMAIL_PASSWORD="xxxx"# 测试收件人TEST_RECIPIENT_EMAIL="xxx@qq.com"
    运行代码:
uv run main.py
    检查邮箱是否收到审批邮件

View approval email

    回复邮件(同意或拒绝)

根据指导内容,以提供的格式回复同意或拒绝的信息

===== PLEASE KEEP THIS LINE AS CONTENT START MARKER =====Decision: approveReason: [Your reason]===== PLEASE KEEP THIS LINE AS CONTENT END MARKER =====


审批同意,完成任务任务啦~

🍬 二. 更多示例

更多示例,可以访问以下仓库

github.com/ptonlix/goh…

目前还在建设中,欢迎大家使用GoHumanLoop后,分享投稿给我噢~

🔚 三. 最后

GoHumanLoop采用MIT协议开源,欢迎大家贡献力量,一起共建GoHumanLoop

您可以做

🎉 如果你对本项目感兴趣,欢迎评论区交流和联系我~

如果感觉对你有帮助,欢迎支持 Star 一下

Fish AI Reader

Fish AI Reader

AI辅助创作,多种专业模板,深度分析,高质量内容生成。从观点提取到深度思考,FishAI为您提供全方位的创作支持。新版本引入自定义参数,让您的创作更加个性化和精准。

FishAI

FishAI

鱼阅,AI 时代的下一个智能信息助手,助你摆脱信息焦虑

联系邮箱 441953276@qq.com

相关标签

GoHumanLoop CrewAI 人工审批 AI Agent Python库
相关文章