.Net: Add Azure OpenAI Agent Model Samples (#80)

* Improved Merge logic

* Add modularization for Model Samples and Azure OpenAI

* Address PR feedback

* Warning fix

* Address PR comments
This commit is contained in:
Roger Barreto
2025-06-18 12:00:38 +00:00
committed by GitHub
parent e6bfc51367
commit 14f3811648
10 changed files with 235 additions and 100 deletions
+31 -1
View File
@@ -1,5 +1,8 @@
// Copyright (c) Microsoft. All rights reserved.
using System.ClientModel;
using Azure.AI.OpenAI;
using Azure.Identity;
using Microsoft.Extensions.AI;
using Microsoft.Shared.Samples;
using OpenAI;
@@ -8,8 +11,35 @@ namespace GettingStarted;
public class AgentSample(ITestOutputHelper output) : BaseSample(output)
{
protected IChatClient GetOpenAIChatClient()
/// <summary>
/// Represents the available providers for <see cref="IChatClient"/> instances.
/// </summary>
public enum ChatClientProviders
{
OpenAI,
AzureOpenAI,
}
protected IChatClient GetChatClient(ChatClientProviders provider)
{
return provider switch
{
ChatClientProviders.OpenAI => GetOpenAIChatClient(),
ChatClientProviders.AzureOpenAI => GetAzureOpenAIChatClient(),
_ => throw new NotSupportedException($"Provider {provider} is not supported.")
};
}
private IChatClient GetOpenAIChatClient()
=> new OpenAIClient(TestConfiguration.OpenAI.ApiKey)
.GetChatClient(TestConfiguration.OpenAI.ChatModelId)
.AsIChatClient();
private IChatClient GetAzureOpenAIChatClient()
=> ((TestConfiguration.AzureOpenAI.ApiKey is null)
// Use Azure CLI credentials if API key is not provided.
? new AzureOpenAIClient(TestConfiguration.AzureOpenAI.Endpoint, new AzureCliCredential())
: new AzureOpenAIClient(TestConfiguration.AzureOpenAI.Endpoint, new ApiKeyCredential(TestConfiguration.AzureOpenAI.ApiKey)))
.GetChatClient(TestConfiguration.AzureOpenAI.DeploymentName)
.AsIChatClient();
}
@@ -15,6 +15,8 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Azure.AI.OpenAI" />
<PackageReference Include="Azure.Identity" />
<PackageReference Include="Microsoft.Extensions.AI.OpenAI" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" />
@@ -0,0 +1,56 @@
// Copyright (c) Microsoft. All rights reserved.
using System.ClientModel;
using Azure.AI.OpenAI;
using Azure.Identity;
using Microsoft.Agents;
using Microsoft.Extensions.AI;
using Microsoft.Shared.Samples;
namespace Providers;
/// <summary>
/// End-to-end sample showing how to use <see cref="ChatClientAgent"/> with Azure OpenAI Chat Completion.
/// </summary>
public sealed class ChatClientAgent_With_AzureOpenAIChatCompletion(ITestOutputHelper output) : AgentSample(output)
{
private const string JokerName = "Joker";
private const string JokerInstructions = "You are good at telling jokes.";
[Fact]
public async Task RunWithChatCompletion()
{
// Get the chat client to use for the agent.
using var chatClient = ((TestConfiguration.AzureOpenAI.ApiKey is null)
// Use Azure CLI credentials if API key is not provided.
? new AzureOpenAIClient(TestConfiguration.AzureOpenAI.Endpoint, new AzureCliCredential())
: new AzureOpenAIClient(TestConfiguration.AzureOpenAI.Endpoint, new ApiKeyCredential(TestConfiguration.AzureOpenAI.ApiKey)))
.GetChatClient(TestConfiguration.AzureOpenAI.DeploymentName)
.AsIChatClient();
// Define the agent
ChatClientAgent agent =
new(chatClient, new()
{
Name = JokerName,
Instructions = JokerInstructions,
});
// Start a new thread for the agent conversation.
AgentThread thread = agent.GetNewThread();
// Respond to user input
await RunAgentAsync("Tell me a joke about a pirate.");
await RunAgentAsync("Now add some emojis to the joke.");
// Local function to invoke agent and display the conversation messages for the thread.
async Task RunAgentAsync(string input)
{
this.WriteUserMessage(input);
var response = await agent.RunAsync(input, thread);
this.WriteResponseOutput(response);
}
}
}
@@ -10,7 +10,7 @@ using OpenAI;
namespace Providers;
/// <summary>
/// Shows how to use <see cref="ChatClientAgent"/> with Open AI Assistants.
/// End-to-end sample showing how to use <see cref="ChatClientAgent"/> with OpenAI Assistants.
/// </summary>
public sealed class ChatClientAgent_With_OpenAIAssistant(ITestOutputHelper output) : AgentSample(output)
{
@@ -45,11 +45,11 @@ public sealed class ChatClientAgent_With_OpenAIAssistant(ITestOutputHelper outpu
AgentThread thread = agent.GetNewThread();
// Respond to user input
await InvokeAgentAsync("Tell me a joke about a pirate.");
await InvokeAgentAsync("Now add some emojis to the joke.");
await RunAgentAsync("Tell me a joke about a pirate.");
await RunAgentAsync("Now add some emojis to the joke.");
// Local function to invoke agent and display the conversation messages for the thread.
async Task InvokeAgentAsync(string input)
async Task RunAgentAsync(string input)
{
this.WriteUserMessage(input);
@@ -8,7 +8,7 @@ using OpenAI;
namespace Providers;
/// <summary>
/// Shows how to use <see cref="ChatClientAgent"/> with Open AI Chat Completion.
/// End-to-end sample showing how to use <see cref="ChatClientAgent"/> with OpenAI Chat Completion.
/// </summary>
public sealed class ChatClientAgent_With_OpenAIChatCompletion(ITestOutputHelper output) : AgentSample(output)
{
@@ -16,7 +16,7 @@ public sealed class ChatClientAgent_With_OpenAIChatCompletion(ITestOutputHelper
private const string JokerInstructions = "You are good at telling jokes.";
[Fact]
public async Task RunWithOpenAIAssistant()
public async Task RunWithChatCompletion()
{
// Get the chat client to use for the agent.
using var chatClient = new OpenAIClient(TestConfiguration.OpenAI.ApiKey)
@@ -35,11 +35,11 @@ public sealed class ChatClientAgent_With_OpenAIChatCompletion(ITestOutputHelper
AgentThread thread = agent.GetNewThread();
// Respond to user input
await InvokeAgentAsync("Tell me a joke about a pirate.");
await InvokeAgentAsync("Now add some emojis to the joke.");
await RunAgentAsync("Tell me a joke about a pirate.");
await RunAgentAsync("Now add some emojis to the joke.");
// Local function to invoke agent and display the conversation messages for the thread.
async Task InvokeAgentAsync(string input)
async Task RunAgentAsync(string input)
{
this.WriteUserMessage(input);
@@ -22,11 +22,13 @@ public sealed class Step01_Running(ITestOutputHelper output) : AgentSample(outpu
/// Demonstrate the usage of <see cref="ChatClientAgent"/> where each invocation is
/// a unique interaction with no conversation history between them.
/// </summary>
[Fact]
public async Task RunWithoutThread()
[Theory]
[InlineData(ChatClientProviders.OpenAI)]
[InlineData(ChatClientProviders.AzureOpenAI)]
public async Task RunWithoutThread(ChatClientProviders provider)
{
// Get the chat client to use for the agent.
using var chatClient = base.GetOpenAIChatClient();
using var chatClient = base.GetChatClient(provider);
// Define the agent
ChatClientAgent agent =
@@ -37,12 +39,12 @@ public sealed class Step01_Running(ITestOutputHelper output) : AgentSample(outpu
});
// Respond to user input
await InvokeAgentAsync("Fortune favors the bold.");
await InvokeAgentAsync("I came, I saw, I conquered.");
await InvokeAgentAsync("Practice makes perfect.");
await RunAgentAsync("Fortune favors the bold.");
await RunAgentAsync("I came, I saw, I conquered.");
await RunAgentAsync("Practice makes perfect.");
// Local function to invoke agent and display the conversation messages.
async Task InvokeAgentAsync(string input)
async Task RunAgentAsync(string input)
{
this.WriteUserMessage(input);
@@ -54,11 +56,13 @@ public sealed class Step01_Running(ITestOutputHelper output) : AgentSample(outpu
/// <summary>
/// Demonstrate the usage of <see cref="ChatClientAgent"/> where a conversation history is maintained.
/// </summary>
[Fact]
public async Task RunWithConversationThread()
[Theory]
[InlineData(ChatClientProviders.OpenAI)]
[InlineData(ChatClientProviders.AzureOpenAI)]
public async Task RunWithConversationThread(ChatClientProviders provider)
{
// Get the chat client to use for the agent.
using var chatClient = base.GetOpenAIChatClient();
using var chatClient = base.GetChatClient(provider);
// Define the agent
ChatClientAgent agent =
@@ -72,11 +76,11 @@ public sealed class Step01_Running(ITestOutputHelper output) : AgentSample(outpu
AgentThread thread = agent.GetNewThread();
// Respond to user input
await InvokeAgentAsync("Tell me a joke about a pirate.");
await InvokeAgentAsync("Now add some emojis to the joke.");
await RunAgentAsync("Tell me a joke about a pirate.");
await RunAgentAsync("Now add some emojis to the joke.");
// Local function to invoke agent and display the conversation messages for the thread.
async Task InvokeAgentAsync(string input)
async Task RunAgentAsync(string input)
{
this.WriteUserMessage(input);
@@ -90,11 +94,13 @@ public sealed class Step01_Running(ITestOutputHelper output) : AgentSample(outpu
/// Demonstrate the usage of <see cref="ChatClientAgent"/> in streaming mode,
/// where a conversation is maintained by the <see cref="AgentThread"/>.
/// </summary>
[Fact]
public async Task StreamingRunWithConversationThread()
[Theory]
[InlineData(ChatClientProviders.OpenAI)]
[InlineData(ChatClientProviders.AzureOpenAI)]
public async Task StreamingRunWithConversationThread(ChatClientProviders provider)
{
// Get the chat client to use for the agent.
using var chatClient = base.GetOpenAIChatClient();
using var chatClient = base.GetChatClient(provider);
// Define the agent
ChatClientAgent agent =
@@ -108,11 +114,11 @@ public sealed class Step01_Running(ITestOutputHelper output) : AgentSample(outpu
AgentThread thread = agent.GetNewThread();
// Respond to user input
await InvokeAgentAsync("Tell me a joke about a pirate.");
await InvokeAgentAsync("Now add some emojis to the joke.");
await RunAgentAsync("Tell me a joke about a pirate.");
await RunAgentAsync("Now add some emojis to the joke.");
// Local function to invoke agent and display the conversation messages.
async Task InvokeAgentAsync(string input)
async Task RunAgentAsync(string input)
{
this.WriteUserMessage(input);
@@ -8,11 +8,13 @@ namespace Steps;
public sealed class Step02_UsingTools(ITestOutputHelper output) : AgentSample(output)
{
[Fact]
public async Task RunningWithTools()
[Theory]
[InlineData(ChatClientProviders.OpenAI)]
[InlineData(ChatClientProviders.AzureOpenAI)]
public async Task RunningWithTools(ChatClientProviders provider)
{
// Get the chat client to use for the agent.
using var chatClient = base.GetOpenAIChatClient();
using var chatClient = base.GetChatClient(provider);
// Define the agent
var menuTools = new MenuTools();
@@ -35,12 +37,12 @@ public sealed class Step02_UsingTools(ITestOutputHelper output) : AgentSample(ou
var thread = agent.GetNewThread();
// Respond to user input, invoking functions where appropriate.
await InvokeAgentAsync("Hello");
await InvokeAgentAsync("What is the special soup and its price?");
await InvokeAgentAsync("What is the special drink and its price?");
await InvokeAgentAsync("Thank you");
await RunAgentAsync("Hello");
await RunAgentAsync("What is the special soup and its price?");
await RunAgentAsync("What is the special drink and its price?");
await RunAgentAsync("Thank you");
async Task InvokeAgentAsync(string input)
async Task RunAgentAsync(string input)
{
this.WriteUserMessage(input);
var response = await agent.RunAsync(input, thread);
@@ -48,11 +50,13 @@ public sealed class Step02_UsingTools(ITestOutputHelper output) : AgentSample(ou
}
}
[Fact]
public async Task StreamingRunWithTools()
[Theory]
[InlineData(ChatClientProviders.OpenAI)]
[InlineData(ChatClientProviders.AzureOpenAI)]
public async Task StreamingRunWithTools(ChatClientProviders provider)
{
// Get the chat client to use for the agent.
using var chatClient = base.GetOpenAIChatClient();
using var chatClient = base.GetChatClient(provider);
// Define the agent
var menuTools = new MenuTools();
@@ -75,12 +79,12 @@ public sealed class Step02_UsingTools(ITestOutputHelper output) : AgentSample(ou
var thread = agent.GetNewThread();
// Respond to user input, invoking functions where appropriate.
await InvokeAgentAsync("Hello");
await InvokeAgentAsync("What is the special soup and its price?");
await InvokeAgentAsync("What is the special drink and its price?");
await InvokeAgentAsync("Thank you");
await RunAgentAsync("Hello");
await RunAgentAsync("What is the special soup and its price?");
await RunAgentAsync("What is the special drink and its price?");
await RunAgentAsync("Thank you");
async Task InvokeAgentAsync(string input)
async Task RunAgentAsync(string input)
{
this.WriteUserMessage(input);
await foreach (var update in agent.RunStreamingAsync(input, thread))