You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
.Net: Represent all embeddings as ReadOnlyMemory<float> (#2419)
### Motivation and Context
Across AI-related libraries, we'd like a consistent representation of an
embedding. It needs to support efficient handling of the data, with the
ability to get a span over the data so that the data can be manipulated
efficiently, which rules out interfaces like `IEnumerable<float>` and
`IList<float>`. It should ideally encourage immutability but not enforce
it, such that developers have escape hatches that don't require them to
continually allocate and copy around large objects. It should be usable
in both synchronous and asynchronous contexts. And it should be a
ubiquitous core type that's available everywhere. Based on that,
`ReadOnlyMemory<float>` is the winning candidate.
### Description
This PR overhauls SK to use `ReadOnlyMemory<float>` for embeddings. In
addition to the advantages listed above, I think it also cleans things
up nicely.
Note that in the System.Text.Json v8.0.0 nuget package, ReadOnlyMemory
is implicitly supported. Once SK moves from having a v6 to a v8
dependency, we can delete the ReadOnlyMemoryConverter and all mentions
of it.
### Contribution Checklist
<!-- Before submitting this PR, please make sure: -->
- [x] The code builds clean without any errors or warnings
- [x] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
- [x] All unit tests pass, and I have added new tests where possible
- [ ] I didn't break anyone 😄
Co-authored-by: Shawn Callegari <[email protected]>
@@ -95,7 +95,7 @@ public async Task<IList<Embedding<float>>> GenerateEmbeddingsAsync(IList<string>
95
95
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
96
96
/// <returns>List of generated embeddings.</returns>
97
97
/// <exception cref="AIException">Exception when backend didn't respond with generated embeddings.</exception>
/// <param name="data">List of strings to generate embeddings for</param>
116
115
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
49
48
/// <returns>List of text embeddings</returns>
50
49
/// <exception cref="AIException">AIException thrown during the request.</exception>
Copy file name to clipboardExpand all lines: dotnet/src/Connectors/Connectors.AI.OpenAI/TextEmbedding/AzureTextEmbeddingGeneration.cs
+2-1
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,6 @@
1
1
// Copyright (c) Microsoft. All rights reserved.
2
2
3
+
usingSystem;
3
4
usingSystem.Collections.Generic;
4
5
usingSystem.Net.Http;
5
6
usingSystem.Threading;
@@ -56,7 +57,7 @@ public AzureTextEmbeddingGeneration(
56
57
/// <param name="data">List of strings to generate embeddings for</param>
57
58
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
Copy file name to clipboardExpand all lines: dotnet/src/Connectors/Connectors.AI.OpenAI/TextEmbedding/OpenAITextEmbeddingGeneration.cs
+2-1
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,6 @@
1
1
// Copyright (c) Microsoft. All rights reserved.
2
2
3
+
usingSystem;
3
4
usingSystem.Collections.Generic;
4
5
usingSystem.Net.Http;
5
6
usingSystem.Threading;
@@ -39,7 +40,7 @@ public OpenAITextEmbeddingGeneration(
39
40
/// <param name="data">List of strings to generate embeddings for</param>
40
41
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
0 commit comments