Skip to content
This repository was archived by the owner on Apr 27, 2026. It is now read-only.
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 @@ -3,7 +3,6 @@

using FluentAssertions;
using Microsoft.DotNet.Interactive.App.Tests;
using Microsoft.DotNet.Interactive.CSharpProject.Tests;
using Microsoft.DotNet.Interactive.Telemetry;

using Xunit;
Expand All @@ -27,10 +26,10 @@ public void can_search_collection_for_similar_element()
"diego",
new CosineSimilarityComparer<string>(a => a.Select(c => (float)c).ToArray())
)
.OrderByDescending(e => e.Value)
.OrderByDescending(e => e.Score)
.Take(1);

search.Should().BeEquivalentTo(new [] { KeyValuePair.Create("diago", 0.9998783f) });
search.Should().BeEquivalentTo(new [] { ScoredValue.Create("diago", 0.9998783f) });
}

[Fact]
Expand All @@ -49,10 +48,10 @@ public void can_search_collection_for_element_similar_to_value()
new CosineSimilarityComparer<string>(a => a.Select(c => (float)c).ToArray()),
e => e.Text
)
.OrderByDescending(e => e.Value)
.OrderByDescending(e => e.Score)
.Take(1);

search.Should().BeEquivalentTo(new[] { KeyValuePair.Create(new { Text = "diago", Number = 1.0f }, 0.9998783f) });
search.Should().BeEquivalentTo(new[] { ScoredValue.Create(new { Text = "diago", Number = 1.0f }, 0.9998783f) });
}

[Fact]
Expand All @@ -68,13 +67,13 @@ public void can_search_collection_for_similar_element_f()
var search = vectorCollection.ScoreBySimilarityTo(
new[] { 1f, 1f },
new CosineSimilarityComparer<float[]>(t => t)
).OrderByDescending(e => e.Value)
).OrderByDescending(e => e.Score)
.Take(2);

var expected = new[]
{
KeyValuePair.Create( new [] { 1f,1.5f }, 0.9805807f ),
KeyValuePair.Create( new[] { 1f,1.7f }, 0.9679969f )
ScoredValue.Create( new [] { 1f,1.5f }, 0.9805807f ),
ScoredValue.Create( new[] { 1f,1.7f }, 0.9679969f )
};

search.Should().BeEquivalentTo(expected);
Expand Down
22 changes: 22 additions & 0 deletions src/Microsoft.DotNet.Interactive.AIUtilities/ScoredValue.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace Microsoft.DotNet.Interactive.AIUtilities;

public static class ScoredValue
{
public static ScoredValue<T> Create<T>(T value, float score) => new(value, score);
}

public class ScoredValue<T>
{
public ScoredValue(T value, float score)
{
Value = value;
Score = score;
}

public T Value { get; }
public float Score { get; }

}
10 changes: 5 additions & 5 deletions src/Microsoft.DotNet.Interactive.AIUtilities/Text.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@ static Text()

private static readonly Logger _logger;

public static IEnumerable<KeyValuePair<T, float>> ScoreBySimilarityTo<T>(this IEnumerable<T> source, T value,
public static IEnumerable<ScoredValue<T>> ScoreBySimilarityTo<T>(this IEnumerable<T> source, T value,
ISimilarityComparer<T> comparer)
{
_logger.Event( properties: ("comparer", comparer.GetType().FullName));
return source.Select(item => new KeyValuePair<T, float>(item, comparer.Score(item, value)));
return source.Select(item => new ScoredValue<T>(item, comparer.Score(item, value)));
}

public static IEnumerable<KeyValuePair<TCollection, float>> ScoreBySimilarityTo<TCollection, TValue>(this IEnumerable<TCollection> source, TValue value,
ISimilarityComparer<TValue> comparer, Func<TCollection, TValue> valueSelector)
public static IEnumerable<ScoredValue<T>> ScoreBySimilarityTo<T, TValue>(this IEnumerable<T> source, TValue value,
ISimilarityComparer<TValue> comparer, Func<T, TValue> valueSelector)
{
_logger.Event(properties: ("comparer", comparer.GetType().FullName));
return source.Select(item => new KeyValuePair<TCollection, float>(item, comparer.Score(valueSelector(item), value)));
return source.Select(item => new ScoredValue<T>(item, comparer.Score(valueSelector(item), value)));
}

public static IObservable<string> ChunkByTokenCountWithOverlap(this IObservable<string> source, ITokenizer tokenizer, int maxTokenCount, int overlapTokenCount)
Expand Down