Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Python: Update sort step method for assistant invoke. (#10191)
### Motivation and Context At times, when the (Azure) OpenAI Assistant Agent makes a tool call, that tool call's creation timestamp is coming after the message creation timestamp (the message creation being the text that the assistant responds with). Currently in our code, if we have a tool call (`FunctionCallContent`), we first yield that, and then we make a call to get completed steps, to then yield more content like `FunctionResultContent` and `TextContent`. There will be two steps (or more, depending upon the number of tool calls). Right now we sort the completed steps in this way: ```python completed_steps_to_process: list[RunStep] = sorted( [s for s in steps if s.completed_at is not None and s.id not in processed_step_ids], key=lambda s: s.created_at, ) ``` When there are no failures, it's because the tool call was created before the final message content (as has been since this assistant was first coded). However, it appears that processing on the server-side can cause fluctuations in when the steps are created/processed. When we have a failure, we now have the message_creation (`TextContent`) yielded before the `FunctionResultContent`, which if sent to an (Azure) OpenAI Chat Completion endpoint will break with a 400 because of the gap in the ordering between the `FunctionCallContent` and the `FunctionResultContent`: ``` FunctionCallContent TextContent # this should follow `FunctionResultContent` (and it does during times when we don't see a 400) FunctionResultContent ``` The 400 error isn't 100% repeatable because of server-side processing, so we will get the correct ordering: ``` FunctionCallContent FunctionResultContent TextContent ``` This PR updates the ordering of how we sort of the completed steps -- if we have a step_type == "tool_calls" and "message_creation", we sort to allow for "tool_calls" to come first, and any ties are broken by the step.completed_at timestamp. With this update, we no longer receive 400s because the ordering of the content types is correct when sending to an OpenAI model. <!-- Thank you for your contribution to the semantic-kernel repo! Please help reviewers and future users, providing the following information: 1. Why is this change required? 2. What problem does it solve? 3. What scenario does it contribute to? 4. If it fixes an open issue, please link to the issue here. --> ### Description This PR: - Updates the way we sort completed messages during an assistant invoke. - Adds logging for the assistant invoke method at the `info` level. - Closes #10141 <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [X] The code builds clean without any errors or warnings - [X] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [X] All unit tests pass, and I have added new tests where possible - [X] I didn't break anyone 😄 --------- Co-authored-by: Eduard van Valkenburg <eavanvalkenburg@users.noreply.github.com>
- Loading branch information