-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
.Net: Agents: Support Azure AI Agent (#10134)
### Motivation and Context <!-- 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. --> Add support for Azure AI Agents: - https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/ai/Azure.AI.Projects/src/Custom/Agent - https://learn.microsoft.com/en-us/azure/ai-services/agents/ Fixes: #10114 ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> - Adds `AzureAIAgent` type in a new project/package - Maintains parity with `OpenAIAssistantAgent` - Supports Azure specific tool offerings ### 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 - [X] I didn't break anyone 😄
- Loading branch information
Showing
65 changed files
with
3,585 additions
and
193 deletions.
There are no files selected for viewing
This file contains 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 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
110 changes: 110 additions & 0 deletions
110
dotnet/samples/Concepts/Agents/AzureAIAgent_FileManipulation.cs
This file contains 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. | ||
using System.Diagnostics; | ||
using Azure.AI.Projects; | ||
using Microsoft.SemanticKernel; | ||
using Microsoft.SemanticKernel.Agents; | ||
using Microsoft.SemanticKernel.Agents.AzureAI; | ||
using Microsoft.SemanticKernel.ChatCompletion; | ||
using Resources; | ||
using Agent = Azure.AI.Projects.Agent; | ||
|
||
namespace Agents; | ||
|
||
/// <summary> | ||
/// Demonstrate using code-interpreter to manipulate and generate csv files with <see cref="AzureAIAgent"/> . | ||
/// </summary> | ||
public class AzureAIAgent_FileManipulation(ITestOutputHelper output) : BaseAgentsTest(output) | ||
{ | ||
[Fact] | ||
public async Task AnalyzeCSVFileUsingAzureAIAgentAsync() | ||
{ | ||
AzureAIClientProvider clientProvider = this.GetAzureProvider(); | ||
AgentsClient client = clientProvider.Client.GetAgentsClient(); | ||
|
||
await using Stream stream = EmbeddedResource.ReadStream("sales.csv")!; | ||
AgentFile fileInfo = await client.UploadFileAsync(stream, AgentFilePurpose.Agents, "sales.csv"); | ||
|
||
// Define the agent | ||
Agent definition = await client.CreateAgentAsync( | ||
TestConfiguration.AzureAI.ChatModelId, | ||
tools: [new CodeInterpreterToolDefinition()], | ||
toolResources: | ||
new() | ||
{ | ||
CodeInterpreter = new() | ||
{ | ||
FileIds = { fileInfo.Id }, | ||
} | ||
}); | ||
AzureAIAgent agent = new(definition, clientProvider); | ||
|
||
// Create a chat for agent interaction. | ||
AgentGroupChat chat = new(); | ||
|
||
// Respond to user input | ||
try | ||
{ | ||
await InvokeAgentAsync("Which segment had the most sales?"); | ||
await InvokeAgentAsync("List the top 5 countries that generated the most profit."); | ||
await InvokeAgentAsync("Create a tab delimited file report of profit by each country per month."); | ||
} | ||
finally | ||
{ | ||
await client.DeleteAgentAsync(agent.Id); | ||
await client.DeleteFileAsync(fileInfo.Id); | ||
await chat.ResetAsync(); | ||
} | ||
|
||
// Local function to invoke agent and display the conversation messages. | ||
async Task InvokeAgentAsync(string input) | ||
{ | ||
ChatMessageContent message = new(AuthorRole.User, input); | ||
chat.AddChatMessage(new(AuthorRole.User, input)); | ||
this.WriteAgentChatMessage(message); | ||
|
||
await foreach (ChatMessageContent response in chat.InvokeAsync(agent)) | ||
{ | ||
this.WriteAgentChatMessage(response); | ||
await this.DownloadContentAsync(client, response); | ||
} | ||
} | ||
} | ||
|
||
private async Task DownloadContentAsync(AgentsClient client, ChatMessageContent message) | ||
{ | ||
foreach (KernelContent item in message.Items) | ||
{ | ||
if (item is AnnotationContent annotation) | ||
{ | ||
await this.DownloadFileAsync(client, annotation.FileId!); | ||
} | ||
} | ||
} | ||
|
||
private async Task DownloadFileAsync(AgentsClient client, string fileId, bool launchViewer = false) | ||
{ | ||
AgentFile fileInfo = client.GetFile(fileId); | ||
if (fileInfo.Purpose == AgentFilePurpose.AgentsOutput) | ||
{ | ||
string filePath = Path.Combine(Path.GetTempPath(), Path.GetFileName(fileInfo.Filename)); | ||
if (launchViewer) | ||
{ | ||
filePath = Path.ChangeExtension(filePath, ".png"); | ||
} | ||
|
||
BinaryData content = await client.GetFileContentAsync(fileId); | ||
File.WriteAllBytes(filePath, content.ToArray()); | ||
Console.WriteLine($" File #{fileId} saved to: {filePath}"); | ||
|
||
if (launchViewer) | ||
{ | ||
Process.Start( | ||
new ProcessStartInfo | ||
{ | ||
FileName = "cmd.exe", | ||
Arguments = $"/C start {filePath}" | ||
}); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.