Files
agent-framework/dotnet/samples/SemanticKernelMigration/Playground/SemanticKernelBasic/Program.cs
T
Roger Barreto 79def868b6 .NET: Add Prompt to support and improve AI Agent SK->AF Migration Results (#1081)
* Prompt progress

* Enriched prompt + Added to SLN visibility

* Set starting point for Copilot Migration

* Address prompt detail + prepare copilot migration to work with Agent nugets

* Address more prompt details

* Prompt update

* Improve description for migration

* Improve description for migration

* Improve description for migration

* Improve description for migration

* SLnx Upgrade Assistant use-case

* Extra tweaks to the migration prompt

* Update prompt

* Adding the migration guida as a visible reference to the project

* Improvements

* IMprovement

* Small improvements

* AI Guidance Overhaul.

* Update migration guide with better straightforward examples

* Update breaking glass instructions

* Namespace change + address feedback

* ChatComletionService considerations

* Add middleware guidance + public packages

* Removing + ignoring future generated files

* Potential gh bug?

* Prompt progress

* Enriched prompt + Added to SLN visibility

* Set starting point for Copilot Migration

* Address prompt detail + prepare copilot migration to work with Agent nugets

* Address more prompt details

* Prompt update

* Improve description for migration

* Improve description for migration

* Improve description for migration

* Improve description for migration

* SLnx Upgrade Assistant use-case

* Extra tweaks to the migration prompt

* Update prompt

* Adding the migration guida as a visible reference to the project

* Improvements

* IMprovement

* Small improvements

* AI Guidance Overhaul.

* Update migration guide with better straightforward examples

* Update breaking glass instructions

* Namespace change + address feedback

* ChatComletionService considerations

* Add middleware guidance + public packages

* Removing + ignoring future generated files

* Potential gh bug?

* Version bump

* Address observation

* Address observation

---------

Co-authored-by: Chris <66376200+crickman@users.noreply.github.com>
2025-10-02 15:00:19 +00:00

90 lines
3.6 KiB
C#

// Copyright (c) Microsoft. All rights reserved.
using Azure.AI.Agents.Persistent;
using Azure.Identity;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Agents;
using Microsoft.SemanticKernel.Agents.AzureAI;
using Microsoft.SemanticKernel.ChatCompletion;
#pragma warning disable SKEXP0110 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
var client = new PersistentAgentsClient(Environment.GetEnvironmentVariable("AZURE_FOUNDRY_PROJECT_ENDPOINT"), new AzureCliCredential());
// Define the agent
PersistentAgent definition = await client.Administration.CreateAgentAsync(
Environment.GetEnvironmentVariable("AZURE_FOUNDRY_PROJECT_DEPLOYMENT_NAME"),
instructions: "You are a coding assistant that always generates code using the code interpreter tool.",
tools: [new CodeInterpreterToolDefinition()]);
AzureAIAgent agent = new(definition, client);
// Create a thread for the agent conversation.
AgentThread thread = new AzureAIAgentThread(client);
try
{
await InvokeAgentAsync("Create a python file where it determines the values in the Fibonacci sequence that that are less then the value of 101.");
}
finally
{
await thread.DeleteAsync();
await client.Administration.DeleteAgentAsync(agent.Id);
}
async Task InvokeAgentAsync(string input)
{
ChatMessageContent message = new(AuthorRole.User, input);
WriteAgentChatMessage(message);
await foreach (ChatMessageContent response in agent.InvokeAsync(message, thread))
{
WriteAgentChatMessage(response);
}
}
void WriteAgentChatMessage(ChatMessageContent message)
{
// Include ChatMessageContent.AuthorName in output, if present.
string authorExpression = message.Role == AuthorRole.User ? string.Empty : FormatAuthor();
// Include TextContent (via ChatMessageContent.Content), if present.
string contentExpression = string.IsNullOrWhiteSpace(message.Content) ? string.Empty : message.Content;
bool isCode = message.Metadata?.ContainsKey(AzureAIAgent.CodeInterpreterMetadataKey) ?? false;
string codeMarker = isCode ? "\n [CODE]\n" : " ";
Console.WriteLine($"\n# {message.Role}{authorExpression}:{codeMarker}{contentExpression}");
// Provide visibility for inner content (that isn't TextContent).
foreach (KernelContent item in message.Items)
{
if (item is AnnotationContent annotation)
{
if (annotation.Kind == AnnotationKind.UrlCitation)
{
Console.WriteLine($" [{item.GetType().Name}] {annotation.Label}: {annotation.ReferenceId} - {annotation.Title}");
}
else
{
Console.WriteLine($" [{item.GetType().Name}] {annotation.Label}: File #{annotation.ReferenceId}");
}
}
else if (item is ActionContent action)
{
Console.WriteLine($" [{item.GetType().Name}] {action.Text}");
}
else if (item is ReasoningContent reasoning)
{
Console.WriteLine($" [{item.GetType().Name}] {reasoning.Text ?? "Thinking..."}");
}
else if (item is FileReferenceContent fileReference)
{
Console.WriteLine($" [{item.GetType().Name}] File #{fileReference.FileId}");
}
}
if ((message.Metadata?.TryGetValue("Usage", out object? usage) ?? false) && usage is RunStepCompletionUsage agentUsage)
{
Console.WriteLine($" [Usage] Tokens: {agentUsage.TotalTokens}, Input: {agentUsage.PromptTokens}, Output: {agentUsage.CompletionTokens}");
}
string FormatAuthor() => message.AuthorName is not null ? $" - {message.AuthorName ?? " * "}" : string.Empty;
}