Skip to content
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

Python: Improve/Simplify the Assistant Agents #10666

Merged
merged 25 commits into from
Feb 26, 2025

Conversation

moonbox3
Copy link
Contributor

@moonbox3 moonbox3 commented Feb 25, 2025

Motivation and Context

The current abstractions we have around the Semantic Kernel Assistant Agents are considered to be "fully-wrapped." This is beneficial in that SK provides a tight/complete abstraction over the agent; however, it doesn't scale well when new features are added to the underlying APIs (OpenAI) - it requires updates from SK to make each and every update. It also adds a lot more complexity in terms of needing to handle everything in SK, requires more code coverage, and chances for bugs to surface due to these complexities.

NOTE: this update is breaking. We're moving to simplify the abstractions/APIs before we move Assistant Agents from experimental to GA.

Similar to how the AzureAIAgent was created, we're moving to a "less-wrapped" abstraction around the OpenAI Assistant v2 APIs. We do have some "convenience methods" on the Agents, via class methods, to configure things like the code interpreter tool and resource, file search tool and resource, the clients required to interact with the Assistants, as well as being able to easily configure structured outputs, similar to how it is done with a ChatCompletion (either provide the Pydantic model or the Python class).

To work with Assistant Agents now, it's as simple as:

from semantic_kernel.agents.open_ai import AzureAssistantAgent
from semantic_kernel.functions import kernel_function

class MenuPlugin:
   # Plugin code here using the @kernel_function decorator

# Create the client using Azure OpenAI resources and configuration
client, model = AzureAssistantAgent.setup_resources()

# Create the assistant definition
definition = await client.beta.assistants.create(
    model=model,
    instructions="Answer questions about the menu.",
    name="Host",
)

agent = AzureAssistantAgent(
    client=client,
    definition=definition,
    plugins=[MenuPlugin()],  # The plugins can be passed in as a list to the constructor
)

# Note: plugins can also be configured on the Kernel and passed in as a parameter to the OpenAIAssistantAgent

# Define a thread and invoke the agent with the user input
thread = await agent.client.beta.threads.create()

I will create a migration guide (issue) showing what needs to be updated from the previous usage to this new pattern.

Description

This PR provides the new abstractions for the AzureAssistantAgent/OpenAIAssistantAgent classes.

Samples will be updated accordingly to show new patterns.

Contribution Checklist

@moonbox3 moonbox3 added python Pull requests for the Python Semantic Kernel PR: breaking change Pull requests that introduce breaking changes experimental Associated with an experimental feature agents labels Feb 25, 2025
@moonbox3 moonbox3 self-assigned this Feb 25, 2025
@moonbox3 moonbox3 requested a review from a team as a code owner February 25, 2025 02:08
@moonbox3 moonbox3 requested a review from Copilot February 25, 2025 02:09
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot reviewed 58 out of 58 changed files in this pull request and generated 1 comment.

Comments suppressed due to low confidence (1)

python/samples/concepts/agents/assistant_agent/assistant_agent_streaming.py:54

  • The 'MenuPlugin' is referenced but not defined or imported; please ensure it is properly defined or imported in this file.
plugins=[MenuPlugin()]

@markwallace-microsoft
Copy link
Member

markwallace-microsoft commented Feb 26, 2025

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
semantic_kernel/agents/azure_ai
   agent_thread_actions.py33812862%139, 141, 176–179, 364, 366, 405–406, 432, 435–439, 442–454, 457–488, 494–501, 504–517, 533–564, 608, 637–671, 734, 739–744, 764–771, 802–811, 854–864
   azure_ai_agent.py861088%96, 98, 104, 113–118, 120, 122, 186, 246
semantic_kernel/agents/bedrock
   bedrock_agent.py1843382%106, 120, 124, 126, 173–186, 252, 276, 293, 325, 360–363, 366, 369, 371, 403, 420–421, 423–424, 466, 477–478, 490, 526–537, 548, 565–578
semantic_kernel/agents/chat_completion
   chat_completion_agent.py110794%82, 85, 94–99, 124, 192
semantic_kernel/agents/group_chat
   agent_chat.py127398%80, 103, 176
semantic_kernel/agents/open_ai
   assistant_content_generation.py1792884%67, 94–97, 101–119, 258–260, 314, 411–419, 469
   assistant_thread_actions.py3163888%104, 107, 110, 187, 189, 226–232, 402, 460, 477, 562–571, 607–621, 633–634, 705, 710, 742
   azure_assistant_agent.py33294%77, 81
   open_ai_assistant_agent.py174597%221, 234, 270, 439, 526
TOTAL18829236187% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
3164 5 💤 0 ❌ 0 🔥 1m 35s ⏱️

@moonbox3 moonbox3 enabled auto-merge February 26, 2025 01:48
@moonbox3 moonbox3 added this pull request to the merge queue Feb 26, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Feb 26, 2025
@moonbox3 moonbox3 enabled auto-merge February 26, 2025 03:00
@moonbox3 moonbox3 disabled auto-merge February 26, 2025 05:54
@moonbox3 moonbox3 enabled auto-merge February 26, 2025 07:19
@moonbox3 moonbox3 added this pull request to the merge queue Feb 26, 2025
Merged via the queue into microsoft:main with commit 9e979f8 Feb 26, 2025
28 checks passed
@moonbox3 moonbox3 deleted the assistant-agent-refresh branch February 26, 2025 11:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
agents documentation experimental Associated with an experimental feature PR: breaking change Pull requests that introduce breaking changes python Pull requests for the Python Semantic Kernel
Projects
Status: Sprint: Done
Development

Successfully merging this pull request may close these issues.

Python: Simplify the AzureAssistantAgent/OpenAIAssistantAgent
5 participants