mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
904a5b843e
* Python: .NET Samples - Restructure and Improve Samples (Feature Branch) (#4091) * Moved by agent (#4094) * Fix readme links * .NET Samples - Create `04-hosting` learning path step (#4098) * Agent move * Agent reorderd * Remove A2A section from README Removed A2A section from the Getting Started README. * Agent fixed links * Fix broken sample links in durable-agents README (#4101) * Initial plan * Fix broken internal links in documentation Co-authored-by: crickman <66376200+crickman@users.noreply.github.com> * Revert template link changes; keep only durable-agents README fix Co-authored-by: crickman <66376200+crickman@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: crickman <66376200+crickman@users.noreply.github.com> * .NET Samples - Create `03-workflows` learning path step (#4102) * Fix solution project path * Python: Fix broken markdown links to repo resources (outside /docs) (#4105) * Initial plan * Fix broken markdown links to repo resources Co-authored-by: crickman <66376200+crickman@users.noreply.github.com> * Update README to rename .NET Workflows Samples section --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: crickman <66376200+crickman@users.noreply.github.com> * .NET Samples - Create `02-agents` learning path step (#4107) * .NET: Fix broken relative link in GroupChatToolApproval README (#4108) * Initial plan * Fix broken link in GroupChatToolApproval README Co-authored-by: crickman <66376200+crickman@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: crickman <66376200+crickman@users.noreply.github.com> * Update labeler configuration for workflow samples * .NET - Reorder Agents samples to start from Step01 instead of Step04 (#4110) * Fix solution * Resolve new sample paths * Move new AgentSkills and AgentWithMemory_Step04 samples * Fix link * Fix readme path * fix: update stale dotnet/samples/Durable path reference in AGENTS.md Co-authored-by: crickman <66376200+crickman@users.noreply.github.com> * Moved new sample * Update solution * Resolve merge (new sample) * Sync to new sample - FoundryAgents_Step21_BingCustomSearch * Updated README * .NET Samples - Configuration Naming Update (#4149) * .NET: Restore AzureFunctions index parity with ConsoleApps under DurableAgents samples (#4221) * Clean-up `05_host_your_agent` * Config setting consistency * Refine samples * AGENTS.md * Move new samples * Re-order samples * Move new project and fixup solution * Fixup model config * Fix up new UT project --------- Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com>
124 lines
5.6 KiB
C#
124 lines
5.6 KiB
C#
// Copyright (c) Microsoft. All rights reserved.
|
|
|
|
using Azure.AI.OpenAI;
|
|
using Azure.Identity;
|
|
using Microsoft.Agents.AI;
|
|
using Microsoft.Agents.AI.Workflows;
|
|
using Microsoft.Extensions.AI;
|
|
|
|
namespace WorkflowConcurrentSample;
|
|
|
|
/// <summary>
|
|
/// This sample introduces concurrent execution using "fan-out" and "fan-in" patterns.
|
|
///
|
|
/// Unlike sequential workflows where executors run one after another, this workflow
|
|
/// runs multiple executors in parallel to process the same input simultaneously.
|
|
///
|
|
/// The workflow structure:
|
|
/// 1. StartExecutor sends the same question to two AI agents concurrently (fan-out)
|
|
/// 2. Physicist Agent and Chemist Agent answer independently and in parallel
|
|
/// 3. AggregationExecutor collects both responses and combines them (fan-in)
|
|
///
|
|
/// This pattern is useful when you want multiple perspectives on the same input,
|
|
/// or when you can break work into independent parallel tasks for better performance.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Pre-requisites:
|
|
/// - Foundational samples should be completed first.
|
|
/// - An Azure OpenAI chat completion deployment must be configured.
|
|
/// </remarks>
|
|
public static class Program
|
|
{
|
|
private static async Task Main()
|
|
{
|
|
// 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";
|
|
var chatClient = new AzureOpenAIClient(new Uri(endpoint), new AzureCliCredential()).GetChatClient(deploymentName).AsIChatClient();
|
|
|
|
// Create the executors
|
|
ChatClientAgent physicist = new(
|
|
chatClient,
|
|
name: "Physicist",
|
|
instructions: "You are an expert in physics. You answer questions from a physics perspective."
|
|
);
|
|
ChatClientAgent chemist = new(
|
|
chatClient,
|
|
name: "Chemist",
|
|
instructions: "You are an expert in chemistry. You answer questions from a chemistry perspective."
|
|
);
|
|
var startExecutor = new ConcurrentStartExecutor();
|
|
var aggregationExecutor = new ConcurrentAggregationExecutor();
|
|
|
|
// Build the workflow by adding executors and connecting them
|
|
var workflow = new WorkflowBuilder(startExecutor)
|
|
.AddFanOutEdge(startExecutor, [physicist, chemist])
|
|
.AddFanInBarrierEdge([physicist, chemist], aggregationExecutor)
|
|
.WithOutputFrom(aggregationExecutor)
|
|
.Build();
|
|
|
|
// Execute the workflow in streaming mode
|
|
await using StreamingRun run = await InProcessExecution.RunStreamingAsync(workflow, input: "What is temperature?");
|
|
await foreach (WorkflowEvent evt in run.WatchStreamAsync())
|
|
{
|
|
if (evt is WorkflowOutputEvent output)
|
|
{
|
|
Console.WriteLine($"Workflow completed with results:\n{output.Data}");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Executor that starts the concurrent processing by sending messages to the agents.
|
|
/// </summary>
|
|
internal sealed partial class ConcurrentStartExecutor() :
|
|
Executor("ConcurrentStartExecutor")
|
|
{
|
|
/// <summary>
|
|
/// Starts the concurrent processing by sending messages to the agents.
|
|
/// </summary>
|
|
/// <param name="message">The user message to process</param>
|
|
/// <param name="context">Workflow context for accessing workflow services and adding events</param>
|
|
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests.
|
|
/// The default is <see cref="CancellationToken.None"/>.</param>
|
|
/// <returns>A task representing the asynchronous operation</returns>
|
|
[MessageHandler]
|
|
public async ValueTask HandleAsync(string message, IWorkflowContext context, CancellationToken cancellationToken = default)
|
|
{
|
|
// Broadcast the message to all connected agents. Receiving agents will queue
|
|
// the message but will not start processing until they receive a turn token.
|
|
await context.SendMessageAsync(new ChatMessage(ChatRole.User, message), cancellationToken: cancellationToken);
|
|
// Broadcast the turn token to kick off the agents.
|
|
await context.SendMessageAsync(new TurnToken(emitEvents: true), cancellationToken: cancellationToken);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Executor that aggregates the results from the concurrent agents.
|
|
/// </summary>
|
|
internal sealed class ConcurrentAggregationExecutor() :
|
|
Executor<List<ChatMessage>>("ConcurrentAggregationExecutor")
|
|
{
|
|
private readonly List<ChatMessage> _messages = [];
|
|
|
|
/// <summary>
|
|
/// Handles incoming messages from the agents and aggregates their responses.
|
|
/// </summary>
|
|
/// <param name="message">The messages from the agent</param>
|
|
/// <param name="context">Workflow context for accessing workflow services and adding events</param>
|
|
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests.
|
|
/// The default is <see cref="CancellationToken.None"/>.</param>
|
|
/// <returns>A task representing the asynchronous operation</returns>
|
|
public override async ValueTask HandleAsync(List<ChatMessage> message, IWorkflowContext context, CancellationToken cancellationToken = default)
|
|
{
|
|
this._messages.AddRange(message);
|
|
|
|
if (this._messages.Count == 2)
|
|
{
|
|
var formattedMessages = string.Join(Environment.NewLine, this._messages.Select(m => $"{m.AuthorName}: {m.Text}"));
|
|
await context.YieldOutputAsync(formattedMessages, cancellationToken);
|
|
}
|
|
}
|
|
}
|