原因:
在自动化或人工智能系统中,人类用户参与决策过程,与代理(如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
运行逻辑解释
- 初始化配置:首先配置LLM的API密钥和模型信息。
- 创建智能体:
agent_with_number
:持有数字53,并根据猜测结果回复“too high”或“too low”。agent_guess_number
:根据回复逐步猜测数字。
- 启动对话:
agent_with_number
发起对话,agent_guess_number
开始猜测。 - 自动回复与人类输入:通过设置
max_consecutive_auto_reply
和human_input_mode
,控制智能体的自动回复和请求人类输入的行为。
通过以上步骤,我们实现了一个简单的猜数字游戏,展示了如何使用autogen
库创建交互式智能体。希望这个教程对您有所帮助!
参考链接:https://microsoft.github.io/autogen/0.2/docs/tutorial/human-in-the-loop
如果有任何问题,欢迎在评论区提问。