diff --git a/.openpublishing.redirection.json b/.openpublishing.redirection.json index 40bb0cf1..8a1b6b90 100644 --- a/.openpublishing.redirection.json +++ b/.openpublishing.redirection.json @@ -824,6 +824,11 @@ "source_path": "semantic-kernel/Frameworks/agent/examples/example-agent-collaboration.md", "redirect_url": "/semantic-kernel/support/archive/agent-chat-example", "redirect_document_id": false + }, + { + "source_path": "agent-framework/tutorials/workflows/visualization.md", + "redirect_url": "/agent-framework/user-guide/workflows/visualization", + "redirect_document_id": true } ] } diff --git a/agent-framework/TOC.yml b/agent-framework/TOC.yml index 9af22a76..a587b6f4 100644 --- a/agent-framework/TOC.yml +++ b/agent-framework/TOC.yml @@ -16,6 +16,8 @@ items: href: user-guide/model-context-protocol/TOC.yml - name: Workflows href: user-guide/workflows/TOC.yml + - name: Hosting + href: user-guide/hosting/TOC.yml - name: Integrations items: - name: AG-UI diff --git a/agent-framework/media/durable-agent-chat-history-tutorial.png b/agent-framework/media/durable-agent-chat-history-tutorial.png new file mode 100644 index 00000000..5046152c Binary files /dev/null and b/agent-framework/media/durable-agent-chat-history-tutorial.png differ diff --git a/agent-framework/media/durable-agent-chat-history.png b/agent-framework/media/durable-agent-chat-history.png new file mode 100644 index 00000000..6b62be1b Binary files /dev/null and b/agent-framework/media/durable-agent-chat-history.png differ diff --git a/agent-framework/media/durable-agent-orchestration.png b/agent-framework/media/durable-agent-orchestration.png new file mode 100644 index 00000000..1df10380 Binary files /dev/null and b/agent-framework/media/durable-agent-orchestration.png differ diff --git a/agent-framework/tutorials/TOC.yml b/agent-framework/tutorials/TOC.yml index e48cfea3..48a83a86 100644 --- a/agent-framework/tutorials/TOC.yml +++ b/agent-framework/tutorials/TOC.yml @@ -2,5 +2,7 @@ href: overview.md - name: Agents href: agents/TOC.yml +- name: Plugins + href: plugins/TOC.yml - name: Workflows href: workflows/TOC.yml diff --git a/agent-framework/tutorials/agents/TOC.yml b/agent-framework/tutorials/agents/TOC.yml index 12436d82..1495879c 100644 --- a/agent-framework/tutorials/agents/TOC.yml +++ b/agent-framework/tutorials/agents/TOC.yml @@ -23,4 +23,10 @@ - name: Third Party chat history storage href: third-party-chat-history-storage.md - name: Adding memory to agents - href: memory.md \ No newline at end of file + href: memory.md +- name: Durable agents + items: + - name: Create and run a durable agent + href: create-and-run-durable-agent.md + - name: Orchestrate durable agents + href: orchestrate-durable-agents.md \ No newline at end of file diff --git a/agent-framework/tutorials/agents/create-and-run-durable-agent.md b/agent-framework/tutorials/agents/create-and-run-durable-agent.md new file mode 100644 index 00000000..fcd37c95 --- /dev/null +++ b/agent-framework/tutorials/agents/create-and-run-durable-agent.md @@ -0,0 +1,622 @@ +--- +title: Create and run a durable agent +description: Learn how to create and run a durable AI agent with Azure Functions and the durable task extension for Microsoft Agent Framework +zone_pivot_groups: programming-languages +author: anthonychu +ms.topic: tutorial +ms.author: antchu +ms.date: 11/05/2025 +ms.service: agent-framework +--- + +# Create and run a durable agent + +This tutorial shows you how to create and run a [durable AI agent](../../user-guide/agents/agent-types/durable-agent/create-durable-agent.md) using the durable task extension for Microsoft Agent Framework. You'll build an Azure Functions app that hosts a stateful agent with built-in HTTP endpoints, and learn how to monitor it using the Durable Task Scheduler dashboard. + +Durable agents provide serverless hosting with automatic state management, allowing your agents to maintain conversation history across multiple interactions without managing infrastructure. + +## Prerequisites + +Before you begin, ensure you have the following prerequisites: + +::: zone pivot="programming-language-csharp" + +- [.NET 9.0 SDK or later](https://dotnet.microsoft.com/download) +- [Azure Functions Core Tools v4.x](/azure/azure-functions/functions-run-local#install-the-azure-functions-core-tools) +- [Azure Developer CLI (azd)](/azure/developer/azure-developer-cli/install-azd) +- [Azure CLI installed](/cli/azure/install-azure-cli) and [authenticated](/cli/azure/authenticate-azure-cli) +- [Docker Desktop](https://www.docker.com/products/docker-desktop/) installed and running (for local development with Azurite and the Durable Task Scheduler emulator) +- An Azure subscription with permissions to create resources + +> [!NOTE] +> Microsoft Agent Framework is supported with all actively supported versions of .NET. For the purposes of this sample, we recommend the .NET 9 SDK or a later version. + +::: zone-end + +::: zone pivot="programming-language-python" + +- [Python 3.10 or later](https://www.python.org/downloads/) +- [Azure Functions Core Tools v4.x](/azure/azure-functions/functions-run-local#install-the-azure-functions-core-tools) +- [Azure Developer CLI (azd)](/azure/developer/azure-developer-cli/install-azd) +- [Azure CLI installed](/cli/azure/install-azure-cli) and [authenticated](/cli/azure/authenticate-azure-cli) +- [Docker Desktop](https://www.docker.com/products/docker-desktop/) installed and running (for local development with Azurite and the Durable Task Scheduler emulator) +- An Azure subscription with permissions to create resources + +::: zone-end + +## Download the quickstart project + +Use Azure Developer CLI to initialize a new project from the durable agents quickstart template. + +::: zone pivot="programming-language-csharp" + +1. Create a new directory for your project and navigate to it: + + # [Bash](#tab/bash) + + ```bash + mkdir MyDurableAgent + cd MyDurableAgent + ``` + + # [PowerShell](#tab/powershell) + + ```powershell + New-Item -ItemType Directory -Path MyDurableAgent + Set-Location MyDurableAgent + ``` + + --- + +1. Initialize the project from the template: + + ```console + azd init --template durable-agents-quickstart-dotnet + ``` + + When prompted for an environment name, enter a name like `my-durable-agent`. + +This downloads the quickstart project with all necessary files, including the Azure Functions configuration, agent code, and infrastructure as code templates. + +::: zone-end + +::: zone pivot="programming-language-python" + +1. Create a new directory for your project and navigate to it: + + # [Bash](#tab/bash) + + ```bash + mkdir MyDurableAgent + cd MyDurableAgent + ``` + + # [PowerShell](#tab/powershell) + + ```powershell + New-Item -ItemType Directory -Path MyDurableAgent + Set-Location MyDurableAgent + ``` + + --- + +1. Initialize the project from the template: + + ```console + azd init --template durable-agents-quickstart-python + ``` + + When prompted for an environment name, enter a name like `my-durable-agent`. + +1. Create and activate a virtual environment: + + # [Bash](#tab/bash) + + ```bash + python3 -m venv .venv + source .venv/bin/activate + ``` + + # [PowerShell](#tab/powershell) + + ```powershell + python3 -m venv .venv + .venv\Scripts\Activate.ps1 + ``` + + --- + + +1. Install the required packages: + + ```console + python -m pip install -r requirements.txt + ``` + +This downloads the quickstart project with all necessary files, including the Azure Functions configuration, agent code, and infrastructure as code templates. It also prepares a virtual environment with the required dependencies. + +::: zone-end + +## Provision Azure resources + +Use Azure Developer CLI to create the required Azure resources for your durable agent. + +1. Provision the infrastructure: + + ```console + azd provision + ``` + + This command creates: + - An Azure OpenAI service with a gpt-4o-mini deployment + - An Azure Functions app with Flex Consumption hosting plan + - An Azure Storage account for the Azure Functions runtime and durable storage + - A Durable Task Scheduler instance (Consumption plan) for managing agent state + - Necessary networking and identity configurations + +1. When prompted, select your Azure subscription and choose a location for the resources. + +The provisioning process takes a few minutes. Once complete, azd stores the created resource information in your environment. + +## Review the agent code + +Now let's examine the code that defines your durable agent. + +::: zone pivot="programming-language-csharp" + +Open `Program.cs` to see the agent configuration: + +```csharp +using Azure.AI.OpenAI; +using Azure.Identity; +using Microsoft.Agents.AI; +using Microsoft.Agents.AI.Hosting.AzureFunctions; +using Microsoft.Azure.Functions.Worker.Builder; +using Microsoft.Extensions.AI; +using Microsoft.Extensions.Hosting; +using OpenAI; + +var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") + ?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT environment variable is not set"); +var deploymentName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT") ?? "gpt-4o-mini"; + +// Create an AI agent following the standard Microsoft Agent Framework pattern +AIAgent agent = new AzureOpenAIClient(new Uri(endpoint), new DefaultAzureCredential()) + .GetChatClient(deploymentName) + .CreateAIAgent( + instructions: "You are a helpful assistant that can answer questions and provide information.", + name: "MyDurableAgent"); + +using IHost app = FunctionsApplication + .CreateBuilder(args) + .ConfigureFunctionsWebApplication() + .ConfigureDurableAgents(options => options.AddAIAgent(agent)) + .Build(); +app.Run(); +``` + +This code: +1. Retrieves your Azure OpenAI configuration from environment variables. +1. Creates an Azure OpenAI client using Azure credentials. +1. Creates an AI agent with instructions and a name. +1. Configures the Azure Functions app to host the agent with durable thread management. + +::: zone-end + +::: zone pivot="programming-language-python" + +Open `function_app.py` to see the agent configuration: + +```python +import os +from agent_framework.azure import AzureOpenAIChatClient, AgentFunctionApp +from azure.identity import DefaultAzureCredential + +endpoint = os.getenv("AZURE_OPENAI_ENDPOINT") +if not endpoint: + raise ValueError("AZURE_OPENAI_ENDPOINT is not set.") +deployment_name = os.getenv("AZURE_OPENAI_DEPLOYMENT_NAME", "gpt-4o-mini") + +# Create an AI agent following the standard Microsoft Agent Framework pattern +agent = AzureOpenAIChatClient( + endpoint=endpoint, + deployment_name=deployment_name, + credential=DefaultAzureCredential() +).create_agent( + instructions="You are a helpful assistant that can answer questions and provide information.", + name="MyDurableAgent" +) + +# Configure the function app to host the agent with durable thread management +app = AgentFunctionApp(agents=[agent]) +``` + +This code: ++ Retrieves your Azure OpenAI configuration from environment variables. ++ Creates an Azure OpenAI client using Azure credentials. ++ Creates an AI agent with instructions and a name. ++ Configures the Azure Functions app to host the agent with durable thread management. + +::: zone-end + +The agent is now ready to be hosted in Azure Functions. The durable task extension automatically creates HTTP endpoints for interacting with your agent and manages conversation state across multiple requests. + +## Configure local settings + +Create a `local.settings.json` file for local development based on the sample file included in the project. + +1. Copy the sample settings file: + + # [Bash](#tab/bash) + + ```bash + cp local.settings.sample.json local.settings.json + ``` + + # [PowerShell](#tab/powershell) + + ```powershell + Copy-Item local.settings.sample.json local.settings.json + ``` + + --- + +1. Get your Azure OpenAI endpoint from the provisioned resources: + + ```console + azd env get-value AZURE_OPENAI_ENDPOINT + ``` + +1. Open `local.settings.json` and replace `` in the `AZURE_OPENAI_ENDPOINT` value with the endpoint from the previous command. + +Your `local.settings.json` should look like this: + +```json +{ + "IsEncrypted": false, + "Values": { + // ... other settings ... + "AZURE_OPENAI_ENDPOINT": "https://your-openai-resource.openai.azure.com", + "AZURE_OPENAI_DEPLOYMENT": "gpt-4o-mini", + "TASKHUB_NAME": "default" + } +} +``` + +> [!NOTE] +> The `local.settings.json` file is used for local development only and is not deployed to Azure. For production deployments, these settings are automatically configured in your Azure Functions app by the infrastructure templates. + +## Start local development dependencies + +To run durable agents locally, you need to start two services: +- **Azurite**: Emulates Azure Storage services (used by Azure Functions for managing triggers and internal state). +- **Durable Task Scheduler (DTS) emulator**: Manages durable state (conversation history, orchestration state) and scheduling for your agents + +### Start Azurite + +Azurite emulates Azure Storage services locally. The Azure Functions uses it for managing internal state. You'll need to run this in a new terminal window and keep it running while you develop and test your durable agent. + +1. Open a new terminal window and pull the Azurite Docker image: + + ```console + docker pull mcr.microsoft.com/azure-storage/azurite + ``` + +1. Start Azurite in a terminal window: + + ```console + docker run -p 10000:10000 -p 10001:10001 -p 10002:10002 mcr.microsoft.com/azure-storage/azurite + ``` + + Azurite will start and listen on the default ports for Blob (10000), Queue (10001), and Table (10002) services. + +Keep this terminal window open while you're developing and testing your durable agent. + +> [!TIP] +> For more information about Azurite, including alternative installation methods, see [Use Azurite emulator for local Azure Storage development](/azure/storage/common/storage-use-azurite). + +### Start the Durable Task Scheduler emulator + +The DTS emulator provides the durable backend for managing agent state and orchestrations. It stores conversation history and ensures your agent's state persists across restarts. It also triggers durable orchestrations and agents. You'll need to run this in a separate new terminal window and keep it running while you develop and test your durable agent. + +1. Open another new terminal window and pull the DTS emulator Docker image: + + ```console + docker pull mcr.microsoft.com/dts/dts-emulator:latest + ``` + +1. Run the DTS emulator: + + ```console + docker run -p 8080:8080 -p 8082:8082 mcr.microsoft.com/dts/dts-emulator:latest + ``` + + This command starts the emulator and exposes: + - Port 8080: The gRPC endpoint for the Durable Task Scheduler (used by your Functions app) + - Port 8082: The administrative dashboard + +1. The dashboard will be available at `http://localhost:8082`. + +Keep this terminal window open while you're developing and testing your durable agent. + +> [!TIP] +> To learn more about the DTS emulator, including how to configure multiple task hubs and access the dashboard, see [Develop with Durable Task Scheduler](/azure/azure-functions/durable/durable-task-scheduler/develop-with-durable-task-scheduler). + +## Run the function app + +Now you're ready to run your Azure Functions app with the durable agent. + +1. In a new terminal window (keeping both Azurite and the DTS emulator running in separate windows), navigate to your project directory. + +1. Start the Azure Functions runtime: + + ```console + func start + ``` + +1. You should see output indicating that your function app is running, including the HTTP endpoints for your agent: + + ``` + Functions: + http-MyDurableAgent: [POST] http://localhost:7071/api/agents/MyDurableAgent/run + dafx-MyDurableAgent: entityTrigger + ``` + +These endpoints manage conversation state automatically - you don't need to create or manage thread objects yourself. + +## Test the agent locally + +Now you can interact with your durable agent using HTTP requests. The agent maintains conversation state across multiple requests, enabling multi-turn conversations. + +### Start a new conversation + +Create a new thread and send your first message: + +# [Bash](#tab/bash) + +```bash +curl -i -X POST http://localhost:7071/api/agents/MyDurableAgent/run \ + -H "Content-Type: text/plain" \ + -d "What are three popular programming languages?" +``` + +# [PowerShell](#tab/powershell) + +```powershell +$response = Invoke-WebRequest -Uri "http://localhost:7071/api/agents/MyDurableAgent/run" ` + -Method POST ` + -Headers @{"Content-Type"="text/plain"} ` + -Body "What are three popular programming languages?" +$response.Headers +$response.Content +``` + +--- + +Sample response (note the `x-ms-thread-id` header contains the thread ID): + +``` +HTTP/1.1 200 OK +Content-Type: text/plain +x-ms-thread-id: @dafx-mydurableagent@263fa373-fa01-4705-abf2-5a114c2bb87d +Content-Length: 189 + +Three popular programming languages are Python, JavaScript, and Java. Python is known for its simplicity and readability, JavaScript powers web interactivity, and Java is widely used in enterprise applications. +``` + +Save the thread ID from the `x-ms-thread-id` header (e.g., `@dafx-mydurableagent@263fa373-fa01-4705-abf2-5a114c2bb87d`) for the next request. + +### Continue the conversation + +Send a follow-up message to the same thread by including the thread ID as a query parameter: + +# [Bash](#tab/bash) + +```bash +curl -X POST "http://localhost:7071/api/agents/MyDurableAgent/run?thread_id=@dafx-mydurableagent@263fa373-fa01-4705-abf2-5a114c2bb87d" \ + -H "Content-Type: text/plain" \ + -d "Which one is best for beginners?" +``` + +# [PowerShell](#tab/powershell) + +```powershell +$threadId = "@dafx-mydurableagent@263fa373-fa01-4705-abf2-5a114c2bb87d" +Invoke-RestMethod -Uri "http://localhost:7071/api/agents/MyDurableAgent/run?thread_id=$threadId" ` + -Method POST ` + -Headers @{"Content-Type"="text/plain"} ` + -Body "Which one is best for beginners?" +``` + +--- + +Replace `@dafx-mydurableagent@263fa373-fa01-4705-abf2-5a114c2bb87d` with the actual thread ID from the previous response's `x-ms-thread-id` header. + +Sample response: + +``` +Python is often considered the best choice for beginners among those three. Its clean syntax reads almost like English, making it easier to learn programming concepts without getting overwhelmed by complex syntax. It's also versatile and widely used in education. +``` + +Notice that the agent remembers the context from the previous message (the three programming languages) without you having to specify them again. Because the conversation state is stored durably by the Durable Task Scheduler, this history persists even if you restart the function app or the conversation is resumed by a different instance. + +## Monitor with the Durable Task Scheduler dashboard + +The Durable Task Scheduler provides a built-in dashboard for monitoring and debugging your durable agents. The dashboard offers deep visibility into agent operations, conversation history, and execution flow. + +### Access the dashboard + +1. Open the dashboard for your local DTS emulator at `http://localhost:8082` in your web browser. + +1. Select the **default** task hub from the list to view its details. + +1. Select the gear icon in the top-right corner to open the settings, and ensure that the **Enable Agent pages** option under *Preview Features* is selected. + +### Explore agent conversations + +1. In the dashboard, navigate to the **Agents** tab. + +1. Select your durable agent thread (e.g., `mydurableagent - 263fa373-fa01-4705-abf2-5a114c2bb87d`) from the list. + + You'll see a detailed view of the agent thread, including the complete conversation history with all messages and responses. + + :::image type="content" source="../../media/durable-agent-chat-history-tutorial.png" alt-text="Screenshot of the Durable Task Scheduler dashboard showing an agent thread's conversation history." lightbox="../../media/durable-agent-chat-history-tutorial.png"::: + +The dashboard provides a timeline view to help you understand the flow of the conversation. Key information include: + +- Timestamps and duration for each interaction +- Prompt and response content +- Number of tokens used + +> [!TIP] +> The DTS dashboard provides real-time updates, so you can watch your agent's behavior as you interact with it through the HTTP endpoints. + +## Deploy to Azure + +Now that you've tested your durable agent locally, deploy it to Azure. + +1. Deploy the application: + + ```console + azd deploy + ``` + + This command packages your application and deploys it to the Azure Functions app created during provisioning. + +1. Wait for the deployment to complete. The output will confirm when your agent is running in Azure. + +## Test the deployed agent + +After deployment, test your agent running in Azure. + +### Get the function key + +Azure Functions requires an API key for HTTP-triggered functions in production: + +# [Bash](#tab/bash) + +```bash +API_KEY=`az functionapp function keys list --name $(azd env get-value AZURE_FUNCTION_NAME) --resource-group $(azd env get-value AZURE_RESOURCE_GROUP) --function-name http-MyDurableAgent --query default -o tsv` +``` + +# [PowerShell](#tab/powershell) + +```powershell +$functionName = azd env get-value AZURE_FUNCTION_NAME +$resourceGroup = azd env get-value AZURE_RESOURCE_GROUP +$API_KEY = az functionapp function keys list --name $functionName --resource-group $resourceGroup --function-name http-MyDurableAgent --query default -o tsv +``` + +--- + +### Start a new conversation + +Create a new thread and send your first message to the deployed agent: + +# [Bash](#tab/bash) + +```bash +curl -i -X POST "https://$(azd env get-value AZURE_FUNCTION_NAME).azurewebsites.net/api/agents/MyDurableAgent/run?code=$API_KEY" \ + -H "Content-Type: text/plain" \ + -d "What are three popular programming languages?" +``` + +# [PowerShell](#tab/powershell) + +```powershell +$functionName = azd env get-value AZURE_FUNCTION_NAME +$response = Invoke-WebRequest -Uri "https://$functionName.azurewebsites.net/api/agents/MyDurableAgent/run?code=$API_KEY" ` + -Method POST ` + -Headers @{"Content-Type"="text/plain"} ` + -Body "What are three popular programming languages?" +$response.Headers +$response.Content +``` + +--- + +Note the thread ID returned in the `x-ms-thread-id` response header. + +### Continue the conversation + +Send a follow-up message in the same thread. Replace `` with the thread ID from the previous response: + +# [Bash](#tab/bash) + +```bash +THREAD_ID="" +curl -X POST "https://$(azd env get-value AZURE_FUNCTION_NAME).azurewebsites.net/api/agents/MyDurableAgent/run?code=$API_KEY&thread_id=$THREAD_ID" \ + -H "Content-Type: text/plain" \ + -d "Which is easiest to learn?" +``` + +# [PowerShell](#tab/powershell) + +```powershell +$THREAD_ID = "" +$functionName = azd env get-value AZURE_FUNCTION_NAME +Invoke-RestMethod -Uri "https://$functionName.azurewebsites.net/api/agents/MyDurableAgent/run?code=$API_KEY&thread_id=$THREAD_ID" ` + -Method POST ` + -Headers @{"Content-Type"="text/plain"} ` + -Body "Which is easiest to learn?" +``` + +--- + +The agent maintains conversation context in Azure just as it did locally, demonstrating the durability of the agent state. + +## Monitor the deployed agent + +You can monitor your deployed agent using the Durable Task Scheduler dashboard in Azure. + +1. Get the name of your Durable Task Scheduler instance: + + ```console + azd env get-value DTS_NAME + ``` + +1. Open the [Azure portal](https://portal.azure.com) and search for the Durable Task Scheduler name from the previous step. + +1. In the overview blade of the Durable Task Scheduler resource, select the **default** task hub from the list. + +1. Select **Open Dashboard** at the top of the task hub page to open the monitoring dashboard. + +1. View your agent's conversations just as you did with the local emulator. + +The Azure-hosted dashboard provides the same debugging and monitoring capabilities as the local emulator, allowing you to inspect conversation history, trace tool calls, and analyze performance in your production environment. + +## Understanding durable agent features + +The durable agent you just created provides several important features that differentiate it from standard agents: + +### Stateful conversations + +The agent automatically maintains conversation state across interactions. Each thread has its own isolated conversation history, stored durably in the Durable Task Scheduler. Unlike stateless APIs where you'd need to send the full conversation history with each request, durable agents manage this for you automatically. + +### Serverless hosting + +Your agent runs in Azure Functions with event-driven, pay-per-invocation pricing. When deployed to Azure with the [Flex Consumption plan](/azure/azure-functions/flex-consumption-plan), your agent can scale to thousands of instances during high traffic or down to zero when not in use, ensuring you only pay for actual usage. + +### Built-in HTTP endpoints + +The durable task extension automatically creates HTTP endpoints for your agent, eliminating the need to write custom HTTP handlers or API code. This includes endpoints for creating threads, sending messages, and retrieving conversation history. + +### Durable state management + +All agent state is managed by the Durable Task Scheduler, ensuring that: +- Conversations survive process crashes and restarts. +- State is distributed across multiple instances for high availability. +- Any instance can resume an agent's execution after interruptions. +- Conversation history is maintained reliably even during scaling events. + +## Next steps + +Now that you have a working durable agent, you can explore more advanced features: + +> [!div class="nextstepaction"] +> [Learn about durable agent features](../../user-guide/agents/agent-types/durable-agent/features.md) + +Additional resources: + +- [Durable Task Scheduler Overview](/azure/azure-functions/durable/durable-task-scheduler/durable-task-scheduler) +- [Azure Functions Flex Consumption Plan](/azure/azure-functions/flex-consumption-plan) diff --git a/agent-framework/tutorials/agents/orchestrate-durable-agents.md b/agent-framework/tutorials/agents/orchestrate-durable-agents.md new file mode 100644 index 00000000..44effea7 --- /dev/null +++ b/agent-framework/tutorials/agents/orchestrate-durable-agents.md @@ -0,0 +1,478 @@ +--- +title: Orchestrate durable agents +description: Learn how to orchestrate multiple durable AI agents with fan-out/fan-in patterns for concurrent processing +zone_pivot_groups: programming-languages +author: anthonychu +ms.topic: tutorial +ms.author: antchu +ms.date: 11/07/2025 +ms.service: agent-framework +--- + +# Orchestrate durable agents + +This tutorial shows you how to orchestrate multiple durable AI agents using the fan-out/fan-in patterns. You'll extend the durable agent from the [Create and run a durable agent](create-and-run-durable-agent.md) tutorial to create a multi-agent system that processes a user's question, then translates the response into multiple languages concurrently. + +This orchestration pattern demonstrates how to: +- Reuse the durable agent from the first tutorial. +- Create additional durable agents for language translation. +- Fan out to multiple agents for concurrent processing. +- Fan in results and return them as structured JSON. + +## Prerequisites + +Before you begin, you must complete the [Create and run a durable agent](create-and-run-durable-agent.md) tutorial. This tutorial extends the project created in that tutorial by adding orchestration capabilities. + +## Understanding the orchestration pattern + +The orchestration you'll build follows this flow: + +1. **User input** - A question or message from the user +2. **Main agent** - The `MyDurableAgent` from the first tutorial processes the question +3. **Fan-out** - The main agent's response is sent concurrently to both translation agents +4. **Translation agents** - Two specialized agents translate the response (French and Spanish) +5. **Fan-in** - Results are aggregated into a single JSON response with the original response and translations + +This pattern enables concurrent processing, reducing total response time compared to sequential translation. + +## Register agents at startup + +To properly use agents in durable orchestrations, register them at application startup. They can be used across orchestration executions. + +::: zone pivot="programming-language-csharp" + +Update your `Program.cs` to register the translation agents alongside the existing `MyDurableAgent`: + +```csharp +using System; +using Azure.AI.OpenAI; +using Azure.Identity; +using Microsoft.Agents.AI; +using Microsoft.Agents.AI.Hosting.AzureFunctions; +using Microsoft.Azure.Functions.Worker.Builder; +using Microsoft.Extensions.Hosting; +using OpenAI; +using OpenAI.Chat; + +// Get the Azure OpenAI configuration +string endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") + ?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT is not set."); +string deploymentName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT") + ?? "gpt-4o-mini"; + +// Create the Azure OpenAI client +AzureOpenAIClient client = new(new Uri(endpoint), new DefaultAzureCredential()); +ChatClient chatClient = client.GetChatClient(deploymentName); + +// Create the main agent from the first tutorial +AIAgent mainAgent = chatClient.CreateAIAgent( + instructions: "You are a helpful assistant that can answer questions and provide information.", + name: "MyDurableAgent"); + +// Create translation agents +AIAgent frenchAgent = chatClient.CreateAIAgent( + instructions: "You are a translator. Translate the following text to French. Return only the translation, no explanations.", + name: "FrenchTranslator"); + +AIAgent spanishAgent = chatClient.CreateAIAgent( + instructions: "You are a translator. Translate the following text to Spanish. Return only the translation, no explanations.", + name: "SpanishTranslator"); + +// Build and configure the Functions host +using IHost app = FunctionsApplication + .CreateBuilder(args) + .ConfigureFunctionsWebApplication() + .ConfigureDurableAgents(options => + { + // Register all agents for use in orchestrations and HTTP endpoints + options.AddAIAgent(mainAgent); + options.AddAIAgent(frenchAgent); + options.AddAIAgent(spanishAgent); + }) + .Build(); + +app.Run(); +``` + +This setup: +- Keeps the original `MyDurableAgent` from the first tutorial. +- Creates two new translation agents (French and Spanish). +- Registers all three agents with the Durable Task framework using `options.AddAIAgent()`. +- Makes agents available throughout the application lifetime for individual interactions and orchestrations. + +::: zone-end + +::: zone pivot="programming-language-python" + +Update your `function_app.py` to register the translation agents alongside the existing `MyDurableAgent`: + +```python +import os +from azure.identity import DefaultAzureCredential +from agent_framework.azure import AzureOpenAIChatClient, AgentFunctionApp + +# Get the Azure OpenAI configuration +endpoint = os.getenv("AZURE_OPENAI_ENDPOINT") +if not endpoint: + raise ValueError("AZURE_OPENAI_ENDPOINT is not set.") +deployment_name = os.getenv("AZURE_OPENAI_DEPLOYMENT", "gpt-4o-mini") + +# Create the Azure OpenAI client +chat_client = AzureOpenAIChatClient( + endpoint=endpoint, + deployment_name=deployment_name, + credential=DefaultAzureCredential() +) + +# Create the main agent from the first tutorial +main_agent = chat_client.create_agent( + instructions="You are a helpful assistant that can answer questions and provide information.", + name="MyDurableAgent" +) + +# Create translation agents +french_agent = chat_client.create_agent( + instructions="You are a translator. Translate the following text to French. Return only the translation, no explanations.", + name="FrenchTranslator" +) + +spanish_agent = chat_client.create_agent( + instructions="You are a translator. Translate the following text to Spanish. Return only the translation, no explanations.", + name="SpanishTranslator" +) + +# Create the function app and register all agents +app = AgentFunctionApp(agents=[main_agent, french_agent, spanish_agent]) +``` + +This setup: +- Keeps the original `MyDurableAgent` from the first tutorial. +- Creates two new translation agents (French and Spanish). +- Registers all three agents with the Durable Task framework using `AgentFunctionApp(agents=[...])`. +- Makes agents available throughout the application lifetime for individual interactions and orchestrations. + +::: zone-end + +## Create an orchestration function + +An orchestration function coordinates the workflow across multiple agents. It retrieves registered agents from the durable context and orchestrates their execution, first calling the main agent, then fanning out to translation agents concurrently. + +::: zone pivot="programming-language-csharp" + +Create a new file named `AgentOrchestration.cs` in your project directory: + +```csharp +using System.Collections.Generic; +using System.Threading.Tasks; +using Microsoft.Agents.AI; +using Microsoft.Agents.AI.DurableTask; +using Microsoft.Azure.Functions.Worker; +using Microsoft.DurableTask; + +namespace MyDurableAgent; + +public static class AgentOrchestration +{ + // Define a strongly-typed response structure for agent outputs + public sealed record TextResponse(string Text); + + [Function("agent_orchestration_workflow")] + public static async Task> AgentOrchestrationWorkflow( + [OrchestrationTrigger] TaskOrchestrationContext context) + { + var input = context.GetInput() ?? throw new ArgumentNullException(nameof(context), "Input cannot be null"); + + // Step 1: Get the main agent's response + DurableAIAgent mainAgent = context.GetAgent("MyDurableAgent"); + AgentRunResponse mainResponse = await mainAgent.RunAsync(input); + string agentResponse = mainResponse.Result.Text; + + // Step 2: Fan out - get the translation agents and run them concurrently + DurableAIAgent frenchAgent = context.GetAgent("FrenchTranslator"); + DurableAIAgent spanishAgent = context.GetAgent("SpanishTranslator"); + + Task> frenchTask = frenchAgent.RunAsync(agentResponse); + Task> spanishTask = spanishAgent.RunAsync(agentResponse); + + // Step 3: Wait for both translation tasks to complete (fan-in) + await Task.WhenAll(frenchTask, spanishTask); + + // Get the translation results + TextResponse frenchResponse = (await frenchTask).Result; + TextResponse spanishResponse = (await spanishTask).Result; + + // Step 4: Combine results into a dictionary + var result = new Dictionary + { + ["original"] = agentResponse, + ["french"] = frenchResponse.Text, + ["spanish"] = spanishResponse.Text + }; + + return result; + } +} +``` + +This orchestration demonstrates the proper durable task pattern: +- **Main agent execution**: First calls `MyDurableAgent` to process the user's input. +- **Agent retrieval**: Uses `context.GetAgent()` to get registered agents by name (agents were registered at startup). +- **Sequential then concurrent**: Main agent runs first, then translation agents run concurrently using `Task.WhenAll`. + +::: zone-end + +::: zone pivot="programming-language-python" + +Add the orchestration function to your `function_app.py` file: + +```python +import azure.durable_functions as df + +@app.orchestration_trigger(context_name="context") +def agent_orchestration_workflow(context: df.DurableOrchestrationContext): + """ + Orchestration function that coordinates multiple agents. + Returns a dictionary with the original response and translations. + """ + input_text = context.get_input() + + # Step 1: Get the main agent's response + main_agent = app.get_agent(context, "MyDurableAgent") + main_response = yield main_agent.run(input_text) + agent_response = main_response.get("response", "") + + # Step 2: Fan out - get the translation agents and run them concurrently + french_agent = app.get_agent(context, "FrenchTranslator") + spanish_agent = app.get_agent(context, "SpanishTranslator") + + parallel_tasks = [ + french_agent.run(agent_response), + spanish_agent.run(agent_response) + ] + + # Step 3: Wait for both translation tasks to complete (fan-in) + translations = yield context.task_all(parallel_tasks) + + # Step 4: Combine results into a dictionary + result = { + "original": agent_response, + "french": translations[0].get("response", ""), + "spanish": translations[1].get("response", "") + } + + return result +``` + +This orchestration demonstrates the proper durable task pattern: +- **Main agent execution**: First calls `MyDurableAgent` to process the user's input. +- **Agent retrieval**: Uses `app.get_agent(context, "AgentName")` to get registered agents by name (agents were registered at startup). +- **Sequential then concurrent**: Main agent runs first, then translation agents run concurrently using `context.task_all`. + +::: zone-end + +## Test the orchestration + +Ensure your local development dependencies from the first tutorial are still running: +- **Azurite** in one terminal window +- **Durable Task Scheduler emulator** in another terminal window + +If you've stopped them, restart them now following the instructions in the [Create and run a durable agent](create-and-run-durable-agent.md#start-local-development-dependencies) tutorial. + +With your local development dependencies running: + +1. Start your Azure Functions app in a new terminal window: + + ```console + func start + ``` + +1. The Durable Functions extension automatically creates built-in HTTP endpoints for managing orchestrations. Start the orchestration using the built-in API: + + # [Bash](#tab/bash) + + ```bash + curl -X POST http://localhost:7071/runtime/webhooks/durabletask/orchestrators/agent_orchestration_workflow \ + -H "Content-Type: application/json" \ + -d '"\"What are three popular programming languages?\""' + ``` + + # [PowerShell](#tab/powershell) + + ```powershell + $body = '"What are three popular programming languages?"' + Invoke-RestMethod -Method Post -Uri "http://localhost:7071/runtime/webhooks/durabletask/orchestrators/agent_orchestration_workflow" ` + -ContentType "application/json" ` + -Body $body + ``` + + --- + +1. The response includes URLs for managing the orchestration instance: + + ```json + { + "id": "abc123def456", + "statusQueryGetUri": "http://localhost:7071/runtime/webhooks/durabletask/instances/abc123def456", + "sendEventPostUri": "http://localhost:7071/runtime/webhooks/durabletask/instances/abc123def456/raiseEvent/{eventName}", + "terminatePostUri": "http://localhost:7071/runtime/webhooks/durabletask/instances/abc123def456/terminate", + "purgeHistoryDeleteUri": "http://localhost:7071/runtime/webhooks/durabletask/instances/abc123def456" + } + ``` + +1. Query the orchestration status using the `statusQueryGetUri` (replace `abc123def456` with your actual instance ID): + + # [Bash](#tab/bash) + + ```bash + curl http://localhost:7071/runtime/webhooks/durabletask/instances/abc123def456 + ``` + + # [PowerShell](#tab/powershell) + + ```powershell + Invoke-RestMethod -Uri "http://localhost:7071/runtime/webhooks/durabletask/instances/abc123def456" + ``` + + --- + +1. Initially, the orchestration will be running: + + ```json + { + "name": "agent_orchestration_workflow", + "instanceId": "abc123def456", + "runtimeStatus": "Running", + "input": "What are three popular programming languages?", + "createdTime": "2025-11-07T10:00:00Z", + "lastUpdatedTime": "2025-11-07T10:00:05Z" + } + ``` + +1. Poll the status endpoint until `runtimeStatus` is `Completed`. When complete, you'll see the orchestration output with the main agent's response and its translations: + + ```json + { + "name": "agent_orchestration_workflow", + "instanceId": "abc123def456", + "runtimeStatus": "Completed", + "output": { + "original": "Three popular programming languages are Python, JavaScript, and Java. Python is known for its simplicity...", + "french": "Trois langages de programmation populaires sont Python, JavaScript et Java. Python est connu pour sa simplicité...", + "spanish": "Tres lenguajes de programación populares son Python, JavaScript y Java. Python es conocido por su simplicidad..." + } + } + ``` + +Note that the `original` field contains the response from `MyDurableAgent`, not the original user input. This demonstrates how the orchestration flows from the main agent to the translation agents. + +## Monitor the orchestration in the dashboard + +The Durable Task Scheduler dashboard provides visibility into your orchestration: + +1. Open `http://localhost:8082` in your browser. + +1. Select the "default" task hub. + +1. Select the "Orchestrations" tab. + +1. Find your orchestration instance in the list. + +1. Select the instance to see: + - The orchestration timeline + - Main agent execution followed by concurrent translation agents + - Each agent execution (MyDurableAgent, then French and Spanish translators) + - Fan-out and fan-in patterns visualized + - Timing and duration for each step + +## Understanding the benefits + +This orchestration pattern provides several advantages: + +### Concurrent processing + +The translation agents run in parallel, significantly reducing total response time compared to sequential execution. The main agent runs first to generate a response, then both translations happen concurrently. + +- **.NET**: Uses `Task.WhenAll` to await multiple agent tasks simultaneously. +- **Python**: Uses `context.task_all` to execute multiple agent runs concurrently. + +### Durability and reliability + +The orchestration state is persisted by the Durable Task Scheduler. If an agent execution fails or times out, the orchestration can retry that specific step without restarting the entire workflow. + +### Scalability + +The Azure Functions Flex Consumption plan can scale out to hundreds of instances to handle concurrent translations across many orchestration instances. + +## Deploy to Azure + +Now that you've tested the orchestration locally, deploy the updated application to Azure. + +1. Deploy the updated application using Azure Developer CLI: + + ```console + azd deploy + ``` + + This deploys your updated code with the new orchestration function and additional agents to the Azure Functions app created in the first tutorial. + +1. Wait for the deployment to complete. + +## Test the deployed orchestration + +After deployment, test your orchestration running in Azure. + +1. Get the system key for the durable extension: + + # [Bash](#tab/bash) + + ```bash + SYSTEM_KEY=$(az functionapp keys list --name $(azd env get-value AZURE_FUNCTION_NAME) --resource-group $(azd env get-value AZURE_RESOURCE_GROUP) --query "systemKeys.durabletask_extension" -o tsv) + ``` + + # [PowerShell](#tab/powershell) + + ```powershell + $functionName = azd env get-value AZURE_FUNCTION_NAME + $resourceGroup = azd env get-value AZURE_RESOURCE_GROUP + $SYSTEM_KEY = (az functionapp keys list --name $functionName --resource-group $resourceGroup --query "systemKeys.durabletask_extension" -o tsv) + ``` + + --- + +1. Start the orchestration using the built-in API: + + # [Bash](#tab/bash) + + ```bash + curl -X POST "https://$(azd env get-value AZURE_FUNCTION_NAME).azurewebsites.net/runtime/webhooks/durabletask/orchestrators/agent_orchestration_workflow?code=$SYSTEM_KEY" \ + -H "Content-Type: application/json" \ + -d '"\"What are three popular programming languages?\""' + ``` + + # [PowerShell](#tab/powershell) + + ```powershell + $functionName = azd env get-value AZURE_FUNCTION_NAME + $body = '"What are three popular programming languages?"' + Invoke-RestMethod -Method Post -Uri "https://$functionName.azurewebsites.net/runtime/webhooks/durabletask/orchestrators/agent_orchestration_workflow?code=$SYSTEM_KEY" ` + -ContentType "application/json" ` + -Body $body + ``` + + --- + +1. Use the `statusQueryGetUri` from the response to poll for completion and view the results with translations. + +## Next steps + +Now that you understand durable agent orchestration, you can explore more advanced patterns: + +- **Sequential orchestrations** - Chain agents where each depends on the previous output. +- **Conditional branching** - Route to different agents based on content. +- **Human-in-the-loop** - Pause orchestration for human approval. +- **External events** - Trigger orchestration steps from external systems. + +Additional resources: + +- [Durable Task Scheduler Overview](/azure/azure-functions/durable/durable-task-scheduler/durable-task-scheduler) +- [Durable Functions patterns and concepts](/azure/azure-functions/durable/durable-functions-overview?tabs=in-process%2Cnodejs-v3%2Cv1-model&pivots=csharp) diff --git a/agent-framework/tutorials/plugins/TOC.yml b/agent-framework/tutorials/plugins/TOC.yml new file mode 100644 index 00000000..b8b000c8 --- /dev/null +++ b/agent-framework/tutorials/plugins/TOC.yml @@ -0,0 +1,2 @@ +- name: Use Microsoft Purview SDK with Agent Framework + href: use-purview-with-agent-framework-sdk.md \ No newline at end of file diff --git a/agent-framework/tutorials/plugins/use-purview-with-agent-framework-sdk.md b/agent-framework/tutorials/plugins/use-purview-with-agent-framework-sdk.md new file mode 100644 index 00000000..49c6c366 --- /dev/null +++ b/agent-framework/tutorials/plugins/use-purview-with-agent-framework-sdk.md @@ -0,0 +1,136 @@ +--- +title: Use Microsoft Purview SDK with Agent Framework +description: Learn how to integrate Microsoft Purview SDK for data security and governance in your Agent Framework project +zone_pivot_groups: programming-languages +author: reezaali149 +ms.topic: conceptual +ms.author: v-reezaali +ms.date: 10/28/2025 +ms.service: purview +--- + +# Use Microsoft Purview SDK with Agent Framework + +Microsoft Purview provides enterprise-grade data security, compliance, and governance capabilities for AI applications. By integrating Purview APIs within the Agent Framework SDK, developers can build intelligent agents that are secure by design, while ensuring sensitive data in prompts and responses are protected and compliant with organizational policies. + +## Why integrate Purview with Agent Framework? + +- **Prevent sensitive data leaks**: Inline blocking of sensitive content based on Data Loss Prevention (DLP) policies. +- **Enable governance**: Log AI interactions in Purview for Audit, Communication Compliance, Insider Risk Management, eDiscovery, and Data Lifecycle Management. +- **Accelerate adoption**: Enterprise customers require compliance for AI apps. Purview integration unblocks deployment. + +## Prerequisites + +Before you begin, ensure you have: + +- Microsoft Azure subscription with Microsoft Purview configured. +- Microsoft 365 subscription with an E5 license and pay-as-you-go billing setup. + - For testing, you can use a Microsoft 365 Developer Program tenant. For more information, see [Join the Microsoft 365 Developer Program](https://developer.microsoft.com/en-us/microsoft-365/dev-program). +- Agent Framework SDK: To install the Agent Framework SDK: + - Python: Run `pip install agent-framework`. + - .NET: Install from NuGet. + +## How to integrate Microsoft Purview into your agent + +In your agent's workflow middleware pipeline, you can add Microsoft Purview policy middleware to intercept prompts and responses to determine if they meet the policies set up in Microsoft Purview. The Agent Framework SDK is capable of intercepting agent-to-agent or end-user chat client prompts and responses. + +The following code sample demonstrates how to add the Microsoft Purview policy middleware to your agent code. If you're new to Agent Framework, see [Create and run an agent with Agent Framework](/agent-framework/tutorials/agents/run-agent?pivots=programming-language-python). + +::: zone pivot="programming-language-csharp" + +```csharp + +using Azure.AI.OpenAI; +using Azure.Core; +using Azure.Identity; +using Microsoft.Agents.AI; +using Microsoft.Agents.AI.Purview; +using Microsoft.Extensions.AI; +using OpenAI; + +string endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT is not set."); +string deploymentName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT_NAME") ?? "gpt-4o-mini"; +string purviewClientAppId = Environment.GetEnvironmentVariable("PURVIEW_CLIENT_APP_ID") ?? throw new InvalidOperationException("PURVIEW_CLIENT_APP_ID is not set."); + +TokenCredential browserCredential = new InteractiveBrowserCredential( + new InteractiveBrowserCredentialOptions + { + ClientId = purviewClientAppId + }); + +AIAgent agent = new AzureOpenAIClient( + new Uri(endpoint), + new AzureCliCredential()) + .GetChatClient(deploymentName) + .CreateAIAgent("You are a secure assistant.") + .AsBuilder() + .WithPurview(browserCredential, new PurviewSettings("My Secure Agent")) + .Build(); + +AgentRunResponse response = await agent.RunAsync("Summarize zero trust in one sentence.").ConfigureAwait(false); +Console.WriteLine(response); + +``` + +::: zone-end +::: zone pivot="programming-language-python" + +```python +import asyncio +import os +from agent_framework import ChatAgent, ChatMessage, Role +from agent_framework.azure import AzureOpenAIChatClient +from agent_framework.microsoft import PurviewPolicyMiddleware, PurviewSettings +from azure.identity import AzureCliCredential, InteractiveBrowserCredential + +# Set default environment variables if not already set +os.environ.setdefault("AZURE_OPENAI_ENDPOINT", "") +os.environ.setdefault("AZURE_OPENAI_CHAT_DEPLOYMENT_NAME", "") + +async def main(): + chat_client = AzureOpenAIChatClient(credential=AzureCliCredential()) + purview_middleware = PurviewPolicyMiddleware( + credential=InteractiveBrowserCredential( + client_id="", + ), + settings=PurviewSettings(app_name="My Secure Agent") + ) + agent = ChatAgent( + chat_client=chat_client, + instructions="You are a secure assistant.", + middleware=[purview_middleware] + ) + response = await agent.run(ChatMessage(role=Role.USER, text="Summarize zero trust in one sentence.")) + print(response) + + if __name__ == "__main__": + asyncio.run(main()) +``` + +::: zone-end + +--- + +## Next steps + +Now that you added the above code to your agent, perform the following steps to test the integration of Microsoft Purview into your code: + +1. **Entra registration**: Register your agent and add the required Microsoft Graph permissions ([ProtectionScopes.Compute.All](/graph/api/userprotectionscopecontainer-compute), [ContentActivity.Write](/graph/api/activitiescontainer-post-contentactivities), [Content.Process.All](/graph/api/userdatasecurityandgovernance-processcontent)) to the Service Principal. For more information, see [Register an application in Microsoft Entra ID](/entra/identity-platform/quickstart-register-app) and [dataSecurityAndGovernance resource type](/graph/api/resources/datasecurityandgovernance). You'll need the Microsoft Entra app ID in the next step. +1. **Purview policies**: Configure Purview policies using the Microsoft Entra app ID to enable agent communications data to flow into Purview. For more information, see [Configure Microsoft Purview](/purview/developer/configurepurview). + +## Resources + +::: zone pivot="programming-language-csharp" + +- Nuget: [Microsoft.Agents.AI.Purview](https://www.nuget.org/packages/Microsoft.Agents.AI.Purview/) +- Github: [Microsoft.Agents.AI.Purview](https://github.com/microsoft/agent-framework/tree/main/dotnet/src/Microsoft.Agents.AI.Purview) +- Sample: [AgentWithPurview](https://github.com/microsoft/agent-framework/tree/main/dotnet/samples/Purview/AgentWithPurview) + +::: zone-end +::: zone pivot="programming-language-python" + +- [PyPI Package: Microsoft Agent Framework - Purview Integration (Python)](https://pypi.org/project/agent-framework-purview/). +- [GitHub: Microsoft Agent Framework – Purview Integration (Python) source code](https://github.com/microsoft/agent-framework/tree/main/python/packages/purview). +- [Code Sample: Purview Policy Enforcement Sample (Python)](https://github.com/microsoft/agent-framework/tree/main/python/samples/getting_started/purview_agent). + +::: zone-end diff --git a/agent-framework/tutorials/workflows/TOC.yml b/agent-framework/tutorials/workflows/TOC.yml index 62b94792..cad37247 100644 --- a/agent-framework/tutorials/workflows/TOC.yml +++ b/agent-framework/tutorials/workflows/TOC.yml @@ -9,6 +9,4 @@ - name: Handle requests and responses in workflows href: requests-and-responses.md - name: Checkpointing and resuming workflows - href: checkpointing-and-resuming.md -- name: Visualizing workflows - href: visualization.md \ No newline at end of file + href: checkpointing-and-resuming.md \ No newline at end of file diff --git a/agent-framework/tutorials/workflows/agents-in-workflows.md b/agent-framework/tutorials/workflows/agents-in-workflows.md index b4169c73..cbcc9774 100644 --- a/agent-framework/tutorials/workflows/agents-in-workflows.md +++ b/agent-framework/tutorials/workflows/agents-in-workflows.md @@ -27,6 +27,12 @@ You'll create a workflow that: - Streams real-time updates as agents process requests - Demonstrates proper resource cleanup for Azure Foundry agents +### Concepts Covered + +- [Agents in Workflows](../../user-guide/workflows/using-agents.md) +- [Direct Edges](../../user-guide/workflows/core-concepts/edges.md#direct-edges) +- [Workflow Builder](../../user-guide/workflows/core-concepts/workflows.md) + ## Prerequisites - [.NET 8.0 SDK or later](https://dotnet.microsoft.com/download) @@ -68,18 +74,7 @@ public static class Program var persistentAgentsClient = new PersistentAgentsClient(endpoint, new AzureCliCredential()); ``` -## Step 3: Create Specialized Azure Foundry Agents - -Create three translation agents using the helper method: - -```csharp - // Create agents - AIAgent frenchAgent = await GetTranslationAgentAsync("French", persistentAgentsClient, model); - AIAgent spanishAgent = await GetTranslationAgentAsync("Spanish", persistentAgentsClient, model); - AIAgent englishAgent = await GetTranslationAgentAsync("English", persistentAgentsClient, model); -``` - -## Step 4: Create Agent Factory Method +## Step 3: Create Agent Factory Method Implement a helper method to create Azure Foundry agents with specific instructions: @@ -106,6 +101,17 @@ Implement a helper method to create Azure Foundry agents with specific instructi } ``` +## Step 4: Create Specialized Azure Foundry Agents + +Create three translation agents using the helper method: + +```csharp + // Create agents + AIAgent frenchAgent = await GetTranslationAgentAsync("French", persistentAgentsClient, model); + AIAgent spanishAgent = await GetTranslationAgentAsync("Spanish", persistentAgentsClient, model); + AIAgent englishAgent = await GetTranslationAgentAsync("English", persistentAgentsClient, model); +``` + ## Step 5: Build the Workflow Connect the agents in a sequential workflow using the WorkflowBuilder: @@ -187,6 +193,12 @@ You'll create a workflow that: - Streams real-time updates as agents process requests - Demonstrates proper async context management for Azure AI clients +### Concepts Covered + +- [Agents in Workflows](../../user-guide/workflows/using-agents.md) +- [Direct Edges](../../user-guide/workflows/core-concepts/edges.md#direct-edges) +- [Workflow Builder](../../user-guide/workflows/core-concepts/workflows.md) + ## Prerequisites - Python 3.10 or later diff --git a/agent-framework/tutorials/workflows/checkpointing-and-resuming.md b/agent-framework/tutorials/workflows/checkpointing-and-resuming.md index 07c79871..3c73cc53 100644 --- a/agent-framework/tutorials/workflows/checkpointing-and-resuming.md +++ b/agent-framework/tutorials/workflows/checkpointing-and-resuming.md @@ -13,6 +13,10 @@ ms.service: agent-framework Checkpointing allows workflows to save their state at specific points and resume execution later, even after process restarts. This is crucial for long-running workflows, error recovery, and human-in-the-loop scenarios. +### Concepts Covered + +- [Checkpoints](../../user-guide/workflows/checkpoints.md) + ::: zone pivot="programming-language-csharp" ## Prerequisites @@ -67,14 +71,14 @@ await using Checkpointed checkpointedRun = await InProcessExecutio Executors can persist local state that survives checkpoints using the `Executor` base class: ```csharp -internal sealed class GuessNumberExecutor : Executor +internal sealed class GuessNumberExecutor : Executor("Guess") { private const string StateKey = "GuessNumberExecutor.State"; public int LowerBound { get; private set; } public int UpperBound { get; private set; } - public GuessNumberExecutor() : base("GuessNumber") + public GuessNumberExecutor() : this() { } @@ -446,37 +450,45 @@ Executors can persist local state that survives checkpoints: ```python from agent_framework import Executor, WorkflowContext, handler -class UpperCaseExecutor(Executor): - @handler - async def to_upper_case(self, text: str, ctx: WorkflowContext[str]) -> None: - result = text.upper() - - # Persist executor-local state for checkpoints - prev = await ctx.get_executor_state() or {} - count = int(prev.get("count", 0)) + 1 - await ctx.set_executor_state({ - "count": count, - "last_input": text, - "last_output": result, - }) - - # Send result to next executor - await ctx.send_message(result) -``` - -### Shared State +class WorkerExecutor(Executor): + """Processes numbers to compute their factor pairs and manages executor state for checkpointing.""" -Use shared state for data that multiple executors need to access: + def __init__(self, id: str) -> None: + super().__init__(id=id) + self._composite_number_pairs: dict[int, list[tuple[int, int]]] = {} -```python -class ProcessorExecutor(Executor): @handler - async def process(self, text: str, ctx: WorkflowContext[str]) -> None: - # Write to shared state for cross-executor visibility - await ctx.set_shared_state("original_input", text) - await ctx.set_shared_state("processed_output", text.upper()) - - await ctx.send_message(text.upper()) + async def compute( + self, + task: ComputeTask, + ctx: WorkflowContext[ComputeTask, dict[int, list[tuple[int, int]]]], + ) -> None: + """Process the next number in the task, computing its factor pairs.""" + next_number = task.remaining_numbers.pop(0) + + print(f"WorkerExecutor: Computing factor pairs for {next_number}") + pairs: list[tuple[int, int]] = [] + for i in range(1, next_number): + if next_number % i == 0: + pairs.append((i, next_number // i)) + self._composite_number_pairs[next_number] = pairs + + if not task.remaining_numbers: + # All numbers processed - output the results + await ctx.yield_output(self._composite_number_pairs) + else: + # More numbers to process - continue with remaining task + await ctx.send_message(task) + + @override + async def on_checkpoint_save(self) -> dict[str, Any]: + """Save the executor's internal state for checkpointing.""" + return {"composite_number_pairs": self._composite_number_pairs} + + @override + async def on_checkpoint_restore(self, state: dict[str, Any]) -> None: + """Restore the executor's internal state from a checkpoint.""" + self._composite_number_pairs = state.get("composite_number_pairs", {}) ``` ## Working with Checkpoints @@ -496,24 +508,6 @@ workflow_checkpoints = await checkpoint_storage.list_checkpoints(workflow_id="my sorted_checkpoints = sorted(all_checkpoints, key=lambda cp: cp.timestamp) ``` -### Checkpoint Information - -Access checkpoint metadata and state: - -```python -from agent_framework import get_checkpoint_summary - -for checkpoint in checkpoints: - # Get human-readable summary - summary = get_checkpoint_summary(checkpoint) - - print(f"Checkpoint: {summary.checkpoint_id}") - print(f"Iteration: {summary.iteration_count}") - print(f"Status: {summary.status}") - print(f"Messages: {len(checkpoint.messages)}") - print(f"Shared State: {checkpoint.shared_state}") -``` - ## Resuming from Checkpoints ### Streaming Resume @@ -682,8 +676,3 @@ if __name__ == "__main__": For the complete working implementation, see the [Checkpoint with Resume sample](https://github.com/microsoft/agent-framework/blob/main/python/samples/getting_started/workflows/checkpoint/checkpoint_with_resume.py). ::: zone-end - -## Next Steps - -> [!div class="nextstepaction"] -> [Learn about Workflow Visualization](visualization.md) diff --git a/agent-framework/tutorials/workflows/requests-and-responses.md b/agent-framework/tutorials/workflows/requests-and-responses.md index b644a945..67f2c7b2 100644 --- a/agent-framework/tutorials/workflows/requests-and-responses.md +++ b/agent-framework/tutorials/workflows/requests-and-responses.md @@ -13,6 +13,10 @@ ms.service: agent-framework This tutorial demonstrates how to handle requests and responses in workflows using Agent Framework Workflows. You'll learn how to create interactive workflows that can pause execution to request input from external sources (like humans or other systems) and then resume once a response is provided. +## Concepts Covered + +- [Requests and Responses](../../user-guide/workflows/requests-and-responses.md) + ::: zone pivot="programming-language-csharp" In .NET, human-in-the-loop workflows use `RequestPort` and external request handling to pause execution and gather user input. This pattern enables interactive workflows where the system can request information from external sources during execution. @@ -68,12 +72,12 @@ Create executors that process user input and provide feedback: /// /// Executor that judges the guess and provides feedback. /// -internal sealed class JudgeExecutor : Executor, IMessageHandler +internal sealed class JudgeExecutor : Executor("Judge") { private readonly int _targetNumber; private int _tries; - public JudgeExecutor(int targetNumber) : base("Judge") + public JudgeExecutor(int targetNumber) : this() { _targetNumber = targetNumber; } diff --git a/agent-framework/tutorials/workflows/simple-concurrent-workflow.md b/agent-framework/tutorials/workflows/simple-concurrent-workflow.md index 787e85b1..76ea1cf1 100644 --- a/agent-framework/tutorials/workflows/simple-concurrent-workflow.md +++ b/agent-framework/tutorials/workflows/simple-concurrent-workflow.md @@ -24,6 +24,14 @@ You'll create a workflow that: - Collects and combines responses from both agents into a single output - Demonstrates concurrent execution with AI agents using fan-out/fan-in patterns +### Concepts Covered + +- [Executors](../../user-guide/workflows/core-concepts/executors.md) +- [Fan-out Edges](../../user-guide/workflows/core-concepts/edges.md#fan-out-edges) +- [Fan-in Edges](../../user-guide/workflows/core-concepts/edges.md#fan-in-edges) +- [Workflow Builder](../../user-guide/workflows/core-concepts/workflows.md) +- [Events](../../user-guide/workflows/core-concepts/events.md) + ## Prerequisites - [.NET 8.0 SDK or later](https://dotnet.microsoft.com/download) @@ -101,8 +109,7 @@ The `ConcurrentStartExecutor` implementation: /// /// Executor that starts the concurrent processing by sending messages to the agents. /// -internal sealed class ConcurrentStartExecutor() : - Executor("ConcurrentStartExecutor") +internal sealed class ConcurrentStartExecutor() : Executor("ConcurrentStartExecutor") { /// /// Starts the concurrent processing by sending messages to the agents. @@ -139,7 +146,7 @@ The `ConcurrentAggregationExecutor` implementation: /// Executor that aggregates the results from the concurrent agents. /// internal sealed class ConcurrentAggregationExecutor() : - Executor("ConcurrentAggregationExecutor") + Executor>("ConcurrentAggregationExecutor") { private readonly List _messages = []; @@ -151,9 +158,9 @@ internal sealed class ConcurrentAggregationExecutor() : /// The to monitor for cancellation requests. /// The default is . /// A task representing the asynchronous operation - public override async ValueTask HandleAsync(ChatMessage message, IWorkflowContext context, CancellationToken cancellationToken = default) + public override async ValueTask HandleAsync(List message, IWorkflowContext context, CancellationToken cancellationToken = default) { - this._messages.Add(message); + this._messages.AddRange(message); if (this._messages.Count == 2) { @@ -231,6 +238,14 @@ You'll create a workflow that: - Aggregates the different result types (float and int) into a final output - Demonstrates how the framework handles different result types from concurrent executors +### Concepts Covered + +- [Executors](../../user-guide/workflows/core-concepts/executors.md) +- [Fan-out Edges](../../user-guide/workflows/core-concepts/edges.md#fan-out-edges) +- [Fan-in Edges](../../user-guide/workflows/core-concepts/edges.md#fan-in-edges) +- [Workflow Builder](../../user-guide/workflows/core-concepts/workflows.md) +- [Events](../../user-guide/workflows/core-concepts/events.md) + ## Prerequisites - Python 3.10 or later diff --git a/agent-framework/tutorials/workflows/simple-sequential-workflow.md b/agent-framework/tutorials/workflows/simple-sequential-workflow.md index f96a0e2a..54def91c 100644 --- a/agent-framework/tutorials/workflows/simple-sequential-workflow.md +++ b/agent-framework/tutorials/workflows/simple-sequential-workflow.md @@ -26,11 +26,19 @@ In this tutorial, you'll create a workflow with two executors: The workflow demonstrates core concepts like: -- Creating custom executors that implement `IMessageHandler` +- Creating a custom executor with one handler +- Creating a custom executor from a function - Using `WorkflowBuilder` to connect executors with edges - Processing data through sequential steps - Observing workflow execution through events +### Concepts Covered + +- [Executors](../../user-guide/workflows/core-concepts/executors.md) +- [Direct Edges](../../user-guide/workflows/core-concepts/edges.md#direct-edges) +- [Workflow Builder](../../user-guide/workflows/core-concepts/workflows.md) +- [Events](../../user-guide/workflows/core-concepts/events.md) + ## Prerequisites - [.NET 8.0 SDK or later](https://dotnet.microsoft.com/download) @@ -62,22 +70,14 @@ using Microsoft.Agents.AI.Workflows; /// /// First executor: converts input text to uppercase. /// -internal sealed class UppercaseExecutor() : ReflectingExecutor("UppercaseExecutor"), - IMessageHandler -{ - public ValueTask HandleAsync(string input, IWorkflowContext context, CancellationToken cancellationToken = default) - { - // Convert input to uppercase and pass to next executor - return ValueTask.FromResult(input.ToUpper()); - } -} +Func uppercaseFunc = s => s.ToUpperInvariant(); +var uppercase = uppercaseFunc.BindExecutor("UppercaseExecutor"); ``` **Key Points:** -- Inherits from `ReflectingExecutor` for basic executor functionality -- Implements `IMessageHandler` - takes string input, produces string output -- The `HandleAsync` method processes the input and returns the result +- Create a function that takes a string and returns the uppercase version +- Use `BindExecutor()` to create an executor from the function ### Step 3: Define the Reverse Text Executor @@ -87,32 +87,28 @@ Define an executor that reverses the text: /// /// Second executor: reverses the input text and completes the workflow. /// -internal sealed class ReverseTextExecutor() : ReflectingExecutor("ReverseTextExecutor"), - IMessageHandler +internal sealed class ReverseTextExecutor() : Executor("ReverseTextExecutor") { - public ValueTask HandleAsync(string input, IWorkflowContext context, CancellationToken cancellationToken = default) + public override ValueTask HandleAsync(string input, IWorkflowContext context, CancellationToken cancellationToken = default) { // Reverse the input text return ValueTask.FromResult(new string(input.Reverse().ToArray())); } } + +ReverseTextExecutor reverse = new(); ``` **Key Points:** -- Same pattern as the first executor. -- Reverses the string using LINQ's `Reverse()` method. -- This will be the final executor in the workflow. +- Create a class that inherits from `Executor` +- Implement `HandleAsync()` to process the input and return the output ### Step 4: Build and Connect the Workflow Connect the executors using `WorkflowBuilder`: ```csharp -// Create the executors -UppercaseExecutor uppercase = new(); -ReverseTextExecutor reverse = new(); - // Build the workflow by connecting executors sequentially WorkflowBuilder builder = new(uppercase); builder.AddEdge(uppercase, reverse).WithOutputFrom(reverse); @@ -140,9 +136,6 @@ foreach (WorkflowEvent evt in run.NewEvents) case ExecutorCompletedEvent executorComplete: Console.WriteLine($"{executorComplete.ExecutorId}: {executorComplete.Data}"); break; - case WorkflowOutputEvent workflowOutput: - Console.WriteLine($"Workflow '{workflowOutput.SourceId}' outputs: {workflowOutput.Data}"); - break; } } ``` @@ -162,7 +155,11 @@ The input "Hello, World!" is first converted to uppercase ("HELLO, WORLD!"), the ### Executor Interface -Executors implement `IMessageHandler`: +Executors from functions: + +- Use `BindExecutor()` to create an executor from a function + +Executors implement `Executor`: - **TInput**: The type of data this executor accepts - **TOutput**: The type of data this executor produces @@ -182,16 +179,6 @@ The `WorkflowBuilder` provides a fluent API for constructing workflows: During execution, you can observe these event types: - `ExecutorCompletedEvent` - When an executor finishes processing -- `WorkflowOutputEvent` - Contains the final workflow result (for streaming execution) - -## Running the .NET Example - -1. Create a new console application -2. Install the `Microsoft.Agents.AI.Workflows` NuGet package -3. Combine all the code snippets from the steps above into your `Program.cs` -4. Run the application - -The workflow will process your input through both executors and display the results. ## Complete .NET Example @@ -222,6 +209,13 @@ The workflow demonstrates core concepts like: - Yielding final output with `ctx.yield_output()` - Streaming events for real-time observability +### Concepts Covered + +- [Executors](../../user-guide/workflows/core-concepts/executors.md) +- [Direct Edges](../../user-guide/workflows/core-concepts/edges.md#direct-edges) +- [Workflow Builder](../../user-guide/workflows/core-concepts/workflows.md) +- [Events](../../user-guide/workflows/core-concepts/events.md) + ## Prerequisites - Python 3.10 or later @@ -244,27 +238,35 @@ from agent_framework import WorkflowBuilder, WorkflowContext, WorkflowOutputEven ### Step 2: Create the First Executor -Create an executor that converts text to uppercase using the `@executor` decorator: +Create an executor that converts text to uppercase by implementing an executor with a handler method: ```python -@executor(id="upper_case_executor") -async def to_upper_case(text: str, ctx: WorkflowContext[str]) -> None: - """Transform the input to uppercase and forward it to the next step.""" - result = text.upper() +class UpperCase(Executor): + def __init__(self, id: str): + super().__init__(id=id) - # Send the intermediate result to the next executor - await ctx.send_message(result) + @handler + async def to_upper_case(self, text: str, ctx: WorkflowContext[str]) -> None: + """Convert the input to uppercase and forward it to the next node. + + Note: The WorkflowContext is parameterized with the type this handler will + emit. Here WorkflowContext[str] means downstream nodes should expect str. + """ + result = text.upper() + + # Send the result to the next executor in the workflow. + await ctx.send_message(result) ``` **Key Points:** - The `@executor` decorator registers this function as a workflow node -- `WorkflowContext[str]` indicates this executor sends a string downstream +- `WorkflowContext[str]` indicates this executor sends a string downstream by specifying the first type parameter - `ctx.send_message()` passes data to the next step ### Step 3: Create the Second Executor -Create an executor that reverses the text and yields the final output: +Create an executor that reverses the text and yields the final output from a method decorated with `@executor`: ```python @executor(id="reverse_text_executor") @@ -278,7 +280,7 @@ async def reverse_text(text: str, ctx: WorkflowContext[Never, str]) -> None: **Key Points:** -- `WorkflowContext[Never, str]` indicates this is a terminal executor +- `WorkflowContext[Never, str]` indicates this is a terminal executor that does not send any messages by specifying `Never` as the first type parameter but produce workflow outputs by specifying `str` as the second parameter - `ctx.yield_output()` provides the final workflow result - The workflow completes when it becomes idle @@ -287,10 +289,12 @@ async def reverse_text(text: str, ctx: WorkflowContext[Never, str]) -> None: Connect the executors using `WorkflowBuilder`: ```python +upper_case = UpperCase(id="upper_case_executor") + workflow = ( WorkflowBuilder() - .add_edge(to_upper_case, reverse_text) - .set_start_executor(to_upper_case) + .add_edge(upper_case, reverse_text) + .set_start_executor(upper_case) .build() ) ``` @@ -355,17 +359,9 @@ The `WorkflowBuilder` provides a fluent API for constructing workflows: - **set_start_executor()**: Defines the workflow entry point - **build()**: Finalizes and returns an immutable workflow object -## Running the Example - -1. Combine all the code snippets from the steps above into a single Python file -2. Save it as `sequential_workflow.py` -3. Run with: `python sequential_workflow.py` - -The workflow will process the input "hello world" through both executors and display the streaming events. - ## Complete Example -For the complete, ready-to-run implementation, see the [sequential_streaming.py sample](https://github.com/microsoft/agent-framework/blob/main/python/samples/getting_started/workflows/control-flow/sequential_streaming.py) in the Agent Framework repository. +For the complete, ready-to-run implementation, see the [sample](https://github.com/microsoft/agent-framework/blob/main/python/samples/getting_started/workflows/_start-here/step1_executors_and_edges.py) in the Agent Framework repository. This sample includes: diff --git a/agent-framework/tutorials/workflows/visualization.md b/agent-framework/tutorials/workflows/visualization.md deleted file mode 100644 index ab886900..00000000 --- a/agent-framework/tutorials/workflows/visualization.md +++ /dev/null @@ -1,306 +0,0 @@ ---- -title: Workflow Visualization -description: Learn how to visualize workflows using Agent Framework. -author: TaoChenOSU -ms.topic: tutorial -ms.author: taochen -ms.date: 09/29/2025 -ms.service: agent-framework ---- - -# Visualizing Workflows - - -## Overview - -The Agent Framework provides powerful visualization capabilities for workflows through the `WorkflowViz` class. This allows you to generate visual diagrams of your workflow structure in multiple formats including Mermaid flowcharts, GraphViz DOT diagrams, and exported image files (SVG, PNG, PDF). - -## Getting Started with WorkflowViz - -### Basic Setup - -```python -from agent_framework import WorkflowBuilder, WorkflowViz - -# Create your workflow -workflow = ( - WorkflowBuilder() - .set_start_executor(start_executor) - .add_edge(start_executor, end_executor) - .build() -) - -# Create visualization -viz = WorkflowViz(workflow) -``` - -### Installation Requirements - -For basic text output (Mermaid and DOT), no additional dependencies are needed. For image export: - -```bash -# Install the viz extra -pip install agent-framework graphviz - -# Install GraphViz binaries (required for image export) -# On Ubuntu/Debian: -sudo apt-get install graphviz - -# On macOS: -brew install graphviz - -# On Windows: Download from https://graphviz.org/download/ -``` - -## Visualization Formats - -### Mermaid Flowcharts - -Generate Mermaid syntax for modern, web-friendly diagrams: - -```python -# Generate Mermaid flowchart -mermaid_content = viz.to_mermaid() -print("Mermaid flowchart:") -print(mermaid_content) - -# Example output: -# flowchart TD -# dispatcher["dispatcher (Start)"]; -# researcher["researcher"]; -# marketer["marketer"]; -# legal["legal"]; -# aggregator["aggregator"]; -# dispatcher --> researcher; -# dispatcher --> marketer; -# dispatcher --> legal; -# researcher --> aggregator; -# marketer --> aggregator; -# legal --> aggregator; -``` - -### GraphViz DOT Format - -Generate DOT format for detailed graph representations: - -```python -# Generate DOT diagram -dot_content = viz.to_digraph() -print("DOT diagram:") -print(dot_content) - -# Example output: -# digraph Workflow { -# rankdir=TD; -# node [shape=box, style=filled, fillcolor=lightblue]; -# "dispatcher" [fillcolor=lightgreen, label="dispatcher\n(Start)"]; -# "researcher" [label="researcher"]; -# "marketer" [label="marketer"]; -# ... -# } -``` - -## Image Export - -### Supported Formats - -Export workflows as high-quality images: - -```python -try: - # Export as SVG (vector format, recommended) - svg_file = viz.export(format="svg") - print(f"SVG exported to: {svg_file}") - - # Export as PNG (raster format) - png_file = viz.export(format="png") - print(f"PNG exported to: {png_file}") - - # Export as PDF (vector format) - pdf_file = viz.export(format="pdf") - print(f"PDF exported to: {pdf_file}") - - # Export raw DOT file - dot_file = viz.export(format="dot") - print(f"DOT file exported to: {dot_file}") - -except ImportError: - print("Install 'viz' extra and GraphViz for image export:") - print("pip install agent-framework[viz]") - print("Also install GraphViz binaries for your platform") -``` - -### Custom Filenames - -Specify custom output filenames: - -```python -# Export with custom filename -svg_path = viz.export(format="svg", filename="my_workflow.svg") -png_path = viz.export(format="png", filename="workflow_diagram.png") - -# Convenience methods -svg_path = viz.save_svg("workflow.svg") -png_path = viz.save_png("workflow.png") -pdf_path = viz.save_pdf("workflow.pdf") -``` - -## Workflow Pattern Visualizations - -### Fan-out/Fan-in Patterns - -Visualizations automatically handle complex routing patterns: - -```python -from agent_framework import ( - WorkflowBuilder, WorkflowViz, AgentExecutor, - AgentExecutorRequest, AgentExecutorResponse -) - -# Create agents -researcher = AgentExecutor(chat_client.create_agent(...), id="researcher") -marketer = AgentExecutor(chat_client.create_agent(...), id="marketer") -legal = AgentExecutor(chat_client.create_agent(...), id="legal") - -# Build fan-out/fan-in workflow -workflow = ( - WorkflowBuilder() - .set_start_executor(dispatcher) - .add_fan_out_edges(dispatcher, [researcher, marketer, legal]) # Fan-out - .add_fan_in_edges([researcher, marketer, legal], aggregator) # Fan-in - .build() -) - -# Visualize -viz = WorkflowViz(workflow) -print(viz.to_mermaid()) -``` - -Fan-in nodes are automatically rendered with special styling: - -- **DOT format**: Ellipse shape with light golden background and "fan-in" label -- **Mermaid format**: Double circle nodes `((fan-in))` for clear identification - -### Conditional Edges - -Conditional routing is visualized with distinct styling: - -```python -def spam_condition(content: str) -> bool: - return "spam" in content.lower() - -workflow = ( - WorkflowBuilder() - .add_edge(classifier, spam_handler, condition=spam_condition) - .add_edge(classifier, normal_processor) # Unconditional edge - .build() -) - -viz = WorkflowViz(workflow) -print(viz.to_digraph()) -``` - -Conditional edges appear as: - -- **DOT format**: Dashed lines with "conditional" labels -- **Mermaid format**: Dotted arrows (`-.->`) with "conditional" labels - -### Sub-workflows - -Nested workflows are visualized as clustered subgraphs: - -```python -from agent_framework import WorkflowExecutor - -# Create sub-workflow -sub_workflow = WorkflowBuilder().add_edge(sub_exec1, sub_exec2).build() -sub_workflow_executor = WorkflowExecutor(sub_workflow, id="sub_workflow") - -# Main workflow containing sub-workflow -main_workflow = ( - WorkflowBuilder() - .add_edge(main_executor, sub_workflow_executor) - .add_edge(sub_workflow_executor, final_executor) - .build() -) - -viz = WorkflowViz(main_workflow) -dot_content = viz.to_digraph() # Shows nested clusters -mermaid_content = viz.to_mermaid() # Shows subgraph structures -``` - -## Complete Example - -For a comprehensive example showing workflow visualization with fan-out/fan-in patterns, custom executors, and multiple export formats, see the [Concurrent with Visualization sample](https://github.com/microsoft/agent-framework/blob/main/python/samples/getting_started/workflows/visualization/concurrent_with_visualization.py). - -The sample demonstrates: - -- Expert agent workflow with researcher, marketer, and legal agents -- Custom dispatcher and aggregator executors -- Mermaid and DOT visualization generation -- SVG, PNG, and PDF export capabilities -- Integration with Azure OpenAI agents - -## Visualization Features - -### Node Styling - -- **Start executors**: Green background with "(Start)" label -- **Regular executors**: Blue background with executor ID -- **Fan-in nodes**: Golden background with ellipse shape (DOT) or double circles (Mermaid) - -### Edge Styling - -- **Normal edges**: Solid arrows -- **Conditional edges**: Dashed/dotted arrows with "conditional" labels -- **Fan-out/Fan-in**: Automatic routing through intermediate nodes - -### Layout Options - -- **Top-down layout**: Clear hierarchical flow visualization -- **Subgraph clustering**: Nested workflows shown as grouped clusters -- **Automatic positioning**: GraphViz handles optimal node placement - -## Integration with Development Workflow - -### Documentation Generation - -```python -# Generate documentation diagrams -workflow_viz = WorkflowViz(my_workflow) -doc_diagram = workflow_viz.save_svg("docs/workflow_architecture.svg") -``` - -### Debugging and Analysis - -```python -# Analyze workflow structure -print("Workflow complexity analysis:") -dot_content = viz.to_digraph() -edge_count = dot_content.count(" -> ") -node_count = dot_content.count('[label=') -print(f"Nodes: {node_count}, Edges: {edge_count}") -``` - -### CI/CD Integration - -```python -# Export diagrams for automated documentation -import os -if os.getenv("CI"): - # Export for docs during CI build - viz.save_svg("build/artifacts/workflow.svg") - viz.export(format="dot", filename="build/artifacts/workflow.dot") -``` - -## Best Practices - -1. **Use descriptive executor IDs** - They become node labels in visualizations -2. **Export SVG for documentation** - Vector format scales well in docs -3. **Use Mermaid for web integration** - Copy-paste into Markdown/wiki systems -4. **Leverage fan-in/fan-out visualization** - Clearly shows parallelism patterns -5. **Include visualization in testing** - Verify workflow structure matches expectations - -### Running the Example - -For the complete working implementation with visualization, see the [Concurrent with Visualization sample](https://github.com/microsoft/agent-framework/blob/main/python/samples/getting_started/workflows/visualization/concurrent_with_visualization.py). diff --git a/agent-framework/tutorials/workflows/workflow-with-branching-logic.md b/agent-framework/tutorials/workflows/workflow-with-branching-logic.md index 9153573e..eaae14bb 100644 --- a/agent-framework/tutorials/workflows/workflow-with-branching-logic.md +++ b/agent-framework/tutorials/workflows/workflow-with-branching-logic.md @@ -29,6 +29,10 @@ You'll create an email processing workflow that demonstrates conditional routing - A spam handler that marks suspicious emails. - Shared state management to persist email data between workflow steps. +### Concepts Covered + +- [Conditional Edges](../../user-guide/workflows/core-concepts/edges.md#conditional-edges) + ### Prerequisites - [.NET 8.0 SDK or later](https://dotnet.microsoft.com/download). @@ -375,6 +379,10 @@ You'll create an email processing workflow that demonstrates conditional routing - A legitimate email handler that drafts professional responses - A spam handler that marks suspicious emails +### Concepts Covered + +- [Conditional Edges](../../user-guide/workflows/core-concepts/edges.md#conditional-edges) + ### Prerequisites - Python 3.10 or later @@ -630,6 +638,10 @@ You'll extend the email processing workflow to handle three decision paths: The key improvement is using the `SwitchBuilder` pattern instead of multiple individual conditional edges, making the workflow easier to understand and maintain as decision complexity grows. +### Concepts Covered + +- [Switch-Case Edges](../../user-guide/workflows/core-concepts/edges.md#switch-case-edges) + ### Data Models for Switch-Case Update your data models to support the three-way classification: @@ -995,6 +1007,10 @@ You'll extend the email processing workflow to handle three decision paths: The key improvement is using a single switch-case edge group instead of multiple individual conditional edges, making the workflow easier to understand and maintain as decision complexity grows. +### Concepts Covered + +- [Switch-Case Edges](../../user-guide/workflows/core-concepts/edges.md#switch-case-edges) + ### Enhanced Data Models Update your data models to support the three-way classification: @@ -1270,6 +1286,10 @@ Building on the switch-case example, you'll create an enhanced email processing This pattern enables parallel processing pipelines that adapt to content characteristics. +### Concepts Covered + +- [Fan-out Edges](../../user-guide/workflows/core-concepts/edges.md#fan-out-edges) + ### Data Models for Multi-Selection Extend the data models to support email length analysis and summarization: @@ -1330,16 +1350,16 @@ public static class EmailProcessingConstants } ``` -### Partitioner Function: The Heart of Multi-Selection +### Target Assigner Function: The Heart of Multi-Selection -The partitioner function determines which executors should receive each message: +The target assigner function determines which executors should receive each message: ```csharp /// -/// Creates a partitioner for routing messages based on the analysis result. +/// Creates a target assigner for routing messages based on the analysis result. /// /// A function that takes an analysis result and returns the target partitions. -private static Func> GetPartitioner() +private static Func> GetTargetAssigner() { return (analysisResult, targetCount) => { @@ -1372,7 +1392,7 @@ private static Func> GetPartitioner() } ``` -### Key Features of the Partitioner Function +### Key Features of the Target Assigner Function 1. **Dynamic Target Selection**: Returns a list of executor indices to activate 2. **Content-Aware Routing**: Makes decisions based on message properties like email length @@ -1630,7 +1650,7 @@ public static class Program emailSummaryExecutor, // Index 2: Summarizer (conditionally for long NotSpam) handleUncertainExecutor, // Index 3: Uncertain handler ], - partitioner: GetPartitioner() + targetSelector: GetTargetAssigner() ) // Email assistant branch .AddEdge(emailAssistantExecutor, sendEmailExecutor) @@ -1689,7 +1709,7 @@ builder.AddSwitch(spamDetectionExecutor, switchBuilder => builder.AddFanOutEdge( emailAnalysisExecutor, targets: [handleSpamExecutor, emailAssistantExecutor, emailSummaryExecutor, handleUncertainExecutor], - partitioner: GetPartitioner() // Returns list of target indices + targetSelector: GetTargetAssigner() // Returns list of target indices ) ``` @@ -1747,6 +1767,10 @@ Building on the switch-case example, you'll create an enhanced email processing This pattern enables parallel processing pipelines that adapt to content characteristics. +### Concepts Covered + +- [Fan-Out Edges](../../user-guide/workflows/core-concepts/edges.md#fan-out-edges) + ### Enhanced Data Models for Multi-Selection Extend the data models to support email length analysis and summarization: diff --git a/agent-framework/user-guide/agents/agent-observability.md b/agent-framework/user-guide/agents/agent-observability.md index ed8b3205..5f80920a 100644 --- a/agent-framework/user-guide/agents/agent-observability.md +++ b/agent-framework/user-guide/agents/agent-observability.md @@ -21,7 +21,7 @@ Agent Framework integrates with [OpenTelemetry](https://opentelemetry.io/), and ::: zone pivot="programming-language-csharp" -## Enable Observability +## Enable Observability (C#) To enable observability for your chat client, you need to build the chat client as follows: @@ -165,15 +165,13 @@ See a full example of an agent with OpenTelemetry enabled in the [Agent Framewor ::: zone pivot="programming-language-python" -## Enable Observability +## Enable Observability (Python) -To enable observability in your python application, you do not need to install anything extra, by default the following package are installed: +To enable observability in your python application, in most cases you do not need to install anything extra, by default the following package are installed: ```text "opentelemetry-api", "opentelemetry-sdk", -"azure-monitor-opentelemetry", -"azure-monitor-opentelemetry-exporter", "opentelemetry-exporter-otlp-proto-grpc", "opentelemetry-semantic-conventions-ai", ``` @@ -220,7 +218,7 @@ The easiest way to enable observability for your application is to set the follo This can be used for any compliant OTLP endpoint, such as [OpenTelemetry Collector](https://opentelemetry.io/docs/collector/), [Aspire Dashboard](/dotnet/aspire/fundamentals/dashboard/overview?tabs=bash) or any other OTLP compliant endpoint. - APPLICATIONINSIGHTS_CONNECTION_STRING Default is `None`, set to your Application Insights connection string to export to Azure Monitor. - You can find the connection string in the Azure portal, in the "Overview" section of your Application Insights resource. + You can find the connection string in the Azure portal, in the "Overview" section of your Application Insights resource. This will require the `azure-monitor-opentelemetry-exporter` package to be installed. - VS_CODE_EXTENSION_PORT Default is `4317`, set to the port the AI Toolkit or AzureAI Foundry VS Code extension is running on. @@ -260,6 +258,14 @@ Azure AI Foundry has built-in support for tracing, with a really great visualiza When you have a Azure AI Foundry project setup with a Application Insights resource, you can do the following: +1) Install the `azure-monitor-opentelemetry-exporter` package: + +```bash +pip install azure-monitor-opentelemetry-exporter>=1.0.0b41 +``` + +2) Then you can setup observability for your Azure AI Foundry project as follows: + ```python from agent_framework.azure import AzureAIAgentClient from azure.identity import AzureCliCredential @@ -269,7 +275,22 @@ agent_client = AzureAIAgentClient(credential=AzureCliCredential(), project_endpo await agent_client.setup_azure_ai_observability() ``` -This is a convenience method, that will use the project client, to get the Application Insights connection string, and then call `setup_observability` with that connection string. +This is a convenience method, that will use the project client, to get the Application Insights connection string, and then call `setup_observability` with that connection string, overriding any existing connection string set via environment variable. + +### Zero-code instrumentation + +Because we use the standard OpenTelemetry SDK, you can also use zero-code instrumentation to instrument your application, run you code like this: + +```bash +opentelemetry-instrument \ + --traces_exporter console,otlp \ + --metrics_exporter console \ + --service_name your-service-name \ + --exporter_otlp_endpoint 0.0.0.0:4317 \ + python agent_framework_app.py +``` + +See the [OpenTelemetry Zero-code Python documentation](https://opentelemetry.io/docs/zero-code/python/) for more information and details of the environment variables used. ## Spans and metrics @@ -288,7 +309,7 @@ The metrics that are created are: - For function invocation during the `execute_tool` operations: - `agent_framework.function.invocation.duration` (histogram): This metric measures the duration of each function execution, in seconds. -## Example trace output +### Example trace output When you run an agent with observability enabled, you'll see trace data similar to the following console output: @@ -328,7 +349,7 @@ This trace shows: - **Model information**: The AI system used (OpenAI) and response ID - **Token usage**: Input and output token counts for cost tracking -## Getting started +## Samples We have a number of samples in our repository that demonstrate these capabilities, see the [observability samples folder](https://github.com/microsoft/agent-framework/tree/main/python/samples/getting_started/observability) on Github. That includes samples for using zero-code telemetry as well. diff --git a/agent-framework/user-guide/agents/agent-types/TOC.yml b/agent-framework/user-guide/agents/agent-types/TOC.yml index 9c6b5468..3a1f6a21 100644 --- a/agent-framework/user-guide/agents/agent-types/TOC.yml +++ b/agent-framework/user-guide/agents/agent-types/TOC.yml @@ -20,6 +20,12 @@ href: openai-assistants-agent.md - name: Agent based on any IChatClient href: chat-client-agent.md +- name: Durable Agents + items: + - name: Create a Durable Agent + href: durable-agent/create-durable-agent.md + - name: Durable Agent Features + href: durable-agent/features.md - name: A2A Agents href: a2a-agent.md - name: Custom Agents diff --git a/agent-framework/user-guide/agents/agent-types/anthropic-agent.md b/agent-framework/user-guide/agents/agent-types/anthropic-agent.md index 41b3c404..36e6ef1b 100644 --- a/agent-framework/user-guide/agents/agent-types/anthropic-agent.md +++ b/agent-framework/user-guide/agents/agent-types/anthropic-agent.md @@ -94,6 +94,35 @@ async def explicit_config_example(): print(result.text) ``` +### Using Anthropic on Foundry + +After you've setup Anthropic on Foundry, ensure you have the following environment variables set: + +```bash +ANTHROPIC_FOUNDRY_API_KEY="your-foundry-api-key" +ANTHROPIC_FOUNDRY_RESOURCE="your-foundry-resource-name" +``` +Then create the agent as follows: + +```python +from agent_framework.anthropic import AnthropicClient +from anthropic import AsyncAnthropicFoundry + +async def foundry_example(): + agent = AnthropicClient( + anthropic_client=AsyncAnthropicFoundry() + ).create_agent( + name="FoundryAgent", + instructions="You are a helpful assistant using Anthropic on Foundry.", + ) + + result = await agent.run("How do I use Anthropic on Foundry?") + print(result.text) +``` + +> Note: +> This requires `anthropic>=0.74.0` to be installed. + ## Agent Features ### Function Tools diff --git a/agent-framework/user-guide/agents/agent-types/azure-ai-foundry-agent.md b/agent-framework/user-guide/agents/agent-types/azure-ai-foundry-agent.md index 6854ae8c..f1164dde 100644 --- a/agent-framework/user-guide/agents/agent-types/azure-ai-foundry-agent.md +++ b/agent-framework/user-guide/agents/agent-types/azure-ai-foundry-agent.md @@ -21,7 +21,7 @@ Add the required NuGet packages to your project. ```powershell dotnet add package Azure.Identity -dotnet add package Microsoft.Agents.AI.AzureAI --prerelease +dotnet add package Microsoft.Agents.AI.AzureAI.Persistent --prerelease ``` ## Creating Azure AI Foundry Agents diff --git a/agent-framework/user-guide/agents/agent-types/durable-agent/create-durable-agent.md b/agent-framework/user-guide/agents/agent-types/durable-agent/create-durable-agent.md new file mode 100644 index 00000000..0dabe741 --- /dev/null +++ b/agent-framework/user-guide/agents/agent-types/durable-agent/create-durable-agent.md @@ -0,0 +1,188 @@ +--- +title: Durable Agents +description: Learn how to use the durable task extension for Microsoft Agent Framework to build stateful AI agents with serverless hosting. +zone_pivot_groups: programming-languages +author: anthonychu +ms.topic: tutorial +ms.author: antchu +ms.date: 11/05/2025 +ms.service: agent-framework +--- + +# Durable Agents + +The durable task extension for Microsoft Agent Framework enables you to build stateful AI agents and multi-agent deterministic orchestrations in a serverless environment on Azure. + +[Azure Functions](/azure/azure-functions/functions-overview) is a serverless compute service that lets you run code on-demand without managing infrastructure. The durable task extension for Microsoft Agent Framework builds on this foundation to provide durable state management, meaning your agent's conversation history and execution state are reliably persisted and survive failures, restarts, and long-running operations. + +The extension manages agent thread state and orchestration coordination, allowing you to focus on your agent logic instead of infrastructure concerns for reliability. + +## Key Features + +The durable task extension provides the following key features: + +- **Serverless hosting**: Deploy and host agents in Azure Functions with automatically generated HTTP endpoints for agent interactions +- **Stateful agent threads**: Maintain persistent threads with conversation history that survive across multiple interactions +- **Deterministic orchestrations**: Coordinate multiple agents reliably with fault-tolerant workflows that can run for days or weeks, supporting sequential, parallel, and human-in-the-loop patterns +- **Observability and debugging**: Visualize agent conversations, orchestration flows, and execution history through the built-in Durable Task Scheduler dashboard + +## Getting Started + +::: zone pivot="programming-language-csharp" + +In a .NET Azure Functions project, add the required NuGet packages. + +```bash +dotnet add package Azure.AI.OpenAI --prerelease +dotnet add package Azure.Identity +dotnet add package Microsoft.Agents.AI.OpenAI --prerelease +dotnet add package Microsoft.Agents.AI.Hosting.AzureFunctions --prerelease +``` + +> [!NOTE] +> In addition to these packages, ensure your project uses version 2.2.0 or later of the [Microsoft.Azure.Functions.Worker](https://www.nuget.org/packages/Microsoft.Azure.Functions.Worker/) package. + +::: zone-end + +::: zone pivot="programming-language-python" + +In a Python Azure Functions project, install the required Python packages. + +```bash +pip install azure-identity +pip install agent-framework +pip install agent-framework-azurefunctions +``` + +::: zone-end + +## Serverless Hosting + +With the durable task extension, you can deploy and host Microsoft Agent Framework agents in Azure Functions with built-in HTTP endpoints and orchestration-based invocation. Azure Functions provides event-driven, pay-per-invocation pricing with automatic scaling and minimal infrastructure management. + +When you configure a durable agent, the durable task extension automatically creates HTTP endpoints for your agent and manages all the underlying infrastructure for storing conversation state, handling concurrent requests, and coordinating multi-agent workflows. + +::: zone pivot="programming-language-csharp" + +```csharp +using System; +using Azure.AI.OpenAI; +using Azure.Identity; +using Microsoft.Agents.AI; +using Microsoft.Agents.AI.Hosting.AzureFunctions; +using Microsoft.Azure.Functions.Worker.Builder; +using Microsoft.Extensions.Hosting; + +var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT"); +var deploymentName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT") ?? "gpt-4o-mini"; + +// Create an AI agent following the standard Microsoft Agent Framework pattern +AIAgent agent = new AzureOpenAIClient(new Uri(endpoint), new DefaultAzureCredential()) + .GetChatClient(deploymentName) + .CreateAIAgent( + instructions: "You are good at telling jokes.", + name: "Joker"); + +// Configure the function app to host the agent with durable thread management +// This automatically creates HTTP endpoints and manages state persistence +using IHost app = FunctionsApplication + .CreateBuilder(args) + .ConfigureFunctionsWebApplication() + .ConfigureDurableAgents(options => + options.AddAIAgent(agent) + ) + .Build(); +app.Run(); +``` + +::: zone-end + +::: zone pivot="programming-language-python" + +```python +import os +from agent_framework.azure import AzureOpenAIChatClient, AgentFunctionApp +from azure.identity import DefaultAzureCredential + +endpoint = os.getenv("AZURE_OPENAI_ENDPOINT") +deployment_name = os.getenv("AZURE_OPENAI_DEPLOYMENT_NAME", "gpt-4o-mini") + +# Create an AI agent following the standard Microsoft Agent Framework pattern +agent = AzureOpenAIChatClient( + endpoint=endpoint, + deployment_name=deployment_name, + credential=DefaultAzureCredential() +).create_agent( + instructions="You are good at telling jokes.", + name="Joker" +) + +# Configure the function app to host the agent with durable thread management +# This automatically creates HTTP endpoints and manages state persistence +app = AgentFunctionApp(agents=[agent]) +``` + +::: zone-end + +### When to Use Durable Agents + +Choose durable agents when you need: + +- **Full code control**: Deploy and manage your own compute environment while maintaining serverless benefits +- **Complex orchestrations**: Coordinate multiple agents with deterministic, reliable workflows that can run for days or weeks +- **Event-driven orchestration**: Integrate with Azure Functions triggers (HTTP, timers, queues, etc.) and bindings for event-driven agent workflows +- **Automatic conversation state**: Agent conversation history is automatically managed and persisted without requiring explicit state handling in your code + +This serverless hosting approach differs from managed service-based agent hosting (such as Azure AI Foundry Agent Service), which provides fully managed infrastructure without requiring you to deploy or manage Azure Functions apps. Durable agents are ideal when you need the flexibility of code-first deployment combined with the reliability of durable state management. + +When hosted in the [Azure Functions Flex Consumption](/azure/azure-functions/flex-consumption-plan) hosting plan, agents can scale to thousands of instances or to zero instances when not in use, allowing you to pay only for the compute you need. + +## Stateful Agent Threads with Conversation History + +Agents maintain persistent threads that survive across multiple interactions. Each thread is identified by a unique thread ID and stores the complete conversation history in durable storage managed by the [Durable Task Scheduler](/azure/azure-functions/durable/durable-task-scheduler/durable-task-scheduler). + +This pattern enables conversational continuity where agent state is preserved through process crashes and restarts, allowing full conversation history to be maintained across user threads. The durable storage ensures that even if your Azure Functions instance restarts or scales to a different instance, the conversation seamlessly continues from where it left off. + +The following example demonstrates multiple HTTP requests to the same thread, showing how conversation context persists: + +```bash +# First interaction - start a new thread +curl -X POST https://your-function-app.azurewebsites.net/api/agents/Joker/run \ + -H "Content-Type: text/plain" \ + -d "Tell me a joke about pirates" + +# Response includes thread ID in x-ms-thread-id header and joke as plain text +# HTTP/1.1 200 OK +# Content-Type: text/plain +# x-ms-thread-id: @dafx-joker@263fa373-fa01-4705-abf2-5a114c2bb87d +# +# Why don't pirates shower before they walk the plank? Because they'll just wash up on shore later! + +# Second interaction - continue the same thread with context +curl -X POST "https://your-function-app.azurewebsites.net/api/agents/Joker/run?thread_id=@dafx-joker@263fa373-fa01-4705-abf2-5a114c2bb87d" \ + -H "Content-Type: text/plain" \ + -d "Tell me another one about the same topic" + +# Agent remembers the pirate context from the first message and responds with plain text +# What's a pirate's favorite letter? You'd think it's R, but it's actually the C! +``` + +Agent state is maintained in durable storage, enabling distributed execution across multiple instances. Any instance can resume an agent's execution after interruptions or failures, ensuring continuous operation. + +## Next Steps + +Learn about advanced capabilities of the durable task extension: + +> [!div class="nextstepaction"] +> [Durable Agent Features](features.md) + +For a step-by-step tutorial on building and running a durable agent: + +> [!div class="nextstepaction"] +> [Create and run a durable agent](../../../../tutorials/agents/create-and-run-durable-agent.md) + +## Related Content + +- [Durable Task Scheduler Overview](/azure/azure-functions/durable/durable-task-scheduler/durable-task-scheduler) +- [Azure Functions Flex Consumption Plan](/azure/azure-functions/flex-consumption-plan) +- [Microsoft Agent Framework Overview](../../../../overview/agent-framework-overview.md) diff --git a/agent-framework/user-guide/agents/agent-types/durable-agent/features.md b/agent-framework/user-guide/agents/agent-types/durable-agent/features.md new file mode 100644 index 00000000..92b7425a --- /dev/null +++ b/agent-framework/user-guide/agents/agent-types/durable-agent/features.md @@ -0,0 +1,378 @@ +--- +title: Durable Agent Features +description: Learn about advanced features of the durable task extension for Microsoft Agent Framework including orchestrations, tool calls, and human-in-the-loop workflows. +zone_pivot_groups: programming-languages +author: anthonychu +ms.topic: tutorial +ms.author: antchu +ms.date: 11/05/2025 +ms.service: agent-framework +--- + +# Durable Agent Features + +When you build AI agents with Microsoft Agent Framework, the durable task extension for Microsoft Agent Framework adds advanced capabilities to your standard agents including automatic conversation state management, deterministic orchestrations, and human-in-the-loop patterns. The extension also makes it easy to host your agents on serverless compute provided by Azure Functions, delivering dynamic scaling and a cost-efficient per-request billing model. + +## Deterministic Multi-Agent Orchestrations + +The durable task extension supports building deterministic workflows that coordinate multiple agents using [Azure Durable Functions](/azure/azure-functions/durable/durable-functions-overview) orchestrations. + +**[Orchestrations](/azure/azure-functions/durable/durable-functions-orchestrations)** are code-based workflows that coordinate multiple operations (like agent calls, external API calls, or timers) in a reliable way. **Deterministic** means the orchestration code executes the same way when replayed after a failure, making workflows reliable and debuggable—when you replay an orchestration's history, you can see exactly what happened at each step. + +Orchestrations execute reliably, surviving failures between agent calls, and provide predictable and repeatable processes. This makes them ideal for complex multi-agent scenarios where you need guaranteed execution order and fault tolerance. + +### Sequential Orchestrations + +In the sequential multi-agent pattern, specialized agents execute in a specific order, where each agent's output can influence the next agent's execution. This pattern supports conditional logic and branching based on agent responses. + +::: zone pivot="programming-language-csharp" + +When using agents in orchestrations, you must use the `context.GetAgent()` API to get a `DurableAIAgent` instance, which is a special subclass of the standard `AIAgent` type that wraps one of your registered agents. The `DurableAIAgent` wrapper ensures that agent calls are properly tracked and checkpointed by the durable orchestration framework. + +```csharp +using Microsoft.Azure.Functions.Worker; +using Microsoft.DurableTask; +using Microsoft.Agents.AI.DurableTask; + +[Function(nameof(SpamDetectionOrchestration))] +public static async Task SpamDetectionOrchestration( + [OrchestrationTrigger] TaskOrchestrationContext context) +{ + Email email = context.GetInput(); + + // Check if the email is spam + DurableAIAgent spamDetectionAgent = context.GetAgent("SpamDetectionAgent"); + AgentThread spamThread = spamDetectionAgent.GetNewThread(); + + AgentRunResponse spamDetectionResponse = await spamDetectionAgent.RunAsync( + message: $"Analyze this email for spam: {email.EmailContent}", + thread: spamThread); + DetectionResult result = spamDetectionResponse.Result; + + if (result.IsSpam) + { + return await context.CallActivityAsync(nameof(HandleSpamEmail), result.Reason); + } + + // Generate response for legitimate email + DurableAIAgent emailAssistantAgent = context.GetAgent("EmailAssistantAgent"); + AgentThread emailThread = emailAssistantAgent.GetNewThread(); + + AgentRunResponse emailAssistantResponse = await emailAssistantAgent.RunAsync( + message: $"Draft a professional response to: {email.EmailContent}", + thread: emailThread); + + return await context.CallActivityAsync(nameof(SendEmail), emailAssistantResponse.Result.Response); +} +``` + +::: zone-end + +::: zone pivot="programming-language-python" + +When using agents in orchestrations, you must use the `app.get_agent()` method to get a durable agent instance, which is a special wrapper around one of your registered agents. The durable agent wrapper ensures that agent calls are properly tracked and checkpointed by the durable orchestration framework. + +```python +import azure.durable_functions as df +from typing import cast +from agent_framework.azure import AgentFunctionApp +from pydantic import BaseModel + +class SpamDetectionResult(BaseModel): + is_spam: bool + reason: str + +class EmailResponse(BaseModel): + response: str + +app = AgentFunctionApp(agents=[spam_detection_agent, email_assistant_agent]) + +@app.orchestration_trigger(context_name="context") +def spam_detection_orchestration(context: df.DurableOrchestrationContext): + email = context.get_input() + + # Check if the email is spam + spam_agent = app.get_agent(context, "SpamDetectionAgent") + spam_thread = spam_agent.get_new_thread() + + spam_result_raw = yield spam_agent.run( + messages=f"Analyze this email for spam: {email['content']}", + thread=spam_thread, + response_format=SpamDetectionResult + ) + spam_result = cast(SpamDetectionResult, spam_result_raw.get("structured_response")) + + if spam_result.is_spam: + result = yield context.call_activity("handle_spam_email", spam_result.reason) + return result + + # Generate response for legitimate email + email_agent = app.get_agent(context, "EmailAssistantAgent") + email_thread = email_agent.get_new_thread() + + email_response_raw = yield email_agent.run( + messages=f"Draft a professional response to: {email['content']}", + thread=email_thread, + response_format=EmailResponse + ) + email_response = cast(EmailResponse, email_response_raw.get("structured_response")) + + result = yield context.call_activity("send_email", email_response.response) + return result +``` + +::: zone-end + +Orchestrations coordinate work across multiple agents, surviving failures between agent calls. The orchestration context provides methods to retrieve and interact with hosted agents within orchestrations. + +### Parallel Orchestrations + +In the parallel multi-agent pattern, you execute multiple agents concurrently and then aggregate their results. This pattern is useful for gathering diverse perspectives or processing independent subtasks simultaneously. + +::: zone pivot="programming-language-csharp" + +```csharp +using Microsoft.Azure.Functions.Worker; +using Microsoft.DurableTask; +using Microsoft.Agents.AI.DurableTask; + +[Function(nameof(ResearchOrchestration))] +public static async Task ResearchOrchestration( + [OrchestrationTrigger] TaskOrchestrationContext context) +{ + string topic = context.GetInput(); + + // Execute multiple research agents in parallel + DurableAIAgent technicalAgent = context.GetAgent("TechnicalResearchAgent"); + DurableAIAgent marketAgent = context.GetAgent("MarketResearchAgent"); + DurableAIAgent competitorAgent = context.GetAgent("CompetitorResearchAgent"); + + // Start all agent runs concurrently + Task> technicalTask = + technicalAgent.RunAsync($"Research technical aspects of {topic}"); + Task> marketTask = + marketAgent.RunAsync($"Research market trends for {topic}"); + Task> competitorTask = + competitorAgent.RunAsync($"Research competitors in {topic}"); + + // Wait for all tasks to complete + await Task.WhenAll(technicalTask, marketTask, competitorTask); + + // Aggregate results + string allResearch = string.Join("\n\n", + technicalTask.Result.Result.Text, + marketTask.Result.Result.Text, + competitorTask.Result.Result.Text); + + DurableAIAgent summaryAgent = context.GetAgent("SummaryAgent"); + AgentRunResponse summaryResponse = + await summaryAgent.RunAsync($"Summarize this research:\n{allResearch}"); + + return summaryResponse.Result.Text; +} +``` + +::: zone-end + +::: zone pivot="programming-language-python" + +```python +import azure.durable_functions as df +from agent_framework.azure import AgentFunctionApp + +app = AgentFunctionApp(agents=[technical_agent, market_agent, competitor_agent, summary_agent]) + +@app.orchestration_trigger(context_name="context") +def research_orchestration(context: df.DurableOrchestrationContext): + topic = context.get_input() + + # Execute multiple research agents in parallel + technical_agent = app.get_agent(context, "TechnicalResearchAgent") + market_agent = app.get_agent(context, "MarketResearchAgent") + competitor_agent = app.get_agent(context, "CompetitorResearchAgent") + + technical_task = technical_agent.run(messages=f"Research technical aspects of {topic}") + market_task = market_agent.run(messages=f"Research market trends for {topic}") + competitor_task = competitor_agent.run(messages=f"Research competitors in {topic}") + + # Wait for all tasks to complete + results = yield context.task_all([technical_task, market_task, competitor_task]) + + # Aggregate results + all_research = "\n\n".join([r.get('response', '') for r in results]) + + summary_agent = app.get_agent(context, "SummaryAgent") + summary = yield summary_agent.run(messages=f"Summarize this research:\n{all_research}") + + return summary.get('response', '') +``` + +::: zone-end + +The parallel execution is tracked using a list of tasks. Automatic checkpointing ensures that completed agent executions are not repeated or lost if a failure occurs during aggregation. + +### Human-in-the-Loop Orchestrations + +Deterministic agent orchestrations can pause for human input, approval, or review without consuming compute resources. Durable execution enables orchestrations to wait for days or even weeks while waiting for human responses. When combined with serverless hosting, all compute resources are spun down during the wait period, eliminating compute costs until the human provides their input. + +::: zone pivot="programming-language-csharp" + +```csharp +using Microsoft.Azure.Functions.Worker; +using Microsoft.DurableTask; +using Microsoft.Agents.AI.DurableTask; + +[Function(nameof(ContentApprovalWorkflow))] +public static async Task ContentApprovalWorkflow( + [OrchestrationTrigger] TaskOrchestrationContext context) +{ + string topic = context.GetInput(); + + // Generate content using an agent + DurableAIAgent contentAgent = context.GetAgent("ContentGenerationAgent"); + AgentRunResponse contentResponse = + await contentAgent.RunAsync($"Write an article about {topic}"); + GeneratedContent draftContent = contentResponse.Result; + + // Send for human review + await context.CallActivityAsync(nameof(NotifyReviewer), draftContent); + + // Wait for approval with timeout + HumanApprovalResponse approvalResponse; + try + { + approvalResponse = await context.WaitForExternalEvent( + eventName: "ApprovalDecision", + timeout: TimeSpan.FromHours(24)); + } + catch (OperationCanceledException) + { + // Timeout occurred - escalate for review + return await context.CallActivityAsync(nameof(EscalateForReview), draftContent); + } + + if (approvalResponse.Approved) + { + return await context.CallActivityAsync(nameof(PublishContent), draftContent); + } + + return "Content rejected"; +} +``` + +::: zone-end + +::: zone pivot="programming-language-python" + +```python +import azure.durable_functions as df +from datetime import timedelta +from agent_framework.azure import AgentFunctionApp + +app = AgentFunctionApp(agents=[content_agent]) + +@app.orchestration_trigger(context_name="context") +def content_approval_workflow(context: df.DurableOrchestrationContext): + topic = context.get_input() + + # Generate content using an agent + content_agent = app.get_agent(context, "ContentGenerationAgent") + draft_content = yield content_agent.run( + messages=f"Write an article about {topic}" + ) + + # Send for human review + yield context.call_activity("notify_reviewer", draft_content) + + # Wait for approval with timeout + approval_task = context.wait_for_external_event("ApprovalDecision") + timeout_task = context.create_timer( + context.current_utc_datetime + timedelta(hours=24) + ) + + winner = yield context.task_any([approval_task, timeout_task]) + + if winner == approval_task: + timeout_task.cancel() + approval_data = approval_task.result + if approval_data.get("approved"): + result = yield context.call_activity("publish_content", draft_content) + return result + return "Content rejected" + + # Timeout occurred - escalate for review + result = yield context.call_activity("escalate_for_review", draft_content) + return result +``` + +::: zone-end + +Deterministic agent orchestrations can wait for external events, durably persisting their state while waiting for human feedback, surviving failures, restarts, and extended waiting periods. When the human response arrives, the orchestration automatically resumes with full conversation context and execution state intact. + +### Providing Human Input + +To send approval or input to a waiting orchestration, you'll need to raise an external event to the orchestration instance using the Durable Functions client SDK. For example, a reviewer might approve content through a web form that calls: + +::: zone pivot="programming-language-csharp" + +```csharp +await client.RaiseEventAsync(instanceId, "ApprovalDecision", new HumanApprovalResponse +{ + Approved = true, + Feedback = "Looks great!" +}); +``` + +::: zone-end + +::: zone pivot="programming-language-python" + +```python +approval_data = { + "approved": True, + "feedback": "Looks great!" +} +await client.raise_event(instance_id, "ApprovalDecision", approval_data) +``` + +::: zone-end + +### Cost Efficiency + +Human-in-the-loop workflows with durable agents are extremely cost-effective when hosted on the [Azure Functions Flex Consumption plan](/azure/azure-functions/flex-consumption-plan). For a workflow waiting 24 hours for approval, you only pay for a few seconds of execution time (the time to generate content, send notification, and process the response)—not the 24 hours of waiting. During the wait period, no compute resources are consumed. + +## Observability with Durable Task Scheduler + +The [Durable Task Scheduler](/azure/azure-functions/durable/durable-task-scheduler/durable-task-scheduler) (DTS) is the recommended durable backend for your durable agents, offering the best performance, fully managed infrastructure, and built-in observability through a UI dashboard. While Azure Functions can use other storage backends (like Azure Storage), DTS is optimized specifically for durable workloads and provides superior performance and monitoring capabilities. + +### Agent Thread Insights + +- **Conversation history**: View complete conversation threads for each agent thread, including all messages, tool calls, and conversation context at any point in time +- **Task timing**: Monitor how long specific tasks and agent interactions take to complete + +:::image type="content" source="../../../../media/durable-agent-chat-history.png" alt-text="Screenshot of the Durable Task Scheduler dashboard showing agent chat history with conversation threads and messages."::: + +### Orchestration Insights + +- **Multi-agent visualization**: See the execution flow when calling multiple specialized agents with visual representation of parallel executions and conditional branching +- **Execution history**: Access detailed execution logs +- **Real-time monitoring**: Track active orchestrations, queued work items, and agent states across your deployment +- **Performance metrics**: Monitor agent response times, token usage, and orchestration duration + +:::image type="content" source="../../../../media/durable-agent-orchestration.png" alt-text="Screenshot of the Durable Task Scheduler dashboard showing orchestration visualization with multiple agent interactions and workflow execution."::: + +### Debugging Capabilities + +- View structured agent outputs and tool call results +- Trace tool invocations and their outcomes +- Monitor external event handling for human-in-the-loop scenarios + +The dashboard enables you to understand exactly what your agents are doing, diagnose issues quickly, and optimize performance based on real execution data. + +## Related Content + +- [User guide: create a Durable Agent](create-durable-agent.md) +- [Tutorial: Create and run a durable agent](../../../../tutorials/agents/create-and-run-durable-agent.md) +- [Durable Task Scheduler Overview](/azure/azure-functions/durable/durable-task-scheduler/durable-task-scheduler) +- [Durable Task Scheduler Dashboard](/azure/azure-functions/durable/durable-task-scheduler/durable-task-scheduler-dashboard) +- [Azure Functions Overview](/azure/azure-functions/functions-overview) diff --git a/agent-framework/user-guide/hosting/TOC.yml b/agent-framework/user-guide/hosting/TOC.yml new file mode 100644 index 00000000..993772e3 --- /dev/null +++ b/agent-framework/user-guide/hosting/TOC.yml @@ -0,0 +1,6 @@ +- name: Overview + href: index.md +- name: A2A Integration + href: agent-to-agent-integration.md +- name: OpenAI Integration + href: openai-integration.md \ No newline at end of file diff --git a/agent-framework/user-guide/hosting/agent-to-agent-integration.md b/agent-framework/user-guide/hosting/agent-to-agent-integration.md new file mode 100644 index 00000000..d2a10044 --- /dev/null +++ b/agent-framework/user-guide/hosting/agent-to-agent-integration.md @@ -0,0 +1,256 @@ +--- +title: A2A Integration +description: Learn how to expose Microsoft Agent Framework agents using the Agent-to-Agent (A2A) protocol for inter-agent communication. +author: dmkorolev +ms.service: agent-framework +ms.topic: tutorial +ms.date: 11/11/2025 +ms.author: dmkorolev +--- + +# A2A Integration + +> [!NOTE] +> This tutorial describes A2A integration in .NET apps; Python integration is in the works... + +The Agent-to-Agent (A2A) protocol enables standardized communication between agents, allowing agents built with different frameworks and technologies to communicate seamlessly. The `Microsoft.Agents.AI.Hosting.A2A.AspNetCore` library provides ASP.NET Core integration for exposing your agents via the A2A protocol. + +**NuGet Packages:** +- [Microsoft.Agents.AI.Hosting.A2A](https://www.nuget.org/packages/Microsoft.Agents.AI.Hosting.A2A) +- [Microsoft.Agents.AI.Hosting.A2A.AspNetCore](https://www.nuget.org/packages/Microsoft.Agents.AI.Hosting.A2A.AspNetCore) + +## What is A2A? + +A2A is a standardized protocol that supports: + +- **Agent discovery** through agent cards +- **Message-based communication** between agents +- **Long-running agentic processes** via tasks +- **Cross-platform interoperability** between different agent frameworks + +For more information, see the [A2A protocol specification](https://a2a-protocol.org/latest/). + +## Example + +This minimal example shows how to expose an agent via A2A. The sample includes OpenAPI and Swagger dependencies to simplify testing. + +#### 1. Create an ASP.NET Core Web API project + +Create a new ASP.NET Core Web API project or use an existing one. + +#### 2. Install required dependencies + +Install the following packages: + + ## [.NET CLI](#tab/dotnet-cli) + + Run the following commands in your project directory to install the required NuGet packages: + + ```bash + # Hosting.A2A.AspNetCore for A2A protocol integration + dotnet add package Microsoft.Agents.AI.Hosting.A2A.AspNetCore --prerelease + + # Libraries to connect to Azure OpenAI + dotnet add package Azure.AI.OpenAI --prerelease + dotnet add package Azure.Identity + dotnet add package Microsoft.Extensions.AI + dotnet add package Microsoft.Extensions.AI.OpenAI --prerelease + + # Swagger to test app + dotnet add package Microsoft.AspNetCore.OpenApi + dotnet add package Swashbuckle.AspNetCore + ``` + ## [Package Reference](#tab/package-reference) + + Add the following `` elements to your `.csproj` file within an ``: + + ```xml + + + + + + + + + + + + + + + ``` + + --- + + +#### 3. Configure Azure OpenAI connection + +The application requires an Azure OpenAI connection. Configure the endpoint and deployment name using `dotnet user-secrets` or environment variables. +You can also simply edit the `appsettings.json`, but that's not recommended for the apps deployed in production since some of the data can be considered to be secret. + + ## [User-Secrets](#tab/user-secrets) + ```bash + dotnet user-secrets set "AZURE_OPENAI_ENDPOINT" "https://.openai.azure.com/" + dotnet user-secrets set "AZURE_OPENAI_DEPLOYMENT_NAME" "gpt-4o-mini" + ``` + ## [ENV Windows](#tab/env-windows) + ```powershell + $env:AZURE_OPENAI_ENDPOINT = "https://.openai.azure.com/" + $env:AZURE_OPENAI_DEPLOYMENT_NAME = "gpt-4o-mini" + ``` + ## [ENV unix](#tab/env-unix) + ```bash + export AZURE_OPENAI_ENDPOINT="https://.openai.azure.com/" + export AZURE_OPENAI_DEPLOYMENT_NAME="gpt-4o-mini" + ``` + ## [appsettings](#tab/appsettings) + ```json + "AZURE_OPENAI_ENDPOINT": "https://.openai.azure.com/", + "AZURE_OPENAI_DEPLOYMENT_NAME": "gpt-4o-mini" + ``` + + --- + + +#### 4. Add the code to Program.cs + +Replace the contents of `Program.cs` with the following code and run the application: +```csharp +using A2A.AspNetCore; +using Azure.AI.OpenAI; +using Azure.Identity; +using Microsoft.Agents.AI.Hosting; +using Microsoft.Extensions.AI; + +var builder = WebApplication.CreateBuilder(args); + +builder.Services.AddOpenApi(); +builder.Services.AddSwaggerGen(); + +string endpoint = builder.Configuration["AZURE_OPENAI_ENDPOINT"] + ?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT is not set."); +string deploymentName = builder.Configuration["AZURE_OPENAI_DEPLOYMENT_NAME"] + ?? throw new InvalidOperationException("AZURE_OPENAI_DEPLOYMENT_NAME is not set."); + +// Register the chat client +IChatClient chatClient = new AzureOpenAIClient( + new Uri(endpoint), + new DefaultAzureCredential()) + .GetChatClient(deploymentName) + .AsIChatClient(); +builder.Services.AddSingleton(chatClient); + +// Register an agent +var pirateAgent = builder.AddAIAgent("pirate", instructions: "You are a pirate. Speak like a pirate."); + +var app = builder.Build(); + +app.MapOpenApi(); +app.UseSwagger(); +app.UseSwaggerUI(); + +// Expose the agent via A2A protocol. You can also customize the agentCard +app.MapA2A(pirateAgent, path: "/a2a/pirate", agentCard: new() +{ + Name = "Pirate Agent", + Description = "An agent that speaks like a pirate.", + Version = "1.0" +}); + +app.Run(); +``` + +### Testing the Agent + +Once the application is running, you can test the A2A agent using the following `.http` file or through Swagger UI. + +The input format complies with the A2A specification. You can provide values for: +- `messageId` - A unique identifier for this specific message. You can create your own ID (e.g., a GUID) or set it to `null` to let the agent generate one automatically. +- `contextId` - The conversation identifier. Provide your own ID to start a new conversation or continue an existing one by reusing a previous `contextId`. The agent will maintain conversation history for the same `contextId`. Agent will generate one for you as well, if none is provided. + +```http +# Send A2A request to the pirate agent +POST {{baseAddress}}/a2a/pirate/v1/message:stream +Content-Type: application/json +{ + "message": { + "kind": "message", + "role": "user", + "parts": [ + { + "kind": "text", + "text": "Hey pirate! Tell me where have you been", + "metadata": {} + } + ], + "messageId": null, + "contextId": "foo" + } +} +``` +_Note: Replace `{{baseAddress}}` with your server endpoint._ + +This request returns the following JSON response: +```json +{ + "kind": "message", + "role": "agent", + "parts": [ + { + "kind": "text", + "text": "Arrr, ye scallywag! Ye’ll have to tell me what yer after, or be I walkin’ the plank? 🏴‍☠️" + } + ], + "messageId": "chatcmpl-CXtJbisgIJCg36Z44U16etngjAKRk", + "contextId": "foo" +} +``` + +The response includes the `contextId` (conversation identifier), `messageId` (message identifier), and the actual content from the pirate agent. + +## AgentCard Configuration + +The `AgentCard` provides metadata about your agent for discovery and integration: +```csharp +app.MapA2A(agent, "/a2a/my-agent", agentCard: new() +{ + Name = "My Agent", + Description = "A helpful agent that assists with tasks.", + Version = "1.0", +}); +``` + +You can access the agent card by sending this request: +```http +# Send A2A request to the pirate agent +GET {{baseAddress}}/a2a/pirate/v1/card +``` +_Note: Replace `{{baseAddress}}` with your server endpoint._ + +### AgentCard Properties + +- **Name**: Display name of the agent +- **Description**: Brief description of the agent +- **Version**: Version string for the agent +- **Url**: Endpoint URL (automatically assigned if not specified) +- **Capabilities**: Optional metadata about streaming, push notifications, and other features + +## Exposing Multiple Agents + +You can expose multiple agents in a single application, as long as their endpoints don't collide. Here's an example: + +```csharp +var mathAgent = builder.AddAIAgent("math", instructions: "You are a math expert."); +var scienceAgent = builder.AddAIAgent("science", instructions: "You are a science expert."); + +app.MapA2A(mathAgent, "/a2a/math"); +app.MapA2A(scienceAgent, "/a2a/science"); +``` + +## See Also + +- [Hosting Overview](index.md) +- [OpenAI Integration](openai-integration.md) +- [A2A Protocol Specification](https://a2a-protocol.org/latest/) +- [Agent Discovery](https://github.com/a2aproject/A2A/blob/main/docs/topics/agent-discovery.md) diff --git a/agent-framework/user-guide/hosting/index.md b/agent-framework/user-guide/hosting/index.md new file mode 100644 index 00000000..0f1b68c9 --- /dev/null +++ b/agent-framework/user-guide/hosting/index.md @@ -0,0 +1,116 @@ +--- +title: Hosting Overview +description: Learn how to host AI agents in ASP.NET Core applications using the Agent Framework hosting libraries. +author: dmkorolev +ms.service: agent-framework +ms.topic: overview +ms.date: 11/11/2025 +ms.author: dmkorolev +--- + +# Hosting AI Agents in ASP.NET Core + +The Agent Framework provides a comprehensive set of hosting libraries that enable you to seamlessly integrate AI agents into ASP.NET Core applications. These libraries simplify the process of registering, configuring, and exposing agents through various protocols and interfaces. + +## Overview +As you may already know from the [AI Agents Overview](../../overview/agent-framework-overview.md#ai-agents), `AIAgent` is the fundamental concept of the Agent Framework. It defines an "LLM wrapper" that processes user inputs, makes decisions, calls tools, and performs additional work to execute actions and generate responses. + +However, exposing AI agents from your ASP.NET Core application is not trivial. The Agent Framework hosting libraries solve this by registering AI agents in a dependency injection container, allowing you to resolve and use them in your application services. Additionally, the hosting libraries enable you to manage agent dependencies, such as tools and thread storage, from the same dependency injection container. + +Agents can be hosted alongside your application infrastructure, independent of the protocols they use. Similarly, workflows can be hosted and leverage your application's common infrastructure. + +## Core Hosting Library + +The `Microsoft.Agents.AI.Hosting` library is the foundation for hosting AI agents in ASP.NET Core. It provides the primary APIs for agent registration and configuration. + +In the context of ASP.NET Core applications, `IHostApplicationBuilder` is the fundamental type that represents the builder for hosted applications and services. It manages configuration, logging, lifetime, and more. The Agent Framework hosting libraries provide extensions for `IHostApplicationBuilder` to register and configure AI agents and workflows. + +### Key APIs + +Before configuring agents or workflows, developer needs the `IChatClient` registered in the dependency injection container. +In the examples below, it is registered as keyed singleton under name `chat-model`. This is an example of `IChatClient` registration: +```csharp +// endpoint is of 'https://.openai.azure.com/' format +// deploymentName is `gpt-4o-mini` for example + +IChatClient chatClient = new AzureOpenAIClient( + new Uri(endpoint), + new DefaultAzureCredential()) + .GetChatClient(deploymentName) + .AsIChatClient(); +builder.Services.AddSingleton(chatClient); +``` + +#### AddAIAgent + +Register an AI agent with dependency injection: + +```csharp +var pirateAgent = builder.AddAIAgent( + "pirate", + instructions: "You are a pirate. Speak like a pirate", + description: "An agent that speaks like a pirate.", + chatClientServiceKey: "chat-model"); +``` + +The `AddAIAgent()` method returns an `IHostedAgentBuilder`, which provides a set of extension methods for configuring the `AIAgent`. +For example, you can add tools to the agent: +```csharp +var pirateAgent = builder.AddAIAgent("pirate", instructions: "You are a pirate. Speak like a pirate") + .WithAITool(new MyTool()); // MyTool is a custom type derived from `AITool` +``` + +You can also configure the thread store (storage for conversation data): +```csharp +var pirateAgent = builder.AddAIAgent("pirate", instructions: "You are a pirate. Speak like a pirate") + .WithInMemoryThreadStore(); +``` + +#### AddWorkflow + +Register workflows that coordinate multiple agents. A workflow is essentially a "graph" where each node is an `AIAgent`, and the agents communicate with each other. + +In this example, we register two agents that work sequentially. The user input is first sent to `agent-1`, which produces a response and sends it to `agent-2`. The workflow then outputs the final response. There is also a `BuildConcurrent` method that creates a concurrent agent workflow. + +```csharp +builder.AddAIAgent("agent-1", instructions: "you are agent 1!"); +builder.AddAIAgent("agent-2", instructions: "you are agent 2!"); + +var workflow = builder.AddWorkflow("my-workflow", (sp, key) => +{ + var agent1 = sp.GetRequiredKeyedService("agent-1"); + var agent2 = sp.GetRequiredKeyedService("agent-2"); + return AgentWorkflowBuilder.BuildSequential(key, [agent1, agent2]); +}); +``` + +#### Expose Workflow as AIAgent + +`AIAgent`s benefit from integration APIs that expose them via well-known protocols (such as A2A, OpenAI, and others): +- [OpenAI Integration](openai-integration.md) - Expose agents via OpenAI-compatible APIs +- [A2A Integration](agent-to-agent-integration.md) - Enable agent-to-agent communication + +Currently, workflows do not provide similar integration capabilities. To use these integrations with a workflow, you can convert the workflow into a standalone agent that can be used like any other agent: + +```csharp +var workflowAsAgent = builder + .AddWorkflow("science-workflow", (sp, key) => { ... }) + .AddAsAIAgent(); // Now the workflow can be used as an agent +``` + +## Implementation Details + +The hosting libraries act as protocol adapters that bridge the gap between external communication protocols and the Agent Framework's internal `AIAgent` implementation. When you use a hosting integration library (such as OpenAI Responses or A2A), the library retrieves the registered `AIAgent` from dependency injection and wraps it with protocol-specific middleware. This middleware handles the translation of incoming requests from the external protocol format into Agent Framework models, invokes the `AIAgent` to process the request, and then translates the agent's response back into the protocol's expected output format. This architecture allows you to use public communication protocols seamlessly with `AIAgent` while keeping your agent implementation protocol-agnostic and focused on business logic. + +## Hosting Integration Libraries + +The Agent Framework includes specialized hosting libraries for different integration scenarios: + +- [OpenAI Integration](openai-integration.md) - Expose agents via OpenAI-compatible APIs +- [A2A Integration](agent-to-agent-integration.md) - Enable agent-to-agent communication + +## See Also + +- [AI Agents Overview](../../overview/agent-framework-overview.md) +- [Workflows](../../user-guide/workflows/overview.md) +- [Tools and Capabilities](../../tutorials/agents/function-tools.md) \ No newline at end of file diff --git a/agent-framework/user-guide/hosting/openai-integration.md b/agent-framework/user-guide/hosting/openai-integration.md new file mode 100644 index 00000000..8762bd92 --- /dev/null +++ b/agent-framework/user-guide/hosting/openai-integration.md @@ -0,0 +1,520 @@ +--- +title: OpenAI Integration +description: Learn how to expose Microsoft Agent Framework agents using OpenAI-compatible protocols including Chat Completions and Responses APIs. +author: dmkorolev +ms.service: agent-framework +ms.topic: tutorial +ms.date: 11/11/2025 +ms.author: dmkorolev +--- + +# OpenAI Integration + +> [!NOTE] +> This tutorial describes OpenAI integration in .NET apps; Integration for Python apps is in the works... + +The `Microsoft.Agents.AI.Hosting.OpenAI` library enables you to expose AI agents through OpenAI-compatible HTTP endpoints, supporting both the Chat Completions and Responses APIs. This allows you to integrate your agents with any OpenAI-compatible client or tool. + +**NuGet Package:** +- [Microsoft.Agents.AI.Hosting.OpenAI](https://www.nuget.org/packages/Microsoft.Agents.AI.Hosting.OpenAI) + +## What Are OpenAI Protocols? + +The hosting library supports two OpenAI protocols: + +- **Chat Completions API** - Standard stateless request/response format for chat interactions +- **Responses API** - Advanced format that supports conversations, streaming, and long-running agent processes + +## When to Use Each Protocol + +**The Responses API is now the default and recommended approach** according to OpenAI's documentation. It provides a more comprehensive and feature-rich interface for building AI applications with built-in conversation management, streaming capabilities, and support for long-running processes. + +Use the **Responses API** when: +- Building new applications (recommended default) +- You need server-side conversation management. However, that is not a requirement: you can still use Responses API in stateless mode. +- You want persistent conversation history +- You're building long-running agent processes +- You need advanced streaming capabilities with detailed event types +- You want to track and manage individual responses (e.g., retrieve a specific response by ID, check its status, or cancel a running response) + +Use the **Chat Completions API** when: +- Migrating existing applications that rely on the Chat Completions format +- You need simple, stateless request/response interactions +- State management is handled entirely by your client +- You're integrating with existing tools that only support Chat Completions +- You need maximum compatibility with legacy systems + +## Chat Completions API + +The Chat Completions API provides a simple, stateless interface for interacting with agents using the standard OpenAI chat format. + +### Setting up an agent in ASP.NET Core with ChatCompletions integration + +Here's a complete example exposing an agent via the Chat Completions API: + +#### Prerequisites + +#### 1. Create an ASP.NET Core Web API project + +Create a new ASP.NET Core Web API project or use an existing one. + +#### 2. Install required dependencies + +Install the following packages: + + ## [.NET CLI](#tab/dotnet-cli) + + Run the following commands in your project directory to install the required NuGet packages: + + ```bash + # Hosting.A2A.AspNetCore for OpenAI ChatCompletions/Responses protocol(s) integration + dotnet add package Microsoft.Agents.AI.Hosting.OpenAI --prerelease + + # Libraries to connect to Azure OpenAI + dotnet add package Azure.AI.OpenAI --prerelease + dotnet add package Azure.Identity + dotnet add package Microsoft.Extensions.AI + dotnet add package Microsoft.Extensions.AI.OpenAI --prerelease + + # Swagger to test app + dotnet add package Microsoft.AspNetCore.OpenApi + dotnet add package Swashbuckle.AspNetCore + ``` + ## [Package Reference](#tab/package-reference) + + Add the following `` elements to your `.csproj` file within an ``: + + ```xml + + + + + + + + + + + + + + + + + ``` + + --- + + +#### 3. Configure Azure OpenAI connection + +The application requires an Azure OpenAI connection. Configure the endpoint and deployment name using `dotnet user-secrets` or environment variables. +You can also simply edit the `appsettings.json`, but that's not recommended for the apps deployed in production since some of the data can be considered to be secret. + + ## [User-Secrets](#tab/user-secrets) + ```bash + dotnet user-secrets set "AZURE_OPENAI_ENDPOINT" "https://.openai.azure.com/" + dotnet user-secrets set "AZURE_OPENAI_DEPLOYMENT_NAME" "gpt-4o-mini" + ``` + ## [ENV Windows](#tab/env-windows) + ```powershell + $env:AZURE_OPENAI_ENDPOINT = "https://.openai.azure.com/" + $env:AZURE_OPENAI_DEPLOYMENT_NAME = "gpt-4o-mini" + ``` + ## [ENV unix](#tab/env-unix) + ```bash + export AZURE_OPENAI_ENDPOINT="https://.openai.azure.com/" + export AZURE_OPENAI_DEPLOYMENT_NAME="gpt-4o-mini" + ``` + ## [appsettings](#tab/appsettings) + ```json + "AZURE_OPENAI_ENDPOINT": "https://.openai.azure.com/", + "AZURE_OPENAI_DEPLOYMENT_NAME": "gpt-4o-mini" + ``` + + --- + + +#### 4. Add the code to Program.cs + +Replace the contents of `Program.cs` with the following code: + +```csharp +using Azure.AI.OpenAI; +using Azure.Identity; +using Microsoft.Agents.AI.Hosting; +using Microsoft.Extensions.AI; + +var builder = WebApplication.CreateBuilder(args); + +builder.Services.AddOpenApi(); +builder.Services.AddSwaggerGen(); + +string endpoint = builder.Configuration["AZURE_OPENAI_ENDPOINT"] + ?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT is not set."); +string deploymentName = builder.Configuration["AZURE_OPENAI_DEPLOYMENT_NAME"] + ?? throw new InvalidOperationException("AZURE_OPENAI_DEPLOYMENT_NAME is not set."); + +// Register the chat client +IChatClient chatClient = new AzureOpenAIClient( + new Uri(endpoint), + new DefaultAzureCredential()) + .GetChatClient(deploymentName) + .AsIChatClient(); +builder.Services.AddSingleton(chatClient); + +builder.AddOpenAIChatCompletions(); + +// Register an agent +var pirateAgent = builder.AddAIAgent("pirate", instructions: "You are a pirate. Speak like a pirate."); + +var app = builder.Build(); + +app.MapOpenApi(); +app.UseSwagger(); +app.UseSwaggerUI(); + +// Expose the agent via OpenAI ChatCompletions protocol +app.MapOpenAIChatCompletions(pirateAgent); + +app.Run(); +``` + +### Testing the Chat Completions Endpoint + +Once the application is running, you can test the agent using the OpenAI SDK or HTTP requests: + +#### Using HTTP Request + +```http +POST {{baseAddress}}/pirate/v1/chat/completions +Content-Type: application/json +{ + "model": "pirate", + "stream": false, + "messages": [ + { + "role": "user", + "content": "Hey mate!" + } + ] +} +``` +_Note: Replace `{{baseAddress}}` with your server endpoint._ + +Here is a sample response: +```json +{ + "id": "chatcmpl-nxAZsM6SNI2BRPMbzgjFyvWWULTFr", + "object": "chat.completion", + "created": 1762280028, + "model": "gpt-5", + "choices": [ + { + "index": 0, + "finish_reason": "stop", + "message": { + "role": "assistant", + "content": "Ahoy there, matey! How be ye farin' on this fine day?" + } + } + ], + "usage": { + "completion_tokens": 18, + "prompt_tokens": 22, + "total_tokens": 40, + "completion_tokens_details": { + "accepted_prediction_tokens": 0, + "audio_tokens": 0, + "reasoning_tokens": 0, + "rejected_prediction_tokens": 0 + }, + "prompt_tokens_details": { + "audio_tokens": 0, + "cached_tokens": 0 + } + }, + "service_tier": "default" +} +``` + +The response includes the message ID, content, and usage statistics. + +Chat Completions also supports **streaming**, where output is returned in chunks as soon as content is available. +This capability enables displaying output progressively. You can enable streaming by specifying `"stream": true`. +The output format consists of Server-Sent Events (SSE) chunks as defined in the OpenAI Chat Completions specification. + +```http +POST {{baseAddress}}/pirate/v1/chat/completions +Content-Type: application/json +{ + "model": "pirate", + "stream": true, + "messages": [ + { + "role": "user", + "content": "Hey mate!" + } + ] +} +``` + +And the output we get is a set of ChatCompletions chunks: +``` +data: {"id":"chatcmpl-xwKgBbFtSEQ3OtMf21ctMS2Q8lo93","choices":[],"object":"chat.completion.chunk","created":0,"model":"gpt-5"} + +data: {"id":"chatcmpl-xwKgBbFtSEQ3OtMf21ctMS2Q8lo93","choices":[{"index":0,"finish_reason":"stop","delta":{"content":"","role":"assistant"}}],"object":"chat.completion.chunk","created":0,"model":"gpt-5"} + +... + +data: {"id":"chatcmpl-xwKgBbFtSEQ3OtMf21ctMS2Q8lo93","choices":[],"object":"chat.completion.chunk","created":0,"model":"gpt-5","usage":{"completion_tokens":34,"prompt_tokens":23,"total_tokens":57,"completion_tokens_details":{"accepted_prediction_tokens":0,"audio_tokens":0,"reasoning_tokens":0,"rejected_prediction_tokens":0},"prompt_tokens_details":{"audio_tokens":0,"cached_tokens":0}}} +``` + +The streaming response contains similar information, but delivered as Server-Sent Events. + +## Responses API + +The Responses API provides advanced features including conversation management, streaming, and support for long-running agent processes. + +### Setting up an agent in ASP.NET Core with Responses API integration + +Here's a complete example using the Responses API: + +#### Prerequisites + +Follow the same prerequisites as the Chat Completions example (steps 1-3). + +#### 4. Add the code to Program.cs + +```csharp +using Azure.AI.OpenAI; +using Azure.Identity; +using Microsoft.Agents.AI.Hosting; +using Microsoft.Extensions.AI; + +var builder = WebApplication.CreateBuilder(args); + +builder.Services.AddOpenApi(); +builder.Services.AddSwaggerGen(); + +string endpoint = builder.Configuration["AZURE_OPENAI_ENDPOINT"] + ?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT is not set."); +string deploymentName = builder.Configuration["AZURE_OPENAI_DEPLOYMENT_NAME"] + ?? throw new InvalidOperationException("AZURE_OPENAI_DEPLOYMENT_NAME is not set."); + +// Register the chat client +IChatClient chatClient = new AzureOpenAIClient( + new Uri(endpoint), + new DefaultAzureCredential()) + .GetChatClient(deploymentName) + .AsIChatClient(); +builder.Services.AddSingleton(chatClient); + +builder.AddOpenAIResponses(); +builder.AddOpenAIConversations(); + +// Register an agent +var pirateAgent = builder.AddAIAgent("pirate", instructions: "You are a pirate. Speak like a pirate."); + +var app = builder.Build(); + +app.MapOpenApi(); +app.UseSwagger(); +app.UseSwaggerUI(); + +// Expose the agent via OpenAI Responses protocol +app.MapOpenAIResponses(pirateAgent); +app.MapOpenAIConversations(); + +app.Run(); +``` + +### Testing the Responses API + +The Responses API is similar to Chat Completions but is stateful, allowing you to pass a `conversation` parameter. +Like Chat Completions, it supports the `stream` parameter, which controls the output format: either a single JSON response or a stream of events. +The Responses API defines its own streaming event types, including `response.created`, `response.output_item.added`, `response.output_item.done`, `response.completed`, and others. + +#### Create a Conversation and Response + +You can send a Responses request directly, or you can first create a conversation using the Conversations API +and then link subsequent requests to that conversation. + +To begin, create a new conversation: +```http +POST http://localhost:5209/v1/conversations +Content-Type: application/json +{ + "items": [ + { + "type": "message", + "role": "user", + "content": "Hello!" + } + ] +} +``` + +The response includes the conversation ID: +```json +{ + "id": "conv_E9Ma6nQpRzYxRHxRRqoOWWsDjZVyZfKxlHhfCf02Yxyy9N2y", + "object": "conversation", + "created_at": 1762881679, + "metadata": {} +} +``` + +Next, send a request and specify the conversation parameter. +_(To receive the response as streaming events, set `"stream": true` in the request.)_ +```http +POST http://localhost:5209/pirate/v1/responses +Content-Type: application/json +{ + "stream": false, + "conversation": "conv_E9Ma6nQpRzYxRHxRRqoOWWsDjZVyZfKxlHhfCf02Yxyy9N2y", + "input": [ + { + "type": "message", + "role": "user", + "content": [ + { + "type": "input_text", + "text": "are you a feminist?" + } + ] + } + ] +} +``` + +The agent returns the response and saves the conversation items to storage for later retrieval: +```json +{ + "id": "resp_FP01K4bnMsyQydQhUpovK6ysJJroZMs1pnYCUvEqCZqGCkac", + "conversation": "conv_E9Ma6nQpRzYxRHxRRqoOWWsDjZVyZfKxlHhfCf02Yxyy9N2y", + "object": "response", + "created_at": 1762881518, + "status": "completed", + "incomplete_details": null, + "output": [ + { + "role": "assistant", + "content": [ + { + "type": "output_text", + "text": "Arrr, matey! As a pirate, I be all about respect for the crew, no matter their gender! We sail these seas together, and every hand on deck be valuable. A true buccaneer knows that fairness and equality be what keeps the ship afloat. So, in me own way, I’d say I be supportin’ all hearty souls who seek what be right! What say ye?" + } + ], + "type": "message", + "status": "completed", + "id": "msg_1FAQyZcWgsBdmgJgiXmDyavWimUs8irClHhfCf02Yxyy9N2y" + } + ], + "usage": { + "input_tokens": 26, + "input_tokens_details": { + "cached_tokens": 0 + }, + "output_tokens": 85, + "output_tokens_details": { + "reasoning_tokens": 0 + }, + "total_tokens": 111 + }, + "tool_choice": null, + "temperature": 1, + "top_p": 1 +} +``` + +The response includes conversation and message identifiers, content, and usage statistics. + +To retrieve the conversation items, send this request: +```http +GET http://localhost:5209/v1/conversations/conv_E9Ma6nQpRzYxRHxRRqoOWWsDjZVyZfKxlHhfCf02Yxyy9N2y/items?include=string +``` + +This returns a JSON response containing both input and output messages: +```JSON +{ + "object": "list", + "data": [ + { + "role": "assistant", + "content": [ + { + "type": "output_text", + "text": "Arrr, matey! As a pirate, I be all about respect for the crew, no matter their gender! We sail these seas together, and every hand on deck be valuable. A true buccaneer knows that fairness and equality be what keeps the ship afloat. So, in me own way, I’d say I be supportin’ all hearty souls who seek what be right! What say ye?", + "annotations": [], + "logprobs": [] + } + ], + "type": "message", + "status": "completed", + "id": "msg_1FAQyZcWgsBdmgJgiXmDyavWimUs8irClHhfCf02Yxyy9N2y" + }, + { + "role": "user", + "content": [ + { + "type": "input_text", + "text": "are you a feminist?" + } + ], + "type": "message", + "status": "completed", + "id": "msg_iLVtSEJL0Nd2b3ayr9sJWeV9VyEASMlilHhfCf02Yxyy9N2y" + } + ], + "first_id": "msg_1FAQyZcWgsBdmgJgiXmDyavWimUs8irClHhfCf02Yxyy9N2y", + "last_id": "msg_lUpquo0Hisvo6cLdFXMKdYACqFRWcFDrlHhfCf02Yxyy9N2y", + "has_more": false +} +``` + +## Exposing Multiple Agents + +You can expose multiple agents simultaneously using both protocols: + +```csharp +var mathAgent = builder.AddAIAgent("math", instructions: "You are a math expert."); +var scienceAgent = builder.AddAIAgent("science", instructions: "You are a science expert."); + +// Add both protocols +builder.AddOpenAIChatCompletions(); +builder.AddOpenAIResponses(); + +var app = builder.Build(); + +// Expose both agents via Chat Completions +app.MapOpenAIChatCompletions(mathAgent); +app.MapOpenAIChatCompletions(scienceAgent); + +// Expose both agents via Responses +app.MapOpenAIResponses(mathAgent); +app.MapOpenAIResponses(scienceAgent); +``` + +Agents will be available at: +- Chat Completions: `/math/v1/chat/completions` and `/science/v1/chat/completions` +- Responses: `/math/v1/responses` and `/science/v1/responses` + +## Custom Endpoints + +You can customize the endpoint paths: + +```csharp +// Custom path for Chat Completions +app.MapOpenAIChatCompletions(mathAgent, path: "/api/chat"); + +// Custom path for Responses +app.MapOpenAIResponses(scienceAgent, responsesPath: "/api/responses"); +``` + +## See Also + +- [Hosting Overview](index.md) +- [A2A Integration](agent-to-agent-integration.md) +- [OpenAI Chat Completions API Reference](https://platform.openai.com/docs/api-reference/chat) +- [OpenAI Responses API Reference](https://platform.openai.com/docs/api-reference/responses) diff --git a/agent-framework/user-guide/workflows/core-concepts/edges.md b/agent-framework/user-guide/workflows/core-concepts/edges.md index 2d278df3..2b4736d1 100644 --- a/agent-framework/user-guide/workflows/core-concepts/edges.md +++ b/agent-framework/user-guide/workflows/core-concepts/edges.md @@ -154,10 +154,10 @@ Distribute messages from one executor to multiple targets: // Send to all targets builder.AddFanOutEdge(splitterExecutor, targets: [worker1, worker2, worker3]); -// Send to specific targets based on partitioner function +// Send to specific targets based on target selector function builder.AddFanOutEdge( source: routerExecutor, - partitioner: (message, targetCount) => message.Priority switch + targetSelector: (message, targetCount) => message.Priority switch { Priority.High => [0], // Route to first worker only Priority.Normal => [1, 2], // Route to workers 2 and 3 diff --git a/agent-framework/user-guide/workflows/observability.md b/agent-framework/user-guide/workflows/observability.md index 4f173118..c13592da 100644 --- a/agent-framework/user-guide/workflows/observability.md +++ b/agent-framework/user-guide/workflows/observability.md @@ -1,6 +1,7 @@ --- title: Microsoft Agent Framework Workflows - Observability description: In-depth look at Observability in Microsoft Agent Framework Workflows. +zone_pivot_groups: programming-languages author: TaoChenOSU ms.topic: tutorial ms.author: taochen @@ -12,24 +13,24 @@ ms.service: agent-framework Observability provides insights into the internal state and behavior of workflows during execution. This includes logging, metrics, and tracing capabilities that help monitor and debug workflows. +> [!TIP] +> Observability is a framework-wide feature and is not limited to workflows. For more information, refer to [Agent Observability](../agents/agent-observability.md). + Aside from the standard [GenAI telemetry](https://opentelemetry.io/docs/specs/semconv/gen-ai/), Agent Framework Workflows emits additional spans, logs, and metrics to provide deeper insights into workflow execution. These observability features help developers understand the flow of messages, the performance of executors, and any errors that may occur. ## Enable Observability -Observability is enabled framework-wide by setting the `ENABLE_OTEL=true` environment variable or calling `setup_observability()` at the beginning of your application. +::: zone pivot="programming-language-csharp" + +Please refer to [Enabling Observability](../agents/agent-observability.md#enable-observability-c) for instructions on enabling observability in your applications. + +::: zone-end -```env -# This is not required if you run `setup_observability()` in your code -ENABLE_OTEL=true -# Sensitive data (e.g., message content) will be included in logs and traces if this is set to true -ENABLE_SENSITIVE_DATA=true -``` +::: zone pivot="programming-language-python" -```python -from agent_framework.observability import setup_observability +Please refer to [Enabling Observability](../agents/agent-observability.md#enable-observability-python) for instructions on enabling observability in your applications. -setup_observability(enable_sensitive_data=True) -``` +::: zone-end ## Workflow Spans diff --git a/agent-framework/user-guide/workflows/visualization.md b/agent-framework/user-guide/workflows/visualization.md index 521ab194..bbb71b4f 100644 --- a/agent-framework/user-guide/workflows/visualization.md +++ b/agent-framework/user-guide/workflows/visualization.md @@ -1,6 +1,7 @@ --- title: Microsoft Agent Framework Workflows - Visualization description: In-depth look at Visualization in Microsoft Agent Framework Workflows. +zone_pivot_groups: programming-languages author: TaoChenOSU ms.topic: tutorial ms.author: taochen @@ -12,10 +13,43 @@ ms.service: agent-framework Sometimes a workflow that has multiple executors and complex interactions can be hard to understand from just reading the code. Visualization can help you see the structure of the workflow more clearly, so that you can verify that it has the intended design. -Workflow visualization is done via a `WorkflowViz` object that can be instantiated with a `Workflow` object. The `WorkflowViz` object can then generate visualizations in different formats, such as Graphviz DOT format or Mermaid diagram format. +::: zone pivot="programming-language-csharp" + +Workflow visualization can be achieved via extension methods on the `Workflow` class: `ToMermaidString()`, and `ToDotString()`, which generate Mermaid diagram format and Graphviz DOT format respectively. + +```csharp +using Microsoft.Agents.AI.Workflows; + +// Create a workflow with a fan-out and fan-in pattern +var workflow = new WorkflowBuilder() + .SetStartExecutor(dispatcher) + .AddFanOutEdges(dispatcher, [researcher, marketer, legal]) + .AddFanInEdges([researcher, marketer, legal], aggregator) + .Build(); + +// Mermaid diagram +Console.WriteLine(workflow.ToMermaidString()); + +// DiGraph string +Console.WriteLine(workflow.ToDotString()); +``` + +To create an image file from the DOT format, you can use GraphViz tools with the following command: + +```bash +dotnet run | tail -n +20 | dot -Tpng -o workflow.png +``` > [!TIP] -> To export visualization images you also need to [install GraphViz](https://graphviz.org/download/). +> To export visualization images you need to [install GraphViz](https://graphviz.org/download/). + +For a complete working implementation with visualization, see the [Visualization sample](https://github.com/microsoft/agent-framework/tree/main/dotnet/samples/GettingStarted/Workflows/Visualization). + +::: zone-end + +::: zone pivot="programming-language-python" + +Workflow visualization is done via a `WorkflowViz` object that can be instantiated with a `Workflow` object. The `WorkflowViz` object can then generate visualizations in different formats, such as Graphviz DOT format or Mermaid diagram format. Creating a `WorkflowViz` object is straightforward: @@ -43,8 +77,25 @@ print(viz.to_mermaid()) print(viz.to_digraph()) # Export to a file print(viz.export(format="svg")) +# Different formats are also supported +print(viz.export(format="png")) +print(viz.export(format="pdf")) +print(viz.export(format="dot")) +# Export with custom filenames +print(viz.export(format="svg", filename="my_workflow.svg")) +# Convenience methods +print(viz.save_svg("workflow.svg")) +print(viz.save_png("workflow.png")) +print(viz.save_pdf("workflow.pdf")) ``` +> [!TIP] +> For basic text output (Mermaid and DOT), no additional dependencies are needed. For image export, you need to install the `graphviz` Python package by running: `pip install graphviz>=0.20.0` and [install GraphViz](https://graphviz.org/download/). + +For a complete working implementation with visualization, see the [Concurrent with Visualization sample](https://github.com/microsoft/agent-framework/blob/main/python/samples/getting_started/workflows/visualization/concurrent_with_visualization.py). + +::: zone-end + The exported diagram will look similar to the following for the example workflow: ```mermaid @@ -67,3 +118,23 @@ flowchart TD or in Graphviz DOT format: ![Workflow Diagram](./resources/images/workflow-viz.svg) + +## Visualization Features + +### Node Styling + +- **Start executors**: Green background with "(Start)" label +- **Regular executors**: Blue background with executor ID +- **Fan-in nodes**: Golden background with ellipse shape (DOT) or double circles (Mermaid) + +### Edge Styling + +- **Normal edges**: Solid arrows +- **Conditional edges**: Dashed/dotted arrows with "conditional" labels +- **Fan-out/Fan-in**: Automatic routing through intermediate nodes + +### Layout Options + +- **Top-down layout**: Clear hierarchical flow visualization +- **Subgraph clustering**: Nested workflows shown as grouped clusters +- **Automatic positioning**: GraphViz handles optimal node placement \ No newline at end of file