autogen 人工输入模式

一、Allowing Human Feedback in Agents 允许代理中的人类反馈

  1. 发起聊天 (initiate_chat)
    功能:用于启动对话过程。
    参数:max_turns:限制对话的最大回合数。如果设置为3,意味着对话将在第三个回合后自动终止,除非提前满足其他终止条件。
  2. 最大连续自动回复 (max_consecutive_auto_reply)
    功能:控制在没有人工干预的情况下,系统可以连续自动回复的最大次数。此参数有助于防止对话陷入无限循环,并确保在适当的时候请求人工输入。
  3. 终止消息 (is_termination_msg)
    功能:定义一个条件,当收到的消息满足该条件时,对话将立即终止。
    自定义:可以通过 is_terminate_msg 类的构造函数中的参数来定制触发终止的具体条件。例如,如果消息中包含特定关键词(如"TERMINATE"),则会触发终止。

二、Human Input Modes

AutoGen 的 human_input_mode 通过 ConversableAgent 支持三种人工输入模式:

NEVER:系统完全自主,不请求人工输入。

TERMINATE(默认):仅在满足终止条件时请求人工输入,人类可以拦截并重置自动回复计数max_consecutive_auto_reply

ALWAYS:每次交互都请求人工输入,人类可选择跳过、拦截或终止对话,忽略max_consecutive_auto_reply基于连续自动回复次数的终止规则。

三、Human Input Mode = NEVER 系统自主

在这种模式下,系统完全自主运行,不会请求任何人工输入或干预。所有操作和决策均由代理根据预设规则和逻辑自行完成。这种设置非常适合不需要人类介入、期望自动化处理的任务场景。
示例:两个代理之间的猜数字游戏

  • 设定:两个代理进行互动,其中一个代理选择一个随机数字,另一个代理尝试猜测这个数字。

  • 终止条件:当猜测的数字正确时,发出特定的终止消息来结束游戏。

  • 运行方式:在整个游戏过程中,不会有人工输入的机会;代理将根据内置逻辑自动进行猜测,直到满足终止条件(即猜中数字)为止。

    bash 复制代码
    import os
    
    from autogen import ConversableAgent
    
    # 创建一个持有数字53的代理,用于玩猜数字游戏
    agent_with_number = ConversableAgent(
        "agent_with_number",  # 代理的名称
        system_message="You are playing a game of guess-my-number. You have the number 53 in your mind, and I will try to guess it. If I guess too high, say 'too high', if I guess too low, say 'too low'. ",
        # system_message="你正在玩一个猜数字的游戏。 在第一轮游戏中,你心中的数字是53,我将尝试猜测这个数字。 如果我猜的数字太高,请说'太高了';如果我猜的数字太低,请说'太低了'。",
        llm_config=llm_config,  # 配置使用的语言模型和API密钥
        max_consecutive_auto_reply=5,  # 设置最大连续自动回复次数,在此之后会请求人工输入
        is_termination_msg=lambda msg: "53" in msg["content"],  # 如果猜中数字则终止
        human_input_mode="TERMINATE",  # 游戏终止前需要人工输入
    )
    
    
    # 创建一个猜测数字的代理,用于尝试猜出对方的数字
    agent_guess_number = ConversableAgent(
        "agent_guess_number",  # 代理的名称
        system_message="我脑子里有一个数字,你会试着猜出来的。如果我说'太高',你应该猜一个较低的数字。如果我说太低,你应该猜一个更高的数字。",  # 系统消息,定义代理的行为
        llm_config={"config_list": [{"model": "gpt-4", "api_key": os.environ["OPENAI_API_KEY"]}]},
        human_input_mode="NEVER",  # 不要求人工干预
    )
    
    # 开始对话,由持有数字的代理发起
    result = agent_with_number.initiate_chat(
        agent_guess_number,  # 对话的另一方是猜测数字的代理
        message="我有一个介于1和100之间的数字。猜猜看!",  # 初始消息,告知对方数字范围并邀请猜测
    )

四、Human Input Mode = ALWAYS 人工输入模式 = ALWAYS

在这种模式下,系统在每次交互时都会主动请求人工输入,适合那些需要人类监督或决策的任务场景

人类用户可以:

跳过:允许系统继续进行自动回复。
拦截:提供反馈或修改系统的行为。
终止:立即结束对话。

bash 复制代码
# 创建一个代表人类代理的ConversableAgent实例,名为"human_proxy"
# llm_config=False 表示这个代理不会使用语言模型(LLM)进行响应
# human_input_mode="ALWAYS" 意味着这个代理在任何时候都会请求人类输入
human_proxy = ConversableAgent(
    "human_proxy",
    llm_config=False,  # 不使用LLM
    human_input_mode="ALWAYS",  # 总是请求人类输入
)

# 使用数字代理开始聊天,并以"10"作为初始消息。
# agent_with_number 是之前定义的、持有某个数字的代理
result = human_proxy.initiate_chat(
    agent_with_number,  # 与之聊天的代理,它持有一个数字
    message="10",  # 初始猜测的消息
)
bash 复制代码
用户可以进行人为干预human_proxy:比如输入数字,比如太高系统会自动给出另一个数字回答;可以退出

human_proxy (to agent_with_number):  解释:初始数字 10

10

--------------------------------------------------------------------------------

