Skip to content

.Net: Add missing Ollama Connector Aspire Friendly Extensions #10775

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

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
// Copyright (c) Microsoft. All rights reserved.

using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.Ollama;
using Microsoft.SemanticKernel.Embeddings;
using Microsoft.SemanticKernel.TextGeneration;
using OllamaSharp;
using Xunit;

namespace SemanticKernel.Connectors.Ollama.UnitTests.Extensions;
Expand Down Expand Up @@ -54,4 +61,155 @@ public void AddOllamaTextEmbeddingGenerationCreatesService()
Assert.NotNull(kernel);
Assert.NotNull(service);
}

[Theory]
[MemberData(nameof(AddOllamaApiClientScenarios))]
public async Task AddOllamaApiClientEmbeddingsFromServiceCollectionAsync(ServiceCollectionRegistration registration)
{
using var myHttpClientHandler = new FakeHttpMessageHandler(File.ReadAllText("TestData/embeddings_test_response.json"));
using var httpClient = new HttpClient(myHttpClientHandler) { BaseAddress = new Uri("http://localhost:11434"), };
using var client = new OllamaApiClient(httpClient);
var builder = Kernel.CreateBuilder();
var services = builder.Services;

string? serviceId = null;
switch (registration)
{
case ServiceCollectionRegistration.KeyedOllamaApiClient:
services.AddKeyedSingleton<OllamaApiClient>(serviceId = "model", client);
break;
case ServiceCollectionRegistration.KeyedIOllamaApiClient:
services.AddKeyedSingleton<IOllamaApiClient>(serviceId = "model", client);
break;
case ServiceCollectionRegistration.OllamaApiClient:
services.AddSingleton<OllamaApiClient>(client);
break;
case ServiceCollectionRegistration.Endpoint:
services.AddSingleton<IOllamaApiClient>(client);
break;
}

services.AddOllamaTextEmbeddingGeneration(serviceId: serviceId);
var serviceProvider = services.BuildServiceProvider();

var kernel = builder.Build();

ITextEmbeddingGenerationService service = kernel.GetRequiredService<ITextEmbeddingGenerationService>(serviceId);

Assert.NotNull(service);

await service.GenerateEmbeddingsAsync(["text"]);

Assert.Equal(1, myHttpClientHandler.InvokedCount);
}

[Theory]
[MemberData(nameof(AddOllamaApiClientScenarios))]
public async Task AddOllamaApiClientChatCompletionFromServiceCollectionAsync(ServiceCollectionRegistration registration)
{
using var myHttpClientHandler = new FakeHttpMessageHandler(File.ReadAllText("TestData/chat_completion_test_response.txt"));
using var httpClient = new HttpClient(myHttpClientHandler) { BaseAddress = new Uri("http://localhost:11434"), };
using var client = new OllamaApiClient(httpClient);
var builder = Kernel.CreateBuilder();
var services = builder.Services;

string? serviceId = null;
switch (registration)
{
case ServiceCollectionRegistration.KeyedOllamaApiClient:
services.AddKeyedSingleton<OllamaApiClient>(serviceId = "model", client);
break;
case ServiceCollectionRegistration.KeyedIOllamaApiClient:
services.AddKeyedSingleton<IOllamaApiClient>(serviceId = "model", client);
break;
case ServiceCollectionRegistration.OllamaApiClient:
services.AddSingleton<OllamaApiClient>(client);
break;
case ServiceCollectionRegistration.Endpoint:
services.AddSingleton<IOllamaApiClient>(client);
break;
}

builder.AddOllamaChatCompletion(serviceId: serviceId);
var kernel = builder.Build();

IChatCompletionService service = kernel.GetRequiredService<IChatCompletionService>(serviceId);

Assert.NotNull(service);

await service.GetChatMessageContentsAsync(new());

Assert.Equal(1, myHttpClientHandler.InvokedCount);
}

