Skip to content
24 changes: 24 additions & 0 deletions camel/agents/chat_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,10 +564,13 @@ async def _aformat_response_if_needed(
message.content = response.output_messages[0].content
self._try_format_message(message, response_format)

import threading

def step(
self,
input_message: Union[BaseMessage, str],
response_format: Optional[Type[BaseModel]] = None,
stop_event: Optional[threading.Event] = None,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks @a7m-1st ! Would it better to add this argument in __init__ instead of step?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely would make the code cleaner! Let me do that.
Thanks @Wendong-Fan .

) -> ChatAgentResponse:
r"""Executes a single step in the chat session, generating a response
to the input message.
Expand All @@ -580,6 +583,9 @@ def step(
model defining the expected structure of the response. Used to
generate a structured response if provided. (default:
:obj:`None`)
stop_event (Optional[threading.Event], optional): Event to signal
termination of the agent's operation. When set, the agent will
terminate its execution. (default: :obj:`None`)

Returns:
ChatAgentResponse: Contains output messages, a termination status
Expand Down Expand Up @@ -613,6 +619,13 @@ def step(
self._get_full_tool_schemas(),
)

# Terminate Agent if stop_event is set
if stop_event and stop_event.is_set():
# Use the _step_token_exceed to terminate the agent with reason
return self._step_token_exceed(
num_tokens, tool_call_records, "termination_triggered"
)

if tool_call_requests := response.tool_call_requests:
# Process all tool calls
for tool_call_request in tool_call_requests:
Expand Down Expand Up @@ -659,6 +672,7 @@ async def astep(
self,
input_message: Union[BaseMessage, str],
response_format: Optional[Type[BaseModel]] = None,
stop_event: Optional[threading.Event] = None,
) -> ChatAgentResponse:
r"""Performs a single step in the chat session by generating a response
to the input message. This agent step can call async function calls.
Expand All @@ -675,6 +689,9 @@ async def astep(
used to generate a structured response by LLM. This schema
helps in defining the expected output format. (default:
:obj:`None`)
stop_event (Optional[threading.Event], optional): Event to signal
termination of the agent's operation. When set, the agent will
terminate its execution. (default: :obj:`None`)

Returns:
ChatAgentResponse: A struct containing the output messages,
Expand Down Expand Up @@ -705,6 +722,13 @@ async def astep(
self._get_full_tool_schemas(),
)

# Terminate Agent if stop_event is set
if stop_event and stop_event.is_set():
# Use the _step_token_exceed to terminate the agent with reason
return self._step_token_exceed(
num_tokens, tool_call_records, "termination_triggered"
)
Comment on lines +868 to +870
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_step_token_exceed is a bit confusing since the agent is not terminated since the number of token exceeding the context windows. Could we improve this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yes thanks I forgot to mention.

@lightaime Since the function is generic, how about we rename it to _step_terminate because it terminates then returns token number and called functions?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated in #2347

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Wendong-Fan and @lightaime !


if tool_call_requests := response.tool_call_requests:
# Process all tool calls
for tool_call_request in tool_call_requests:
Expand Down
5 changes: 1 addition & 4 deletions camel/responses/agent_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,5 @@ class ChatAgentResponse(BaseModel):
@property
def msg(self):
if len(self.msgs) != 1:
raise RuntimeError(
"Property msg is only available "
"for a single message in msgs."
)
return None
return self.msgs[0]
Loading