>>>>>>>> USING AUTO REPLY...
agent_with_number (to human_proxy): 解释:数字agent 太小啦

Too low.

--------------------------------------------------------------------------------
Replying as human_proxy. Provide feedback to agent_with_number. Press enter to skip and use auto-reply, or type 'exit' to end the conversation: 50
human_proxy (to agent_with_number):
解释:向数字agent提供反馈。然后按enter键自动回复;输入exit 结束对话;
本人输入50,回车

50

--------------------------------------------------------------------------------

>>>>>>>> USING AUTO REPLY...
agent_with_number (to human_proxy): 解释:数字agent 太小啦

Too low.

--------------------------------------------------------------------------------
Replying as human_proxy. Provide feedback to agent_with_number. Press enter to skip and use auto-reply, or type 'exit' to end the conversation: 

>>>>>>>> NO HUMAN INPUT RECEIVED.

>>>>>>>> USING AUTO REPLY...
human_proxy (to agent_with_number):



--------------------------------------------------------------------------------

>>>>>>>> USING AUTO REPLY...
agent_with_number (to human_proxy):

Too high.


--------------------------------------------------------------------------------
Replying as human_proxy. Provide feedback to agent_with_number. Press enter to skip and use auto-reply, or type 'exit' to end the conversation: 53
human_proxy (to agent_with_number): 解释:我又输入 53;

53

--------------------------------------------------------------------------------
Please give feedback to human_proxy. Press enter or type 'exit' to stop the conversation:  
解释:本人猜对了,告诉human_proxy反馈。按enter或键入"exit"停止对话

>>>>>>>> NO HUMAN INPUT RECEIVED. # 未收到人工输入 输出结果
ChatResult(chat_id=None, chat_history=[{'content': '10', 'role': 'assistant', 'name': 'human_proxy'}, {'content': 'Too low.', 'role': 'user', 'name': 'agent_with_number'}, {'content': '50', 'role': 'assistant', 'name': 'human_proxy'}, {'content': 'Too low.', 'role': 'user', 'name': 'agent_with_number'}, {'content': '53', 'role': 'assistant', 'name': 'human_proxy'}], summary='53', cost={'usage_including_cached_inference': {'total_cost': 0.000845, 'gpt-4o-2024-05-13': {'cost': 0.000845, 'prompt_tokens': 151, 'completion_tokens': 6, 'total_tokens': 157}}, 'usage_excluding_cached_inference': {'total_cost': 0.00047, 'gpt-4o-2024-05-13': {'cost': 0.00047, 'prompt_tokens': 85, 'completion_tokens': 3, 'total_tokens': 88}}}, human_input=['50', '53'])

五、Human Input Mode = TERMINATE 人机输入模式

请求人工输入:仅当满足终止条件时。
拦截并回复:人类介入并提供新输入,计数器重置。
跳过:使用自动回复机制继续,计数器重置。
终止:人类选择立即结束对话。

这种方式展示了 TERMINATE 模式下,系统可以在特定条件下请求人类帮助,确保对话可以根据需要灵活调整,同时保留自动化处理的能力。

bash 复制代码
# 定义一个具有数字猜测功能的对话代理
agent_with_number = ConversableAgent(
    "agent_with_number",
    system_message="You are playing a game of guess-my-number. "
    "In the first game, you have the "
    "number 53 in your mind, and I will try to guess it. "
    "If I guess too high, say 'too high', if I guess too low, say 'too low'. ",
    llm_config=llm_config,
    max_consecutive_auto_reply=1,  # maximum number of consecutive auto-replies before asking for human input
    is_termination_msg=lambda msg: "53" in msg["content"],  # 当消息中包含数字53时,认为游戏结束
    human_input_mode="TERMINATE",  # 游戏终止前需要人工输入
)

# 定义一个用于猜测数字的对话代理
agent_guess_number = ConversableAgent(
    "agent_guess_number",
    system_message="I have a number in my mind, and you will try to guess it. "
    "If I say 'too high', you should guess a lower number. If I say 'too low', "
    "you should guess a higher number. ",
    llm_config=llm_config,
    human_input_mode="NEVER", # 从不需要人工输入
)

# 开始对话,由agent_with_number发起,并向agent_guess_number发送第一条消息
result = agent_with_number.initiate_chat(
    agent_guess_number,
    message="I have a number between 1 and 100. Guess it!",
)
相关推荐
数据智能老司机3 小时前
精通 Python 设计模式——分布式系统模式
python·设计模式·架构
数据智能老司机4 小时前
精通 Python 设计模式——并发与异步模式
python·设计模式·编程语言
数据智能老司机4 小时前
精通 Python 设计模式——测试模式
python·设计模式·架构
数据智能老司机4 小时前
精通 Python 设计模式——性能模式
python·设计模式·架构
c8i4 小时前
drf初步梳理
python·django
每日AI新事件4 小时前
python的异步函数
python
这里有鱼汤5 小时前
miniQMT下载历史行情数据太慢怎么办?一招提速10倍!
前端·python
databook14 小时前
Manim实现脉冲闪烁特效
后端·python·动效
程序设计实验室15 小时前
2025年了,在 Django 之外,Python Web 框架还能怎么选?
python
倔强青铜三16 小时前
苦练Python第46天:文件写入与上下文管理器
人工智能·python·面试