From 91c66f8a2c68c4f283b24fa02ee32d8570af1545 Mon Sep 17 00:00:00 2001 From: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com> Date: Mon, 3 Nov 2025 17:53:38 +0000 Subject: [PATCH] .NET: Sample demonstrating the use of agents in workflows (#1836) * add sample demonstrating the use of agents in workflows * Update dotnet/samples/Catalog/AgentsInWorkflows/README.md Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * use type instead of var --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- dotnet/agent-framework-dotnet.slnx | 1 + .../AgentsInWorkflows.csproj | 23 +++++++++ .../Catalog/AgentsInWorkflows/Program.cs | 48 +++++++++++++++++++ .../Catalog/AgentsInWorkflows/README.md | 26 ++++++++++ 4 files changed, 98 insertions(+) create mode 100644 dotnet/samples/Catalog/AgentsInWorkflows/AgentsInWorkflows.csproj create mode 100644 dotnet/samples/Catalog/AgentsInWorkflows/Program.cs create mode 100644 dotnet/samples/Catalog/AgentsInWorkflows/README.md diff --git a/dotnet/agent-framework-dotnet.slnx b/dotnet/agent-framework-dotnet.slnx index 95812435d8..cbda6c2809 100644 --- a/dotnet/agent-framework-dotnet.slnx +++ b/dotnet/agent-framework-dotnet.slnx @@ -137,6 +137,7 @@ + diff --git a/dotnet/samples/Catalog/AgentsInWorkflows/AgentsInWorkflows.csproj b/dotnet/samples/Catalog/AgentsInWorkflows/AgentsInWorkflows.csproj new file mode 100644 index 0000000000..284ea6ceec --- /dev/null +++ b/dotnet/samples/Catalog/AgentsInWorkflows/AgentsInWorkflows.csproj @@ -0,0 +1,23 @@ + + + + Exe + net9.0 + + enable + enable + + + + + + + + + + + + + + + diff --git a/dotnet/samples/Catalog/AgentsInWorkflows/Program.cs b/dotnet/samples/Catalog/AgentsInWorkflows/Program.cs new file mode 100644 index 0000000000..3e01f6e717 --- /dev/null +++ b/dotnet/samples/Catalog/AgentsInWorkflows/Program.cs @@ -0,0 +1,48 @@ +// Copyright (c) Microsoft. All rights reserved. + +// This sample demonstrates how to integrate AI agents into a workflow pipeline. +// Three translation agents are connected sequentially to create a translation chain: +// English → French → Spanish → English, showing how agents can be composed as workflow executors. + +using Azure.AI.OpenAI; +using Azure.Identity; +using Microsoft.Agents.AI; +using Microsoft.Agents.AI.Workflows; +using Microsoft.Extensions.AI; + +// Set up the Azure OpenAI client +var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT is not set."); +var deploymentName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT_NAME") ?? "gpt-4o-mini"; + +IChatClient chatClient = new AzureOpenAIClient(new Uri(endpoint), new AzureCliCredential()) + .GetChatClient(deploymentName) + .AsIChatClient(); + +// Create agents +AIAgent frenchAgent = GetTranslationAgent("French", chatClient); +AIAgent spanishAgent = GetTranslationAgent("Spanish", chatClient); +AIAgent englishAgent = GetTranslationAgent("English", chatClient); + +// Build the workflow by adding executors and connecting them +Workflow workflow = new WorkflowBuilder(frenchAgent) + .AddEdge(frenchAgent, spanishAgent) + .AddEdge(spanishAgent, englishAgent) +.Build(); + +// Execute the workflow +await using StreamingRun run = await InProcessExecution.StreamAsync(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 AgentRunUpdateEvent executorComplete) + { + Console.WriteLine($"{executorComplete.ExecutorId}: {executorComplete.Data}"); + } +} + +static ChatClientAgent GetTranslationAgent(string targetLanguage, IChatClient chatClient) => + new(chatClient, $"You are a translation assistant that translates the provided text to {targetLanguage}."); diff --git a/dotnet/samples/Catalog/AgentsInWorkflows/README.md b/dotnet/samples/Catalog/AgentsInWorkflows/README.md new file mode 100644 index 0000000000..a92012157e --- /dev/null +++ b/dotnet/samples/Catalog/AgentsInWorkflows/README.md @@ -0,0 +1,26 @@ +# What this sample demonstrates + +This sample demonstrates the use of AI agents as executors within a workflow. + +This workflow uses three translation agents: +1. French Agent - translates input text to French +2. Spanish Agent - translates French text to Spanish +3. English Agent - translates Spanish text back to English + +The agents are connected sequentially, creating a translation chain that demonstrates how AI-powered components can be seamlessly integrated into workflow pipelines. + +## Prerequisites + +Before you begin, ensure you have the following prerequisites: + +- .NET 8.0 SDK or later +- Azure OpenAI service endpoint and deployment configured +- Azure CLI installed and authenticated (for Azure credential authentication) + +**Note**: This demo uses Azure CLI credentials for authentication. Make sure you're logged in with `az login` and have access to the Azure OpenAI resource. For more information, see the [Azure CLI documentation](https://learn.microsoft.com/cli/azure/authenticate-azure-cli-interactively). + +Set the following environment variables: + +```powershell +$env:AZURE_OPENAI_ENDPOINT="https://your-resource.openai.azure.com/" # Replace with your Azure OpenAI resource endpoint +$env:AZURE_OPENAI_DEPLOYMENT_NAME="gpt-4o-mini" # Optional, defaults to gpt-4o-mini \ No newline at end of file