mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
c79f886dc3
* dotnet: refresh Foundry sample guidance Carry forward the still-relevant sample guidance and Foundry-specific documentation fixes from the old stacked sample migration work, adapted to the current repo layout and policy. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * dotnet: rename Foundry sample env vars Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * dotnet: remove persistent provider sample Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * dotnet: drop SAMPLE_GUIDELINES.md from this PR Defer the guidelines doc and its cross-link to a follow-on PR to avoid broken-link failures in CI. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * dotnet: add DefaultAzureCredential warning to remaining samples Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * dotnet: address PR review feedback Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
104 lines
5.0 KiB
C#
104 lines
5.0 KiB
C#
// Copyright (c) Microsoft. All rights reserved.
|
|
|
|
using Azure.AI.Projects;
|
|
using Azure.AI.Projects.Agents;
|
|
using Azure.Identity;
|
|
using Microsoft.Agents.AI;
|
|
using Microsoft.Agents.AI.Foundry;
|
|
using Microsoft.Agents.AI.Workflows;
|
|
using Microsoft.Extensions.AI;
|
|
|
|
namespace WorkflowFoundryAgentSample;
|
|
|
|
/// <summary>
|
|
/// This sample shows how to use Microsoft Foundry Agents within a workflow.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Pre-requisites:
|
|
/// - Foundational samples should be completed first.
|
|
/// - A Microsoft Foundry project endpoint and model ID.
|
|
/// </remarks>
|
|
public static class Program
|
|
{
|
|
private static async Task Main()
|
|
{
|
|
// Set up the Azure AI Project client
|
|
var endpoint = Environment.GetEnvironmentVariable("FOUNDRY_PROJECT_ENDPOINT")
|
|
?? throw new InvalidOperationException("FOUNDRY_PROJECT_ENDPOINT is not set.");
|
|
var deploymentName = Environment.GetEnvironmentVariable("FOUNDRY_MODEL") ?? "gpt-5.4-mini";
|
|
// WARNING: DefaultAzureCredential is convenient for development but requires careful consideration in production.
|
|
// In production, consider using a specific credential (e.g., ManagedIdentityCredential) to avoid
|
|
// latency issues, unintended credential probing, and potential security risks from fallback mechanisms.
|
|
var aiProjectClient = new AIProjectClient(new Uri(endpoint), new DefaultAzureCredential());
|
|
|
|
// Create agents
|
|
AIAgent frenchAgent = await CreateTranslationAgentAsync("French", aiProjectClient, deploymentName);
|
|
AIAgent spanishAgent = await CreateTranslationAgentAsync("Spanish", aiProjectClient, deploymentName);
|
|
AIAgent englishAgent = await CreateTranslationAgentAsync("English", aiProjectClient, deploymentName);
|
|
|
|
try
|
|
{
|
|
// Build the workflow by adding executors and connecting them
|
|
var workflow = new WorkflowBuilder(frenchAgent)
|
|
.AddEdge(frenchAgent, spanishAgent)
|
|
.AddEdge(spanishAgent, englishAgent)
|
|
.Build();
|
|
|
|
// Execute the workflow
|
|
await using StreamingRun run = await InProcessExecution.RunStreamingAsync(workflow, new ChatMessage(ChatRole.User, "Hello World!"));
|
|
// Must send the turn token to trigger the agents.
|
|
// The agents are wrapped as executors. When they receive messages,
|
|
// they will cache the messages and only start processing when they receive a TurnToken.
|
|
await run.TrySendMessageAsync(new TurnToken(emitEvents: true));
|
|
await foreach (WorkflowEvent evt in run.WatchStreamAsync())
|
|
{
|
|
if (evt is AgentResponseUpdateEvent executorComplete)
|
|
{
|
|
Console.WriteLine($"{executorComplete.ExecutorId}: {executorComplete.Data}");
|
|
}
|
|
else if (evt is WorkflowErrorEvent workflowError)
|
|
{
|
|
Console.ForegroundColor = ConsoleColor.Red;
|
|
Console.Error.WriteLine(workflowError.Exception?.ToString() ?? "Unknown workflow error occurred.");
|
|
Console.ResetColor();
|
|
}
|
|
else if (evt is ExecutorFailedEvent executorFailed)
|
|
{
|
|
Console.ForegroundColor = ConsoleColor.Red;
|
|
Console.Error.WriteLine($"Executor '{executorFailed.ExecutorId}' failed with {(executorFailed.Data == null ? "unknown error" : $"exception {executorFailed.Data}")}.");
|
|
Console.ResetColor();
|
|
}
|
|
}
|
|
}
|
|
finally
|
|
{
|
|
// Cleanup the agents created for the sample.
|
|
await aiProjectClient.AgentAdministrationClient.DeleteAgentAsync(frenchAgent.Name);
|
|
await aiProjectClient.AgentAdministrationClient.DeleteAgentAsync(spanishAgent.Name);
|
|
await aiProjectClient.AgentAdministrationClient.DeleteAgentAsync(englishAgent.Name);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates a translation agent for the specified target language.
|
|
/// </summary>
|
|
/// <param name="targetLanguage">The target language for translation</param>
|
|
/// <param name="aiProjectClient">The <see cref="AIProjectClient"/> to create the agent with.</param>
|
|
/// <param name="model">The model to use for the agent</param>
|
|
/// <returns>A FoundryAgent configured for the specified language</returns>
|
|
private static async Task<FoundryAgent> CreateTranslationAgentAsync(
|
|
string targetLanguage,
|
|
AIProjectClient aiProjectClient,
|
|
string model)
|
|
{
|
|
ProjectsAgentVersion agentVersion = await aiProjectClient.AgentAdministrationClient.CreateAgentVersionAsync(
|
|
$"{targetLanguage} Translator",
|
|
new ProjectsAgentVersionCreationOptions(
|
|
new DeclarativeAgentDefinition(model: model)
|
|
{
|
|
Instructions = $"You are a translation assistant that translates the provided text to {targetLanguage}.",
|
|
}));
|
|
return aiProjectClient.AsAIAgent(agentVersion);
|
|
}
|
|
}
|