[Theory]
[MemberData(nameof(AddOllamaApiClientScenarios))]
public async Task AddOllamaApiClientTextGenerationFromServiceCollectionAsync(ServiceCollectionRegistration registration)
{
using var myHttpClientHandler = new FakeHttpMessageHandler(File.ReadAllText("TestData/chat_completion_test_response.txt"));
using var httpClient = new HttpClient(myHttpClientHandler) { BaseAddress = new Uri("http://localhost:11434"), };
using var client = new OllamaApiClient(httpClient, "model");
var builder = Kernel.CreateBuilder();
var services = builder.Services;

string? serviceId = null;
switch (registration)
{
case ServiceCollectionRegistration.KeyedOllamaApiClient:
services.AddKeyedSingleton<OllamaApiClient>(serviceId = "model", client);
break;
case ServiceCollectionRegistration.KeyedIOllamaApiClient:
services.AddKeyedSingleton<IOllamaApiClient>(serviceId = "model", client);
break;
case ServiceCollectionRegistration.OllamaApiClient:
services.AddSingleton<OllamaApiClient>(client);
break;
case ServiceCollectionRegistration.Endpoint:
services.AddSingleton<IOllamaApiClient>(client);
break;
}

builder.AddOllamaTextGeneration(serviceId: serviceId);
var kernel = builder.Build();

ITextGenerationService service = kernel.GetRequiredService<ITextGenerationService>(serviceId);

Assert.NotNull(service);

await service.GetStreamingTextContentsAsync("test prompt").GetAsyncEnumerator().MoveNextAsync();

Assert.Equal(1, myHttpClientHandler.InvokedCount);
}

public enum ServiceCollectionRegistration
{
KeyedOllamaApiClient,
KeyedIOllamaApiClient,
OllamaApiClient,
Endpoint,
}

public static TheoryData<ServiceCollectionRegistration> AddOllamaApiClientScenarios => new()
{
{ ServiceCollectionRegistration.KeyedOllamaApiClient },
{ ServiceCollectionRegistration.KeyedIOllamaApiClient },
{ ServiceCollectionRegistration.OllamaApiClient },
{ ServiceCollectionRegistration.Endpoint },
};

private sealed class FakeHttpMessageHandler(string responseContent) : HttpMessageHandler
{
public int InvokedCount { get; private set; }

protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
this.InvokedCount++;

return Task.FromResult(
new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(responseContent)
});
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
// Copyright (c) Microsoft. All rights reserved.

using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
Expand Down Expand Up @@ -79,4 +84,174 @@ public void AddOllamaTextEmbeddingsGenerationToServiceCollection()

Assert.NotNull(service);
}

[Theory]
[MemberData(nameof(AddOllamaApiClientScenarios))]
public async Task AddOllamaApiClientEmbeddingsFromServiceCollectionAsync(ServiceCollectionRegistration registration)
{
using var myHttpClientHandler = new FakeHttpMessageHandler(File.ReadAllText("TestData/embeddings_test_response.json"));
using var httpClient = new HttpClient(myHttpClientHandler) { BaseAddress = new Uri("http://localhost:11434"), };
using var client = new OllamaApiClient(httpClient);
var services = new ServiceCollection();
string? serviceId = null;
switch (registration)
{
case ServiceCollectionRegistration.KeyedOllamaApiClient:
services.AddKeyedSingleton<OllamaApiClient>(serviceId = "model", client);
break;
case ServiceCollectionRegistration.KeyedIOllamaApiClient:
services.AddKeyedSingleton<IOllamaApiClient>(serviceId = "model", client);
break;
case ServiceCollectionRegistration.OllamaApiClient:
services.AddSingleton<OllamaApiClient>(client);
break;
case ServiceCollectionRegistration.Endpoint:
services.AddSingleton<IOllamaApiClient>(client);
break;
}

services.AddOllamaTextEmbeddingGeneration(serviceId: serviceId);
var serviceProvider = services.BuildServiceProvider();

ITextEmbeddingGenerationService service;
if (registration is ServiceCollectionRegistration.KeyedOllamaApiClient
or ServiceCollectionRegistration.KeyedIOllamaApiClient)
{
service = serviceProvider.GetRequiredKeyedService<ITextEmbeddingGenerationService>(serviceId);
}
else
{
service = serviceProvider.GetRequiredService<ITextEmbeddingGenerationService>();
}

Assert.NotNull(service);

await service.GenerateEmbeddingsAsync(["text"]);

Assert.Equal(1, myHttpClientHandler.InvokedCount);
}

