mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
WIP
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>net10.0</TargetFrameworks>
|
||||
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
|
||||
<OutputType>Exe</OutputType>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<!-- The Functions build tools don't like namespaces that start with a number -->
|
||||
<AssemblyName>SingleAgent</AssemblyName>
|
||||
<RootNamespace>SingleAgent</RootNamespace>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<FrameworkReference Include="Microsoft.AspNetCore.App" />
|
||||
</ItemGroup>
|
||||
|
||||
<!-- Azure Functions packages -->
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Azure.Functions.Worker" />
|
||||
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.DurableTask" />
|
||||
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.DurableTask.AzureManaged" />
|
||||
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" />
|
||||
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore" />
|
||||
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Azure.AI.OpenAI" />
|
||||
<PackageReference Include="Azure.Identity" />
|
||||
</ItemGroup>
|
||||
|
||||
<!-- Local projects that should be switched to package references when using the sample outside of this MAF repo -->
|
||||
<!--
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Agents.AI.Hosting.AzureFunctions" />
|
||||
<PackageReference Include="Microsoft.Agents.AI.OpenAI" />
|
||||
</ItemGroup>
|
||||
-->
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\..\src\Microsoft.Agents.AI.Hosting.AzureFunctions\Microsoft.Agents.AI.Hosting.AzureFunctions.csproj" />
|
||||
<ProjectReference Include="..\..\..\src\Microsoft.Agents.AI.OpenAI\Microsoft.Agents.AI.OpenAI.csproj" />
|
||||
<ProjectReference Include="..\..\..\src\Microsoft.Agents.AI.Workflows\Microsoft.Agents.AI.Workflows.csproj" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -0,0 +1,38 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
using Microsoft.Agents.AI.Workflows;
|
||||
|
||||
namespace SingleAgent;
|
||||
|
||||
internal sealed class ConcurrentStartExecutor() : Executor<string, string>("ConcurrentStartExecutor")
|
||||
{
|
||||
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 ConcurrentAggregationExecutor() : Executor<string[], string>("ConcurrentAggregationExecutor")
|
||||
{
|
||||
/// <summary>
|
||||
/// Handles incoming messages from the agents and aggregates their responses.
|
||||
/// </summary>
|
||||
/// <param name="message">The messages from the parallel agents.</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 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}");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
using Azure;
|
||||
using Azure.AI.OpenAI;
|
||||
using Azure.Identity;
|
||||
using Microsoft.Agents.AI;
|
||||
using Microsoft.Agents.AI.Hosting.AzureFunctions;
|
||||
using Microsoft.Agents.AI.Workflows;
|
||||
using Microsoft.Azure.Functions.Worker.Builder;
|
||||
using Microsoft.Extensions.AI;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using OpenAI.Chat;
|
||||
using SingleAgent;
|
||||
|
||||
// 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 ConcurrentStartExecutor();
|
||||
var aggregationExecutor = new ConcurrentAggregationExecutor();
|
||||
|
||||
// Build the workflow by adding executors and connecting them
|
||||
var workflow = new WorkflowBuilder(startExecutor)
|
||||
.WithName("FanOutWorkflow")
|
||||
.AddFanOutEdge(startExecutor, [physicist, chemist])
|
||||
.AddFanInEdge([physicist, chemist], aggregationExecutor)
|
||||
.WithOutputFrom(aggregationExecutor)
|
||||
.Build();
|
||||
|
||||
// Configure the function app to host AI agents and workflows in a unified way.
|
||||
// This will automatically generate HTTP API endpoints for agents and workflows.
|
||||
var functionBuilder = FunctionsApplication.CreateBuilder(args);
|
||||
functionBuilder
|
||||
.ConfigureFunctionsWebApplication()
|
||||
.ConfigureDurableOptions(options =>
|
||||
{
|
||||
// Configure workflows
|
||||
options.Workflows.AddWorkflow(workflow);
|
||||
});
|
||||
functionBuilder.Build().Run();
|
||||
@@ -0,0 +1,89 @@
|
||||
# Single Agent Sample
|
||||
|
||||
This sample demonstrates how to use the Durable Agent Framework (DAFx) to create a simple Azure Functions app that hosts a single AI agent and provides direct HTTP API access for interactive conversations.
|
||||
|
||||
## Key Concepts Demonstrated
|
||||
|
||||
- Using the Microsoft Agent Framework to define a simple AI agent with a name and instructions.
|
||||
- Registering agents with the Function app and running them using HTTP.
|
||||
- Conversation management (via session IDs) for isolated interactions.
|
||||
|
||||
## Environment Setup
|
||||
|
||||
See the [README.md](../README.md) file in the parent directory for more information on how to configure the environment, including how to install and run common sample dependencies.
|
||||
|
||||
## Running the Sample
|
||||
|
||||
With the environment setup and function app running, you can test the sample by sending an HTTP request to the agent endpoint.
|
||||
|
||||
You can use the `demo.http` file to send a message to the agent, or a command line tool like `curl` as shown below:
|
||||
|
||||
Bash (Linux/macOS/WSL):
|
||||
|
||||
```bash
|
||||
curl -X POST http://localhost:7071/api/agents/Joker/run \
|
||||
-H "Content-Type: text/plain" \
|
||||
-d "Tell me a joke about a pirate."
|
||||
```
|
||||
|
||||
PowerShell:
|
||||
|
||||
```powershell
|
||||
Invoke-RestMethod -Method Post `
|
||||
-Uri http://localhost:7071/api/agents/Joker/run `
|
||||
-ContentType text/plain `
|
||||
-Body "Tell me a joke about a pirate."
|
||||
```
|
||||
|
||||
You can also send JSON requests:
|
||||
|
||||
```bash
|
||||
curl -X POST http://localhost:7071/api/agents/Joker/run \
|
||||
-H "Content-Type: application/json" \
|
||||
-H "Accept: application/json" \
|
||||
-d '{"message": "Tell me a joke about a pirate."}'
|
||||
```
|
||||
|
||||
To continue a conversation, include the `thread_id` in the query string or JSON body:
|
||||
|
||||
```bash
|
||||
curl -X POST "http://localhost:7071/api/agents/Joker/run?thread_id=your-thread-id" \
|
||||
-H "Content-Type: application/json" \
|
||||
-H "Accept: application/json" \
|
||||
-d '{"message": "Tell me another one."}'
|
||||
```
|
||||
|
||||
The response from the agent will be displayed in the terminal where you ran `func start`. The expected `text/plain` output will look something like:
|
||||
|
||||
```text
|
||||
Why don't pirates ever learn the alphabet? Because they always get stuck at "C"!
|
||||
```
|
||||
|
||||
The expected `application/json` output will look something like:
|
||||
|
||||
```json
|
||||
{
|
||||
"status": 200,
|
||||
"thread_id": "ee6e47a0-f24b-40b1-ade8-16fcebb9eb40",
|
||||
"response": {
|
||||
"Messages": [
|
||||
{
|
||||
"AuthorName": "Joker",
|
||||
"CreatedAt": "2025-11-11T12:00:00.0000000Z",
|
||||
"Role": "assistant",
|
||||
"Contents": [
|
||||
{
|
||||
"Type": "text",
|
||||
"Text": "Why don't pirates ever learn the alphabet? Because they always get stuck at 'C'!"
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"Usage": {
|
||||
"InputTokenCount": 78,
|
||||
"OutputTokenCount": 36,
|
||||
"TotalTokenCount": 114
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,27 @@
|
||||
//// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
//using Microsoft.Agents.AI.Workflows;
|
||||
|
||||
//namespace SingleAgent;
|
||||
|
||||
///// <summary>
|
||||
///// Routes survey responses to appropriate teams based on rating and category.
|
||||
///// </summary>
|
||||
//public sealed class ResponseRouterExecutor() : Executor<string, string>("ResponseRouterExecutor")
|
||||
//{
|
||||
// public override ValueTask<string> HandleAsync(string message, IWorkflowContext context, CancellationToken cancellationToken = default)
|
||||
// {
|
||||
// if (message.Contains("billing", StringComparison.OrdinalIgnoreCase))
|
||||
// {
|
||||
// return ValueTask.FromResult("Routed to Billing Team");
|
||||
// }
|
||||
// else if (message.Contains("technical", StringComparison.OrdinalIgnoreCase))
|
||||
// {
|
||||
// return ValueTask.FromResult("Routed to Technical Support Team");
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// return ValueTask.FromResult("Routed to General Support Team");
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
@@ -0,0 +1,82 @@
|
||||
//// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
//using System.Text.Json;
|
||||
//using System.Text.RegularExpressions;
|
||||
//using Microsoft.Agents.AI.Workflows;
|
||||
|
||||
//namespace SingleAgent;
|
||||
|
||||
///// <summary>
|
||||
///// This executor parses survey responses and produces structured output.
|
||||
///// Example input: "Rating: 8. The app is good but checkout process is confusing."
|
||||
///// </summary>
|
||||
//[System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "CA1812:Avoid uninstantiated internal classes", Justification = "Instantiated by workflow framework")]
|
||||
//internal sealed partial class SurveyResponseParserExecutor() : Executor<string, string>("SurveyResponseParserExecutor")
|
||||
//{
|
||||
// private static readonly JsonSerializerOptions s_jsonOptions = new()
|
||||
// {
|
||||
// WriteIndented = true
|
||||
// };
|
||||
|
||||
// [GeneratedRegex(@"Rating:\s*(\d+)", RegexOptions.IgnoreCase)]
|
||||
// private static partial Regex RatingRegex();
|
||||
|
||||
// public override ValueTask<string> HandleAsync(string message, IWorkflowContext context, CancellationToken cancellationToken = default)
|
||||
// {
|
||||
// SurveyResponse response = this.ParseSurveyResponse(message);
|
||||
// string jsonResult = JsonSerializer.Serialize(response, s_jsonOptions);
|
||||
// return ValueTask.FromResult(jsonResult);
|
||||
// }
|
||||
|
||||
// private SurveyResponse ParseSurveyResponse(string message)
|
||||
// {
|
||||
// // Parse the message to extract rating and comment
|
||||
// int? rating = null;
|
||||
// string comment = message;
|
||||
|
||||
// // Try to extract rating using pattern "Rating: {number}"
|
||||
// Match ratingMatch = RatingRegex().Match(message);
|
||||
// if (ratingMatch.Success && int.TryParse(ratingMatch.Groups[1].Value, out int parsedRating))
|
||||
// {
|
||||
// rating = parsedRating;
|
||||
|
||||
// // Remove the rating part from the message to get the comment
|
||||
// // Find the position after the rating number
|
||||
// int ratingEndIndex = ratingMatch.Index + ratingMatch.Length;
|
||||
|
||||
// // Skip any separators (period, comma, dash, etc.) and whitespace
|
||||
// while (ratingEndIndex < message.Length &&
|
||||
// (char.IsWhiteSpace(message[ratingEndIndex]) ||
|
||||
// message[ratingEndIndex] == '.' ||
|
||||
// message[ratingEndIndex] == ',' ||
|
||||
// message[ratingEndIndex] == '-'))
|
||||
// {
|
||||
// ratingEndIndex++;
|
||||
// }
|
||||
|
||||
// if (ratingEndIndex < message.Length)
|
||||
// {
|
||||
// comment = message[ratingEndIndex..].Trim();
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// comment = string.Empty;
|
||||
// }
|
||||
// }
|
||||
|
||||
// // Create and return the structured response
|
||||
// return new SurveyResponse
|
||||
// {
|
||||
// Rating = rating,
|
||||
// Comment = comment,
|
||||
// OriginalMessage = message
|
||||
// };
|
||||
// }
|
||||
|
||||
// private sealed class SurveyResponse
|
||||
// {
|
||||
// public int? Rating { get; set; }
|
||||
// public string Comment { get; set; } = string.Empty;
|
||||
// public string OriginalMessage { get; set; } = string.Empty;
|
||||
// }
|
||||
//}
|
||||
@@ -0,0 +1,8 @@
|
||||
# Default endpoint address for local testing
|
||||
@authority=http://localhost:7071
|
||||
|
||||
### Prompt the agent
|
||||
POST {{authority}}/api/workflows/FanOutWorkflow/run
|
||||
Content-Type: text/plain
|
||||
|
||||
What is temperature?
|
||||
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"version": "2.0",
|
||||
"logging": {
|
||||
"logLevel": {
|
||||
"Microsoft.Agents.AI.DurableTask": "Information",
|
||||
"Microsoft.Agents.AI.Hosting.AzureFunctions": "Information",
|
||||
"DurableTask": "Information",
|
||||
"Microsoft.DurableTask": "Information"
|
||||
}
|
||||
},
|
||||
"extensions": {
|
||||
"durableTask": {
|
||||
"hubName": "default",
|
||||
"storageProvider": {
|
||||
"type": "AzureManaged",
|
||||
"connectionStringName": "DURABLE_TASK_SCHEDULER_CONNECTION_STRING"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>net10.0</TargetFrameworks>
|
||||
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
|
||||
<OutputType>Exe</OutputType>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<!-- The Functions build tools don't like namespaces that start with a number -->
|
||||
<AssemblyName>SingleAgent</AssemblyName>
|
||||
<RootNamespace>SingleAgent</RootNamespace>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<FrameworkReference Include="Microsoft.AspNetCore.App" />
|
||||
</ItemGroup>
|
||||
|
||||
<!-- Azure Functions packages -->
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Azure.Functions.Worker" />
|
||||
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.DurableTask" />
|
||||
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.DurableTask.AzureManaged" />
|
||||
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" />
|
||||
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore" />
|
||||
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Azure.AI.OpenAI" />
|
||||
<PackageReference Include="Azure.Identity" />
|
||||
</ItemGroup>
|
||||
|
||||
<!-- Local projects that should be switched to package references when using the sample outside of this MAF repo -->
|
||||
<!--
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Agents.AI.Hosting.AzureFunctions" />
|
||||
<PackageReference Include="Microsoft.Agents.AI.OpenAI" />
|
||||
</ItemGroup>
|
||||
-->
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\..\src\Microsoft.Agents.AI.Hosting.AzureFunctions\Microsoft.Agents.AI.Hosting.AzureFunctions.csproj" />
|
||||
<ProjectReference Include="..\..\..\src\Microsoft.Agents.AI.OpenAI\Microsoft.Agents.AI.OpenAI.csproj" />
|
||||
<ProjectReference Include="..\..\..\src\Microsoft.Agents.AI.Workflows\Microsoft.Agents.AI.Workflows.csproj" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
+215
@@ -0,0 +1,215 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
using Microsoft.Agents.AI.Workflows;
|
||||
|
||||
namespace SingleAgent;
|
||||
|
||||
/// <summary>
|
||||
/// Represents the details of a customer order.
|
||||
/// </summary>
|
||||
public sealed class OrderDetails
|
||||
{
|
||||
public int OrderId { get; set; }
|
||||
|
||||
public string CustomerName { get; set; } = string.Empty;
|
||||
|
||||
public string CustomerEmail { get; set; } = string.Empty;
|
||||
|
||||
public List<OrderItem> Items { get; set; } = [];
|
||||
|
||||
public decimal TotalAmount { get; set; }
|
||||
|
||||
public OrderStatus Status { get; set; }
|
||||
|
||||
public DateTime OrderDate { get; set; }
|
||||
|
||||
public DateTime? EstimatedDelivery { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Represents an item in an order.
|
||||
/// </summary>
|
||||
public sealed class OrderItem
|
||||
{
|
||||
public string ProductName { get; set; } = string.Empty;
|
||||
|
||||
public int Quantity { get; set; }
|
||||
|
||||
public decimal UnitPrice { get; set; }
|
||||
|
||||
public decimal Total => this.Quantity * this.UnitPrice;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Represents the status of an order.
|
||||
/// </summary>
|
||||
public enum OrderStatus
|
||||
{
|
||||
Pending,
|
||||
Processing,
|
||||
Shipped,
|
||||
Delivered,
|
||||
Cancelled
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Executor that looks up order details by order ID.
|
||||
/// Input: int (orderId)
|
||||
/// Output: OrderDetails
|
||||
/// </summary>
|
||||
internal sealed class OrderLookupExecutor() : Executor<int, OrderDetails>("OrderLookupExecutor")
|
||||
{
|
||||
// Simulated order database
|
||||
private static readonly Dictionary<int, OrderDetails> s_orders = new()
|
||||
{
|
||||
[1001] = new OrderDetails
|
||||
{
|
||||
OrderId = 1001,
|
||||
CustomerName = "Alice Johnson",
|
||||
CustomerEmail = "alice@example.com",
|
||||
Items =
|
||||
[
|
||||
new OrderItem { ProductName = "Wireless Headphones", Quantity = 1, UnitPrice = 79.99m },
|
||||
new OrderItem { ProductName = "Phone Case", Quantity = 2, UnitPrice = 15.99m }
|
||||
],
|
||||
TotalAmount = 111.97m,
|
||||
Status = OrderStatus.Shipped,
|
||||
OrderDate = DateTime.UtcNow.AddDays(-3),
|
||||
EstimatedDelivery = DateTime.UtcNow.AddDays(2)
|
||||
},
|
||||
[1002] = new OrderDetails
|
||||
{
|
||||
OrderId = 1002,
|
||||
CustomerName = "Bob Smith",
|
||||
CustomerEmail = "bob@example.com",
|
||||
Items =
|
||||
[
|
||||
new OrderItem { ProductName = "Laptop Stand", Quantity = 1, UnitPrice = 49.99m }
|
||||
],
|
||||
TotalAmount = 49.99m,
|
||||
Status = OrderStatus.Processing,
|
||||
OrderDate = DateTime.UtcNow.AddDays(-1),
|
||||
EstimatedDelivery = DateTime.UtcNow.AddDays(5)
|
||||
},
|
||||
[1003] = new OrderDetails
|
||||
{
|
||||
OrderId = 1003,
|
||||
CustomerName = "Carol Davis",
|
||||
CustomerEmail = "carol@example.com",
|
||||
Items =
|
||||
[
|
||||
new OrderItem { ProductName = "USB-C Hub", Quantity = 1, UnitPrice = 35.00m },
|
||||
new OrderItem { ProductName = "HDMI Cable", Quantity = 3, UnitPrice = 12.99m },
|
||||
new OrderItem { ProductName = "Webcam", Quantity = 1, UnitPrice = 89.99m }
|
||||
],
|
||||
TotalAmount = 163.96m,
|
||||
Status = OrderStatus.Delivered,
|
||||
OrderDate = DateTime.UtcNow.AddDays(-7),
|
||||
EstimatedDelivery = null
|
||||
}
|
||||
};
|
||||
|
||||
public override ValueTask<OrderDetails> HandleAsync(int message, IWorkflowContext context, CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (s_orders.TryGetValue(message, out OrderDetails? order))
|
||||
{
|
||||
return ValueTask.FromResult(order);
|
||||
}
|
||||
|
||||
// Return a "not found" order
|
||||
return ValueTask.FromResult(new OrderDetails
|
||||
{
|
||||
OrderId = message,
|
||||
CustomerName = "Unknown",
|
||||
Status = OrderStatus.Cancelled,
|
||||
OrderDate = DateTime.UtcNow
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Executor that generates a human-readable summary from order details.
|
||||
/// Input: OrderDetails
|
||||
/// Output: string
|
||||
/// </summary>
|
||||
internal sealed class OrderSummaryExecutor() : Executor<OrderDetails, string>("OrderSummaryExecutor")
|
||||
{
|
||||
public override ValueTask<string> HandleAsync(OrderDetails message, IWorkflowContext context, CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (message.CustomerName == "Unknown")
|
||||
{
|
||||
return ValueTask.FromResult($"❌ Order #{message.OrderId} was not found in our system.");
|
||||
}
|
||||
|
||||
string statusEmoji = message.Status switch
|
||||
{
|
||||
OrderStatus.Pending => "⏳",
|
||||
OrderStatus.Processing => "🔄",
|
||||
OrderStatus.Shipped => "📦",
|
||||
OrderStatus.Delivered => "✅",
|
||||
OrderStatus.Cancelled => "❌",
|
||||
_ => "❓"
|
||||
};
|
||||
|
||||
string itemsList = string.Join("\n", message.Items.Select(i =>
|
||||
$" • {i.ProductName} (x{i.Quantity}) - ${i.Total:F2}"));
|
||||
|
||||
string deliveryInfo = message.Status == OrderStatus.Delivered
|
||||
? "Delivered!"
|
||||
: message.EstimatedDelivery.HasValue
|
||||
? $"Expected: {message.EstimatedDelivery.Value:MMM dd, yyyy}"
|
||||
: "Calculating...";
|
||||
|
||||
string summary = $"""
|
||||
═══════════════════════════════════════
|
||||
📋 ORDER SUMMARY - #{message.OrderId}
|
||||
═══════════════════════════════════════
|
||||
|
||||
👤 Customer: {message.CustomerName}
|
||||
📧 Email: {message.CustomerEmail}
|
||||
📅 Order Date: {message.OrderDate:MMM dd, yyyy}
|
||||
|
||||
{statusEmoji} Status: {message.Status}
|
||||
🚚 Delivery: {deliveryInfo}
|
||||
|
||||
─────────────────────────────────────────
|
||||
📦 ITEMS:
|
||||
{itemsList}
|
||||
─────────────────────────────────────────
|
||||
|
||||
💰 TOTAL: ${message.TotalAmount:F2}
|
||||
═══════════════════════════════════════
|
||||
""";
|
||||
|
||||
return ValueTask.FromResult(summary);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Executor that parses a string input to extract an order ID.
|
||||
/// Input: string (e.g., "Check order 1001" or just "1001")
|
||||
/// Output: int (orderId)
|
||||
/// </summary>
|
||||
internal sealed class OrderIdParserExecutor() : Executor<string, int>("OrderIdParserExecutor")
|
||||
{
|
||||
public override ValueTask<int> HandleAsync(string message, IWorkflowContext context, CancellationToken cancellationToken = default)
|
||||
{
|
||||
// Try to extract order ID from the message
|
||||
// Handles formats like: "1001", "order 1001", "Check order #1001", etc.
|
||||
string cleanedInput = message
|
||||
.Replace("order", "", StringComparison.OrdinalIgnoreCase)
|
||||
.Replace("#", "")
|
||||
.Trim();
|
||||
|
||||
// Find the first number in the string
|
||||
string numberStr = new(cleanedInput.Where(char.IsDigit).ToArray());
|
||||
|
||||
if (int.TryParse(numberStr, out int orderId))
|
||||
{
|
||||
return ValueTask.FromResult(orderId);
|
||||
}
|
||||
|
||||
// Default to an invalid order ID if parsing fails
|
||||
return ValueTask.FromResult(-1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
// This sample demonstrates a workflow with different input/output types:
|
||||
// - OrderIdParserExecutor: string → int
|
||||
// - OrderLookupExecutor: int → OrderDetails (custom POCO)
|
||||
// - OrderSummaryExecutor: OrderDetails → string
|
||||
//
|
||||
// Workflow: HTTP Request (string) → Parse Order ID → Lookup Order → Generate Summary
|
||||
|
||||
using Microsoft.Agents.AI.Hosting.AzureFunctions;
|
||||
using Microsoft.Agents.AI.Workflows;
|
||||
using Microsoft.Azure.Functions.Worker.Builder;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using SingleAgent;
|
||||
|
||||
// Create the executors with different input/output types
|
||||
OrderIdParserExecutor orderIdParser = new(); // string → int
|
||||
OrderLookupExecutor orderLookup = new(); // int → OrderDetails
|
||||
OrderSummaryExecutor orderSummary = new(); // OrderDetails → string
|
||||
|
||||
// Build the workflow: Parse → Lookup → Summarize
|
||||
Workflow workflow = new WorkflowBuilder(orderIdParser)
|
||||
.WithName("OrderLookupWorkflow")
|
||||
.WithDescription("Looks up an order by ID and returns a formatted summary")
|
||||
.AddEdge(orderIdParser, orderLookup) // string → int → OrderDetails
|
||||
.AddEdge(orderLookup, orderSummary) // OrderDetails → string
|
||||
.WithOutputFrom(orderSummary)
|
||||
.Build();
|
||||
|
||||
// Configure the function app to host workflows.
|
||||
// This will automatically generate HTTP API endpoints for the workflow.
|
||||
FunctionsApplicationBuilder functionBuilder = FunctionsApplication.CreateBuilder(args);
|
||||
functionBuilder.ConfigureFunctionsWebApplication().ConfigureDurableOptions(options =>
|
||||
{
|
||||
// Register the workflow
|
||||
options.Workflows.AddWorkflow(workflow);
|
||||
});
|
||||
functionBuilder.Build().Run();
|
||||
@@ -0,0 +1,89 @@
|
||||
# Single Agent Sample
|
||||
|
||||
This sample demonstrates how to use the Durable Agent Framework (DAFx) to create a simple Azure Functions app that hosts a single AI agent and provides direct HTTP API access for interactive conversations.
|
||||
|
||||
## Key Concepts Demonstrated
|
||||
|
||||
- Using the Microsoft Agent Framework to define a simple AI agent with a name and instructions.
|
||||
- Registering agents with the Function app and running them using HTTP.
|
||||
- Conversation management (via session IDs) for isolated interactions.
|
||||
|
||||
## Environment Setup
|
||||
|
||||
See the [README.md](../README.md) file in the parent directory for more information on how to configure the environment, including how to install and run common sample dependencies.
|
||||
|
||||
## Running the Sample
|
||||
|
||||
With the environment setup and function app running, you can test the sample by sending an HTTP request to the agent endpoint.
|
||||
|
||||
You can use the `demo.http` file to send a message to the agent, or a command line tool like `curl` as shown below:
|
||||
|
||||
Bash (Linux/macOS/WSL):
|
||||
|
||||
```bash
|
||||
curl -X POST http://localhost:7071/api/agents/Joker/run \
|
||||
-H "Content-Type: text/plain" \
|
||||
-d "Tell me a joke about a pirate."
|
||||
```
|
||||
|
||||
PowerShell:
|
||||
|
||||
```powershell
|
||||
Invoke-RestMethod -Method Post `
|
||||
-Uri http://localhost:7071/api/agents/Joker/run `
|
||||
-ContentType text/plain `
|
||||
-Body "Tell me a joke about a pirate."
|
||||
```
|
||||
|
||||
You can also send JSON requests:
|
||||
|
||||
```bash
|
||||
curl -X POST http://localhost:7071/api/agents/Joker/run \
|
||||
-H "Content-Type: application/json" \
|
||||
-H "Accept: application/json" \
|
||||
-d '{"message": "Tell me a joke about a pirate."}'
|
||||
```
|
||||
|
||||
To continue a conversation, include the `thread_id` in the query string or JSON body:
|
||||
|
||||
```bash
|
||||
curl -X POST "http://localhost:7071/api/agents/Joker/run?thread_id=your-thread-id" \
|
||||
-H "Content-Type: application/json" \
|
||||
-H "Accept: application/json" \
|
||||
-d '{"message": "Tell me another one."}'
|
||||
```
|
||||
|
||||
The response from the agent will be displayed in the terminal where you ran `func start`. The expected `text/plain` output will look something like:
|
||||
|
||||
```text
|
||||
Why don't pirates ever learn the alphabet? Because they always get stuck at "C"!
|
||||
```
|
||||
|
||||
The expected `application/json` output will look something like:
|
||||
|
||||
```json
|
||||
{
|
||||
"status": 200,
|
||||
"thread_id": "ee6e47a0-f24b-40b1-ade8-16fcebb9eb40",
|
||||
"response": {
|
||||
"Messages": [
|
||||
{
|
||||
"AuthorName": "Joker",
|
||||
"CreatedAt": "2025-11-11T12:00:00.0000000Z",
|
||||
"Role": "assistant",
|
||||
"Contents": [
|
||||
{
|
||||
"Type": "text",
|
||||
"Text": "Why don't pirates ever learn the alphabet? Because they always get stuck at 'C'!"
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"Usage": {
|
||||
"InputTokenCount": 78,
|
||||
"OutputTokenCount": 36,
|
||||
"TotalTokenCount": 114
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,27 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
using Microsoft.Agents.AI.Workflows;
|
||||
|
||||
namespace SingleAgent;
|
||||
|
||||
/// <summary>
|
||||
/// Routes survey responses to appropriate teams based on rating and category.
|
||||
/// </summary>
|
||||
public sealed class ResponseRouterExecutor() : Executor<string, string>("ResponseRouterExecutor")
|
||||
{
|
||||
public override ValueTask<string> HandleAsync(string message, IWorkflowContext context, CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (message.Contains("billing", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return ValueTask.FromResult("Routed to Billing Team");
|
||||
}
|
||||
else if (message.Contains("technical", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return ValueTask.FromResult("Routed to Technical Support Team");
|
||||
}
|
||||
else
|
||||
{
|
||||
return ValueTask.FromResult("Routed to General Support Team");
|
||||
}
|
||||
}
|
||||
}
|
||||
+82
@@ -0,0 +1,82 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
using System.Text.Json;
|
||||
using System.Text.RegularExpressions;
|
||||
using Microsoft.Agents.AI.Workflows;
|
||||
|
||||
namespace SingleAgent;
|
||||
|
||||
/// <summary>
|
||||
/// This executor parses survey responses and produces structured output.
|
||||
/// Example input: "Rating: 8. The app is good but checkout process is confusing."
|
||||
/// </summary>
|
||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "CA1812:Avoid uninstantiated internal classes", Justification = "Instantiated by workflow framework")]
|
||||
internal sealed partial class SurveyResponseParserExecutor() : Executor<string, string>("SurveyResponseParserExecutor")
|
||||
{
|
||||
private static readonly JsonSerializerOptions s_jsonOptions = new()
|
||||
{
|
||||
WriteIndented = true
|
||||
};
|
||||
|
||||
[GeneratedRegex(@"Rating:\s*(\d+)", RegexOptions.IgnoreCase)]
|
||||
private static partial Regex RatingRegex();
|
||||
|
||||
public override ValueTask<string> HandleAsync(string message, IWorkflowContext context, CancellationToken cancellationToken = default)
|
||||
{
|
||||
SurveyResponse response = this.ParseSurveyResponse(message);
|
||||
string jsonResult = JsonSerializer.Serialize(response, s_jsonOptions);
|
||||
return ValueTask.FromResult(jsonResult);
|
||||
}
|
||||
|
||||
private SurveyResponse ParseSurveyResponse(string message)
|
||||
{
|
||||
// Parse the message to extract rating and comment
|
||||
int? rating = null;
|
||||
string comment = message;
|
||||
|
||||
// Try to extract rating using pattern "Rating: {number}"
|
||||
Match ratingMatch = RatingRegex().Match(message);
|
||||
if (ratingMatch.Success && int.TryParse(ratingMatch.Groups[1].Value, out int parsedRating))
|
||||
{
|
||||
rating = parsedRating;
|
||||
|
||||
// Remove the rating part from the message to get the comment
|
||||
// Find the position after the rating number
|
||||
int ratingEndIndex = ratingMatch.Index + ratingMatch.Length;
|
||||
|
||||
// Skip any separators (period, comma, dash, etc.) and whitespace
|
||||
while (ratingEndIndex < message.Length &&
|
||||
(char.IsWhiteSpace(message[ratingEndIndex]) ||
|
||||
message[ratingEndIndex] == '.' ||
|
||||
message[ratingEndIndex] == ',' ||
|
||||
message[ratingEndIndex] == '-'))
|
||||
{
|
||||
ratingEndIndex++;
|
||||
}
|
||||
|
||||
if (ratingEndIndex < message.Length)
|
||||
{
|
||||
comment = message[ratingEndIndex..].Trim();
|
||||
}
|
||||
else
|
||||
{
|
||||
comment = string.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
// Create and return the structured response
|
||||
return new SurveyResponse
|
||||
{
|
||||
Rating = rating,
|
||||
Comment = comment,
|
||||
OriginalMessage = message
|
||||
};
|
||||
}
|
||||
|
||||
private sealed class SurveyResponse
|
||||
{
|
||||
public int? Rating { get; set; }
|
||||
public string Comment { get; set; } = string.Empty;
|
||||
public string OriginalMessage { get; set; } = string.Empty;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
# Default endpoint address for local testing
|
||||
@authority=http://localhost:7071
|
||||
|
||||
### Look up Order #1001 (Shipped order with multiple items)
|
||||
POST {{authority}}/api/workflows/OrderLookupWorkflow/run
|
||||
Content-Type: text/plain
|
||||
|
||||
Check order 1001
|
||||
|
||||
### Look up Order #1002 (Processing order)
|
||||
POST {{authority}}/api/workflows/OrderLookupWorkflow/run
|
||||
Content-Type: text/plain
|
||||
|
||||
order 1002
|
||||
|
||||
### Look up Order #1003 (Delivered order)
|
||||
POST {{authority}}/api/workflows/OrderLookupWorkflow/run
|
||||
Content-Type: text/plain
|
||||
|
||||
1003
|
||||
|
||||
### Look up non-existent order
|
||||
POST {{authority}}/api/workflows/OrderLookupWorkflow/run
|
||||
Content-Type: text/plain
|
||||
|
||||
order #9999
|
||||
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"version": "2.0",
|
||||
"logging": {
|
||||
"logLevel": {
|
||||
"Microsoft.Agents.AI.DurableTask": "Information",
|
||||
"Microsoft.Agents.AI.Hosting.AzureFunctions": "Information",
|
||||
"DurableTask": "Information",
|
||||
"Microsoft.DurableTask": "Information"
|
||||
}
|
||||
},
|
||||
"extensions": {
|
||||
"durableTask": {
|
||||
"hubName": "default",
|
||||
"storageProvider": {
|
||||
"type": "AzureManaged",
|
||||
"connectionStringName": "DURABLE_TASK_SCHEDULER_CONNECTION_STRING"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user