Skip to content

Commit 1cc71b4

Browse files
authored
.Net: Adding UT for Ollama KernelBuilder Extensions (#12237)
### Motivation and Context Inspired in the Use-Case raised in the issue reported below, adding this UT to ensure the behavior is as expected. - #12230
1 parent 19fa17e commit 1cc71b4

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

dotnet/src/Connectors/Connectors.Ollama.UnitTests/Extensions/OllamaKernelBuilderExtensionsTests.cs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,67 @@ public async Task AddOllamaApiClientEmbeddingGeneratorFromServiceCollectionAsync
118118
Assert.Equal(1, myHttpClientHandler.InvokedCount);
119119
}
120120

121+
[Theory]
122+
[Obsolete("Temporarily test for obsolete TextEmbeddingGenerationService.")]
123+
[MemberData(nameof(AddOllamaApiClientScenarios))]
124+
public void AddOllamaTextEmbeddingGenerationShouldGetRequiredServiceFromKernel(ServiceCollectionRegistration registration)
125+
{
126+
using var httpClient = new HttpClient() { BaseAddress = new Uri("http://localhost:11434"), };
127+
using var client = new OllamaApiClient(httpClient);
128+
var builder = Kernel.CreateBuilder();
129+
string? serviceId = null;
130+
131+
switch (registration)
132+
{
133+
case ServiceCollectionRegistration.KeyedOllamaApiClient:
134+
builder.AddOllamaTextEmbeddingGeneration(serviceId: serviceId = "model", ollamaClient: client);
135+
break;
136+
case ServiceCollectionRegistration.OllamaApiClient:
137+
builder.AddOllamaTextEmbeddingGeneration(ollamaClient: client);
138+
break;
139+
case ServiceCollectionRegistration.Endpoint:
140+
builder.AddOllamaTextEmbeddingGeneration("model", httpClient.BaseAddress);
141+
break;
142+
case ServiceCollectionRegistration.KeyedIOllamaApiClient:
143+
return; // IOllamaApiClient is not supported for KernelBuilder extensions, skipping
144+
}
145+
146+
var kernel = builder.Build();
147+
148+
ITextEmbeddingGenerationService service = kernel.GetRequiredService<ITextEmbeddingGenerationService>(serviceId);
149+
Assert.NotNull(service);
150+
}
151+
152+
[Theory]
153+
[MemberData(nameof(AddOllamaApiClientScenarios))]
154+
public void AddOllamaEmbeddingGeneratorShouldGetRequiredServiceFromKernel(ServiceCollectionRegistration registration)
155+
{
156+
using var httpClient = new HttpClient() { BaseAddress = new Uri("http://localhost:11434"), };
157+
using var client = new OllamaApiClient(httpClient);
158+
var builder = Kernel.CreateBuilder();
159+
string? serviceId = null;
160+
161+
switch (registration)
162+
{
163+
case ServiceCollectionRegistration.KeyedOllamaApiClient:
164+
builder.AddOllamaEmbeddingGenerator(serviceId: serviceId = "model", ollamaClient: client);
165+
break;
166+
case ServiceCollectionRegistration.OllamaApiClient:
167+
builder.AddOllamaEmbeddingGenerator(ollamaClient: client);
168+
break;
169+
case ServiceCollectionRegistration.Endpoint:
170+
builder.AddOllamaEmbeddingGenerator("model", httpClient.BaseAddress);
171+
break;
172+
case ServiceCollectionRegistration.KeyedIOllamaApiClient:
173+
return; // IOllamaApiClient is not supported for KernelBuilder extensions, skipping
174+
}
175+
176+
var kernel = builder.Build();
177+
178+
var service = kernel.GetRequiredService<IEmbeddingGenerator<string, Embedding<float>>>(serviceId);
179+
Assert.NotNull(service);
180+
}
181+
121182
[Theory]
122183
[MemberData(nameof(AddOllamaApiClientScenarios))]
123184
[Obsolete("Temporarily test for obsolete TextEmbeddingGenerationService.")]

0 commit comments

Comments
 (0)