-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat: add termination parameter to ChatAgent step and astep #2285
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
6942b79
6a96e91
ceb3ec4
8d8fab2
9c09814
1df5ebe
ae419f2
0990d8a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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, | ||
| ) -> ChatAgentResponse: | ||
| r"""Executes a single step in the chat session, generating a response | ||
| to the input message. | ||
|
|
@@ -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 | ||
|
|
@@ -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: | ||
|
|
@@ -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. | ||
|
|
@@ -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, | ||
|
|
@@ -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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updated in #2347
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||
|
|
||
There was a problem hiding this comment.
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 ofstep?There was a problem hiding this comment.
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 .