Skip to content

Python: Common agent invocation API updates #11224

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

Merged
merged 3 commits into from
Mar 27, 2025

Conversation

moonbox3
Copy link
Contributor

Motivation and Context

After this week's release of the common agent invocation API, there are some things that we can do as further improvements.

Description

This PR includes updates for:

  • Right now we force any agent invocation to provide some type of input, whether a str, a CMC, or a list of str | CMC. At times, there could be a reason, based on using an existing thread or just providing instructions to the agent, that one doesn't need to provide a message to invoke the agent. Updating to make messages optional. Updating the ABC contracts as well.
  • For invoke_stream calls on agents, there's no need to call thread.on_new_message that contains a streaming chunks -- once we move to support memory, this is where the "hook" will be. Removing this call.
  • The get_messages(...) methods on the AutoGenConversableAgentThread and the ChatHistoryAgentThread returned concrete ChatHistory objects, whereas the AssistantAgentThread and AzureAIAgentThread returned AsyncIterable[ChatMessageContent]. To align to a common API, the AutoGenConversableAgentThread and ChatHistoryAgentThread's get_messages(...) methods were moved to return AsyncIterable[ChatMessageContent].
  • Removing a public facing output_messages for streaming invoke, and replacing it with a callback to get a chat history back of "full" messages. Two samples are added in samples/concepts/agents:
    • azure_ai_agent/azure_ai_agent_streaming_chat_history_callback.py
    • openai_assistant_streaming_chat_history_callback.py
  • Update the README for OpenAI Assistants to showcase new thread abstraction.
  • Include unit tests for chat history (on_complete) callback.

Contribution Checklist

@moonbox3 moonbox3 requested a review from a team as a code owner March 27, 2025 04:15
@moonbox3 moonbox3 self-assigned this Mar 27, 2025
@markwallace-microsoft markwallace-microsoft added python Pull requests for the Python Semantic Kernel documentation labels Mar 27, 2025
@markwallace-microsoft
Copy link
Member

markwallace-microsoft commented Mar 27, 2025

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
semantic_kernel/agents
   agent.py1603081%47, 54, 58, 67–78, 125, 130, 135, 140, 145, 149, 153, 157, 192–194, 222–229, 331–333, 390, 404
semantic_kernel/agents/autogen
   autogen_conversable_agent.py1221588%67, 73, 89, 91, 97–101, 286–287, 296–299
semantic_kernel/agents/azure_ai
   agent_thread_actions.py34112962%142, 144, 179–182, 369, 371, 410–411, 437, 440–444, 447–459, 462–493, 499–506, 509–522, 538–569, 613, 616, 647–681, 744, 749–754, 774–781, 812–821, 864–876
   azure_ai_agent.py1794078%93, 104–114, 119–124, 131–140, 151–157, 208, 210, 216, 227–232, 234, 236, 257–260, 330, 431, 529, 595
semantic_kernel/agents/bedrock
   bedrock_agent.py2505478%77–84, 93, 159, 286, 299, 318, 334–337, 343, 345, 348, 353, 383, 396, 428–448, 452, 482, 495, 514–517, 519–522, 564, 575–576, 588, 624–635, 646, 663–676, 693–702
semantic_kernel/agents/chat_completion
   chat_completion_agent.py2072190%69, 79, 85, 101, 103, 109–113, 171, 174, 184, 192–197, 206, 236, 366, 432, 493
semantic_kernel/agents/open_ai
   open_ai_assistant_agent.py2332788%93, 110–111, 119–124, 131–141, 152–158, 315, 328, 364, 527, 631, 732
TOTAL21257247088% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
3339 5 💤 0 ❌ 0 🔥 1m 53s ⏱️

@TaoChenOSU TaoChenOSU added this pull request to the merge queue Mar 27, 2025
Merged via the queue into microsoft:main with commit d7172a5 Mar 27, 2025
30 checks passed
@github-project-automation github-project-automation bot moved this to Sprint: Done in Semantic Kernel Mar 27, 2025
glorious-beard pushed a commit to glorious-beard/semantic-kernel that referenced this pull request Apr 6, 2025
### Motivation and Context

After this week's release of the common agent invocation API, there are
some things that we can do as further improvements.

<!-- 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 includes updates for:
- Right now we force any agent invocation to provide some type of input,
whether a str, a CMC, or a list of str | CMC. At times, there could be a
reason, based on using an existing thread or just providing instructions
to the agent, that one doesn't need to provide a message to invoke the
agent. Updating to make messages optional. Updating the ABC contracts as
well.
- For `invoke_stream` calls on agents, there's no need to call
`thread.on_new_message` that contains a streaming chunks -- once we move
to support memory, this is where the "hook" will be. Removing this call.
- The `get_messages(...)` methods on the `AutoGenConversableAgentThread`
and the `ChatHistoryAgentThread` returned concrete `ChatHistory`
objects, whereas the `AssistantAgentThread` and `AzureAIAgentThread`
returned `AsyncIterable[ChatMessageContent]`. To align to a common API,
the `AutoGenConversableAgentThread` and `ChatHistoryAgentThread`'s
`get_messages(...)` methods were moved to return
`AsyncIterable[ChatMessageContent]`.
- Removing a public facing `output_messages` for streaming invoke, and
replacing it with a callback to get a chat history back of "full"
messages. Two samples are added in `samples/concepts/agents`:
  - `azure_ai_agent/azure_ai_agent_streaming_chat_history_callback.py`
  - `openai_assistant_streaming_chat_history_callback.py`
- Update the README for OpenAI Assistants to showcase new thread
abstraction.
- Include unit tests for chat history (`on_complete`) callback.

<!-- 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
- [ ] I didn't break anyone 😄
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
agents documentation python Pull requests for the Python Semantic Kernel
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

4 participants