// Copyright (c) Microsoft. All rights reserved.
using ChatWithAgent.Configuration;
namespace ChatWithAgent.AppHost.Extensions;
///
/// Resource builder extensions.
///
public static class ResourceBuilderExtensions
{
///
/// Adds host configuration as environment variables to the resource.
///
/// The resource type.
/// The resource builder.
/// The host configuration.
/// The .
public static IResourceBuilder WithEnvironment(this IResourceBuilder builder, HostConfig config) where T : IResourceWithEnvironment
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(config);
// Add AI chat service configuration to the environment variables so that Api Service can access it.
builder.WithEnvironment(nameof(config.AIChatService), config.AIChatService);
switch (config.AIChatService)
{
case AzureOpenAIChatConfig.ConfigSectionName:
{
builder.WithEnvironment($"{HostConfig.AIServicesSectionName}__{nameof(config.AzureOpenAIChat)}__{nameof(config.AzureOpenAIChat.DeploymentName)}", config.AzureOpenAIChat.DeploymentName);
builder.WithEnvironment($"{HostConfig.AIServicesSectionName}__{nameof(config.AzureOpenAIChat)}__{nameof(config.AzureOpenAIChat.ModelName)}", config.AzureOpenAIChat.ModelName);
break;
}
case OpenAIChatConfig.ConfigSectionName:
{
builder.WithEnvironment($"{HostConfig.AIServicesSectionName}__{nameof(config.OpenAIChat)}__{nameof(config.OpenAIChat.ModelName)}", config.OpenAIChat.ModelName);
break;
}
default:
throw new NotSupportedException($"AI service '{config.AIChatService}' is not supported.");
}
// Add RAG configuration to the environment variables so that Api Service can access it.
builder.WithEnvironment($"{nameof(config.Rag)}__{nameof(config.Rag.AIEmbeddingService)}", config.Rag.AIEmbeddingService);
builder.WithEnvironment($"{nameof(config.Rag)}__{nameof(config.Rag.VectorStoreType)}", config.Rag.VectorStoreType);
builder.WithEnvironment($"{nameof(config.Rag)}__{nameof(config.Rag.CollectionName)}", config.Rag.CollectionName);
switch (config.Rag.AIEmbeddingService)
{
case AzureOpenAIEmbeddingsConfig.ConfigSectionName:
{
builder.WithEnvironment($"{HostConfig.AIServicesSectionName}__{nameof(config.AzureOpenAIEmbeddings)}__{nameof(config.AzureOpenAIEmbeddings.DeploymentName)}", config.AzureOpenAIEmbeddings.DeploymentName);
builder.WithEnvironment($"{HostConfig.AIServicesSectionName}__{nameof(config.AzureOpenAIEmbeddings)}__{nameof(config.AzureOpenAIEmbeddings.ModelName)}", config.AzureOpenAIEmbeddings.ModelName);
break;
}
case OpenAIEmbeddingsConfig.ConfigSectionName:
{
builder.WithEnvironment($"{HostConfig.AIServicesSectionName}__{nameof(config.OpenAIEmbeddings)}__{nameof(config.OpenAIEmbeddings.ModelName)}", config.OpenAIEmbeddings.ModelName);
break;
}
default:
throw new NotSupportedException($"AI service '{config.Rag.AIEmbeddingService}' is not supported.");
}
return builder;
}
///
/// Adds connection strings of source resources to a destination resource.
///
/// The type of the destination resource.
/// The destination resource.
/// The source resource with the connection string.
/// The updated resource builder.
public static IResourceBuilder WithReferences(this IResourceBuilder builder, IList> resources) where T : IResourceWithEnvironment
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(resources);
foreach (var resource in resources)
{
builder.WithReference(resource);
}
return builder;
}
}