[Theory]
[MemberData(nameof(AddOllamaApiClientScenarios))]
public async Task AddOllamaApiClientChatCompletionFromServiceCollectionAsync(ServiceCollectionRegistration registration)
{
using var myHttpClientHandler = new FakeHttpMessageHandler(File.ReadAllText("TestData/chat_completion_test_response.txt"));
using var httpClient = new HttpClient(myHttpClientHandler) { BaseAddress = new Uri("http://localhost:11434"), };
using var client = new OllamaApiClient(httpClient);
var services = new ServiceCollection();
string? serviceId = null;
switch (registration)
{
case ServiceCollectionRegistration.KeyedOllamaApiClient:
services.AddKeyedSingleton<OllamaApiClient>(serviceId = "model", client);
break;
case ServiceCollectionRegistration.KeyedIOllamaApiClient:
services.AddKeyedSingleton<IOllamaApiClient>(serviceId = "model", client);
break;
case ServiceCollectionRegistration.OllamaApiClient:
services.AddSingleton<OllamaApiClient>(client);
break;
case ServiceCollectionRegistration.Endpoint:
services.AddSingleton<IOllamaApiClient>(client);
break;
}

services.AddOllamaChatCompletion(serviceId: serviceId);
var serviceProvider = services.BuildServiceProvider();

IChatCompletionService service;
if (registration is ServiceCollectionRegistration.KeyedOllamaApiClient
or ServiceCollectionRegistration.KeyedIOllamaApiClient)
{
service = serviceProvider.GetRequiredKeyedService<IChatCompletionService>(serviceId);
}
else
{
service = serviceProvider.GetRequiredService<IChatCompletionService>();
}

Assert.NotNull(service);

await service.GetChatMessageContentsAsync(new());

Assert.Equal(1, myHttpClientHandler.InvokedCount);
}

[Theory]
[MemberData(nameof(AddOllamaApiClientScenarios))]
public async Task AddOllamaApiClientTextGenerationFromServiceCollectionAsync(ServiceCollectionRegistration registration)
{
using var myHttpClientHandler = new FakeHttpMessageHandler(File.ReadAllText("TestData/text_generation_test_response_stream.txt"));
using var httpClient = new HttpClient(myHttpClientHandler) { BaseAddress = new Uri("http://localhost:11434"), };
using var client = new OllamaApiClient(httpClient, "model");
var services = new ServiceCollection();
string? serviceId = null;
switch (registration)
{
case ServiceCollectionRegistration.KeyedOllamaApiClient:
services.AddKeyedSingleton<OllamaApiClient>(serviceId = "model", client);
break;
case ServiceCollectionRegistration.KeyedIOllamaApiClient:
services.AddKeyedSingleton<IOllamaApiClient>(serviceId = "model", client);
break;
case ServiceCollectionRegistration.OllamaApiClient:
services.AddSingleton<OllamaApiClient>(client);
break;
case ServiceCollectionRegistration.Endpoint:
services.AddSingleton<IOllamaApiClient>(client);
break;
}

services.AddOllamaTextGeneration(serviceId: serviceId);
var serviceProvider = services.BuildServiceProvider();

ITextGenerationService service;
if (registration is ServiceCollectionRegistration.KeyedOllamaApiClient
or ServiceCollectionRegistration.KeyedIOllamaApiClient)
{
service = serviceProvider.GetRequiredKeyedService<ITextGenerationService>(serviceId);
}
else
{
service = serviceProvider.GetRequiredService<ITextGenerationService>();
}

Assert.NotNull(service);

await service.GetStreamingTextContentsAsync("test prompt").GetAsyncEnumerator().MoveNextAsync();

Assert.Equal(1, myHttpClientHandler.InvokedCount);
}

public enum ServiceCollectionRegistration
{
KeyedOllamaApiClient,
KeyedIOllamaApiClient,
OllamaApiClient,
Endpoint,
}

public static TheoryData<ServiceCollectionRegistration> AddOllamaApiClientScenarios => new()
{
{ ServiceCollectionRegistration.KeyedOllamaApiClient },
{ ServiceCollectionRegistration.KeyedIOllamaApiClient },
{ ServiceCollectionRegistration.OllamaApiClient },
{ ServiceCollectionRegistration.Endpoint },
};

private sealed class FakeHttpMessageHandler(string responseContent) : HttpMessageHandler
{
public int InvokedCount { get; private set; }

protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
this.InvokedCount++;

return Task.FromResult(
new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(responseContent)
});
}
}
}
Loading
Loading