终止代理之间的对话
在本章中,我们将探讨如何终止AutoGen代理之间的对话。
但为什么这很重要? 这是因为在任何复杂的自主工作流程中,知道何时停止工作流程至关重要。例如,当任务完成时,或者当进程消耗了足够的资源并需要停止或采用不同的策略时,比如用户干预。因此,AutoGen 原生支持几种终止对话的机制。
如何使用AutoGen控制终止?目前有两种主要的机制来控制代理之间对话的终止:
-
在
initiate_chat
中指定参数:在发起聊天时, 你可以定义决定对话何时应结束的参数。 -
配置代理以触发终止: 在定义单个代理时,您可以指定参数,这些参数允许代理基于特定(可配置)条件终止对话。
initiate_chat
中的参数
在上一章中,我们实际上已经展示了这一点,当我们使用max_turns
参数来限制轮数时。如果我们将max_turns
增加到3
,注意对话需要更多轮才能结束:
import os
from autogen import ConversableAgent
cathy = ConversableAgent(
"cathy",
system_message="Your name is Cathy and you are a part of a duo of comedians.",
llm_config={"config_list": [{"model": "gpt-4", "temperature": 0.9, "api_key": os.environ.get("OPENAI_API_KEY")}]},
human_input_mode="NEVER", # Never ask for human input.
)
joe = ConversableAgent(
"joe",
system_message="Your name is Joe and you are a part of a duo of comedians.",
llm_config={"config_list": [{"model": "gpt-4", "temperature": 0.7, "api_key": os.environ.get("OPENAI_API_KEY")}]},
human_input_mode="NEVER", # Never ask for human input.
)
result = joe.initiate_chat(cathy, message="Cathy, tell me a joke.", max_turns=2)
Cathy, tell me a joke.
--------------------------------------------------------------------------------
Sure, here's one for you:
Why don't scientists trust atoms?
Because they make up everything!
--------------------------------------------------------------------------------
Haha, that's a good one, Cathy! Okay, my turn.
Why don't we ever tell secrets on a farm?
Because the potatoes have eyes, the corn has ears, and the beans stalk.
--------------------------------------------------------------------------------
Haha, that's a great one! A farm is definitely not the place for secrets. Okay, my turn again.
Why couldn't the bicycle stand up by itself?
Because it was two-tired!
--------------------------------------------------------------------------------
result = joe.initiate_chat(
cathy, message="Cathy, tell me a joke.", max_turns=3
) # increase the number of max turns before termination
Cathy, tell me a joke.
--------------------------------------------------------------------------------
Sure, here's one for you:
Why don't scientists trust atoms?
Because they make up everything!
--------------------------------------------------------------------------------
Haha, that's a good one, Cathy! Okay, my turn.
Why don't we ever tell secrets on a farm?
Because the potatoes have eyes, the corn has ears, and the beans stalk.
--------------------------------------------------------------------------------
Haha, that's a great one! A farm is definitely not the place for secrets. Okay, my turn again.
Why couldn't the bicycle stand up by itself?
Because it was two-tired!
--------------------------------------------------------------------------------
Haha, that's a wheely good one, Cathy!
Why did the golfer bring two pairs of pants?
In case he got a hole in one!
--------------------------------------------------------------------------------
Haha, that's a perfect swing of a joke!
Why did the scarecrow win an award?
Because he was outstanding in his field!
--------------------------------------------------------------------------------
Agent-triggered termination
你也可以通过配置代理的参数来终止对话。目前,你可以配置两个参数:
max_consecutive_auto_reply
: 如果对同一发送者的自动回复次数超过阈值,此条件将触发终止。您可以使用ConversableAgent
类的max_consecutive_auto_reply
参数来自定义此值。为了实现这一点,代理会维护一个计数器,用于记录对同一发送者的连续自动回复次数。请注意,由于人为干预,此计数器可以重置。我们将在下一章中更详细地描述这一点。is_termination_msg
: 如果收到的消息满足特定条件,例如包含单词“TERMINATE”,则此条件可以触发终止。您可以通过在ConversableAgent
类的构造函数中使用is_terminate_msg
参数来自定义此条件。
使用 max_consecutive_auto_reply
在下面的例子中,让我们将max_consecutive_auto_reply
设置为1
,并注意这如何确保Joe只回复一次。
joe = ConversableAgent(
"joe",
system_message="Your name is Joe and you are a part of a duo of comedians.",
llm_config={"config_list": [{"model": "gpt-4", "temperature": 0.7, "api_key": os.environ.get("OPENAI_API_KEY")}]},
human_input_mode="NEVER", # Never ask for human input.
max_consecutive_auto_reply=1, # Limit the number of consecutive auto-replies.
)
result = joe.initiate_chat(cathy, message="Cathy, tell me a joke.")
Cathy, tell me a joke.
--------------------------------------------------------------------------------
Sure, here's one for you:
Why don't scientists trust atoms?
Because they make up everything!
--------------------------------------------------------------------------------
Haha, that's a good one, Cathy! Okay, my turn.
Why don't we ever tell secrets on a farm?
Because the potatoes have eyes, the corn has ears, and the beans stalk.
--------------------------------------------------------------------------------
Haha, that's a great one! A farm is definitely not the place for secrets. Okay, my turn again.
Why couldn't the bicycle stand up by itself?
Because it was two-tired!
--------------------------------------------------------------------------------
使用 is_termination_msg
让我们将终止消息设置为“GOOD BYE”,并查看对话如何终止。
joe = ConversableAgent(
"joe",
system_message="Your name is Joe and you are a part of a duo of comedians.",
llm_config={"config_list": [{"model": "gpt-4", "temperature": 0.7, "api_key": os.environ.get("OPENAI_API_KEY")}]},
human_input_mode="NEVER", # Never ask for human input.
is_termination_msg=lambda msg: "good bye" in msg["content"].lower(),
)
result = joe.initiate_chat(cathy, message="Cathy, tell me a joke and then say the words GOOD BYE.")
Cathy, tell me a joke and then say the words GOOD BYE.
--------------------------------------------------------------------------------
Why don't scientists trust atoms?
Because they make up everything!
GOOD BYE!
--------------------------------------------------------------------------------
注意,根据Cathy消息的内容,对话是如何结束的!
总结
在本章中,我们介绍了终止代理之间对话的机制。你可以在initiate_chat
和代理的配置中配置这些参数。
也就是说,需要注意的是,当触发终止条件时,对话可能不会立即终止。实际的终止取决于ConversableAgent
类的human_input_mode
参数。例如,当模式为NEVER
时,上述终止条件将结束对话。但当模式为ALWAYS
或TERMINATE
时,它不会立即终止。我们将在下一章中描述这种行为并解释其重要性。