Today we’re 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’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.
What is an Azure AI Agent?
An Azure AI Agent is 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 Azure AI Agent supports a variety of built-in tools, including file retrieval, code execution, and data interaction via Bing, Azure AI Search, Azure Functions, and OpenAPI.
To use an Azure 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:
- What is Azure AI Foundry?
- The Azure AI Foundry SDK
- What is Azure AI Agent Service
- Quickstart: Create a new agent
Integrating Azure AI Agents with Semantic Kernel in .NET:
using Azure.AI.Projects;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Agents.AzureAI;
AIProjectClient client =
AzureAIAgent.CreateAzureAIClient(
"<your connection-string>",
new AzureCliCredential());
AgentsClient agentsClient = client.GetAgentsClient();
// 1. Define an agent on the Azure AI agent service
Agent definition = agentsClient.CreateAgentAsync(
"<name of the the model used by the agent>",
name: "<agent name>",
description: "<agent description>",
instructions: "<agent instructions>");
// 2. Create an agent based on the definition
AzureAIAgent agent = new(definition, agentsClient);
AgentThread thread = await agentsClient.CreateThreadAsync();
// 3. Generate a response from the agent
try
{
ChatMessageContent message = new(AuthorRole.User, "<your user input>");
await agent.AddChatMessageAsync(threadId, message);
await foreach (
ChatMessageContent response in
agent.InvokeAsync(thread.Id))
{
Console.WriteLine(response.Content);
}
}
finally
{
await agentsClient.DeleteThreadAsync(thread.Id);
await agentsClient.DeleteAgentAsync(agent.Id);
}
Integrating Azure AI Agents with Semantic Kernel in Python:
Installing Semantic Kernel for Azure AI Agents
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:pip install semantic-kernel[azure]
. If you need to upgrade, use: pip install semantic-kernel[azure] --upgrade
.import asyncio
from azure.identity.aio import DefaultAzureCredential
from semantic_kernel.agents.azure_ai import AzureAIAgent, AzureAIAgentSettings
async def main() -> None:
ai_agent_settings = AzureAIAgentSettings.create()
async with (
DefaultAzureCredential() as creds,
AzureAIAgent.create_client(credential=creds) as client,
):
# 1. Create an agent on the Azure AI agent service
agent_definition = await client.agents.create_agent(
model=ai_agent_settings.model_deployment_name,
name="your-agent-name",
instructions="your-agent-instructions",
)
# 2. Create a Semantic Kernel agent for the Azure AI agent
agent = AzureAIAgent(
client=client,
definition=agent_definition,
)
# 3. Create a new thread on the Azure AI agent service
thread = await client.agents.create_thread()
try:
# 4. Add the user input as a chat message
await agent.add_chat_message(thread_id=thread.id, message="user-input-message")
# 5. Invoke the agent for the specified thread for response
response = await agent.get_response(thread_id=thread.id)
print(f"# {response.name}: {response}")
finally:
# 6. Cleanup: Delete the thread and agent
await client.agents.delete_thread(thread.id)
await client.agents.delete_agent(agent.id)
if __name__ == "__main__":
asyncio.run(main())
Exploring More Samples in our Repo:
To view more getting started with Azure AI Agent samples in our repo, head here:
- .Net Getting Started with Azure AI Agents
- Python Getting Started with Azure AI Agents
- Advanced Python Samples
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 Semantic Kernel GitHub Discussion Channel. We look forward to hearing from you! We would also love your support, if you’ve enjoyed using Semantic Kernel, give us a star on GitHub.
0 comments
Be the first to start the discussion.