Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add OpenAISettingsBuilder #191

Merged
merged 3 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/AzureOpenAIProxy.ApiApp/Builders/OpenAISettingsBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using AzureOpenAIProxy.ApiApp.Configurations;

namespace AzureOpenAIProxy.ApiApp.Builders;

/// <summary>
/// This provides interface to the <see cref="OpenAISettingsBuilder"/> class.
/// </summary>
public interface IOpenAISettingsBuilder
{
/// <summary>
/// Builds the <see cref="OpenAISettings"/> instance.
/// </summary>
/// <returns>Returns the <see cref="OpenAISettings"/> instance.</returns>
OpenAISettings Build();
}

/// <summary>
/// This represents the builder entity for <see cref="OpenAISettings"/> class.
/// </summary>
public class OpenAISettingsBuilder : IOpenAISettingsBuilder
{
private OpenAISettings? _settings;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no way to set this field value. Maybe changing it to the list of OpenAIInstanceSettings and inject it from outside?


/// <inheritdoc />
public OpenAISettings Build()
{
this._settings ??= new OpenAISettings();

return this._settings;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@ public static class ServiceCollectionExtensions
/// </summary>
/// <param name="services"><see cref="IServiceCollection"/> instance.</param>
/// <returns>Returns <see cref="IServiceCollection"/> instance.</returns>
public static IServiceCollection AddOpenAISettingsFromAppSettings(this IServiceCollection services)
public static IServiceCollection AddOpenAISettings(this IServiceCollection services)
{
services.AddSingleton<OpenAISettings>(sp =>
{
//var settings = new OpenAISettingsBuilder()
// .WithAppSettings(sp)
// .WithKeyVault(sp)
// .Build();

var configuration = sp.GetService<IConfiguration>()
?? throw new InvalidOperationException($"{nameof(IConfiguration)} service is not registered.");

Expand All @@ -39,7 +44,7 @@ public static IServiceCollection AddOpenAISettingsFromAppSettings(this IServiceC
/// <returns>Returns <see cref="IServiceCollection"/> instance.</returns>
public static IServiceCollection AddOpenAIService(this IServiceCollection services)
{
services.AddOpenAISettingsFromAppSettings();
services.AddOpenAISettings();
services.AddHttpClient<IOpenAIService, OpenAIService>();

return services;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using AzureOpenAIProxy.ApiApp.Builders;
using AzureOpenAIProxy.ApiApp.Configurations;

using FluentAssertions;

namespace AzureOpenAIProxy.ApiApp.Tests.Builders;

public class OpenAISettingsBuilderTests
{
[Fact]
public void Given_OpenAISettingsBuilder_When_Invoked_Build_Then_It_Should_Return_Instance()
{
// Arrange
var builder = new OpenAISettingsBuilder();

// Act
var result = builder.Build();

// Assert
result.Should().NotBeNull().And.BeOfType<OpenAISettings>();
}
}