在代理中允许人工反馈
在最后两章中,我们介绍了ConversableAgent
类,并展示了如何使用它来创建可以完成任务的自主体(human_input_mode=NEVER
)。我们还展示了如何正确终止代理之间的对话。
但许多应用程序可能需要将人类置于与代理的循环中。例如,允许人类反馈指导代理走向正确的方向,指定目标等。在本章中,我们将展示AutoGen如何支持人类干预。
在AutoGen的ConversableAgent
中,人机交互组件位于自动回复组件的前端。它可以拦截传入的消息,并决定是将它们传递给自动回复组件还是提供人工反馈。下图展示了这一设计。
可以通过human_input_mode
参数自定义人工参与组件。我们将在以下部分中展示如何使用它。
人工输入模式
目前AutoGen支持三种人类输入模式。模式是通过ConversableAgent
的human_input_mode
参数指定的。这三种模式分别是:
NEVER
: 从不请求用户输入。TERMINATE
(默认):仅在满足终止条件时请求人工输入。请注意,在此模式下,如果人工选择拦截并回复,对话将继续,并且max_consecutive_auto_reply
使用的计数器将被重置。ALWAYS
: 总是请求人类输入,人类可以选择跳过并触发自动回复,拦截并提供反馈,或终止对话。请注意,在此模式下,基于max_consecutive_auto_reply
的终止机制将被忽略。
前面的章节已经展示了许多human_input_mode
为NEVER
时的例子。下面我们再次展示一个这样的例子,然后展示当此模式设置为ALWAYS
和TERMINATE
时的差异。
人类输入模式 = NEVER
在这种模式下,从不请求人工输入,并使用终止条件来结束。当您希望代理完全自主运作时,这种模式非常有用。
这里是一个使用此模式在两个代理之间运行一个简单的猜数字游戏的示例,终止消息被设置为检查是否为正确猜测的数字。
import os
from autogen import ConversableAgent
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={"config_list": [{"model": "gpt-4", "api_key": os.environ["OPENAI_API_KEY"]}]},
is_termination_msg=lambda msg: "53" in msg["content"], # terminate if the number is guessed by the other agent
human_input_mode="NEVER", # never ask for human input
)
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={"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="I have a number between 1 and 100. Guess it!",
)
I have a number between 1 and 100. Guess it!
--------------------------------------------------------------------------------
Is it 50?
--------------------------------------------------------------------------------
Too low.
--------------------------------------------------------------------------------
Is it 75?
--------------------------------------------------------------------------------
Too high.
--------------------------------------------------------------------------------
Is it 63?
--------------------------------------------------------------------------------
Too high.
--------------------------------------------------------------------------------
Is it 57?
--------------------------------------------------------------------------------
Too high.
--------------------------------------------------------------------------------
Is it 54?
--------------------------------------------------------------------------------
Too high.
--------------------------------------------------------------------------------
Is it 52?
--------------------------------------------------------------------------------
Too low.
--------------------------------------------------------------------------------
Is it 53?
--------------------------------------------------------------------------------
太好了!游戏结束了。猜测代理使用二分查找正确猜出了数字——非常高效!你可以看到对话在猜测代理说出正确数字后终止,这触发了基于消息的终止条件。
人类输入模式 = ALWAYS
在这种模式下,总是需要人工输入,人类可以选择跳过、拦截或终止对话。让我们通过玩与之前相同的游戏来看到这种模式的实际效果,这次我们作为人类参与游戏。我们将成为猜测数字的代理,并与之前拥有数字的代理进行对战。
human_proxy = ConversableAgent(
"human_proxy",
llm_config=False, # no LLM used for human proxy
human_input_mode="ALWAYS", # always ask for human input
)
# Start a chat with the agent with number with an initial guess.
result = human_proxy.initiate_chat(
agent_with_number, # this is the same agent with the number as before
message="10",
)
10
--------------------------------------------------------------------------------
Too low.
--------------------------------------------------------------------------------
79
--------------------------------------------------------------------------------
Too high.
--------------------------------------------------------------------------------
76
--------------------------------------------------------------------------------
Too high.
--------------------------------------------------------------------------------
I give up
--------------------------------------------------------------------------------
That's okay! The number I was thinking of was 53.
--------------------------------------------------------------------------------
如果你运行上面的代码,每次轮到你说话时,系统都会提示你输入一个回复。你可以看到对话中的人类并不擅长猜数字……不过,幸好autogen很友好,最后给出了数字。
人类输入模式 = TERMINATE
在此模式下,人类输入仅在满足终止条件时才会被请求。如果人类选择拦截并回复,计数器将被重置;如果人类选择跳过,将使用自动回复机制;如果人类选择终止,对话将被终止。
让我们通过再次玩同样的游戏来看看这种模式的运作,但这次猜测的代理只有两次机会来猜数字,如果它失败了,将询问人类提供反馈,而猜测的代理将再获得两次机会。如果最终猜对了数字,对话将被终止。
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={"config_list": [{"model": "gpt-4", "api_key": os.environ["OPENAI_API_KEY"]}]},
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"], # terminate if the number is guessed by the other agent
human_input_mode="TERMINATE", # ask for human input until the game is terminated
)
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={"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="I have a number between 1 and 100. Guess it!",
)
I have a number between 1 and 100. Guess it!
--------------------------------------------------------------------------------
Is it 50?
--------------------------------------------------------------------------------
>>>>>>>> USING AUTO REPLY...
Too low.
--------------------------------------------------------------------------------
Is it 75?
--------------------------------------------------------------------------------
It is too high my friend.
--------------------------------------------------------------------------------
Is it 60?
--------------------------------------------------------------------------------
>>>>>>>> USING AUTO REPLY...
Too high.
--------------------------------------------------------------------------------
Is it 55?
--------------------------------------------------------------------------------
still too high, but you are very close.
--------------------------------------------------------------------------------
Is it 52?
--------------------------------------------------------------------------------
>>>>>>>> USING AUTO REPLY...
Too low.
--------------------------------------------------------------------------------
Is it 54?
--------------------------------------------------------------------------------
Almost there!
--------------------------------------------------------------------------------
Is it 53?
--------------------------------------------------------------------------------
在之前的对话中,
- 当代理猜测“74”时,人类说“这太高了,我的朋友。”
- 当代理猜测“55”时,人类说“还是太高了,但你已经非常接近了。”
- 当代理猜测“54”时,人类说“差不多对了!”
每次带有编号的代理自动回复后,人类被要求提供反馈。一旦人类提供了反馈,计数器就会被重置。在代理正确猜中“53”后,对话终止。
总结
在本章中,我们向您展示了如何使用人机交互组件为代理提供人类反馈并终止对话。我们还向您展示了不同的人类输入模式以及它们对人机交互组件行为的影响。
下一章将全部关于代码执行器——仅次于LLM的最强大的组件。