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:
2026-04-04 04:10:39 +02:00
parent 63818df4b5
commit 45d3ee1ed6
18 changed files with 515 additions and 0 deletions
@@ -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;
}
}
}