minor tweaks

This commit is contained in:
Shyju Krishnankutty
2026-01-23 10:16:31 -08:00
Unverified
parent 859ac0939d
commit 52da946efd
5 changed files with 63 additions and 25 deletions
@@ -32,17 +32,9 @@ Workflow fulfillOrder = new WorkflowBuilder(orderParserExecutor)
.AddEdge(orderEnricherExeecutor, paymentProcessorExecutor)
.Build();
//OrderCancel orderArchiverExecutor = new();
//Workflow cancelOrder = new WorkflowBuilder(orderParserExecutor)
// .WithName("CancelOrder")
// .WithDescription("Cancel an order")
// .AddEdge(orderParserExecutor, orderLookupExecutor)
// .AddEdge(orderLookupExecutor, orderArchiverExecutor)
// .Build();
var host = FunctionsApplication.CreateBuilder(args)
.ConfigureFunctionsWebApplication()
.ConfigureDurableOptions(options => options.Workflows.AddWorkflow(fulfillOrder))
.ConfigureDurableOptions(options => options.Workflows.AddWorkflow(fulfillOrder, enableMcpToolTrigger: true))
.Build();
host.Run();
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft. All rights reserved.
// This sample demonstrates how to use durable state management in Azure Functions workflows.
// The OrderIdParserExecutor writes a value to shared state, and the FraudValidation reads it back.
// The OrderIdParser writes a value to shared state, and the FraudValidation reads it back.
// The state is persisted durably using Durable Entities behind the scenes.
using Microsoft.Agents.AI.Workflows;
@@ -32,7 +32,7 @@ internal sealed class Order
public sealed record Customer(int Id, string Name, bool IsBlocked);
internal sealed class OrderIdParserExecutor() : Executor<string, Order>("OrderIdParserExecutor")
internal sealed class OrderIdParser() : Executor<string, Order>("OrderIdParser")
{
public override async ValueTask<Order> HandleAsync(string message, IWorkflowContext context, CancellationToken cancellationToken = default)
{
@@ -65,7 +65,7 @@ internal sealed class OrderEnrich() : Executor<Order, Order>("EnrichOrder")
}
}
internal sealed class PaymentProcesserExecutor() : Executor<Order, Order>("PaymentProcesserExecutor")
internal sealed class PaymentProcesser() : Executor<Order, Order>("PaymentProcesser")
{
public override async ValueTask<Order> HandleAsync(Order message, IWorkflowContext context, CancellationToken cancellationToken = default)
{
@@ -75,7 +75,7 @@ internal sealed class PaymentProcesserExecutor() : Executor<Order, Order>("Payme
}
}
internal sealed class NotifyFraudExecutor() : Executor<Order, string>("NotifyFraud")
internal sealed class NotifyFraud() : Executor<Order, string>("NotifyFraud")
{
public override async ValueTask<string> HandleAsync(Order message, IWorkflowContext context, CancellationToken cancellationToken = default)
{
@@ -6,10 +6,10 @@ using Microsoft.Azure.Functions.Worker.Builder;
using Microsoft.Extensions.Hosting;
using SingleAgent;
OrderIdParserExecutor orderParser = new();
OrderIdParser orderParser = new();
OrderEnrich orderEnrich = new();
PaymentProcesserExecutor paymentProcessor = new();
NotifyFraudExecutor notifyFraud = new();
PaymentProcesser paymentProcessor = new();
NotifyFraud notifyFraud = new();
WorkflowBuilder builder = new(orderParser);
builder
@@ -21,6 +21,6 @@ var workflow = builder.WithName("AuditOrder").Build();
FunctionsApplication.CreateBuilder(args)
.ConfigureFunctionsWebApplication()
.ConfigureDurableOptions(options => options.Workflows.AddWorkflow(workflow))
.ConfigureDurableOptions(options => options.Workflows.AddWorkflow(workflow, enableMcpToolTrigger: true))
.Build()
.Run();
@@ -1,6 +1,11 @@
// Copyright (c) Microsoft. All rights reserved.
using Azure;
using Azure.AI.OpenAI;
using Azure.Identity;
using Microsoft.Agents.AI;
using Microsoft.Agents.AI.Workflows;
using OpenAI.Chat;
namespace WorkflowVisualizationSample;
@@ -20,8 +25,29 @@ internal static class Program
/// <param name="args">Command line arguments (not used).</param>
private static void Main(string[] args)
{
// Step 1: Build the workflow you want to visualize
Workflow workflow = WorkflowMapReduceSample.Program.BuildWorkflow();
// Get the Azure OpenAI endpoint and deployment name from environment variables.
string endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT")
?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT is not set.");
string deploymentName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT")
?? throw new InvalidOperationException("AZURE_OPENAI_DEPLOYMENT is not set.");
// Use Azure Key Credential if provided, otherwise use Azure CLI Credential.
string? azureOpenAiKey = System.Environment.GetEnvironmentVariable("AZURE_OPENAI_KEY");
AzureOpenAIClient client = !string.IsNullOrEmpty(azureOpenAiKey)
? new AzureOpenAIClient(new Uri(endpoint), new AzureKeyCredential(azureOpenAiKey))
: new AzureOpenAIClient(new Uri(endpoint), new AzureCliCredential());
AIAgent physicist = client.GetChatClient(deploymentName).CreateAIAgent("You are an expert in physics. You answer questions from a physics perspective.", "Physicist");
AIAgent chemist = client.GetChatClient(deploymentName).CreateAIAgent("You are an expert in chemistry. You answer questions from a chemistry perspective.", "Chemist");
var startExecutor = new PrepareQuery();
var aggregationExecutor = new ResultAggregator();
var workflow = new WorkflowBuilder(startExecutor)
.WithName("ExpertReview")
.AddFanOutEdge(startExecutor, [physicist, chemist])
.AddFanInEdge([physicist, chemist], aggregationExecutor)
.Build();
// Step 2: Generate and display workflow visualization
Console.WriteLine("Generating workflow visualization...");
@@ -31,11 +57,30 @@ internal static class Program
var mermaid = workflow.ToMermaidString();
Console.WriteLine(mermaid);
Console.WriteLine("=======");
// DOT
Console.WriteLine("DiGraph string: *** Tip: To export DOT as an image, install Graphviz and pipe the DOT output to 'dot -Tsvg', 'dot -Tpng', etc. *** \n=======");
var dotString = workflow.ToDotString();
Console.WriteLine(dotString);
Console.WriteLine("=======");
}
}
internal sealed class PrepareQuery() : Executor<string, string>("PrepareQuery")
{
public override ValueTask<string> HandleAsync(string message, IWorkflowContext context, CancellationToken cancellationToken = default)
{
// do some initial parsing and validation of the message.
// Return a polished version ith additional metadta.
if (!message.StartsWith("Query for the agent:", StringComparison.OrdinalIgnoreCase))
{
message = "Query for the agent: " + message;
}
return ValueTask.FromResult(message);
}
}
internal sealed class ResultAggregator() : Executor<string[], string>("ResultAggregator")
{
public override ValueTask<string> HandleAsync(string[] message, IWorkflowContext context, CancellationToken cancellationToken = default)
{
// Aggregate all responses from parallel executors.
string aggregatedResponse = string.Join("\n---\n", message);
return ValueTask.FromResult($"Aggregated {message.Length} responses:\n{aggregatedResponse}");
}
}
@@ -9,6 +9,7 @@
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\..\src\Microsoft.Agents.AI.OpenAI\Microsoft.Agents.AI.OpenAI.csproj" />
<ProjectReference Include="..\..\..\..\src\Microsoft.Agents.AI.Workflows\Microsoft.Agents.AI.Workflows.csproj" />
<ProjectReference Include="..\Concurrent\MapReduce\MapReduce.csproj" />
</ItemGroup>