From 2616bb8eb96f067a025de74d75fd36b9ea8f1687 Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Thu, 24 Oct 2024 08:00:28 -0400 Subject: [PATCH] Add string constructor to OllamaEmbeddingGenerator (#5562) We'd previously added one to OllamaChatClient but neglected to add one here. --- .../OllamaEmbeddingGenerator.cs | 15 ++++++++++++++- .../OllamaEmbeddingGeneratorTests.cs | 8 ++++---- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/Libraries/Microsoft.Extensions.AI.Ollama/OllamaEmbeddingGenerator.cs b/src/Libraries/Microsoft.Extensions.AI.Ollama/OllamaEmbeddingGenerator.cs index 6a34a2ff81..5779b60cbc 100644 --- a/src/Libraries/Microsoft.Extensions.AI.Ollama/OllamaEmbeddingGenerator.cs +++ b/src/Libraries/Microsoft.Extensions.AI.Ollama/OllamaEmbeddingGenerator.cs @@ -21,6 +21,18 @@ public sealed class OllamaEmbeddingGenerator : IEmbeddingGeneratorThe to use for sending requests. private readonly HttpClient _httpClient; + /// Initializes a new instance of the class. + /// The endpoint URI where Ollama is hosted. + /// + /// The id of the model to use. This may also be overridden per request via . + /// Either this parameter or must provide a valid model id. + /// + /// An instance to use for HTTP operations. + public OllamaEmbeddingGenerator(string endpoint, string? modelId = null, HttpClient? httpClient = null) + : this(new Uri(Throw.IfNull(endpoint)), modelId, httpClient) + { + } + /// Initializes a new instance of the class. /// The endpoint URI where Ollama is hosted. /// @@ -59,7 +71,8 @@ public sealed class OllamaEmbeddingGenerator : IEmbeddingGenerator - public async Task>> GenerateAsync(IEnumerable values, EmbeddingGenerationOptions? options = null, CancellationToken cancellationToken = default) + public async Task>> GenerateAsync( + IEnumerable values, EmbeddingGenerationOptions? options = null, CancellationToken cancellationToken = default) { _ = Throw.IfNull(values); diff --git a/test/Libraries/Microsoft.Extensions.AI.Ollama.Tests/OllamaEmbeddingGeneratorTests.cs b/test/Libraries/Microsoft.Extensions.AI.Ollama.Tests/OllamaEmbeddingGeneratorTests.cs index 205398c9a1..541aab244f 100644 --- a/test/Libraries/Microsoft.Extensions.AI.Ollama.Tests/OllamaEmbeddingGeneratorTests.cs +++ b/test/Libraries/Microsoft.Extensions.AI.Ollama.Tests/OllamaEmbeddingGeneratorTests.cs @@ -17,14 +17,14 @@ public class OllamaEmbeddingGeneratorTests [Fact] public void Ctor_InvalidArgs_Throws() { - Assert.Throws("endpoint", () => new OllamaEmbeddingGenerator(null!)); - Assert.Throws("modelId", () => new OllamaEmbeddingGenerator(new("http://localhost"), " ")); + Assert.Throws("endpoint", () => new OllamaEmbeddingGenerator((string)null!)); + Assert.Throws("modelId", () => new OllamaEmbeddingGenerator(new Uri("http://localhost"), " ")); } [Fact] public void GetService_SuccessfullyReturnsUnderlyingClient() { - using OllamaEmbeddingGenerator generator = new(new("http://localhost")); + using OllamaEmbeddingGenerator generator = new("http://localhost"); Assert.Same(generator, generator.GetService()); Assert.Same(generator, generator.GetService>>()); @@ -76,7 +76,7 @@ public class OllamaEmbeddingGeneratorTests using VerbatimHttpHandler handler = new(Input, Output); using HttpClient httpClient = new(handler); - using IEmbeddingGenerator> generator = new OllamaEmbeddingGenerator(new("http://localhost:11434"), "all-minilm", httpClient); + using IEmbeddingGenerator> generator = new OllamaEmbeddingGenerator("http://localhost:11434", "all-minilm", httpClient); var response = await generator.GenerateAsync([ "hello, world!",