🚀 GoHumanLoop 介绍
- GitHub地址:github.com/ptonlix/goh… 🌟🌟🌟
GoHumanLoop:是一个Python库,使AI Agent能够在关键阶段动态请求人类输入(批准/反馈/对话)。
🚣 CrewAI介绍
- 官网:www.crewai.com/
- Github:github.com/crewAIInc/c…
目前最流行的 Mutil-Agent 框架之一
💯 一. 高效构建Human-in-the-Loop流程
今天我们就一起来使用 GoHumanLoop 在 CrewAI 框架上构建一个通过邮件来发送审批请求的例子
- 需求背景:
- 构建一个Agent,其中存在一个关键步骤需要特定管理人员进行审批,审批通过后方可继续执行。
- 审批人通过邮件来获取审批信息,并通过回复邮件完成审批
好,我们就根据以上备件,先构建一个CrewAI Agent
import os
from crewai import Agent, Crew, Task
from crewai.tools import tool
PROMPT = """multiply 2 and 5, then add 32 to the result"""
@tool
def add(a: int, b: int) -> int:
"""Add two numbers together."""
return a + b
@tool
def multiply(a: int, b: int, approval_result=None) -> int:
"""multiply two numbers"""
print(f"approval_result: {approval_result}")
return a * b
general_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 框架中,支持两种方式
- 终端输入
- webhook
这两种方式都不够灵活,并且支持有限,不能满足我们审批需求。
为此 GoHumanLoop 就发挥威力了,通过GoHumanLoop的简单封装,即可实现
让我们来看看使用 GoHumanLoop 后的代码 ➡️
import os
from crewai import Agent, Crew, Task
from crewai.tools import tool
from gohumanloop.adapters import HumanloopAdapter
from gohumanloop import DefaultHumanLoopManager, EmailProvider
from dotenv import load_dotenv
load_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 instance
manager = DefaultHumanLoopManager(
initial_providers=[provider],
)
hl = HumanloopAdapter(manager=manager)
PROMPT = """multiply 2 and 5, then add 32 to the result"""
@tool
def 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 * b
general_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)
让我们来看看上述代码
EmailProvider是GoHumanLoop提供的对外审批、请求等的服务,专门用于邮件场景DefaultHumanLoopManager是GoHumanLoop默认的管理器,负责管理Provider等HumanloopAdapter是GoHumanLoop对外提供的通用适配器,可以适配不同的 Agent 框架, 如 LangGraph、CrewAI 等,负责对外输出GoHumanLoop的各项能力。require_approval是GoHumanLoop的核心能力之一,提供审批功能。metadata参数中设定好用于EmailProvider接收人的邮箱
运行示例代码
- 创建一个 .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: approve
Reason: [Your reason]
===== PLEASE KEEP THIS LINE AS CONTENT END MARKER =====
审批同意,完成任务任务啦~
🍬 二. 更多示例
更多示例,可以访问以下仓库
目前还在建设中,欢迎大家使用GoHumanLoop后,分享投稿给我噢~
🔚 三. 最后
GoHumanLoop采用MIT协议开源,欢迎大家贡献力量,一起共建GoHumanLoop
您可以做
- 报告错误
- 建议改进
- 文档贡献
- 代码贡献
...
👏👏👏
🎉 如果你对本项目感兴趣,欢迎评论区交流和联系我~
- GitHub地址:github.com/ptonlix/goh… 🌟🌟🌟
如果感觉对你有帮助,欢迎支持 Star 一下