From 5a470136c03f6b0ae0152872948fb34fb5908926 Mon Sep 17 00:00:00 2001 From: hobostay <110hqc@gmail.com> Date: Fri, 6 Mar 2026 10:26:21 +0800 Subject: [PATCH 1/2] Fix: Standardize agent construction pattern in samples Remove redundant .AsIChatClient() call before .AsAIAgent() since ChatClient already implements IChatClient. This simplifies the code and reduces dependencies as suggested in issue #4505. Changes: - Update all sample files to use consistent pattern: chatClient.AsAIAgent() - Update README documentation to reflect the change - Affects AGUI samples and AGUIWebChat samples --- .../AGUI/Step01_GettingStarted/Server/Program.cs | 2 +- .../AGUI/Step02_BackendTools/Server/Program.cs | 2 +- .../AGUI/Step03_FrontendTools/Server/Program.cs | 2 +- .../AGUI/Step04_HumanInLoop/Server/Program.cs | 2 +- .../AGUI/Step05_StateManagement/Server/Program.cs | 2 +- .../AGUIDojoServer/ChatClientAgentFactory.cs | 14 +++++++------- dotnet/samples/05-end-to-end/AGUIWebChat/README.md | 4 ++-- .../05-end-to-end/AGUIWebChat/Server/Program.cs | 2 +- 8 files changed, 15 insertions(+), 15 deletions(-) diff --git a/dotnet/samples/02-agents/AGUI/Step01_GettingStarted/Server/Program.cs b/dotnet/samples/02-agents/AGUI/Step01_GettingStarted/Server/Program.cs index 936d9430fb..67390ea394 100644 --- a/dotnet/samples/02-agents/AGUI/Step01_GettingStarted/Server/Program.cs +++ b/dotnet/samples/02-agents/AGUI/Step01_GettingStarted/Server/Program.cs @@ -27,7 +27,7 @@ new DefaultAzureCredential()) .GetChatClient(deploymentName); -AIAgent agent = chatClient.AsIChatClient().AsAIAgent( +AIAgent agent = chatClient.AsAIAgent( name: "AGUIAssistant", instructions: "You are a helpful assistant."); diff --git a/dotnet/samples/02-agents/AGUI/Step02_BackendTools/Server/Program.cs b/dotnet/samples/02-agents/AGUI/Step02_BackendTools/Server/Program.cs index 5b55829b45..33a32410e2 100644 --- a/dotnet/samples/02-agents/AGUI/Step02_BackendTools/Server/Program.cs +++ b/dotnet/samples/02-agents/AGUI/Step02_BackendTools/Server/Program.cs @@ -82,7 +82,7 @@ static RestaurantSearchResponse SearchRestaurants( new DefaultAzureCredential()) .GetChatClient(deploymentName); -ChatClientAgent agent = chatClient.AsIChatClient().AsAIAgent( +ChatClientAgent agent = chatClient.AsAIAgent( name: "AGUIAssistant", instructions: "You are a helpful assistant with access to restaurant information.", tools: tools); diff --git a/dotnet/samples/02-agents/AGUI/Step03_FrontendTools/Server/Program.cs b/dotnet/samples/02-agents/AGUI/Step03_FrontendTools/Server/Program.cs index 936d9430fb..67390ea394 100644 --- a/dotnet/samples/02-agents/AGUI/Step03_FrontendTools/Server/Program.cs +++ b/dotnet/samples/02-agents/AGUI/Step03_FrontendTools/Server/Program.cs @@ -27,7 +27,7 @@ new DefaultAzureCredential()) .GetChatClient(deploymentName); -AIAgent agent = chatClient.AsIChatClient().AsAIAgent( +AIAgent agent = chatClient.AsAIAgent( name: "AGUIAssistant", instructions: "You are a helpful assistant."); diff --git a/dotnet/samples/02-agents/AGUI/Step04_HumanInLoop/Server/Program.cs b/dotnet/samples/02-agents/AGUI/Step04_HumanInLoop/Server/Program.cs index b90f59a1d0..edfcd03219 100644 --- a/dotnet/samples/02-agents/AGUI/Step04_HumanInLoop/Server/Program.cs +++ b/dotnet/samples/02-agents/AGUI/Step04_HumanInLoop/Server/Program.cs @@ -60,7 +60,7 @@ static string ApproveExpenseReport(string expenseReportId) new DefaultAzureCredential()) .GetChatClient(deploymentName); -ChatClientAgent baseAgent = openAIChatClient.AsIChatClient().AsAIAgent( +ChatClientAgent baseAgent = openAIChatClient.AsAIAgent( name: "AGUIAssistant", instructions: "You are a helpful assistant in charge of approving expenses", tools: tools); diff --git a/dotnet/samples/02-agents/AGUI/Step05_StateManagement/Server/Program.cs b/dotnet/samples/02-agents/AGUI/Step05_StateManagement/Server/Program.cs index 46637e376b..ba85523124 100644 --- a/dotnet/samples/02-agents/AGUI/Step05_StateManagement/Server/Program.cs +++ b/dotnet/samples/02-agents/AGUI/Step05_StateManagement/Server/Program.cs @@ -37,7 +37,7 @@ new DefaultAzureCredential()) .GetChatClient(deploymentName); -AIAgent baseAgent = chatClient.AsIChatClient().AsAIAgent( +AIAgent baseAgent = chatClient.AsAIAgent( name: "RecipeAgent", instructions: """ You are a helpful recipe assistant. When users ask you to create or suggest a recipe, diff --git a/dotnet/samples/05-end-to-end/AGUIClientServer/AGUIDojoServer/ChatClientAgentFactory.cs b/dotnet/samples/05-end-to-end/AGUIClientServer/AGUIDojoServer/ChatClientAgentFactory.cs index cfb07d2850..0b69e23e04 100644 --- a/dotnet/samples/05-end-to-end/AGUIClientServer/AGUIDojoServer/ChatClientAgentFactory.cs +++ b/dotnet/samples/05-end-to-end/AGUIClientServer/AGUIDojoServer/ChatClientAgentFactory.cs @@ -36,7 +36,7 @@ public static ChatClientAgent CreateAgenticChat() { ChatClient chatClient = s_azureOpenAIClient!.GetChatClient(s_deploymentName!); - return chatClient.AsIChatClient().AsAIAgent( + return chatClient.AsAIAgent( name: "AgenticChat", description: "A simple chat agent using Azure OpenAI"); } @@ -45,7 +45,7 @@ public static ChatClientAgent CreateBackendToolRendering() { ChatClient chatClient = s_azureOpenAIClient!.GetChatClient(s_deploymentName!); - return chatClient.AsIChatClient().AsAIAgent( + return chatClient.AsAIAgent( name: "BackendToolRenderer", description: "An agent that can render backend tools using Azure OpenAI", tools: [AIFunctionFactory.Create( @@ -59,7 +59,7 @@ public static ChatClientAgent CreateHumanInTheLoop() { ChatClient chatClient = s_azureOpenAIClient!.GetChatClient(s_deploymentName!); - return chatClient.AsIChatClient().AsAIAgent( + return chatClient.AsAIAgent( name: "HumanInTheLoopAgent", description: "An agent that involves human feedback in its decision-making process using Azure OpenAI"); } @@ -68,7 +68,7 @@ public static ChatClientAgent CreateToolBasedGenerativeUI() { ChatClient chatClient = s_azureOpenAIClient!.GetChatClient(s_deploymentName!); - return chatClient.AsIChatClient().AsAIAgent( + return chatClient.AsAIAgent( name: "ToolBasedGenerativeUIAgent", description: "An agent that uses tools to generate user interfaces using Azure OpenAI"); } @@ -76,7 +76,7 @@ public static ChatClientAgent CreateToolBasedGenerativeUI() public static AIAgent CreateAgenticUI(JsonSerializerOptions options) { ChatClient chatClient = s_azureOpenAIClient!.GetChatClient(s_deploymentName!); - var baseAgent = chatClient.AsIChatClient().AsAIAgent(new ChatClientAgentOptions + var baseAgent = chatClient.AsAIAgent(new ChatClientAgentOptions { Name = "AgenticUIAgent", Description = "An agent that generates agentic user interfaces using Azure OpenAI", @@ -119,7 +119,7 @@ public static AIAgent CreateSharedState(JsonSerializerOptions options) { ChatClient chatClient = s_azureOpenAIClient!.GetChatClient(s_deploymentName!); - var baseAgent = chatClient.AsIChatClient().AsAIAgent( + var baseAgent = chatClient.AsAIAgent( name: "SharedStateAgent", description: "An agent that demonstrates shared state patterns using Azure OpenAI"); @@ -130,7 +130,7 @@ public static AIAgent CreatePredictiveStateUpdates(JsonSerializerOptions options { ChatClient chatClient = s_azureOpenAIClient!.GetChatClient(s_deploymentName!); - var baseAgent = chatClient.AsIChatClient().AsAIAgent(new ChatClientAgentOptions + var baseAgent = chatClient.AsAIAgent(new ChatClientAgentOptions { Name = "PredictiveStateUpdatesAgent", Description = "An agent that demonstrates predictive state updates using Azure OpenAI", diff --git a/dotnet/samples/05-end-to-end/AGUIWebChat/README.md b/dotnet/samples/05-end-to-end/AGUIWebChat/README.md index 0e42757fa1..721d1bdf41 100644 --- a/dotnet/samples/05-end-to-end/AGUIWebChat/README.md +++ b/dotnet/samples/05-end-to-end/AGUIWebChat/README.md @@ -74,7 +74,7 @@ AzureOpenAIClient azureOpenAIClient = new AzureOpenAIClient( ChatClient chatClient = azureOpenAIClient.GetChatClient(deploymentName); // Create AI agent -ChatClientAgent agent = chatClient.AsIChatClient().AsAIAgent( +ChatClientAgent agent = chatClient.AsAIAgent( name: "ChatAssistant", instructions: "You are a helpful assistant."); @@ -162,7 +162,7 @@ dotnet run Edit the instructions in `Server/Program.cs`: ```csharp -ChatClientAgent agent = chatClient.AsIChatClient().AsAIAgent( +ChatClientAgent agent = chatClient.AsAIAgent( name: "ChatAssistant", instructions: "You are a helpful coding assistant specializing in C# and .NET."); ``` diff --git a/dotnet/samples/05-end-to-end/AGUIWebChat/Server/Program.cs b/dotnet/samples/05-end-to-end/AGUIWebChat/Server/Program.cs index 0b474bb7f4..ca237667a7 100644 --- a/dotnet/samples/05-end-to-end/AGUIWebChat/Server/Program.cs +++ b/dotnet/samples/05-end-to-end/AGUIWebChat/Server/Program.cs @@ -28,7 +28,7 @@ ChatClient chatClient = azureOpenAIClient.GetChatClient(deploymentName); -ChatClientAgent agent = chatClient.AsIChatClient().AsAIAgent( +ChatClientAgent agent = chatClient.AsAIAgent( name: "ChatAssistant", instructions: "You are a helpful assistant."); From 73bf783d0f08c7869f08a1c09b1161a4daac26ba Mon Sep 17 00:00:00 2001 From: hobostay <110hqc@gmail.com> Date: Fri, 6 Mar 2026 23:41:40 +0800 Subject: [PATCH 2/2] fix: add missing using Microsoft.Agents.AI.OpenAI statements Address review feedback from @westey-m: - Add using Microsoft.Agents.AI.OpenAI to files that use chatClient.AsAIAgent() - This is required as AsAIAgent() extension method is defined in this namespace --- .../02-agents/AGUI/Step01_GettingStarted/Server/Program.cs | 1 + .../samples/02-agents/AGUI/Step02_BackendTools/Server/Program.cs | 1 + .../02-agents/AGUI/Step03_FrontendTools/Server/Program.cs | 1 + .../samples/02-agents/AGUI/Step04_HumanInLoop/Server/Program.cs | 1 + .../02-agents/AGUI/Step05_StateManagement/Server/Program.cs | 1 + .../AGUIClientServer/AGUIDojoServer/ChatClientAgentFactory.cs | 1 + dotnet/samples/05-end-to-end/AGUIWebChat/Server/Program.cs | 1 + 7 files changed, 7 insertions(+) diff --git a/dotnet/samples/02-agents/AGUI/Step01_GettingStarted/Server/Program.cs b/dotnet/samples/02-agents/AGUI/Step01_GettingStarted/Server/Program.cs index 67390ea394..30d4e34ac5 100644 --- a/dotnet/samples/02-agents/AGUI/Step01_GettingStarted/Server/Program.cs +++ b/dotnet/samples/02-agents/AGUI/Step01_GettingStarted/Server/Program.cs @@ -4,6 +4,7 @@ using Azure.Identity; using Microsoft.Agents.AI; using Microsoft.Agents.AI.Hosting.AGUI.AspNetCore; +using Microsoft.Agents.AI.OpenAI; using Microsoft.Extensions.AI; using OpenAI.Chat; diff --git a/dotnet/samples/02-agents/AGUI/Step02_BackendTools/Server/Program.cs b/dotnet/samples/02-agents/AGUI/Step02_BackendTools/Server/Program.cs index 33a32410e2..b6223d90ce 100644 --- a/dotnet/samples/02-agents/AGUI/Step02_BackendTools/Server/Program.cs +++ b/dotnet/samples/02-agents/AGUI/Step02_BackendTools/Server/Program.cs @@ -6,6 +6,7 @@ using Azure.Identity; using Microsoft.Agents.AI; using Microsoft.Agents.AI.Hosting.AGUI.AspNetCore; +using Microsoft.Agents.AI.OpenAI; using Microsoft.Extensions.AI; using Microsoft.Extensions.Options; using OpenAI.Chat; diff --git a/dotnet/samples/02-agents/AGUI/Step03_FrontendTools/Server/Program.cs b/dotnet/samples/02-agents/AGUI/Step03_FrontendTools/Server/Program.cs index 67390ea394..30d4e34ac5 100644 --- a/dotnet/samples/02-agents/AGUI/Step03_FrontendTools/Server/Program.cs +++ b/dotnet/samples/02-agents/AGUI/Step03_FrontendTools/Server/Program.cs @@ -4,6 +4,7 @@ using Azure.Identity; using Microsoft.Agents.AI; using Microsoft.Agents.AI.Hosting.AGUI.AspNetCore; +using Microsoft.Agents.AI.OpenAI; using Microsoft.Extensions.AI; using OpenAI.Chat; diff --git a/dotnet/samples/02-agents/AGUI/Step04_HumanInLoop/Server/Program.cs b/dotnet/samples/02-agents/AGUI/Step04_HumanInLoop/Server/Program.cs index edfcd03219..fabf5b361f 100644 --- a/dotnet/samples/02-agents/AGUI/Step04_HumanInLoop/Server/Program.cs +++ b/dotnet/samples/02-agents/AGUI/Step04_HumanInLoop/Server/Program.cs @@ -5,6 +5,7 @@ using Azure.Identity; using Microsoft.Agents.AI; using Microsoft.Agents.AI.Hosting.AGUI.AspNetCore; +using Microsoft.Agents.AI.OpenAI; using Microsoft.AspNetCore.Http.Json; using Microsoft.AspNetCore.HttpLogging; using Microsoft.Extensions.AI; diff --git a/dotnet/samples/02-agents/AGUI/Step05_StateManagement/Server/Program.cs b/dotnet/samples/02-agents/AGUI/Step05_StateManagement/Server/Program.cs index ba85523124..e27a04ed1f 100644 --- a/dotnet/samples/02-agents/AGUI/Step05_StateManagement/Server/Program.cs +++ b/dotnet/samples/02-agents/AGUI/Step05_StateManagement/Server/Program.cs @@ -4,6 +4,7 @@ using Azure.Identity; using Microsoft.Agents.AI; using Microsoft.Agents.AI.Hosting.AGUI.AspNetCore; +using Microsoft.Agents.AI.OpenAI; using Microsoft.Extensions.AI; using Microsoft.Extensions.Options; using OpenAI.Chat; diff --git a/dotnet/samples/05-end-to-end/AGUIClientServer/AGUIDojoServer/ChatClientAgentFactory.cs b/dotnet/samples/05-end-to-end/AGUIClientServer/AGUIDojoServer/ChatClientAgentFactory.cs index 0b69e23e04..920948a15d 100644 --- a/dotnet/samples/05-end-to-end/AGUIClientServer/AGUIDojoServer/ChatClientAgentFactory.cs +++ b/dotnet/samples/05-end-to-end/AGUIClientServer/AGUIDojoServer/ChatClientAgentFactory.cs @@ -9,6 +9,7 @@ using Azure.AI.OpenAI; using Azure.Identity; using Microsoft.Agents.AI; +using Microsoft.Agents.AI.OpenAI; using Microsoft.Extensions.AI; using ChatClient = OpenAI.Chat.ChatClient; diff --git a/dotnet/samples/05-end-to-end/AGUIWebChat/Server/Program.cs b/dotnet/samples/05-end-to-end/AGUIWebChat/Server/Program.cs index ca237667a7..5f4ddc7fad 100644 --- a/dotnet/samples/05-end-to-end/AGUIWebChat/Server/Program.cs +++ b/dotnet/samples/05-end-to-end/AGUIWebChat/Server/Program.cs @@ -6,6 +6,7 @@ using Azure.Identity; using Microsoft.Agents.AI; using Microsoft.Agents.AI.Hosting.AGUI.AspNetCore; +using Microsoft.Agents.AI.OpenAI; using Microsoft.Extensions.AI; using OpenAI.Chat;