Add provider abstraction layer with Mistral and Ollama implementations
Define IProvider interface and implement concrete providers for Mistral (with custom DTOs for chat completion API) and Ollama (via OllamaSharp). Includes DI registration extensions using Microsoft.Extensions.AI.
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
|
||||
using Luna.Configuration.Extensions;
|
||||
using Luna.Providers.Abstractions;
|
||||
using Luna.Providers.Mistral;
|
||||
using Luna.Providers.Ollama;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Luna.Providers.Extensions;
|
||||
|
||||
public static class ServiceCollectionExtensions
|
||||
{
|
||||
extension(IServiceCollection services)
|
||||
{
|
||||
public IServiceCollection AddProviders()
|
||||
{
|
||||
return services
|
||||
.AddMistralProvider()
|
||||
.AddOllamaProvider();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Registers the <see cref="MistralProvider"/> and maps <see cref="Provider"/> to it as a singleton.
|
||||
/// </summary>
|
||||
/// <param name="services">The service collection.</param>
|
||||
/// <returns>The updated service collection.</returns>
|
||||
private IServiceCollection AddMistralProvider()
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(services);
|
||||
|
||||
services.AddProviderOptions(MistralProvider.Name);
|
||||
services.AddHttpClient(
|
||||
MistralProvider.Name,
|
||||
(provider, httpClient) =>
|
||||
{
|
||||
var options = provider.GetProviderOptions<MistralProvider>(MistralProvider.Name);
|
||||
httpClient.BaseAddress = new Uri(options.ApiUrl);
|
||||
httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {options.ApiKey}");
|
||||
});
|
||||
|
||||
services.AddKeyedSingleton<IProvider, MistralProvider>(MistralProvider.Name);
|
||||
return services;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Registers the <see cref="OllamaProvider"/> and maps <see cref="Provider"/> to it as a singleton.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private IServiceCollection AddOllamaProvider()
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(services);
|
||||
|
||||
services.AddProviderOptions(OllamaProvider.Name);
|
||||
services.AddHttpClient(
|
||||
OllamaProvider.Name,
|
||||
(provider, httpClient) =>
|
||||
{
|
||||
var options = provider.GetProviderOptions<OllamaProvider>(OllamaProvider.Name);
|
||||
|
||||
httpClient.BaseAddress = new Uri(options.ApiUrl);
|
||||
httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {options.ApiKey}");
|
||||
});
|
||||
|
||||
services.AddKeyedSingleton<IProvider, OllamaProvider>(OllamaProvider.Name);
|
||||
return services;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
using Luna.Configuration;
|
||||
using Luna.Providers.Abstractions;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace Luna.Providers.Extensions;
|
||||
|
||||
public static class ServiceProviderExtensions
|
||||
{
|
||||
extension(IServiceProvider provider)
|
||||
{
|
||||
public TProvider GetProvider<TProvider>(string? name = null)
|
||||
where TProvider : IProvider
|
||||
{
|
||||
name ??= TProvider.Name;
|
||||
return provider.GetRequiredKeyedService<TProvider>(name);
|
||||
}
|
||||
|
||||
public ProviderOptions GetProviderOptions<TProvider>(string? name = null)
|
||||
where TProvider : IProvider
|
||||
{
|
||||
name ??= TProvider.Name;
|
||||
return provider
|
||||
.GetRequiredService<IOptionsMonitor<ProviderOptions>>()
|
||||
.Get(name);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user