mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
71 lines
2.9 KiB
C#
71 lines
2.9 KiB
C#
// Copyright (c) Microsoft. All rights reserved.
|
|
|
|
using System.Text.Json;
|
|
using Microsoft.Agents.Orchestration;
|
|
using Microsoft.Agents.Orchestration.Concurrent;
|
|
using Microsoft.Agents.Orchestration.Transforms;
|
|
using Microsoft.Extensions.AI.Agents;
|
|
using Microsoft.Extensions.AI.Agents.Runtime.InProcess;
|
|
using Microsoft.Shared.Samples;
|
|
|
|
namespace Orchestration;
|
|
|
|
/// <summary>
|
|
/// Demonstrates how to use the <see cref="ConcurrentOrchestration"/> with structured output.
|
|
/// </summary>
|
|
public class ConcurrentOrchestration_With_StructuredOutput(ITestOutputHelper output) : OrchestrationSample(output)
|
|
{
|
|
private static readonly JsonSerializerOptions s_options = new() { WriteIndented = true };
|
|
|
|
[Fact]
|
|
public async Task RunOrchestrationAsync()
|
|
{
|
|
// Define the agents
|
|
ChatClientAgent agent1 =
|
|
this.CreateAgent(
|
|
instructions: "You are an expert in identifying themes in articles. Given an article, identify the main themes.",
|
|
description: "An expert in identifying themes in articles");
|
|
ChatClientAgent agent2 =
|
|
this.CreateAgent(
|
|
instructions: "You are an expert in sentiment analysis. Given an article, identify the sentiment.",
|
|
description: "An expert in sentiment analysis");
|
|
ChatClientAgent agent3 =
|
|
this.CreateAgent(
|
|
instructions: "You are an expert in entity recognition. Given an article, extract the entities.",
|
|
description: "An expert in entity recognition");
|
|
|
|
// Define the orchestration with transform
|
|
StructuredOutputTransform<Analysis> outputTransform = new(this.CreateChatClient());
|
|
ConcurrentOrchestration<string, Analysis> orchestration =
|
|
new(agent1, agent2, agent3)
|
|
{
|
|
LoggerFactory = this.LoggerFactory,
|
|
ResultTransform = outputTransform.TransformAsync,
|
|
};
|
|
|
|
// Start the runtime
|
|
await using InProcessRuntime runtime = new();
|
|
await runtime.StartAsync();
|
|
|
|
// Run the orchestration
|
|
const string resourceId = "Hamlet_full_play_summary.txt";
|
|
string input = Resources.Read(resourceId);
|
|
Console.WriteLine($"\n# INPUT: @{resourceId}\n");
|
|
OrchestrationResult<Analysis> result = await orchestration.InvokeAsync(input, runtime);
|
|
|
|
Analysis output = await result.GetValueAsync(TimeSpan.FromSeconds(ResultTimeoutInSeconds * 2));
|
|
Console.WriteLine($"\n# RESULT:\n{JsonSerializer.Serialize(output, s_options)}");
|
|
|
|
await runtime.RunUntilIdleAsync();
|
|
}
|
|
|
|
#pragma warning disable CA1812 // Avoid uninstantiated internal classes
|
|
private sealed class Analysis
|
|
{
|
|
public IList<string> Themes { get; set; } = [];
|
|
public IList<string> Sentiments { get; set; } = [];
|
|
public IList<string> Entities { get; set; } = [];
|
|
}
|
|
#pragma warning restore CA1812 // Avoid uninstantiated internal classes
|
|
}
|