-
Notifications
You must be signed in to change notification settings - Fork 4.2k
.Net: Agents: Support Azure AI Agent #10134
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 all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
f75b6be
Checkpoint
crickman 7e35910
Merge branch 'main' into agents-azureai
crickman e14e242
Namespace
crickman e39ea79
Namespace update
crickman d22be4f
Namespace
crickman 1d6504b
Order
crickman 8fbc3ca
Merge branch 'main' into agents-azureai
crickman 8496cf2
Update SDK package
crickman 2c4998f
Merge branch 'main' into agents-azureai
crickman 7c3395b
Checkpoint
crickman 8e6a01d
Resolve merge
crickman 9943f12
Spelling
crickman c9869dd
Fix solution
crickman d39dc99
Typo
crickman ae5c96c
Namespace
crickman d9c3c49
Namespace
crickman 1b7b5bb
Namespace update
crickman a26e722
Merge branch 'main' into agents-azureai
crickman f648887
Namespaces
crickman 4b05fcb
Namespace
crickman 70bf716
Code clean-up
crickman d90bb27
Merge branch 'main' into agents-azureai
crickman e561a96
Merge branch 'agents-azureai' of https://github.com/microsoft/semanti…
crickman 87ed20e
Update tool association for file-attachments
crickman 096ab38
Formatting
crickman 4abac96
Merge branch 'main' into agents-azureai
crickman 25226c0
PR comments
crickman 58c7592
Merge branch 'agents-azureai' of https://github.com/microsoft/semanti…
crickman b72ea78
Utilities #1
crickman 4d7f1e9
Utilities #2
crickman 112780c
Restructure
crickman 359e4b3
Merge branch 'main' into agents-azureai
crickman 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
110 changes: 110 additions & 0 deletions
110
dotnet/samples/Concepts/Agents/AzureAIAgent_FileManipulation.cs
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. | ||
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.
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.