Skip to content
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
Expand Up @@ -24,7 +24,7 @@ namespace Microsoft.Extensions.AI;
/// are used which might employ such mutation.
/// </para>
/// </remarks>
public interface IEmbeddingGenerator<TInput, TEmbedding> : IDisposable
public interface IEmbeddingGenerator<in TInput, TEmbedding> : IDisposable
where TEmbedding : Embedding
{
/// <summary>Generates embeddings for each of the supplied <paramref name="values"/>.</summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Xunit;

#pragma warning disable xUnit2013 // Do not use equality check to check for collection size.
Expand Down Expand Up @@ -243,4 +244,21 @@ public void Indexer_InvalidIndex_Throws()
Assert.Throws<ArgumentOutOfRangeException>("index", () => embeddings[-1]);
Assert.Throws<ArgumentOutOfRangeException>("index", () => embeddings[2]);
}

[Fact]
public async Task Generator_SupportsCovariantInput()
{
var expectedGeneratedEmbeddings = new GeneratedEmbeddings<Embedding<float>>([new Embedding<float>(new float[] { 1, 2, 3 })]);

using IEmbeddingGenerator<object, Embedding<float>> acceptsObject = new TestEmbeddingGenerator<object, Embedding<float>>
{
GenerateAsyncCallback = (values, options, cancellationToken) => Task.FromResult(expectedGeneratedEmbeddings),
};

IEmbeddingGenerator<string, Embedding<float>> acceptsString = acceptsObject;

var actual = await acceptsString.GenerateAsync(["hello"]);

Assert.Same(expectedGeneratedEmbeddings, actual);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,27 @@
using System.Threading;
using System.Threading.Tasks;

#pragma warning disable SA1402 // File may only contain a single type
#pragma warning disable CA1816 // Dispose methods should call SuppressFinalize

namespace Microsoft.Extensions.AI;

public sealed class TestEmbeddingGenerator : IEmbeddingGenerator<string, Embedding<float>>
public class TestEmbeddingGenerator<TInput, TEmbedding> : IEmbeddingGenerator<TInput, TEmbedding>
where TEmbedding : Embedding
{
public TestEmbeddingGenerator()
{
GetServiceCallback = DefaultGetServiceCallback;
}

public Func<IEnumerable<string>, EmbeddingGenerationOptions?, CancellationToken, Task<GeneratedEmbeddings<Embedding<float>>>>? GenerateAsyncCallback { get; set; }
public Func<IEnumerable<TInput>, EmbeddingGenerationOptions?, CancellationToken, Task<GeneratedEmbeddings<TEmbedding>>>? GenerateAsyncCallback { get; set; }

public Func<Type, object?, object?> GetServiceCallback { get; set; }

private object? DefaultGetServiceCallback(Type serviceType, object? serviceKey) =>
serviceType is not null && serviceKey is null && serviceType.IsInstanceOfType(this) ? this : null;

public Task<GeneratedEmbeddings<Embedding<float>>> GenerateAsync(IEnumerable<string> values, EmbeddingGenerationOptions? options = null, CancellationToken cancellationToken = default)
public Task<GeneratedEmbeddings<TEmbedding>> GenerateAsync(IEnumerable<TInput> values, EmbeddingGenerationOptions? options = null, CancellationToken cancellationToken = default)
=> GenerateAsyncCallback!.Invoke(values, options, cancellationToken);

public object? GetService(Type serviceType, object? serviceKey = null)
Expand All @@ -33,3 +37,5 @@ void IDisposable.Dispose()
// No resources to dispose
}
}

public sealed class TestEmbeddingGenerator : TestEmbeddingGenerator<string, Embedding<float>>;
Loading