r"""Executes a single step in the chat session, generating a response to the input message. Args: input_message (Union[BaseMessage, str]): The input message for the agent. If provided as a BaseMessage, the `role` is adjusted to `user` to i
(
self,
input_message: Union[BaseMessage, str],
response_format: Optional[Type[BaseModel]] = None,
)
| 548 | self.update_memory(message, OpenAIBackendRole.ASSISTANT) |
| 549 | |
| 550 | def step( |
| 551 | self, |
| 552 | input_message: Union[BaseMessage, str], |
| 553 | response_format: Optional[Type[BaseModel]] = None, |
| 554 | ) -> ChatAgentResponse: |
| 555 | r"""Executes a single step in the chat session, generating a response |
| 556 | to the input message. |
| 557 | |
| 558 | Args: |
| 559 | input_message (Union[BaseMessage, str]): The input message for the |
| 560 | agent. If provided as a BaseMessage, the `role` is adjusted to |
| 561 | `user` to indicate an external message. |
| 562 | response_format (Optional[Type[BaseModel]], optional): A Pydantic |
| 563 | model defining the expected structure of the response. Used to |
| 564 | generate a structured response if provided. (default: |
| 565 | :obj:`None`) |
| 566 | |
| 567 | Returns: |
| 568 | ChatAgentResponse: Contains output messages, a termination status |
| 569 | flag, and session information. |
| 570 | """ |
| 571 | |
| 572 | if ( |
| 573 | self.model_backend.model_config_dict.get("response_format") |
| 574 | and response_format |
| 575 | ): |
| 576 | raise ValueError( |
| 577 | "The `response_format` parameter cannot be set both in " |
| 578 | "the model configuration and in the ChatAgent step." |
| 579 | ) |
| 580 | |
| 581 | self.original_model_dict = self.model_backend.model_config_dict |
| 582 | model_response_format_modified = False |
| 583 | if ( |
| 584 | response_format |
| 585 | and self.model_type.support_native_structured_output |
| 586 | ): |
| 587 | self.model_backend.model_config_dict = ( |
| 588 | self.original_model_dict.copy() |
| 589 | ) |
| 590 | self.model_backend.model_config_dict["response_format"] = ( |
| 591 | response_format |
| 592 | ) |
| 593 | model_response_format_modified = True |
| 594 | |
| 595 | # Convert input message to BaseMessage if necessary |
| 596 | if isinstance(input_message, str): |
| 597 | input_message = BaseMessage.make_user_message( |
| 598 | role_name='User', content=input_message |
| 599 | ) |
| 600 | |
| 601 | # Handle tool prompt injection if needed |
| 602 | if ( |
| 603 | self.is_tools_added() |
| 604 | and not self.model_type.support_native_tool_calling |
| 605 | and not self.tool_prompt_added |
| 606 | ): |
| 607 | self._inject_tool_prompt() |
no test coverage detected