3. Autogen官网教程 (Allowing Human Feedback in Agents)

原因:

在自动化或人工智能系统中,人类用户参与决策过程,与代理(如AI或机器人)一起工作,以提高效率、准确性或提供更复杂的决策支持。这种模式在需要人类判断、直觉或道德考量的情况下特别有用,因为代理可以处理大量数据或执行重复性任务,而人类则监督、指导或干预以确保结果符合预期目标和标准。

本教程将介绍如何使用autogen库创建一个猜数字游戏。游戏中包含两个智能体:一个持有数字的智能体(agent_with_number)和一个猜测数字的智能体(agent_guess_number)。我们将通过代码示例和运行逻辑解释,帮助您理解Human Feedback in Agents的运作过程。

1. 导入必要的库

import os
from autogen import ConversableAgent

2. 配置LLM

llm_config = {
    "config_list": [{
        "model": "GLM-4-Plus",
        "api_key": "your api key",
        "base_url": "https://open.bigmodel.cn/api/paas/v4/"
    }]
}

3. 创建智能体

持有数字的智能体
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'. ",
    llm_config=llm_config,
    is_termination_msg=lambda msg: "53" in msg["content"],  # 如果猜中数字则终止
    human_input_mode="NEVER",  # 从不请求人类输入
)
猜测数字的智能体
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",
)

4. 启动对话

result = agent_with_number.initiate_chat(
    agent_guess_number,
    message="I have a number between 1 and 100. Guess it!",
)

5. 运行结果

以下是程序运行的部分输出:

[33magent_with_number[0m (to agent_guess_number):
I have a number between 1 and 100. Guess it!

--------------------------------------------------------------------------------
[33magent_guess_number[0m (to agent_with_number):
Great! Let's start with 50. Is 50 the number you're thinking of?

--------------------------------------------------------------------------------
[33magent_with_number[0m (to agent_guess_number):
too low

--------------------------------------------------------------------------------
[33magent_guess_number[0m (to agent_with_number):
Alright, let's try a higher number. Is 75 the number you have in mind?

--------------------------------------------------------------------------------
[33magent_with_number[0m (to agent_guess_number):
too high

--------------------------------------------------------------------------------
[33magent_guess_number[0m (to agent_with_number):
Got it, we're narrowing it down. How about 62? Is 62 the number you're thinking of?

--------------------------------------------------------------------------------
[33magent_with_number[0m (to agent_guess_number):
too high

--------------------------------------------------------------------------------
[33magent_guess_number[0m (to agent_with_number):
Understood. Let's go a bit lower. Is 56 the number you have in mind?

--------------------------------------------------------------------------------
[33magent_with_number[0m (to agent_guess_number):
too high

--------------------------------------------------------------------------------
[33magent_guess_number[0m (to agent_with_number):
Alright, we're getting closer. Let's try 53. Is 53 the number you're thinking of?

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

6. 人类代理智能体

我们可以创建一个人类代理智能体,让人类参与游戏。

human_proxy = ConversableAgent(
    "human_proxy",
    llm_config=False,  # 不使用LLM
    human_input_mode="ALWAYS",  # 总是请求人类输入
)

result = human_proxy.initiate_chat(
    agent_with_number,
    message="10",
)

7. 运行结果

以下是程序运行的部分输出:

[33mhuman_proxy[0m (to agent_with_number):
10

--------------------------------------------------------------------------------
[33magent_with_number[0m (to human_proxy):
too low

--------------------------------------------------------------------------------
[33mhuman_proxy[0m (to agent_with_number):
50

--------------------------------------------------------------------------------
[33magent_with_number[0m (to human_proxy):
too low

--------------------------------------------------------------------------------
[33mhuman_proxy[0m (to agent_with_number):
53

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

8. 自动回复与人类输入

我们可以设置智能体在特定条件下自动回复,并在需要时请求人类输入。

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,  # 最大连续自动回复次数
    is_termination_msg=lambda msg: "53" in msg["content"],  # 如果猜中数字则终止
    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",
)

result = agent_with_number.initiate_chat(
    agent_guess_number,
    message="I have a number between 1 and 100. Guess it!",
)

9. 运行结果

以下是程序运行的部分输出:

[33magent_with_number[0m (to agent_guess_number):
I have a number between 1 and 100. Guess it!

--------------------------------------------------------------------------------
[33magent_guess_number[0m (to agent_with_number):
Great! Let's start with 50. Is 50 the number you're thinking of?

--------------------------------------------------------------------------------
[31m
>>>>>>>> USING AUTO REPLY...[0m
[33magent_with_number[0m (to agent_guess_number):
too low

--------------------------------------------------------------------------------
[33magent_guess_number[0m (to agent_with_number):
Alright, let's try a higher number. Is 75 the number you have in mind?

--------------------------------------------------------------------------------
[33magent_with_number[0m (to agent_guess_number):
still too high

--------------------------------------------------------------------------------
[33magent_guess_number[0m (to agent_with_number):
Understood. Let's go with a number between 50 and 75. Is 62 the number you're thinking of?

--------------------------------------------------------------------------------
[31m
>>>>>>>> USING AUTO REPLY...[0m
[33magent_with_number[0m (to agent_guess_number):
too low

--------------------------------------------------------------------------------
[33magent_guess_number[0m (to agent_with_number):
Got it. We're narrowing it down. Let's try a number between 62 and 75. Is 68 the number you have in mind?

--------------------------------------------------------------------------------
[33magent_with_number[0m (to agent_guess_number):
No, it's between 50 and 55

--------------------------------------------------------------------------------
[33magent_guess_number[0m (to agent_with_number):
Thanks for the hint! Let's try a number in that range. Is 53 the number you're thinking of?

--------------------------------------------------------------------------------
[31m
>>>>>>>> NO HUMAN INPUT RECEIVED.[0m

运行逻辑解释

  1. 初始化配置:首先配置LLM的API密钥和模型信息。
  2. 创建智能体
    • agent_with_number:持有数字53,并根据猜测结果回复“too high”或“too low”。
    • agent_guess_number:根据回复逐步猜测数字。
  3. 启动对话agent_with_number发起对话,agent_guess_number开始猜测。
  4. 自动回复与人类输入:通过设置max_consecutive_auto_replyhuman_input_mode,控制智能体的自动回复和请求人类输入的行为。

通过以上步骤,我们实现了一个简单的猜数字游戏,展示了如何使用autogen库创建交互式智能体。希望这个教程对您有所帮助!

参考链接:https://microsoft.github.io/autogen/0.2/docs/tutorial/human-in-the-loop
如果有任何问题,欢迎在评论区提问。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值