Termination
Termination conditions in AgentOpera provide flexible mechanisms to control when a conversation should end, whether it's because a goal has been reached, a certain limit has been hit, or a specific message pattern has been detected.
Overview
A termination condition in AgentOpera is a stateful callable object that:
Takes a sequence of messages as input
Determines whether the conversation should be terminated
Built-in Termination Conditions
AgentOpera provides several built-in termination conditions to cover common scenarios:
MaxMessageTermination
MaxMessageTermination
Terminates the conversation after a maximum number of messages have been exchanged:
from agentopera.chatflow.conditions import MaxMessageTermination
# Terminate after 10 messages
termination = MaxMessageTermination(max_messages=10, include_agent_event=False)
The include_agent_event
parameter specifies whether AgentEvent
objects are included in the message count.
TextMentionTermination
TextMentionTermination
Terminates the conversation if specific text is mentioned in a message:
from agentopera.chatflow.conditions import TextMentionTermination
# Terminate when "DONE" is mentioned in any message
termination = TextMentionTermination(text="DONE")
# Terminate when "STOP" is mentioned, but only from specific agents
termination = TextMentionTermination(text="STOP", sources=["agent1", "agent2"])
TimeoutTermination
TimeoutTermination
Terminates the conversation after a specified duration:
from agentopera.chatflow.conditions import TimeoutTermination
# Terminate after 60 seconds
termination = TimeoutTermination(timeout_seconds=60)
TextMessageTermination
TextMessageTermination
Terminates the conversation when a TextMessage
is received:
from agentopera.chatflow.conditions import TextMessageTermination
# Terminate on any TextMessage
termination = TextMessageTermination()
# Terminate on TextMessage from a specific source
termination = TextMessageTermination(source="user")
Combining Termination Conditions
AgentOpera’s termination system allows you to combine conditions using logical operators:
OR Combination
Terminate when ANY of the conditions are met:
from agentopera.chatflow.conditions import MaxMessageTermination, TextMentionTermination
# Terminate after 20 messages OR when "DONE" is mentioned
termination = MaxMessageTermination(20) | TextMentionTermination("DONE")
AND Combination
Terminate when ALL of the conditions are met:
from agentopera.chatflow.conditions import MaxMessageTermination, TextMentionTermination
# Terminate only if 10 messages have been sent AND ‘READY’ has been mentioned.
termination = MaxMessageTermination(10) & TextMentionTermination("READY")
Complete Example
Here's a complete example demonstrating how to use termination conditions with an agent team:
import asyncio
from agentopera.chatflow.conditions import MaxMessageTermination, TextMentionTermination
from agentopera.chatflow.models import OpenAIChat
from agentopera.chatflow.agents import ChatAgent
from agentopera.chatflow.teams import RoundRobinTeam
async def main():
# Create LLM model
llm = OpenAIChat(model="gpt-3.5-turbo")
# Create agents
researcher = ChatAgent(
name="researcher",
llm=llm,
system_message="You are a research assistant who finds information on topics."
)
writer = ChatAgent(
name="writer",
llm=llm,
system_message="You are a writer who crafts clear explanations based on research."
)
# Combine termination conditions
termination = MaxMessageTermination(max_messages=10) | TextMentionTermination("TASK COMPLETE")
# Create team
team = RoundRobinTeam(
agents=[researcher, writer],
termination_condition=termination
)
# Run conversation
result = await team.run(task="Research and explain quantum computing in simple terms.")
# Print results
for message in result.messages:
print(f"{message.source}: {message.content}")
print(f"Termination reason: {result.stop_reason}")
if __name__ == "__main__":
asyncio.run(main())
Last updated