-
Notifications
You must be signed in to change notification settings - Fork 4k
Python: add support for prompts and sample #11508
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
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d723358
add support for prompts and sample
eavanvalkenburg c18f9a3
change ordering
eavanvalkenburg 040ce9b
add new sample with local agent
eavanvalkenburg e54ff9c
cleanup
eavanvalkenburg 007db71
cleanup and one more sample
eavanvalkenburg 01da88b
sampling sample with bunch of other updates
eavanvalkenburg 12f9af5
updates from feedback
eavanvalkenburg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
# Copyright (c) Microsoft. All rights reserved. | ||
|
||
import asyncio | ||
import logging | ||
import os | ||
from pathlib import Path | ||
|
||
from semantic_kernel.agents.chat_completion.chat_completion_agent import ChatCompletionAgent | ||
from semantic_kernel.connectors.ai.open_ai.services.open_ai_chat_completion import OpenAIChatCompletion | ||
from semantic_kernel.connectors.mcp import MCPStdioPlugin | ||
|
||
# set this lower or higher depending on your needs | ||
logging.basicConfig(level=logging.INFO) | ||
logger = logging.getLogger(__name__) | ||
|
||
""" | ||
The following sample demonstrates how to use a MCP Server that requires sampling | ||
to generate release notes from a list of issues. | ||
""" | ||
|
||
PR_MESSAGES = """* Python: Add ChatCompletionAgent integration tests by @moonbox3 in https://github.com/microsoft/semantic-kernel/pull/11430 | ||
* Python: Update Doc Gen demo based on latest agent invocation api pattern by @moonbox3 in https://github.com/microsoft/semantic-kernel/pull/11426 | ||
* Python: Update Python min version in README by @moonbox3 in https://github.com/microsoft/semantic-kernel/pull/11428 | ||
* Python: Fix `TypeError` when required is missing in MCP tool’s inputSchema by @KanchiShimono in https://github.com/microsoft/semantic-kernel/pull/11458 | ||
* Python: Update chromadb requirement from <0.7,>=0.5 to >=0.5,<1.1 in /python by @dependabot in https://github.com/microsoft/semantic-kernel/pull/11420 | ||
* Python: Bump google-cloud-aiplatform from 1.86.0 to 1.87.0 in /python by @dependabot in https://github.com/microsoft/semantic-kernel/pull/11423 | ||
* Python: Support Auto Function Invocation Filter for AzureAIAgent and OpenAIAssistantAgent by @moonbox3 in https://github.com/microsoft/semantic-kernel/pull/11460 | ||
* Python: Improve agent integration tests by @moonbox3 in https://github.com/microsoft/semantic-kernel/pull/11475 | ||
* Python: Allow Kernel Functions from Prompt for image and audio content by @eavanvalkenburg in https://github.com/microsoft/semantic-kernel/pull/11403 | ||
* Python: Introducing SK as a MCP Server by @eavanvalkenburg in https://github.com/microsoft/semantic-kernel/pull/11362 | ||
* Python: sample using GitHub MCP Server and Azure AI Agent by @eavanvalkenburg in https://github.com/microsoft/semantic-kernel/pull/11465 | ||
* Python: allow settings to be created directly by @eavanvalkenburg in https://github.com/microsoft/semantic-kernel/pull/11468 | ||
* Python: Bug fix for azure ai agent truncate strategy. Add sample. by @moonbox3 in https://github.com/microsoft/semantic-kernel/pull/11503 | ||
* Python: small code improvements in code of call automation sample by @eavanvalkenburg in https://github.com/microsoft/semantic-kernel/pull/11477 | ||
* Added missing import asyncio to agent with plugin python by @sphenry in https://github.com/microsoft/semantic-kernel/pull/11472 | ||
* Python: version updated to 1.28.0 by @eavanvalkenburg in https://github.com/microsoft/semantic-kernel/pull/11504""" | ||
|
||
|
||
async def main(): | ||
# 1. Create the agent | ||
async with MCPStdioPlugin( | ||
name="ReleaseNotes", | ||
description="SK Release Notes Plugin", | ||
command="uv", | ||
args=[ | ||
f"--directory={str(Path(os.path.dirname(__file__)).parent.parent.joinpath('demos', 'sk_mcp_server'))}", | ||
"run", | ||
"mcp_server_with_sampling.py", | ||
], | ||
) as plugin: | ||
agent = ChatCompletionAgent( | ||
service=OpenAIChatCompletion(), | ||
name="IssueAgent", | ||
instructions="For the messages supplied, call the release_notes_prompt function to get the broader " | ||
"prompt, then call the run_prompt function to get the final output, return that without any other text." | ||
"Do not add any other text to the output, or rewrite the output from run_prompt.", | ||
plugins=[plugin], | ||
) | ||
|
||
print(f"# Task: {PR_MESSAGES}") | ||
# 3. Invoke the agent for a response | ||
response = await agent.get_response(messages=PR_MESSAGES) | ||
print(str(response)) | ||
|
||
# 4. Cleanup: Clear the thread | ||
await response.thread.delete() | ||
|
||
""" | ||
# Task: * Python: Add ChatCompletionAgent integration tests by @moonbox3 in https://github.com/microsoft/semantic-kernel/pull/11430 | ||
* Python: Update Doc Gen demo based on latest agent invocation api pattern by @moonbox3 in https://github.com/microsoft/semantic-kernel/pull/11426 | ||
* Python: Update Python min version in README by @moonbox3 in https://github.com/microsoft/semantic-kernel/pull/11428 | ||
* Python: Fix `TypeError` when required is missing in MCP tool’s inputSchema by @KanchiShimono in https://github.com/microsoft/semantic-kernel/pull/11458 | ||
* Python: Update chromadb requirement from <0.7,>=0.5 to >=0.5,<1.1 in /python by @dependabot in https://github.com/microsoft/semantic-kernel/pull/11420 | ||
* Python: Bump google-cloud-aiplatform from 1.86.0 to 1.87.0 in /python by @dependabot in https://github.com/microsoft/semantic-kernel/pull/11423 | ||
* Python: Support Auto Function Invocation Filter for AzureAIAgent and OpenAIAssistantAgent by @moonbox3 in https://github.com/microsoft/semantic-kernel/pull/11460 | ||
* Python: Improve agent integration tests by @moonbox3 in https://github.com/microsoft/semantic-kernel/pull/11475 | ||
* Python: Allow Kernel Functions from Prompt for image and audio content by @eavanvalkenburg in https://github.com/microsoft/semantic-kernel/pull/11403 | ||
* Python: Introducing SK as a MCP Server by @eavanvalkenburg in https://github.com/microsoft/semantic-kernel/pull/11362 | ||
* Python: sample using GitHub MCP Server and Azure AI Agent by @eavanvalkenburg in https://github.com/microsoft/semantic-kernel/pull/11465 | ||
* Python: allow settings to be created directly by @eavanvalkenburg in https://github.com/microsoft/semantic-kernel/pull/11468 | ||
* Python: Bug fix for azure ai agent truncate strategy. Add sample. by @moonbox3 in https://github.com/microsoft/semantic-kernel/pull/11503 | ||
* Python: small code improvements in code of call automation sample by @eavanvalkenburg in https://github.com/microsoft/semantic-kernel/pull/11477 | ||
* Added missing import asyncio to agent with plugin python by @sphenry in https://github.com/microsoft/semantic-kernel/pull/11472 | ||
* Python: version updated to 1.28.0 by @eavanvalkenburg in https://github.com/microsoft/semantic-kernel/pull/11504 | ||
|
||
Here’s a summary of the recent changes and contributions made to the Microsoft Semantic Kernel repository: | ||
|
||
1. **Integration Tests**: | ||
- Added integration tests for `ChatCompletionAgent` by @moonbox3 ([PR #11430](https://github.com/microsoft/semantic-kernel/pull/11430)). | ||
- Improved agent integration tests by @moonbox3 ([PR #11475](https://github.com/microsoft/semantic-kernel/pull/11475)). | ||
|
||
2. **Documentation and Demos**: | ||
- Updated the Doc Gen demo to align with the latest agent invocation API pattern by @moonbox3 ([PR #11426](https://github.com/microsoft/semantic-kernel/pull/11426)). | ||
- Small code improvements made in the code of the call automation sample by @eavanvalkenburg ([PR #11477](https://github.com/microsoft/semantic-kernel/pull/11477)). | ||
|
||
3. **Version Updates**: | ||
- Updated the minimum Python version in the README by @moonbox3 ([PR #11428](https://github.com/microsoft/semantic-kernel/pull/11428)). | ||
- Updated `chromadb` requirement to allow versions >=0.5 and <1.1 by @dependabot ([PR #11420](https://github.com/microsoft/semantic-kernel/pull/11420)). | ||
- Bumped `google-cloud-aiplatform` from 1.86.0 to 1.87.0 by @dependabot ([PR #11423](https://github.com/microsoft/semantic-kernel/pull/11423)). | ||
- Version updated to 1.28.0 by @eavanvalkenburg ([PR #11504](https://github.com/microsoft/semantic-kernel/pull/11504)). | ||
|
||
4. **Bug Fixes**: | ||
- Fixed a `TypeError` in the MCP tool’s input schema when the required field is missing by @KanchiShimono ([PR #11458](https://github.com/microsoft/semantic-kernel/pull/11458)). | ||
- Bug fix for Azure AI agent truncate strategy with an added sample by @moonbox3 ([PR #11503](https://github.com/microsoft/semantic-kernel/pull/11503)). | ||
- Added a missing import for `asyncio` in the agent with plugin Python by @sphenry ([PR #11472](https://github.com/microsoft/semantic-kernel/pull/11472)). | ||
""" | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.run(main()) |
107 changes: 107 additions & 0 deletions
107
python/samples/concepts/mcp/azure_ai_agent_with_local_server.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
# Copyright (c) Microsoft. All rights reserved. | ||
|
||
import asyncio | ||
import os | ||
from pathlib import Path | ||
|
||
from azure.identity.aio import DefaultAzureCredential | ||
|
||
from semantic_kernel.agents import ( | ||
AzureAIAgent, | ||
AzureAIAgentSettings, | ||
AzureAIAgentThread, | ||
) | ||
from semantic_kernel.connectors.mcp import MCPStdioPlugin | ||
from semantic_kernel.functions import KernelArguments | ||
|
||
""" | ||
The following sample demonstrates how to create a chat completion agent that | ||
answers questions about Github using a Local Agent with two local MCP Servers. | ||
""" | ||
|
||
USER_INPUTS = [ | ||
"list the latest 10 issues that have the label: triage and python and are open", | ||
"""generate release notes with this list: | ||
* Python: Add ChatCompletionAgent integration tests by @moonbox3 in https://github.com/microsoft/semantic-kernel/pull/11430 | ||
* Python: Update Doc Gen demo based on latest agent invocation api pattern by @moonbox3 in https://github.com/microsoft/semantic-kernel/pull/11426 | ||
* Python: Update Python min version in README by @moonbox3 in https://github.com/microsoft/semantic-kernel/pull/11428 | ||
* Python: Fix `TypeError` when required is missing in MCP tool’s inputSchema by @KanchiShimono in https://github.com/microsoft/semantic-kernel/pull/11458 | ||
* Python: Update chromadb requirement from <0.7,>=0.5 to >=0.5,<1.1 in /python by @dependabot in https://github.com/microsoft/semantic-kernel/pull/11420 | ||
* Python: Bump google-cloud-aiplatform from 1.86.0 to 1.87.0 in /python by @dependabot in https://github.com/microsoft/semantic-kernel/pull/11423 | ||
* Python: Support Auto Function Invocation Filter for AzureAIAgent and OpenAIAssistantAgent by @moonbox3 in https://github.com/microsoft/semantic-kernel/pull/11460 | ||
* Python: Improve agent integration tests by @moonbox3 in https://github.com/microsoft/semantic-kernel/pull/11475 | ||
* Python: Allow Kernel Functions from Prompt for image and audio content by @eavanvalkenburg in https://github.com/microsoft/semantic-kernel/pull/11403 | ||
* Python: Introducing SK as a MCP Server by @eavanvalkenburg in https://github.com/microsoft/semantic-kernel/pull/11362 | ||
* Python: sample using GitHub MCP Server and Azure AI Agent by @eavanvalkenburg in https://github.com/microsoft/semantic-kernel/pull/11465 | ||
* Python: allow settings to be created directly by @eavanvalkenburg in https://github.com/microsoft/semantic-kernel/pull/11468 | ||
* Python: Bug fix for azure ai agent truncate strategy. Add sample. by @moonbox3 in https://github.com/microsoft/semantic-kernel/pull/11503 | ||
* Python: small code improvements in code of call automation sample by @eavanvalkenburg in https://github.com/microsoft/semantic-kernel/pull/11477 | ||
* Added missing import asyncio to agent with plugin python by @sphenry in https://github.com/microsoft/semantic-kernel/pull/11472 | ||
* Python: version updated to 1.28.0 by @eavanvalkenburg in https://github.com/microsoft/semantic-kernel/pull/11504""", | ||
] | ||
|
||
|
||
async def main(): | ||
# Load the MCP Servers as Plugins | ||
async with ( | ||
# 1. Login to Azure and create a Azure AI Project Client | ||
DefaultAzureCredential() as creds, | ||
AzureAIAgent.create_client(credential=creds) as client, | ||
MCPStdioPlugin( | ||
name="Github", | ||
description="Github Plugin", | ||
command="docker", | ||
args=["run", "-i", "--rm", "-e", "GITHUB_PERSONAL_ACCESS_TOKEN", "ghcr.io/github/github-mcp-server"], | ||
env={"GITHUB_PERSONAL_ACCESS_TOKEN": os.getenv("GITHUB_PERSONAL_ACCESS_TOKEN")}, | ||
) as github_plugin, | ||
MCPStdioPlugin( | ||
name="ReleaseNotes", | ||
description="SK Release Notes Plugin", | ||
command="uv", | ||
args=[ | ||
f"--directory={str(Path(os.path.dirname(__file__)).parent.parent.joinpath('demos', 'sk_mcp_server'))}", | ||
"run", | ||
"mcp_server_with_prompts.py", | ||
], | ||
) as release_notes_plugin, | ||
): | ||
# 3. Create the agent, with the MCP plugin and the thread | ||
agent = AzureAIAgent( | ||
client=client, | ||
definition=await client.agents.create_agent( | ||
model=AzureAIAgentSettings().model_deployment_name, | ||
name="GithubAgent", | ||
instructions="You interact with the user to help them with the Microsoft semantic-kernel github " | ||
"project. You have dedicated tools for this, including one to write release notes, " | ||
"make sure to use that when needed. The repo is always semantic-kernel (aka SK) with owner Microsoft. " | ||
"and when doing lists, always return 5 items and sort descending by created or updated" | ||
"You are specialized in Python, so always include label, python, in addition to the other labels.", | ||
), | ||
plugins=[github_plugin, release_notes_plugin], # add the sample plugin to the agent | ||
) | ||
|
||
# Create a thread to hold the conversation | ||
# If no thread is provided, a new thread will be | ||
# created and returned with the initial response | ||
thread: AzureAIAgentThread | None = None | ||
for user_input in USER_INPUTS: | ||
print(f"# User: {user_input}", end="\n\n") | ||
first_chunk = True | ||
async for response in agent.invoke_stream( | ||
messages=user_input, | ||
thread=thread, | ||
arguments=KernelArguments(owner="microsoft", repo="semantic-kernel"), | ||
): | ||
if first_chunk: | ||
print(f"# {response.name}: ", end="", flush=True) | ||
first_chunk = False | ||
print(response.content, end="", flush=True) | ||
thread = response.thread | ||
print() | ||
|
||
# Cleanup: Clear the thread | ||
await thread.delete() if thread else None | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.run(main()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.