// Copyright (c) Microsoft. All rights reserved.
using System.Runtime.CompilerServices;
using Microsoft.Extensions.Configuration;
namespace Microsoft.Shared.Samples;
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable.
///
/// Provides access to application configuration settings.
///
public sealed class TestConfiguration
{
/// Gets the configuration settings for the OpenAI integration.
public static OpenAIConfig OpenAI => LoadSection();
/// Gets the configuration settings for the Azure OpenAI integration.
public static AzureOpenAIConfig AzureOpenAI => LoadSection();
/// Represents the configuration settings required to interact with the OpenAI service.
public class OpenAIConfig
{
/// Gets or sets the identifier for the chat completion model used in the application.
public string ChatModelId { get; set; }
/// Gets or sets the API key used for authentication with the OpenAI service.
public string ApiKey { get; set; }
}
///
/// Represents the configuration settings required to interact with the Azure OpenAI service.
///
public class AzureOpenAIConfig
{
/// Gets the URI endpoint used to connect to the service.
public Uri Endpoint { get; set; }
/// Gets or sets the name of the deployment.
public string DeploymentName { get; set; }
/// Gets or sets the API key used for authentication with the OpenAI service.
public string? ApiKey { get; set; }
}
/// Represents the configuration settings required to interact with the Azure AI service.
public sealed class AzureAIConfig
{
/// Gets or sets the endpoint of Azure AI Foundry project.
public string? Endpoint { get; set; }
/// Gets or sets the name of the model deployment.
public string? DeploymentName { get; set; }
}
///
/// Initializes the configuration system with the specified configuration root.
///
/// The root of the configuration hierarchy used to initialize the system. Must not be .
public static void Initialize(IConfigurationRoot configRoot)
{
s_instance = new TestConfiguration(configRoot);
}
#region Private Members
private readonly IConfigurationRoot _configRoot;
private static TestConfiguration? s_instance;
private TestConfiguration(IConfigurationRoot configRoot)
{
this._configRoot = configRoot;
}
///
/// Gets the configuration settings for the AzureAI integration.
///
public static AzureAIConfig AzureAI => LoadSection();
private static T LoadSection([CallerMemberName] string? caller = null)
{
if (s_instance is null)
{
throw new InvalidOperationException(
"TestConfiguration must be initialized with a call to Initialize(IConfigurationRoot) before accessing configuration values.");
}
if (string.IsNullOrEmpty(caller))
{
throw new ArgumentNullException(nameof(caller));
}
return s_instance._configRoot.GetSection(caller).Get() ??
throw new InvalidOperationException(caller);
}
#endregion
}