{"id":4407,"date":"2025-03-13T07:33:22","date_gmt":"2025-03-13T14:33:22","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/semantic-kernel\/?p=4407"},"modified":"2025-03-13T07:33:22","modified_gmt":"2025-03-13T14:33:22","slug":"using-azure-ai-agents-with-semantic-kernel-in-net-and-python","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/agent-framework\/using-azure-ai-agents-with-semantic-kernel-in-net-and-python\/","title":{"rendered":"Using Azure AI Agents with Semantic Kernel in .NET and Python"},"content":{"rendered":"<p>Today we\u2019re excited to dive into Semantic Kernel and Azure AI Agents. There are additional details about using an <code>AzureAIAgent<\/code> within Semantic Kernel covered in our documentation <a href=\"https:\/\/learn.microsoft.com\/semantic-kernel\/frameworks\/agent\/azure-ai-agent\">here<\/a>.<\/p>\n<p>Azure AI Agents are powerful tools for developers seeking to integrate AI capabilities into their applications. In this blog post, we&#8217;ll explore how to utilize Azure AI Agents alongside the Semantic Kernel in both .NET and Python, showcasing the potential of these technologies to create intelligent and responsive applications.<\/p>\n<h3><strong>What is an Azure AI Agent?<\/strong><\/h3>\n<p>An\u00a0Azure AI Agent\u00a0is a specialized agent within the Semantic Kernel framework, designed to provide advanced conversational capabilities with seamless tool integration. It automates tool calling, eliminating the need for manual parsing and invocation. The agent also securely manages conversation history using threads, reducing the overhead of maintaining state. Additionally, the\u00a0Azure AI Agent\u00a0supports a variety of built-in tools, including file retrieval, code execution, and data interaction via Bing, Azure AI Search, Azure Functions, and OpenAPI.<\/p>\n<p>To use an\u00a0Azure AI Agent, an Azure AI Foundry Project must be utilized. The following articles provide an overview of the Azure AI Foundry, how to create and configure a project, and the agent service:<\/p>\n<ul>\n<li><a href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/ai-foundry\/what-is-ai-foundry\">What is Azure AI Foundry?<\/a><\/li>\n<li><a href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/ai-foundry\/how-to\/develop\/sdk-overview\">The Azure AI Foundry SDK<\/a><\/li>\n<li><a href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/ai-services\/agents\/overview\">What is Azure AI Agent Service<\/a><\/li>\n<li><a href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/ai-services\/agents\/quickstart\">Quickstart: Create a new agent<\/a><\/li>\n<\/ul>\n<h3><strong>Integrating Azure AI Agents with Semantic Kernel in .NET<\/strong>:<\/h3>\n<pre><code>using Azure.AI.Projects;\r\nusing Microsoft.SemanticKernel;\r\nusing Microsoft.SemanticKernel.Agents.AzureAI;\r\n\r\nAIProjectClient client = \r\n    AzureAIAgent.CreateAzureAIClient(\r\n        \"&lt;your connection-string&gt;\",\r\n        new AzureCliCredential());\r\n\r\nAgentsClient agentsClient = client.GetAgentsClient();\r\n \r\n\/\/ 1. Define an agent on the Azure AI agent service\r\nAgent definition = agentsClient.CreateAgentAsync(\r\n    \"&lt;name of the the model used by the agent&gt;\",\r\n    name: \"&lt;agent name&gt;\",\r\n    description: \"&lt;agent description&gt;\",\r\n    instructions: \"&lt;agent instructions&gt;\");\r\n \r\n\/\/ 2. Create an agent based on the definition\r\nAzureAIAgent agent = new(definition, agentsClient);\r\nAgentThread thread = await agentsClient.CreateThreadAsync();\r\n\r\n\/\/ 3. Generate a response from the agent \r\ntry\r\n{\r\n    ChatMessageContent message = new(AuthorRole.User, \"&lt;your user input&gt;\");\r\n    await agent.AddChatMessageAsync(threadId, message);\r\n    await foreach (\r\n        ChatMessageContent response in \r\n        agent.InvokeAsync(thread.Id))\r\n    {\r\n        Console.WriteLine(response.Content);\r\n    }\r\n}\r\nfinally\r\n{\r\n    await agentsClient.DeleteThreadAsync(thread.Id);\r\n    await agentsClient.DeleteAgentAsync(agent.Id);\r\n}\r\n<\/code><\/pre>\n<h3>Integrating Azure AI Agents with Semantic Kernel in Python:<\/h3>\n<p><div class=\"alert alert-success\"><p class=\"alert-divider\"><i class=\"fabric-icon fabric-icon--Lightbulb\"><\/i><strong>Installing Semantic Kernel for Azure AI Agents<\/strong><\/p>For Azure AI Agents, please use Semantic Kernel Python version 1.24.0 or later. To install the required packages, include the azure extra by running: <code>pip install semantic-kernel[azure]<\/code>. If you need to upgrade, use: <code>pip install semantic-kernel[azure] --upgrade<\/code>.<\/div><\/p>\n<pre><code>import asyncio\r\nfrom azure.identity.aio import DefaultAzureCredential\r\nfrom semantic_kernel.agents.azure_ai import AzureAIAgent, AzureAIAgentSettings\r\n\r\nasync def main() -&gt; None:\r\n    ai_agent_settings = AzureAIAgentSettings.create()\r\n\r\n    async with (\r\n        DefaultAzureCredential() as creds,\r\n        AzureAIAgent.create_client(credential=creds) as client,\r\n    ):\r\n        # 1. Create an agent on the Azure AI agent service\r\n        agent_definition = await client.agents.create_agent(\r\n            model=ai_agent_settings.model_deployment_name,\r\n            name=\"your-agent-name\",\r\n            instructions=\"your-agent-instructions\",\r\n        )\r\n\r\n        # 2. Create a Semantic Kernel agent for the Azure AI agent\r\n        agent = AzureAIAgent(\r\n            client=client,\r\n            definition=agent_definition,\r\n        )\r\n\r\n        # 3. Create a new thread on the Azure AI agent service\r\n        thread = await client.agents.create_thread()\r\n\r\n        try:\r\n            # 4. Add the user input as a chat message\r\n            await agent.add_chat_message(thread_id=thread.id, message=\"user-input-message\")\r\n            # 5. Invoke the agent for the specified thread for response\r\n            response = await agent.get_response(thread_id=thread.id)\r\n            print(f\"# {response.name}: {response}\")\r\n        finally:\r\n            # 6. Cleanup: Delete the thread and agent\r\n            await client.agents.delete_thread(thread.id)\r\n            await client.agents.delete_agent(agent.id)\r\n\r\n\r\nif __name__ == \"__main__\":\r\n    asyncio.run(main())\r\n<\/code><\/pre>\n<h3>Exploring More Samples in our Repo:<\/h3>\n<p>To view more getting started with Azure AI Agent samples in our repo, head here:<\/p>\n<ul>\n<li><a href=\"https:\/\/github.com\/microsoft\/semantic-kernel\/tree\/main\/dotnet\/samples\/GettingStartedWithAgents\/AzureAIAgent\">.Net Getting Started with Azure AI Agents<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/microsoft\/semantic-kernel\/tree\/main\/python\/samples\/getting_started_with_agents\/azure_ai_agent\">Python Getting Started with Azure AI Agents<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/microsoft\/semantic-kernel\/tree\/main\/python\/samples\/concepts\/agents\/azure_ai_agent\">Advanced Python Samples<\/a><\/li>\n<\/ul>\n<p><em>The Semantic Kernel team is dedicated to empowering developers by providing access to the latest advancements in the industry. We encourage you to leverage your creativity and build remarkable solutions with SK! Please reach out if you have any questions or feedback through our\u00a0<a href=\"https:\/\/github.com\/microsoft\/semantic-kernel\/discussions\/categories\/general\" target=\"_blank\" rel=\"noopener\">Semantic Kernel GitHub Discussion Channel<\/a>. We look forward to hearing from you!\u00a0We would also love your support, if you\u2019ve enjoyed using Semantic Kernel, give us a star on\u00a0<a href=\"https:\/\/github.com\/microsoft\/semantic-kernel\" target=\"_blank\" rel=\"noopener\">GitHub<\/a>.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today we\u2019re excited to dive into Semantic Kernel and Azure AI Agents. There are additional details about using an AzureAIAgent within Semantic Kernel covered in our documentation here. Azure AI Agents are powerful tools for developers seeking to integrate AI capabilities into their applications. In this blog post, we&#8217;ll explore how to utilize Azure AI [&hellip;]<\/p>\n","protected":false},"author":149071,"featured_media":4493,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[27,1],"tags":[79,48,127,63,53,9],"class_list":["post-4407","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-agents","category-semantic-kernel","tag-net","tag-ai","tag-azure-ai-agents","tag-microsoft-semantic-kernel","tag-python","tag-semantic-kernel"],"acf":[],"blog_post_summary":"<p>Today we\u2019re excited to dive into Semantic Kernel and Azure AI Agents. There are additional details about using an AzureAIAgent within Semantic Kernel covered in our documentation here. Azure AI Agents are powerful tools for developers seeking to integrate AI capabilities into their applications. In this blog post, we&#8217;ll explore how to utilize Azure AI [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/posts\/4407","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/users\/149071"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/comments?post=4407"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/posts\/4407\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/media\/4493"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/media?parent=4407"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/categories?post=4407"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/tags?post=4407"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}