diff --git a/dotnet/samples/A2AClientServer/A2AClient/HostClientAgent.cs b/dotnet/samples/A2AClientServer/A2AClient/HostClientAgent.cs index 817e5084e6..5ebae80ffe 100644 --- a/dotnet/samples/A2AClientServer/A2AClient/HostClientAgent.cs +++ b/dotnet/samples/A2AClientServer/A2AClient/HostClientAgent.cs @@ -4,6 +4,7 @@ using Microsoft.Extensions.AI; using Microsoft.Extensions.Logging; using OpenAI; +using OpenAI.Chat; namespace A2A; diff --git a/dotnet/samples/A2AClientServer/A2AServer/HostAgentFactory.cs b/dotnet/samples/A2AClientServer/A2AServer/HostAgentFactory.cs index 81fd24c595..9c4fbaaf2c 100644 --- a/dotnet/samples/A2AClientServer/A2AServer/HostAgentFactory.cs +++ b/dotnet/samples/A2AClientServer/A2AServer/HostAgentFactory.cs @@ -6,6 +6,7 @@ using Microsoft.Agents.AI; using Microsoft.Extensions.AI; using OpenAI; +using OpenAI.Chat; namespace A2AServer; diff --git a/dotnet/samples/AGUIClientServer/AGUIServer/Program.cs b/dotnet/samples/AGUIClientServer/AGUIServer/Program.cs index 4ecf9a8429..bcfd86e60d 100644 --- a/dotnet/samples/AGUIClientServer/AGUIServer/Program.cs +++ b/dotnet/samples/AGUIClientServer/AGUIServer/Program.cs @@ -6,7 +6,7 @@ using Azure.Identity; using Microsoft.Agents.AI.Hosting.AGUI.AspNetCore; using Microsoft.Extensions.AI; -using OpenAI; +using OpenAI.Chat; WebApplicationBuilder builder = WebApplication.CreateBuilder(args); builder.Services.AddHttpClient().AddLogging(); diff --git a/dotnet/samples/AzureFunctions/01_SingleAgent/Program.cs b/dotnet/samples/AzureFunctions/01_SingleAgent/Program.cs index 60b3103adc..b3d40a120c 100644 --- a/dotnet/samples/AzureFunctions/01_SingleAgent/Program.cs +++ b/dotnet/samples/AzureFunctions/01_SingleAgent/Program.cs @@ -7,7 +7,7 @@ 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 endpoint and deployment name from environment variables. string endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") diff --git a/dotnet/samples/AzureFunctions/02_AgentOrchestration_Chaining/Program.cs b/dotnet/samples/AzureFunctions/02_AgentOrchestration_Chaining/Program.cs index 41f643a763..3776ecd062 100644 --- a/dotnet/samples/AzureFunctions/02_AgentOrchestration_Chaining/Program.cs +++ b/dotnet/samples/AzureFunctions/02_AgentOrchestration_Chaining/Program.cs @@ -7,7 +7,7 @@ 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 endpoint and deployment name from environment variables. string endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") diff --git a/dotnet/samples/AzureFunctions/03_AgentOrchestration_Concurrency/Program.cs b/dotnet/samples/AzureFunctions/03_AgentOrchestration_Concurrency/Program.cs index 5a6fbaf203..dfc2049d45 100644 --- a/dotnet/samples/AzureFunctions/03_AgentOrchestration_Concurrency/Program.cs +++ b/dotnet/samples/AzureFunctions/03_AgentOrchestration_Concurrency/Program.cs @@ -7,7 +7,7 @@ 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 endpoint and deployment name from environment variables. string endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") diff --git a/dotnet/samples/AzureFunctions/04_AgentOrchestration_Conditionals/Program.cs b/dotnet/samples/AzureFunctions/04_AgentOrchestration_Conditionals/Program.cs index 971f862f21..a04b4c3e70 100644 --- a/dotnet/samples/AzureFunctions/04_AgentOrchestration_Conditionals/Program.cs +++ b/dotnet/samples/AzureFunctions/04_AgentOrchestration_Conditionals/Program.cs @@ -7,7 +7,7 @@ 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 endpoint and deployment name from environment variables. string endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") diff --git a/dotnet/samples/AzureFunctions/05_AgentOrchestration_HITL/Program.cs b/dotnet/samples/AzureFunctions/05_AgentOrchestration_HITL/Program.cs index 457fc4936e..741e5407e0 100644 --- a/dotnet/samples/AzureFunctions/05_AgentOrchestration_HITL/Program.cs +++ b/dotnet/samples/AzureFunctions/05_AgentOrchestration_HITL/Program.cs @@ -7,7 +7,7 @@ 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 endpoint and deployment name from environment variables. string endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") diff --git a/dotnet/samples/AzureFunctions/06_LongRunningTools/Program.cs b/dotnet/samples/AzureFunctions/06_LongRunningTools/Program.cs index 657a80d21f..581b2bce11 100644 --- a/dotnet/samples/AzureFunctions/06_LongRunningTools/Program.cs +++ b/dotnet/samples/AzureFunctions/06_LongRunningTools/Program.cs @@ -11,7 +11,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; -using OpenAI; +using OpenAI.Chat; // Get the Azure OpenAI endpoint and deployment name from environment variables. string endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") diff --git a/dotnet/samples/AzureFunctions/07_AgentAsMcpTool/Program.cs b/dotnet/samples/AzureFunctions/07_AgentAsMcpTool/Program.cs index 1c55f41f16..bc0a69cbf2 100644 --- a/dotnet/samples/AzureFunctions/07_AgentAsMcpTool/Program.cs +++ b/dotnet/samples/AzureFunctions/07_AgentAsMcpTool/Program.cs @@ -13,7 +13,7 @@ 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 endpoint and deployment name from environment variables. string endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") diff --git a/dotnet/samples/GettingStarted/A2A/A2AAgent_AsFunctionTools/Program.cs b/dotnet/samples/GettingStarted/A2A/A2AAgent_AsFunctionTools/Program.cs index a6f701cde3..c813464d0c 100644 --- a/dotnet/samples/GettingStarted/A2A/A2AAgent_AsFunctionTools/Program.cs +++ b/dotnet/samples/GettingStarted/A2A/A2AAgent_AsFunctionTools/Program.cs @@ -10,7 +10,7 @@ using Azure.Identity; using Microsoft.Agents.AI; using Microsoft.Extensions.AI; -using OpenAI; +using OpenAI.Chat; var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT is not set."); var deploymentName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT_NAME") ?? "gpt-4o-mini"; diff --git a/dotnet/samples/GettingStarted/AgentProviders/Agent_With_AzureFoundryModel/Program.cs b/dotnet/samples/GettingStarted/AgentProviders/Agent_With_AzureFoundryModel/Program.cs index 264a9e45e8..718b8906f9 100644 --- a/dotnet/samples/GettingStarted/AgentProviders/Agent_With_AzureFoundryModel/Program.cs +++ b/dotnet/samples/GettingStarted/AgentProviders/Agent_With_AzureFoundryModel/Program.cs @@ -9,6 +9,7 @@ using Azure.Identity; using Microsoft.Agents.AI; using OpenAI; +using OpenAI.Chat; var endpoint = Environment.GetEnvironmentVariable("AZURE_FOUNDRY_OPENAI_ENDPOINT") ?? throw new InvalidOperationException("AZURE_FOUNDRY_OPENAI_ENDPOINT is not set."); var apiKey = Environment.GetEnvironmentVariable("AZURE_FOUNDRY_OPENAI_APIKEY"); diff --git a/dotnet/samples/GettingStarted/AgentProviders/Agent_With_AzureOpenAIChatCompletion/Program.cs b/dotnet/samples/GettingStarted/AgentProviders/Agent_With_AzureOpenAIChatCompletion/Program.cs index bd31350258..cf717550d2 100644 --- a/dotnet/samples/GettingStarted/AgentProviders/Agent_With_AzureOpenAIChatCompletion/Program.cs +++ b/dotnet/samples/GettingStarted/AgentProviders/Agent_With_AzureOpenAIChatCompletion/Program.cs @@ -5,7 +5,7 @@ using Azure.AI.OpenAI; using Azure.Identity; using Microsoft.Agents.AI; -using OpenAI; +using OpenAI.Chat; var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT is not set."); var deploymentName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT_NAME") ?? "gpt-4o-mini"; diff --git a/dotnet/samples/GettingStarted/AgentProviders/Agent_With_AzureOpenAIResponses/Program.cs b/dotnet/samples/GettingStarted/AgentProviders/Agent_With_AzureOpenAIResponses/Program.cs index 6d162ebfd6..83d5619382 100644 --- a/dotnet/samples/GettingStarted/AgentProviders/Agent_With_AzureOpenAIResponses/Program.cs +++ b/dotnet/samples/GettingStarted/AgentProviders/Agent_With_AzureOpenAIResponses/Program.cs @@ -5,7 +5,7 @@ using Azure.AI.OpenAI; using Azure.Identity; using Microsoft.Agents.AI; -using OpenAI; +using OpenAI.Responses; var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT is not set."); var deploymentName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT_NAME") ?? "gpt-4o-mini"; diff --git a/dotnet/samples/GettingStarted/AgentProviders/Agent_With_OpenAIAssistants/Program.cs b/dotnet/samples/GettingStarted/AgentProviders/Agent_With_OpenAIAssistants/Program.cs index b39a21f71f..a4d79ea24a 100644 --- a/dotnet/samples/GettingStarted/AgentProviders/Agent_With_OpenAIAssistants/Program.cs +++ b/dotnet/samples/GettingStarted/AgentProviders/Agent_With_OpenAIAssistants/Program.cs @@ -9,6 +9,7 @@ using Microsoft.Agents.AI; using OpenAI; +using OpenAI.Assistants; var apiKey = Environment.GetEnvironmentVariable("OPENAI_APIKEY") ?? throw new InvalidOperationException("OPENAI_APIKEY is not set."); var model = Environment.GetEnvironmentVariable("OPENAI_MODEL") ?? "gpt-4o-mini"; diff --git a/dotnet/samples/GettingStarted/AgentProviders/Agent_With_OpenAIChatCompletion/Program.cs b/dotnet/samples/GettingStarted/AgentProviders/Agent_With_OpenAIChatCompletion/Program.cs index 331109fba9..3230a2bc65 100644 --- a/dotnet/samples/GettingStarted/AgentProviders/Agent_With_OpenAIChatCompletion/Program.cs +++ b/dotnet/samples/GettingStarted/AgentProviders/Agent_With_OpenAIChatCompletion/Program.cs @@ -5,6 +5,7 @@ using Microsoft.Agents.AI; using Microsoft.Extensions.AI; using OpenAI; +using OpenAI.Chat; var apiKey = Environment.GetEnvironmentVariable("OPENAI_APIKEY") ?? throw new InvalidOperationException("OPENAI_APIKEY is not set."); var model = Environment.GetEnvironmentVariable("OPENAI_MODEL") ?? "gpt-4o-mini"; diff --git a/dotnet/samples/GettingStarted/AgentProviders/Agent_With_OpenAIResponses/Program.cs b/dotnet/samples/GettingStarted/AgentProviders/Agent_With_OpenAIResponses/Program.cs index 1abefa0fca..9811f796ef 100644 --- a/dotnet/samples/GettingStarted/AgentProviders/Agent_With_OpenAIResponses/Program.cs +++ b/dotnet/samples/GettingStarted/AgentProviders/Agent_With_OpenAIResponses/Program.cs @@ -4,6 +4,7 @@ using Microsoft.Agents.AI; using OpenAI; +using OpenAI.Responses; var apiKey = Environment.GetEnvironmentVariable("OPENAI_APIKEY") ?? throw new InvalidOperationException("OPENAI_APIKEY is not set."); var model = Environment.GetEnvironmentVariable("OPENAI_MODEL") ?? "gpt-4o-mini"; diff --git a/dotnet/samples/GettingStarted/AgentWithMemory/AgentWithMemory_Step01_ChatHistoryMemory/Program.cs b/dotnet/samples/GettingStarted/AgentWithMemory/AgentWithMemory_Step01_ChatHistoryMemory/Program.cs index 19e1fead1e..a11edafabc 100644 --- a/dotnet/samples/GettingStarted/AgentWithMemory/AgentWithMemory_Step01_ChatHistoryMemory/Program.cs +++ b/dotnet/samples/GettingStarted/AgentWithMemory/AgentWithMemory_Step01_ChatHistoryMemory/Program.cs @@ -9,7 +9,7 @@ using Microsoft.Extensions.AI; using Microsoft.Extensions.VectorData; using Microsoft.SemanticKernel.Connectors.InMemory; -using OpenAI; +using OpenAI.Chat; var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT is not set."); var deploymentName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT_NAME") ?? "gpt-4o-mini"; diff --git a/dotnet/samples/GettingStarted/AgentWithMemory/AgentWithMemory_Step02_MemoryUsingMem0/Program.cs b/dotnet/samples/GettingStarted/AgentWithMemory/AgentWithMemory_Step02_MemoryUsingMem0/Program.cs index 87f5842e2c..739c5e3f13 100644 --- a/dotnet/samples/GettingStarted/AgentWithMemory/AgentWithMemory_Step02_MemoryUsingMem0/Program.cs +++ b/dotnet/samples/GettingStarted/AgentWithMemory/AgentWithMemory_Step02_MemoryUsingMem0/Program.cs @@ -11,7 +11,7 @@ using Microsoft.Agents.AI; using Microsoft.Agents.AI.Mem0; using Microsoft.Extensions.AI; -using OpenAI; +using OpenAI.Chat; var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT is not set."); var deploymentName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT_NAME") ?? "gpt-4o-mini"; diff --git a/dotnet/samples/GettingStarted/AgentWithMemory/AgentWithMemory_Step03_CustomMemory/Program.cs b/dotnet/samples/GettingStarted/AgentWithMemory/AgentWithMemory_Step03_CustomMemory/Program.cs index 4b9b1866a9..5727e8ca3c 100644 --- a/dotnet/samples/GettingStarted/AgentWithMemory/AgentWithMemory_Step03_CustomMemory/Program.cs +++ b/dotnet/samples/GettingStarted/AgentWithMemory/AgentWithMemory_Step03_CustomMemory/Program.cs @@ -12,7 +12,6 @@ using Azure.Identity; using Microsoft.Agents.AI; using Microsoft.Extensions.AI; -using OpenAI; using OpenAI.Chat; using SampleApp; diff --git a/dotnet/src/Microsoft.Agents.AI.OpenAI/OpenAIChatClientAgent.cs b/dotnet/samples/GettingStarted/AgentWithOpenAI/Agent_OpenAI_Step03_CreateFromChatClient/OpenAIChatClientAgent.cs similarity index 96% rename from dotnet/src/Microsoft.Agents.AI.OpenAI/OpenAIChatClientAgent.cs rename to dotnet/samples/GettingStarted/AgentWithOpenAI/Agent_OpenAI_Step03_CreateFromChatClient/OpenAIChatClientAgent.cs index 5870e2fdcc..b295bfecea 100644 --- a/dotnet/src/Microsoft.Agents.AI.OpenAI/OpenAIChatClientAgent.cs +++ b/dotnet/samples/GettingStarted/AgentWithOpenAI/Agent_OpenAI_Step03_CreateFromChatClient/OpenAIChatClientAgent.cs @@ -3,11 +3,10 @@ using Microsoft.Agents.AI; using Microsoft.Extensions.AI; using Microsoft.Extensions.Logging; -using Microsoft.Shared.Diagnostics; using OpenAI.Chat; using ChatMessage = OpenAI.Chat.ChatMessage; -namespace OpenAI; +namespace OpenAIChatClientSample; /// /// Provides an backed by an OpenAI chat completion implementation. @@ -45,7 +44,7 @@ public OpenAIChatClientAgent( /// Optional instance of public OpenAIChatClientAgent( ChatClient client, ChatClientAgentOptions options, ILoggerFactory? loggerFactory = null) : - base(new ChatClientAgent(Throw.IfNull(client).AsIChatClient(), options, loggerFactory)) + base(new ChatClientAgent((client ?? throw new ArgumentNullException(nameof(client))).AsIChatClient(), options, loggerFactory)) { } diff --git a/dotnet/samples/GettingStarted/AgentWithOpenAI/Agent_OpenAI_Step03_CreateFromChatClient/Program.cs b/dotnet/samples/GettingStarted/AgentWithOpenAI/Agent_OpenAI_Step03_CreateFromChatClient/Program.cs index dd72019125..b046afbf0d 100644 --- a/dotnet/samples/GettingStarted/AgentWithOpenAI/Agent_OpenAI_Step03_CreateFromChatClient/Program.cs +++ b/dotnet/samples/GettingStarted/AgentWithOpenAI/Agent_OpenAI_Step03_CreateFromChatClient/Program.cs @@ -4,6 +4,7 @@ using OpenAI; using OpenAI.Chat; +using OpenAIChatClientSample; string apiKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY") ?? throw new InvalidOperationException("OPENAI_API_KEY is not set."); string model = Environment.GetEnvironmentVariable("OPENAI_MODEL") ?? "gpt-4o-mini"; diff --git a/dotnet/src/Microsoft.Agents.AI.OpenAI/OpenAIResponseClientAgent.cs b/dotnet/samples/GettingStarted/AgentWithOpenAI/Agent_OpenAI_Step04_CreateFromOpenAIResponseClient/OpenAIResponseClientAgent.cs similarity index 97% rename from dotnet/src/Microsoft.Agents.AI.OpenAI/OpenAIResponseClientAgent.cs rename to dotnet/samples/GettingStarted/AgentWithOpenAI/Agent_OpenAI_Step04_CreateFromOpenAIResponseClient/OpenAIResponseClientAgent.cs index 9d554e6a84..456de02836 100644 --- a/dotnet/src/Microsoft.Agents.AI.OpenAI/OpenAIResponseClientAgent.cs +++ b/dotnet/samples/GettingStarted/AgentWithOpenAI/Agent_OpenAI_Step04_CreateFromOpenAIResponseClient/OpenAIResponseClientAgent.cs @@ -4,10 +4,9 @@ using Microsoft.Agents.AI; using Microsoft.Extensions.AI; using Microsoft.Extensions.Logging; -using Microsoft.Shared.Diagnostics; using OpenAI.Responses; -namespace OpenAI; +namespace OpenAIResponseClientSample; /// /// Provides an backed by an OpenAI Responses implementation. @@ -45,7 +44,7 @@ public OpenAIResponseClientAgent( /// Optional instance of public OpenAIResponseClientAgent( OpenAIResponseClient client, ChatClientAgentOptions options, ILoggerFactory? loggerFactory = null) : - base(new ChatClientAgent(Throw.IfNull(client).AsIChatClient(), options, loggerFactory)) + base(new ChatClientAgent((client ?? throw new ArgumentNullException(nameof(client))).AsIChatClient(), options, loggerFactory)) { } diff --git a/dotnet/samples/GettingStarted/AgentWithOpenAI/Agent_OpenAI_Step04_CreateFromOpenAIResponseClient/Program.cs b/dotnet/samples/GettingStarted/AgentWithOpenAI/Agent_OpenAI_Step04_CreateFromOpenAIResponseClient/Program.cs index 555ec60ac5..89a96bc0fb 100644 --- a/dotnet/samples/GettingStarted/AgentWithOpenAI/Agent_OpenAI_Step04_CreateFromOpenAIResponseClient/Program.cs +++ b/dotnet/samples/GettingStarted/AgentWithOpenAI/Agent_OpenAI_Step04_CreateFromOpenAIResponseClient/Program.cs @@ -4,6 +4,7 @@ using OpenAI; using OpenAI.Responses; +using OpenAIResponseClientSample; var apiKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY") ?? throw new InvalidOperationException("OPENAI_API_KEY is not set."); var model = Environment.GetEnvironmentVariable("OPENAI_MODEL") ?? "gpt-4o-mini"; diff --git a/dotnet/samples/GettingStarted/AgentWithRAG/AgentWithRAG_Step01_BasicTextRAG/Program.cs b/dotnet/samples/GettingStarted/AgentWithRAG/AgentWithRAG_Step01_BasicTextRAG/Program.cs index 5470b1ed48..42015d87cd 100644 --- a/dotnet/samples/GettingStarted/AgentWithRAG/AgentWithRAG_Step01_BasicTextRAG/Program.cs +++ b/dotnet/samples/GettingStarted/AgentWithRAG/AgentWithRAG_Step01_BasicTextRAG/Program.cs @@ -12,7 +12,7 @@ using Microsoft.Extensions.AI; using Microsoft.Extensions.VectorData; using Microsoft.SemanticKernel.Connectors.InMemory; -using OpenAI; +using OpenAI.Chat; var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT is not set."); var deploymentName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT_NAME") ?? "gpt-4o-mini"; diff --git a/dotnet/samples/GettingStarted/AgentWithRAG/AgentWithRAG_Step02_CustomVectorStoreRAG/Program.cs b/dotnet/samples/GettingStarted/AgentWithRAG/AgentWithRAG_Step02_CustomVectorStoreRAG/Program.cs index eac14217d3..f20e42f01d 100644 --- a/dotnet/samples/GettingStarted/AgentWithRAG/AgentWithRAG_Step02_CustomVectorStoreRAG/Program.cs +++ b/dotnet/samples/GettingStarted/AgentWithRAG/AgentWithRAG_Step02_CustomVectorStoreRAG/Program.cs @@ -10,7 +10,7 @@ using Microsoft.Extensions.AI; using Microsoft.Extensions.VectorData; using Microsoft.SemanticKernel.Connectors.Qdrant; -using OpenAI; +using OpenAI.Chat; using Qdrant.Client; var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT is not set."); diff --git a/dotnet/samples/GettingStarted/AgentWithRAG/AgentWithRAG_Step03_CustomRAGDataSource/Program.cs b/dotnet/samples/GettingStarted/AgentWithRAG/AgentWithRAG_Step03_CustomRAGDataSource/Program.cs index 21f6643684..e9a62e382f 100644 --- a/dotnet/samples/GettingStarted/AgentWithRAG/AgentWithRAG_Step03_CustomRAGDataSource/Program.cs +++ b/dotnet/samples/GettingStarted/AgentWithRAG/AgentWithRAG_Step03_CustomRAGDataSource/Program.cs @@ -10,7 +10,7 @@ using Azure.Identity; using Microsoft.Agents.AI; using Microsoft.Extensions.AI; -using OpenAI; +using OpenAI.Chat; var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT is not set."); var deploymentName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT_NAME") ?? "gpt-4o-mini"; diff --git a/dotnet/samples/GettingStarted/Agents/Agent_Step01_Running/Program.cs b/dotnet/samples/GettingStarted/Agents/Agent_Step01_Running/Program.cs index c67756299c..889045c228 100644 --- a/dotnet/samples/GettingStarted/Agents/Agent_Step01_Running/Program.cs +++ b/dotnet/samples/GettingStarted/Agents/Agent_Step01_Running/Program.cs @@ -5,7 +5,7 @@ using Azure.AI.OpenAI; using Azure.Identity; using Microsoft.Agents.AI; -using OpenAI; +using OpenAI.Chat; var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT is not set."); var deploymentName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT_NAME") ?? "gpt-4o-mini"; diff --git a/dotnet/samples/GettingStarted/Agents/Agent_Step02_MultiturnConversation/Program.cs b/dotnet/samples/GettingStarted/Agents/Agent_Step02_MultiturnConversation/Program.cs index 626a3e98c4..e27a5bb36d 100644 --- a/dotnet/samples/GettingStarted/Agents/Agent_Step02_MultiturnConversation/Program.cs +++ b/dotnet/samples/GettingStarted/Agents/Agent_Step02_MultiturnConversation/Program.cs @@ -5,7 +5,7 @@ using Azure.AI.OpenAI; using Azure.Identity; using Microsoft.Agents.AI; -using OpenAI; +using OpenAI.Chat; var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT is not set."); var deploymentName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT_NAME") ?? "gpt-4o-mini"; diff --git a/dotnet/samples/GettingStarted/Agents/Agent_Step03_UsingFunctionTools/Program.cs b/dotnet/samples/GettingStarted/Agents/Agent_Step03_UsingFunctionTools/Program.cs index 48a6378e1f..ae41572cc2 100644 --- a/dotnet/samples/GettingStarted/Agents/Agent_Step03_UsingFunctionTools/Program.cs +++ b/dotnet/samples/GettingStarted/Agents/Agent_Step03_UsingFunctionTools/Program.cs @@ -8,7 +8,7 @@ using Azure.Identity; using Microsoft.Agents.AI; using Microsoft.Extensions.AI; -using OpenAI; +using OpenAI.Chat; var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT is not set."); var deploymentName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT_NAME") ?? "gpt-4o-mini"; diff --git a/dotnet/samples/GettingStarted/Agents/Agent_Step04_UsingFunctionToolsWithApprovals/Program.cs b/dotnet/samples/GettingStarted/Agents/Agent_Step04_UsingFunctionToolsWithApprovals/Program.cs index 41ea8a5c92..be2a4801ae 100644 --- a/dotnet/samples/GettingStarted/Agents/Agent_Step04_UsingFunctionToolsWithApprovals/Program.cs +++ b/dotnet/samples/GettingStarted/Agents/Agent_Step04_UsingFunctionToolsWithApprovals/Program.cs @@ -10,7 +10,8 @@ using Azure.Identity; using Microsoft.Agents.AI; using Microsoft.Extensions.AI; -using OpenAI; +using OpenAI.Chat; +using ChatMessage = Microsoft.Extensions.AI.ChatMessage; var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT is not set."); var deploymentName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT_NAME") ?? "gpt-4o-mini"; diff --git a/dotnet/samples/GettingStarted/Agents/Agent_Step05_StructuredOutput/Program.cs b/dotnet/samples/GettingStarted/Agents/Agent_Step05_StructuredOutput/Program.cs index ef1849fe02..3b923069f4 100644 --- a/dotnet/samples/GettingStarted/Agents/Agent_Step05_StructuredOutput/Program.cs +++ b/dotnet/samples/GettingStarted/Agents/Agent_Step05_StructuredOutput/Program.cs @@ -8,7 +8,6 @@ using Azure.AI.OpenAI; using Azure.Identity; using Microsoft.Agents.AI; -using OpenAI; using OpenAI.Chat; using SampleApp; diff --git a/dotnet/samples/GettingStarted/Agents/Agent_Step06_PersistedConversations/Program.cs b/dotnet/samples/GettingStarted/Agents/Agent_Step06_PersistedConversations/Program.cs index 559fc03d8c..5d3247b69c 100644 --- a/dotnet/samples/GettingStarted/Agents/Agent_Step06_PersistedConversations/Program.cs +++ b/dotnet/samples/GettingStarted/Agents/Agent_Step06_PersistedConversations/Program.cs @@ -6,7 +6,7 @@ using Azure.AI.OpenAI; using Azure.Identity; using Microsoft.Agents.AI; -using OpenAI; +using OpenAI.Chat; var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT is not set."); var deploymentName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT_NAME") ?? "gpt-4o-mini"; diff --git a/dotnet/samples/GettingStarted/Agents/Agent_Step07_3rdPartyThreadStorage/Program.cs b/dotnet/samples/GettingStarted/Agents/Agent_Step07_3rdPartyThreadStorage/Program.cs index d1316b6c80..e9794e871a 100644 --- a/dotnet/samples/GettingStarted/Agents/Agent_Step07_3rdPartyThreadStorage/Program.cs +++ b/dotnet/samples/GettingStarted/Agents/Agent_Step07_3rdPartyThreadStorage/Program.cs @@ -11,8 +11,9 @@ using Microsoft.Extensions.AI; using Microsoft.Extensions.VectorData; using Microsoft.SemanticKernel.Connectors.InMemory; -using OpenAI; +using OpenAI.Chat; using SampleApp; +using ChatMessage = Microsoft.Extensions.AI.ChatMessage; var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT is not set."); var deploymentName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT_NAME") ?? "gpt-4o-mini"; diff --git a/dotnet/samples/GettingStarted/Agents/Agent_Step08_Observability/Program.cs b/dotnet/samples/GettingStarted/Agents/Agent_Step08_Observability/Program.cs index c48242f5ca..e43e80d664 100644 --- a/dotnet/samples/GettingStarted/Agents/Agent_Step08_Observability/Program.cs +++ b/dotnet/samples/GettingStarted/Agents/Agent_Step08_Observability/Program.cs @@ -6,7 +6,7 @@ using Azure.Identity; using Azure.Monitor.OpenTelemetry.Exporter; using Microsoft.Agents.AI; -using OpenAI; +using OpenAI.Chat; using OpenTelemetry; using OpenTelemetry.Trace; diff --git a/dotnet/samples/GettingStarted/Agents/Agent_Step11_UsingImages/Program.cs b/dotnet/samples/GettingStarted/Agents/Agent_Step11_UsingImages/Program.cs index 8e8c5701ad..f534e4edd7 100644 --- a/dotnet/samples/GettingStarted/Agents/Agent_Step11_UsingImages/Program.cs +++ b/dotnet/samples/GettingStarted/Agents/Agent_Step11_UsingImages/Program.cs @@ -5,7 +5,8 @@ using Azure.AI.OpenAI; using Azure.Identity; using Microsoft.Extensions.AI; -using OpenAI; +using OpenAI.Chat; +using ChatMessage = Microsoft.Extensions.AI.ChatMessage; var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT is not set."); var deploymentName = System.Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT_NAME") ?? "gpt-4o"; diff --git a/dotnet/samples/GettingStarted/Agents/Agent_Step12_AsFunctionTool/Program.cs b/dotnet/samples/GettingStarted/Agents/Agent_Step12_AsFunctionTool/Program.cs index cce53ef3c0..5e37ff4039 100644 --- a/dotnet/samples/GettingStarted/Agents/Agent_Step12_AsFunctionTool/Program.cs +++ b/dotnet/samples/GettingStarted/Agents/Agent_Step12_AsFunctionTool/Program.cs @@ -7,7 +7,7 @@ using Azure.Identity; using Microsoft.Agents.AI; using Microsoft.Extensions.AI; -using OpenAI; +using OpenAI.Chat; var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT is not set."); var deploymentName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT_NAME") ?? "gpt-4o-mini"; diff --git a/dotnet/samples/GettingStarted/Agents/Agent_Step13_BackgroundResponsesWithToolsAndPersistence/Program.cs b/dotnet/samples/GettingStarted/Agents/Agent_Step13_BackgroundResponsesWithToolsAndPersistence/Program.cs index 83a97d76c5..41493f6d79 100644 --- a/dotnet/samples/GettingStarted/Agents/Agent_Step13_BackgroundResponsesWithToolsAndPersistence/Program.cs +++ b/dotnet/samples/GettingStarted/Agents/Agent_Step13_BackgroundResponsesWithToolsAndPersistence/Program.cs @@ -12,7 +12,7 @@ using Azure.Identity; using Microsoft.Agents.AI; using Microsoft.Extensions.AI; -using OpenAI; +using OpenAI.Responses; var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT is not set."); var deploymentName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT_NAME") ?? "gpt-5"; diff --git a/dotnet/samples/GettingStarted/Agents/Agent_Step15_Plugins/Program.cs b/dotnet/samples/GettingStarted/Agents/Agent_Step15_Plugins/Program.cs index 7284efcc42..38cd20b8d6 100644 --- a/dotnet/samples/GettingStarted/Agents/Agent_Step15_Plugins/Program.cs +++ b/dotnet/samples/GettingStarted/Agents/Agent_Step15_Plugins/Program.cs @@ -14,7 +14,7 @@ using Microsoft.Agents.AI; using Microsoft.Extensions.AI; using Microsoft.Extensions.DependencyInjection; -using OpenAI; +using OpenAI.Chat; var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT is not set."); var deploymentName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT_NAME") ?? "gpt-4o-mini"; diff --git a/dotnet/samples/GettingStarted/Agents/Agent_Step16_ChatReduction/Program.cs b/dotnet/samples/GettingStarted/Agents/Agent_Step16_ChatReduction/Program.cs index 04704b5da0..decf0de25a 100644 --- a/dotnet/samples/GettingStarted/Agents/Agent_Step16_ChatReduction/Program.cs +++ b/dotnet/samples/GettingStarted/Agents/Agent_Step16_ChatReduction/Program.cs @@ -9,7 +9,8 @@ using Azure.Identity; using Microsoft.Agents.AI; using Microsoft.Extensions.AI; -using OpenAI; +using OpenAI.Chat; +using ChatMessage = Microsoft.Extensions.AI.ChatMessage; var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT is not set."); var deploymentName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT_NAME") ?? "gpt-4o-mini"; diff --git a/dotnet/samples/GettingStarted/Agents/Agent_Step17_BackgroundResponses/Program.cs b/dotnet/samples/GettingStarted/Agents/Agent_Step17_BackgroundResponses/Program.cs index 456d968c34..510a5dfbd0 100644 --- a/dotnet/samples/GettingStarted/Agents/Agent_Step17_BackgroundResponses/Program.cs +++ b/dotnet/samples/GettingStarted/Agents/Agent_Step17_BackgroundResponses/Program.cs @@ -5,7 +5,7 @@ using Azure.AI.OpenAI; using Azure.Identity; using Microsoft.Agents.AI; -using OpenAI; +using OpenAI.Responses; var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT is not set."); var deploymentName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT_NAME") ?? "gpt-4o-mini"; diff --git a/dotnet/samples/GettingStarted/ModelContextProtocol/Agent_MCP_Server/Program.cs b/dotnet/samples/GettingStarted/ModelContextProtocol/Agent_MCP_Server/Program.cs index 1a0d236961..774c33ed58 100644 --- a/dotnet/samples/GettingStarted/ModelContextProtocol/Agent_MCP_Server/Program.cs +++ b/dotnet/samples/GettingStarted/ModelContextProtocol/Agent_MCP_Server/Program.cs @@ -7,7 +7,7 @@ using Microsoft.Agents.AI; using Microsoft.Extensions.AI; using ModelContextProtocol.Client; -using OpenAI; +using OpenAI.Chat; var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT is not set."); var deploymentName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT_NAME") ?? "gpt-4o-mini"; diff --git a/dotnet/samples/GettingStarted/ModelContextProtocol/Agent_MCP_Server_Auth/Program.cs b/dotnet/samples/GettingStarted/ModelContextProtocol/Agent_MCP_Server_Auth/Program.cs index aae520eec9..c9197c573c 100644 --- a/dotnet/samples/GettingStarted/ModelContextProtocol/Agent_MCP_Server_Auth/Program.cs +++ b/dotnet/samples/GettingStarted/ModelContextProtocol/Agent_MCP_Server_Auth/Program.cs @@ -11,7 +11,7 @@ using Microsoft.Agents.AI; using Microsoft.Extensions.Logging; using ModelContextProtocol.Client; -using OpenAI; +using OpenAI.Chat; var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT is not set."); var deploymentName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT_NAME") ?? "gpt-4o-mini"; diff --git a/dotnet/samples/GettingStarted/ModelContextProtocol/ResponseAgent_Hosted_MCP/Program.cs b/dotnet/samples/GettingStarted/ModelContextProtocol/ResponseAgent_Hosted_MCP/Program.cs index 19793e64df..ba4249c765 100644 --- a/dotnet/samples/GettingStarted/ModelContextProtocol/ResponseAgent_Hosted_MCP/Program.cs +++ b/dotnet/samples/GettingStarted/ModelContextProtocol/ResponseAgent_Hosted_MCP/Program.cs @@ -8,7 +8,7 @@ using Azure.Identity; using Microsoft.Agents.AI; using Microsoft.Extensions.AI; -using OpenAI; +using OpenAI.Responses; var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT is not set."); var deploymentName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT_NAME") ?? "gpt-4o-mini"; diff --git a/dotnet/src/Microsoft.Agents.AI.OpenAI/ChatClient/AsyncStreamingUpdateCollectionResult.cs b/dotnet/src/Microsoft.Agents.AI.OpenAI/ChatClient/AsyncStreamingChatCompletionUpdateCollectionResult.cs similarity index 78% rename from dotnet/src/Microsoft.Agents.AI.OpenAI/ChatClient/AsyncStreamingUpdateCollectionResult.cs rename to dotnet/src/Microsoft.Agents.AI.OpenAI/ChatClient/AsyncStreamingChatCompletionUpdateCollectionResult.cs index a118c6c1de..17c9c2d95a 100644 --- a/dotnet/src/Microsoft.Agents.AI.OpenAI/ChatClient/AsyncStreamingUpdateCollectionResult.cs +++ b/dotnet/src/Microsoft.Agents.AI.OpenAI/ChatClient/AsyncStreamingChatCompletionUpdateCollectionResult.cs @@ -5,11 +5,11 @@ namespace Microsoft.Agents.AI.OpenAI; -internal sealed class AsyncStreamingUpdateCollectionResult : AsyncCollectionResult +internal sealed class AsyncStreamingChatCompletionUpdateCollectionResult : AsyncCollectionResult { private readonly IAsyncEnumerable _updates; - internal AsyncStreamingUpdateCollectionResult(IAsyncEnumerable updates) + internal AsyncStreamingChatCompletionUpdateCollectionResult(IAsyncEnumerable updates) { this._updates = updates; } diff --git a/dotnet/src/Microsoft.Agents.AI.OpenAI/ChatClient/AsyncStreamingResponseUpdateCollectionResult.cs b/dotnet/src/Microsoft.Agents.AI.OpenAI/ChatClient/AsyncStreamingResponseUpdateCollectionResult.cs new file mode 100644 index 0000000000..c67f4d1462 --- /dev/null +++ b/dotnet/src/Microsoft.Agents.AI.OpenAI/ChatClient/AsyncStreamingResponseUpdateCollectionResult.cs @@ -0,0 +1,48 @@ +// Copyright (c) Microsoft. All rights reserved. + +using System.ClientModel; +using OpenAI.Responses; + +namespace Microsoft.Agents.AI.OpenAI; + +internal sealed class AsyncStreamingResponseUpdateCollectionResult : AsyncCollectionResult +{ + private readonly IAsyncEnumerable _updates; + + internal AsyncStreamingResponseUpdateCollectionResult(IAsyncEnumerable updates) + { + this._updates = updates; + } + + public override ContinuationToken? GetContinuationToken(ClientResult page) => null; + + public override async IAsyncEnumerable GetRawPagesAsync() + { + yield return ClientResult.FromValue(this._updates, new StreamingUpdatePipelineResponse(this._updates)); + } + + protected async override IAsyncEnumerable GetValuesFromPageAsync(ClientResult page) + { + var updates = ((ClientResult>)page).Value; + + await foreach (var update in updates.ConfigureAwait(false)) + { + switch (update.RawRepresentation) + { + case StreamingResponseUpdate rawUpdate: + yield return rawUpdate; + break; + + case Extensions.AI.ChatResponseUpdate { RawRepresentation: StreamingResponseUpdate rawUpdate }: + yield return rawUpdate; + break; + + default: + // TODO: The OpenAI library does not currently expose model factory methods for creating + // StreamingResponseUpdates. We are thus unable to manufacture such instances when there isn't + // already one in the update and instead skip them. + break; + } + } + } +} diff --git a/dotnet/src/Microsoft.Agents.AI.OpenAI/Extensions/AIAgentWithOpenAIExtensions.cs b/dotnet/src/Microsoft.Agents.AI.OpenAI/Extensions/AIAgentWithOpenAIExtensions.cs index 32bb50080d..4abc6915a6 100644 --- a/dotnet/src/Microsoft.Agents.AI.OpenAI/Extensions/AIAgentWithOpenAIExtensions.cs +++ b/dotnet/src/Microsoft.Agents.AI.OpenAI/Extensions/AIAgentWithOpenAIExtensions.cs @@ -1,12 +1,12 @@ // Copyright (c) Microsoft. All rights reserved. using System.ClientModel; -using Microsoft.Agents.AI; using Microsoft.Agents.AI.OpenAI; using Microsoft.Shared.Diagnostics; using OpenAI.Chat; +using OpenAI.Responses; -namespace OpenAI; +namespace Microsoft.Agents.AI; /// /// Provides extension methods for to simplify interaction with OpenAI chat messages @@ -69,6 +69,60 @@ public static AsyncCollectionResult RunStreamingA IAsyncEnumerable response = agent.RunStreamingAsync([.. messages.AsChatMessages()], thread, options, cancellationToken); - return new AsyncStreamingUpdateCollectionResult(response); + return new AsyncStreamingChatCompletionUpdateCollectionResult(response); + } + + /// + /// Runs the AI agent with a collection of OpenAI response items and returns the response as a native OpenAI . + /// + /// The AI agent to run. + /// The collection of OpenAI response items to send to the agent. + /// The conversation thread to continue with this invocation. If not provided, creates a new thread. The thread will be mutated with the provided messages and agent response. + /// Optional parameters for agent invocation. + /// The to monitor for cancellation requests. The default is . + /// A representing the asynchronous operation that returns a native OpenAI response. + /// Thrown when or is . + /// Thrown when the agent's response cannot be converted to an , typically when the underlying representation is not an OpenAI response. + /// Thrown when any message in has a type that is not supported by the message conversion method. + /// + /// This method converts the OpenAI response items to the Microsoft Extensions AI format using the appropriate conversion method, + /// runs the agent with the converted message collection, and then extracts the native OpenAI from the response using . + /// + public static async Task RunAsync(this AIAgent agent, IEnumerable messages, AgentThread? thread = null, AgentRunOptions? options = null, CancellationToken cancellationToken = default) + { + Throw.IfNull(agent); + Throw.IfNull(messages); + + var response = await agent.RunAsync(messages.AsChatMessages(), thread, options, cancellationToken).ConfigureAwait(false); + + return response.AsOpenAIResponse(); + } + + /// + /// Runs the AI agent in streaming mode with a collection of OpenAI response items and returns the response as a collection of native OpenAI . + /// + /// The AI agent to run. + /// The collection of OpenAI response items to send to the agent. + /// The conversation thread to continue with this invocation. If not provided, creates a new thread. The thread will be mutated with the provided messages and agent response updates. + /// Optional parameters for agent invocation. + /// The to monitor for cancellation requests. The default is . + /// An representing the asynchronous enumerable that yields native OpenAI instances as they are streamed. + /// Thrown when or is . + /// Thrown when the agent's response cannot be converted to instances, typically when the underlying representation is not an OpenAI response. + /// Thrown when any message in has a type that is not supported by the message conversion method. + /// + /// This method converts the OpenAI response items to the Microsoft Extensions AI format using the appropriate conversion method, + /// runs the agent in streaming mode, and then yields native OpenAI instances as they are produced. + /// The method attempts to extract from the underlying response representation. If a raw update is not available, + /// it is skipped because the OpenAI library does not currently expose model factory methods for creating such instances. + /// + public static AsyncCollectionResult RunStreamingAsync(this AIAgent agent, IEnumerable messages, AgentThread? thread = null, AgentRunOptions? options = null, CancellationToken cancellationToken = default) + { + Throw.IfNull(agent); + Throw.IfNull(messages); + + IAsyncEnumerable response = agent.RunStreamingAsync([.. messages.AsChatMessages()], thread, options, cancellationToken); + + return new AsyncStreamingResponseUpdateCollectionResult(response); } } diff --git a/dotnet/src/Microsoft.Agents.AI.OpenAI/Extensions/AgentRunResponseExtensions.cs b/dotnet/src/Microsoft.Agents.AI.OpenAI/Extensions/AgentRunResponseExtensions.cs index 1660192fad..9a164d862b 100644 --- a/dotnet/src/Microsoft.Agents.AI.OpenAI/Extensions/AgentRunResponseExtensions.cs +++ b/dotnet/src/Microsoft.Agents.AI.OpenAI/Extensions/AgentRunResponseExtensions.cs @@ -5,7 +5,7 @@ using OpenAI.Chat; using OpenAI.Responses; -namespace OpenAI; +namespace Microsoft.Agents.AI; /// /// Provides extension methods for and instances to diff --git a/dotnet/src/Microsoft.Agents.AI.OpenAI/Extensions/OpenAIAssistantClientExtensions.cs b/dotnet/src/Microsoft.Agents.AI.OpenAI/Extensions/OpenAIAssistantClientExtensions.cs index f103a7fc1c..881266fe8b 100644 --- a/dotnet/src/Microsoft.Agents.AI.OpenAI/Extensions/OpenAIAssistantClientExtensions.cs +++ b/dotnet/src/Microsoft.Agents.AI.OpenAI/Extensions/OpenAIAssistantClientExtensions.cs @@ -5,9 +5,8 @@ using Microsoft.Extensions.AI; using Microsoft.Extensions.Logging; using Microsoft.Shared.Diagnostics; -using OpenAI.Assistants; -namespace OpenAI; +namespace OpenAI.Assistants; /// /// Provides extension methods for OpenAI diff --git a/dotnet/src/Microsoft.Agents.AI.OpenAI/Extensions/OpenAIChatClientExtensions.cs b/dotnet/src/Microsoft.Agents.AI.OpenAI/Extensions/OpenAIChatClientExtensions.cs index b51679e42e..aa4f38e5f4 100644 --- a/dotnet/src/Microsoft.Agents.AI.OpenAI/Extensions/OpenAIChatClientExtensions.cs +++ b/dotnet/src/Microsoft.Agents.AI.OpenAI/Extensions/OpenAIChatClientExtensions.cs @@ -4,9 +4,8 @@ using Microsoft.Extensions.AI; using Microsoft.Extensions.Logging; using Microsoft.Shared.Diagnostics; -using OpenAI.Chat; -namespace OpenAI; +namespace OpenAI.Chat; /// /// Provides extension methods for diff --git a/dotnet/src/Microsoft.Agents.AI.OpenAI/Extensions/OpenAIResponseClientExtensions.cs b/dotnet/src/Microsoft.Agents.AI.OpenAI/Extensions/OpenAIResponseClientExtensions.cs index dd25d46047..0d48147c77 100644 --- a/dotnet/src/Microsoft.Agents.AI.OpenAI/Extensions/OpenAIResponseClientExtensions.cs +++ b/dotnet/src/Microsoft.Agents.AI.OpenAI/Extensions/OpenAIResponseClientExtensions.cs @@ -4,9 +4,8 @@ using Microsoft.Extensions.AI; using Microsoft.Extensions.Logging; using Microsoft.Shared.Diagnostics; -using OpenAI.Responses; -namespace OpenAI; +namespace OpenAI.Responses; /// /// Provides extension methods for diff --git a/dotnet/tests/Microsoft.Agents.AI.DurableTask.IntegrationTests/AgentEntityTests.cs b/dotnet/tests/Microsoft.Agents.AI.DurableTask.IntegrationTests/AgentEntityTests.cs index 1ae3c66834..98e40ad4fb 100644 --- a/dotnet/tests/Microsoft.Agents.AI.DurableTask.IntegrationTests/AgentEntityTests.cs +++ b/dotnet/tests/Microsoft.Agents.AI.DurableTask.IntegrationTests/AgentEntityTests.cs @@ -8,7 +8,7 @@ using Microsoft.DurableTask.Client.Entities; using Microsoft.DurableTask.Entities; using Microsoft.Extensions.Configuration; -using OpenAI; +using OpenAI.Chat; using Xunit.Abstractions; namespace Microsoft.Agents.AI.DurableTask.IntegrationTests; diff --git a/dotnet/tests/Microsoft.Agents.AI.DurableTask.IntegrationTests/ExternalClientTests.cs b/dotnet/tests/Microsoft.Agents.AI.DurableTask.IntegrationTests/ExternalClientTests.cs index ad57ea9a52..c43b86e330 100644 --- a/dotnet/tests/Microsoft.Agents.AI.DurableTask.IntegrationTests/ExternalClientTests.cs +++ b/dotnet/tests/Microsoft.Agents.AI.DurableTask.IntegrationTests/ExternalClientTests.cs @@ -8,7 +8,7 @@ using Microsoft.DurableTask.Client; using Microsoft.Extensions.AI; using Microsoft.Extensions.Configuration; -using OpenAI; +using OpenAI.Chat; using Xunit.Abstractions; namespace Microsoft.Agents.AI.DurableTask.IntegrationTests; diff --git a/dotnet/tests/Microsoft.Agents.AI.DurableTask.IntegrationTests/OrchestrationTests.cs b/dotnet/tests/Microsoft.Agents.AI.DurableTask.IntegrationTests/OrchestrationTests.cs index 6b905f2623..0c702e6062 100644 --- a/dotnet/tests/Microsoft.Agents.AI.DurableTask.IntegrationTests/OrchestrationTests.cs +++ b/dotnet/tests/Microsoft.Agents.AI.DurableTask.IntegrationTests/OrchestrationTests.cs @@ -6,7 +6,7 @@ using Microsoft.DurableTask.Client; using Microsoft.Extensions.AI; using Microsoft.Extensions.Configuration; -using OpenAI; +using OpenAI.Chat; using Xunit.Abstractions; namespace Microsoft.Agents.AI.DurableTask.IntegrationTests; diff --git a/dotnet/tests/Microsoft.Agents.AI.OpenAI.UnitTests/Extensions/AIAgentWithOpenAIExtensionsTests.cs b/dotnet/tests/Microsoft.Agents.AI.OpenAI.UnitTests/Extensions/AIAgentWithOpenAIExtensionsTests.cs new file mode 100644 index 0000000000..de8c459be0 --- /dev/null +++ b/dotnet/tests/Microsoft.Agents.AI.OpenAI.UnitTests/Extensions/AIAgentWithOpenAIExtensionsTests.cs @@ -0,0 +1,198 @@ +// Copyright (c) Microsoft. All rights reserved. + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using Moq; +using ChatMessage = Microsoft.Extensions.AI.ChatMessage; +using ChatRole = Microsoft.Extensions.AI.ChatRole; +using OpenAIChatMessage = OpenAI.Chat.ChatMessage; +using TextContent = Microsoft.Extensions.AI.TextContent; + +namespace Microsoft.Agents.AI.OpenAI.UnitTests.Extensions; + +/// +/// Unit tests for the class. +/// +public sealed class AIAgentWithOpenAIExtensionsTests +{ + /// + /// Verify that RunAsync throws ArgumentNullException when agent is null. + /// + [Fact] + public async Task RunAsync_WithNullAgent_ThrowsArgumentNullExceptionAsync() + { + // Arrange + AIAgent? agent = null; + var messages = new List + { + OpenAIChatMessage.CreateUserMessage("Test message") + }; + + // Act & Assert + var exception = await Assert.ThrowsAsync( + () => agent!.RunAsync(messages)); + + Assert.Equal("agent", exception.ParamName); + } + + /// + /// Verify that RunAsync throws ArgumentNullException when messages is null. + /// + [Fact] + public async Task RunAsync_WithNullMessages_ThrowsArgumentNullExceptionAsync() + { + // Arrange + var mockAgent = new Mock(); + IEnumerable? messages = null; + + // Act & Assert + var exception = await Assert.ThrowsAsync( + () => mockAgent.Object.RunAsync(messages!)); + + Assert.Equal("messages", exception.ParamName); + } + + /// + /// Verify that the RunAsync extension method calls the underlying agent's RunAsync with converted messages and parameters. + /// + [Fact] + public async Task RunAsync_CallsUnderlyingAgentAsync() + { + // Arrange + var mockAgent = new Mock(); + var mockThread = new Mock(); + var options = new AgentRunOptions(); + var cancellationToken = new CancellationToken(false); + const string TestMessageText = "Hello, assistant!"; + const string ResponseText = "This is the assistant's response."; + var openAiMessages = new List + { + OpenAIChatMessage.CreateUserMessage(TestMessageText) + }; + + var responseMessage = new ChatMessage(ChatRole.Assistant, [new TextContent(ResponseText)]); + + mockAgent + .Setup(a => a.RunAsync(It.IsAny>(), It.IsAny(), It.IsAny(), It.IsAny())) + .ReturnsAsync(new AgentRunResponse([responseMessage])); + + // Act + var result = await mockAgent.Object.RunAsync(openAiMessages, mockThread.Object, options, cancellationToken); + + // Assert + mockAgent.Verify( + a => a.RunAsync( + It.Is>(msgs => + msgs.ToList().Count == 1 && + msgs.ToList()[0].Text == TestMessageText), + mockThread.Object, + options, + cancellationToken), + Times.Once); + + Assert.NotNull(result); + Assert.NotEmpty(result.Content); + Assert.Equal(ResponseText, result.Content.Last().Text); + } + + /// + /// Verify that RunStreamingAsync throws ArgumentNullException when agent is null. + /// + [Fact] + public void RunStreamingAsync_WithNullAgent_ThrowsArgumentNullException() + { + // Arrange + AIAgent? agent = null; + var messages = new List + { + OpenAIChatMessage.CreateUserMessage("Test message") + }; + + // Act & Assert + Assert.Throws( + "agent", + () => agent!.RunStreamingAsync(messages)); + } + + /// + /// Verify that RunStreamingAsync throws ArgumentNullException when messages is null. + /// + [Fact] + public void RunStreamingAsync_WithNullMessages_ThrowsArgumentNullException() + { + // Arrange + var mockAgent = new Mock(); + IEnumerable? messages = null; + + // Act & Assert + var exception = Assert.Throws( + () => mockAgent.Object.RunStreamingAsync(messages!)); + + Assert.Equal("messages", exception.ParamName); + } + + /// + /// Verify that the RunStreamingAsync extension method calls the underlying agent's RunStreamingAsync with converted messages and parameters. + /// + [Fact] + public async Task RunStreamingAsync_CallsUnderlyingAgentAsync() + { + // Arrange + var mockAgent = new Mock(); + var mockThread = new Mock(); + var options = new AgentRunOptions(); + var cancellationToken = new CancellationToken(false); + const string TestMessageText = "Hello, assistant!"; + const string ResponseText1 = "This is "; + const string ResponseText2 = "the assistant's response."; + var openAiMessages = new List + { + OpenAIChatMessage.CreateUserMessage(TestMessageText) + }; + + var responseUpdates = new List + { + new(ChatRole.Assistant, ResponseText1), + new(ChatRole.Assistant, ResponseText2) + }; + + mockAgent + .Setup(a => a.RunStreamingAsync(It.IsAny>(), It.IsAny(), It.IsAny(), It.IsAny())) + .Returns(ToAsyncEnumerableAsync(responseUpdates)); + + // Act + var result = mockAgent.Object.RunStreamingAsync(openAiMessages, mockThread.Object, options, cancellationToken); + var updateCount = 0; + await foreach (var update in result) + { + updateCount++; + } + + // Assert + mockAgent.Verify( + a => a.RunStreamingAsync( + It.Is>(msgs => + msgs.ToList().Count == 1 && + msgs.ToList()[0].Text == TestMessageText), + mockThread.Object, + options, + cancellationToken), + Times.Once); + + Assert.True(updateCount > 0, "Expected at least one streaming update"); + } + + /// + /// Helper method to convert a list of AgentRunResponseUpdate to an async enumerable. + /// + private static async IAsyncEnumerable ToAsyncEnumerableAsync(IEnumerable updates) + { + foreach (var update in updates) + { + yield return await Task.FromResult(update); + } + } +} diff --git a/dotnet/tests/Microsoft.Agents.AI.OpenAI.UnitTests/Extensions/OpenAIAssistantClientExtensionsTests.cs b/dotnet/tests/Microsoft.Agents.AI.OpenAI.UnitTests/Extensions/OpenAIAssistantClientExtensionsTests.cs index c75d72cf34..3e9fe4d82a 100644 --- a/dotnet/tests/Microsoft.Agents.AI.OpenAI.UnitTests/Extensions/OpenAIAssistantClientExtensionsTests.cs +++ b/dotnet/tests/Microsoft.Agents.AI.OpenAI.UnitTests/Extensions/OpenAIAssistantClientExtensionsTests.cs @@ -10,7 +10,6 @@ using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.AI; -using OpenAI; using OpenAI.Assistants; namespace Microsoft.Agents.AI.OpenAI.UnitTests.Extensions; diff --git a/dotnet/tests/Microsoft.Agents.AI.OpenAI.UnitTests/Extensions/OpenAIChatClientExtensionsTests.cs b/dotnet/tests/Microsoft.Agents.AI.OpenAI.UnitTests/Extensions/OpenAIChatClientExtensionsTests.cs index ef9f27b01a..09c36ef218 100644 --- a/dotnet/tests/Microsoft.Agents.AI.OpenAI.UnitTests/Extensions/OpenAIChatClientExtensionsTests.cs +++ b/dotnet/tests/Microsoft.Agents.AI.OpenAI.UnitTests/Extensions/OpenAIChatClientExtensionsTests.cs @@ -6,7 +6,8 @@ using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.AI; -using OpenAI; +using OpenAI.Chat; +using ChatMessage = Microsoft.Extensions.AI.ChatMessage; using OpenAIChatClient = OpenAI.Chat.ChatClient; namespace Microsoft.Agents.AI.OpenAI.UnitTests.Extensions; diff --git a/dotnet/tests/Microsoft.Agents.AI.OpenAI.UnitTests/Extensions/OpenAIResponseClientExtensionsTests.cs b/dotnet/tests/Microsoft.Agents.AI.OpenAI.UnitTests/Extensions/OpenAIResponseClientExtensionsTests.cs index f31d343157..781ccb123e 100644 --- a/dotnet/tests/Microsoft.Agents.AI.OpenAI.UnitTests/Extensions/OpenAIResponseClientExtensionsTests.cs +++ b/dotnet/tests/Microsoft.Agents.AI.OpenAI.UnitTests/Extensions/OpenAIResponseClientExtensionsTests.cs @@ -7,7 +7,6 @@ using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.AI; -using OpenAI; using OpenAI.Responses; namespace Microsoft.Agents.AI.OpenAI.UnitTests.Extensions;