// 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; using OpenAI.Responses; namespace GettingStarted; public class AgentSample(ITestOutputHelper output) : BaseSample(output) { /// /// Represents the available providers for instances. /// public enum ChatClientProviders { OpenAI, AzureOpenAI, OpenAIResponses, OpenAIResponses_InMemoryMessage, OpenAIResponses_ConversationId } protected IChatClient GetChatClient(ChatClientProviders provider) => provider switch { ChatClientProviders.OpenAI => GetOpenAIChatClient(), ChatClientProviders.AzureOpenAI => GetAzureOpenAIChatClient(), ChatClientProviders.OpenAIResponses or ChatClientProviders.OpenAIResponses_InMemoryMessage or ChatClientProviders.OpenAIResponses_ConversationId => GetOpenAIResponsesClient(), _ => throw new NotSupportedException($"Provider {provider} is not supported.") }; protected ChatOptions? GetChatOptions(ChatClientProviders? provider) => provider switch { ChatClientProviders.OpenAIResponses_InMemoryMessage => new() { RawRepresentationFactory = static (_) => new ResponseCreationOptions() { StoredOutputEnabled = false } }, ChatClientProviders.OpenAIResponses_ConversationId => new() { RawRepresentationFactory = static (_) => new ResponseCreationOptions() { StoredOutputEnabled = true } }, _ => null }; 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(); private IChatClient GetOpenAIResponsesClient() => new OpenAIClient(TestConfiguration.OpenAI.ApiKey) .GetOpenAIResponseClient(TestConfiguration.OpenAI.ChatModelId) .AsIChatClient(); }