Skip to content

Commit 64cbf3d

Browse files
committed
Fix remaining warnings
1 parent e4aa6de commit 64cbf3d

18 files changed

+250
-106
lines changed

dotnet/samples/Concepts/Memory/VectorStore_VectorSearch_MultiStore_InMemory.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public async Task ExampleWithDIAsync()
3030
.CreateBuilder();
3131

3232
// Register an embedding generation service with the DI container.
33-
kernelBuilder.AddAzureOpenAITextEmbeddingGeneration(
33+
kernelBuilder.AddAzureOpenAIEmbeddingGenerator(
3434
deploymentName: TestConfiguration.AzureOpenAIEmbeddings.DeploymentName,
3535
endpoint: TestConfiguration.AzureOpenAIEmbeddings.Endpoint,
3636
credential: new AzureCliCredential());
@@ -50,7 +50,7 @@ public async Task ExampleWithDIAsync()
5050

5151
// Run the process and pass a key generator function to it, to generate unique record keys.
5252
// The key generator function is required, since different vector stores may require different key types.
53-
// E.g. InMemory supports any comparible type, but others may only support string or Guid or ulong, etc.
53+
// E.g. InMemory supports any compatible type, but others may only support string or Guid or ulong, etc.
5454
// For this example we'll use int.
5555
var uniqueId = 0;
5656
await processor.IngestDataAndSearchAsync("skglossaryWithDI", () => uniqueId++);
@@ -73,7 +73,7 @@ public async Task ExampleWithoutDIAsync()
7373

7474
// Run the process and pass a key generator function to it, to generate unique record keys.
7575
// The key generator function is required, since different vector stores may require different key types.
76-
// E.g. InMemory supports any comparible type, but others may only support string or Guid or ulong, etc.
76+
// E.g. InMemory supports any compatible type, but others may only support string or Guid or ulong, etc.
7777
// For this example we'll use int.
7878
var uniqueId = 0;
7979
await processor.IngestDataAndSearchAsync("skglossaryWithoutDI", () => uniqueId++);

dotnet/samples/Demos/ModelContextProtocolClientServer/MCPServer/Extensions/VectorStoreExtensions.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) Microsoft. All rights reserved.
22

3+
using Microsoft.Extensions.AI;
34
using Microsoft.Extensions.VectorData;
4-
using Microsoft.SemanticKernel.Embeddings;
55

66
namespace MCPServer;
77

@@ -25,14 +25,14 @@ public static class VectorStoreExtensions
2525
/// <param name="vectorStore">The instance of <see cref="IVectorStore"/> used to create the collection.</param>
2626
/// <param name="collectionName">The name of the collection.</param>
2727
/// <param name="entries">The list of strings to create records from.</param>
28-
/// <param name="embeddingGenerationService">The text embedding generation service.</param>
28+
/// <param name="embeddingGenerator">The text embedding generation service.</param>
2929
/// <param name="createRecord">The delegate which can create a record for each string and its embedding.</param>
3030
/// <returns>The created collection.</returns>
3131
public static async Task<IVectorStoreRecordCollection<TKey, TRecord>> CreateCollectionFromListAsync<TKey, TRecord>(
3232
this IVectorStore vectorStore,
3333
string collectionName,
3434
string[] entries,
35-
ITextEmbeddingGenerationService embeddingGenerationService,
35+
IEmbeddingGenerator<string, Embedding<float>> embeddingGenerator,
3636
CreateRecordFromString<TKey, TRecord> createRecord)
3737
where TKey : notnull
3838
where TRecord : notnull
@@ -44,7 +44,7 @@ public static async Task<IVectorStoreRecordCollection<TKey, TRecord>> CreateColl
4444
// Create records and generate embeddings for them.
4545
var tasks = entries.Select(entry => Task.Run(async () =>
4646
{
47-
var record = createRecord(entry, await embeddingGenerationService.GenerateEmbeddingAsync(entry).ConfigureAwait(false));
47+
var record = createRecord(entry, (await embeddingGenerator.GenerateAsync(entry).ConfigureAwait(false)).Vector);
4848
await collection.UpsertAsync(record).ConfigureAwait(false);
4949
}));
5050
await Task.WhenAll(tasks).ConfigureAwait(false);

dotnet/samples/Demos/ModelContextProtocolClientServer/MCPServer/Program.cs

+4-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
using Microsoft.SemanticKernel;
1111
using Microsoft.SemanticKernel.Agents;
1212
using Microsoft.SemanticKernel.Connectors.InMemory;
13-
using Microsoft.SemanticKernel.Embeddings;
1413
using ModelContextProtocol.Protocol.Types;
1514
using ModelContextProtocol.Server;
1615

@@ -32,7 +31,7 @@
3231

3332
// Register embedding generation service and in-memory vector store
3433
kernelBuilder.Services.AddSingleton<IVectorStore, InMemoryVectorStore>();
35-
kernelBuilder.Services.AddOpenAITextEmbeddingGeneration(embeddingModelId, apiKey);
34+
kernelBuilder.Services.AddOpenAIEmbeddingGenerator(embeddingModelId, apiKey);
3635

3736
// Register MCP server
3837
builder.Services
@@ -96,7 +95,7 @@ static ResourceTemplateDefinition CreateVectorStoreSearchResourceTemplate(Kernel
9695
RequestContext<ReadResourceRequestParams> context,
9796
string collection,
9897
string prompt,
99-
[FromKernelServices] ITextEmbeddingGenerationService embeddingGenerationService,
98+
[FromKernelServices] IEmbeddingGenerator<string, Embedding<float>> embeddingGenerator,
10099
[FromKernelServices] IVectorStore vectorStore,
101100
CancellationToken cancellationToken) =>
102101
{
@@ -119,11 +118,11 @@ static TextDataModel CreateRecord(string text, ReadOnlyMemory<float> embedding)
119118
string content = EmbeddedResource.ReadAsString("semantic-kernel-info.txt");
120119

121120
// Create a collection from the lines in the file
122-
await vectorStore.CreateCollectionFromListAsync<Guid, TextDataModel>(collection, content.Split('\n'), embeddingGenerationService, CreateRecord);
121+
await vectorStore.CreateCollectionFromListAsync<Guid, TextDataModel>(collection, content.Split('\n'), embeddingGenerator, CreateRecord);
123122
}
124123

125124
// Generate embedding for the prompt
126-
ReadOnlyMemory<float> promptEmbedding = await embeddingGenerationService.GenerateEmbeddingAsync(prompt, cancellationToken: cancellationToken);
125+
ReadOnlyMemory<float> promptEmbedding = (await embeddingGenerator.GenerateAsync(prompt, cancellationToken: cancellationToken)).Vector;
127126

128127
// Retrieve top three matching records from the vector store
129128
var result = vsCollection.SearchEmbeddingAsync(promptEmbedding, top: 3, cancellationToken: cancellationToken);

dotnet/samples/Demos/OnnxSimpleRAG/Program.cs

+8-9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System;
44
using System.IO;
55
using System.Linq;
6+
using Microsoft.Extensions.AI;
67
using Microsoft.Extensions.Configuration;
78
using Microsoft.Extensions.VectorData;
89
using Microsoft.ML.OnnxRuntimeGenAI;
@@ -11,7 +12,6 @@
1112
using Microsoft.SemanticKernel.Connectors.InMemory;
1213
using Microsoft.SemanticKernel.Connectors.Onnx;
1314
using Microsoft.SemanticKernel.Data;
14-
using Microsoft.SemanticKernel.Embeddings;
1515
using Microsoft.SemanticKernel.PromptTemplates.Handlebars;
1616

1717
Console.OutputEncoding = System.Text.Encoding.UTF8;
@@ -36,17 +36,17 @@
3636
// Load the services
3737
var builder = Kernel.CreateBuilder()
3838
.AddOnnxRuntimeGenAIChatCompletion(chatModelId, chatModelPath)
39-
.AddBertOnnxTextEmbeddingGeneration(embeddingModelPath, embeddingVocabPath);
39+
.AddBertOnnxEmbeddingGenerator(embeddingModelPath, embeddingVocabPath);
4040

4141
// Build Kernel
4242
var kernel = builder.Build();
4343

4444
// Get the instances of the services
4545
using var chatService = kernel.GetRequiredService<IChatCompletionService>() as OnnxRuntimeGenAIChatCompletionService;
46-
var embeddingService = kernel.GetRequiredService<ITextEmbeddingGenerationService>();
46+
var embeddingService = kernel.GetRequiredService<IEmbeddingGenerator<string, Embedding<float>>>();
4747

4848
// Create a vector store and a collection to store information
49-
var vectorStore = new InMemoryVectorStore();
49+
var vectorStore = new InMemoryVectorStore(new() { EmbeddingGenerator = embeddingService });
5050
var collection = vectorStore.GetCollection<string, InformationItem>("ExampleCollection");
5151
await collection.CreateCollectionIfNotExistsAsync();
5252

@@ -58,15 +58,14 @@
5858
await collection.UpsertAsync(new InformationItem()
5959
{
6060
Id = Guid.NewGuid().ToString(),
61-
Text = factContent,
62-
Embedding = await embeddingService.GenerateEmbeddingAsync(factContent)
61+
Text = factContent
6362
});
6463
}
6564

6665
// Add a plugin to search the database with.
6766
// TODO: Once OpenAITextEmbeddingGenerationService implements MEAI's IEmbeddingGenerator (#10811), configure it with the InMemoryVectorStore above instead of passing it here.
68-
#pragma warning disable CS0618 // VectorStoreTextSearch with ITextEmbeddingGenerationService is obsolete
69-
var vectorStoreTextSearch = new VectorStoreTextSearch<InformationItem>(collection, embeddingService);
67+
#pragma warning disable CS0618 // VectorStoreTextSearch with ITextEmbeddingGenerationService is obsolete.
68+
var vectorStoreTextSearch = new VectorStoreTextSearch<InformationItem>(collection);//, (embeddingService as ITextEmbeddingGenerationService)!);
7069
#pragma warning restore CS0618
7170
kernel.Plugins.Add(vectorStoreTextSearch.CreateWithSearch("SearchPlugin"));
7271

@@ -139,5 +138,5 @@ internal sealed class InformationItem
139138
public string Text { get; set; } = string.Empty;
140139

141140
[VectorStoreRecordVector(Dimensions: 384)]
142-
public ReadOnlyMemory<float> Embedding { get; set; }
141+
public string Embedding => this.Text;
143142
}

dotnet/samples/GettingStartedWithTextSearch/InMemoryVectorStoreFixture.cs

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
// Copyright (c) Microsoft. All rights reserved.
22

33
using System.Reflection;
4+
using Microsoft.Extensions.AI;
45
using Microsoft.Extensions.Configuration;
56
using Microsoft.Extensions.VectorData;
67
using Microsoft.SemanticKernel.Connectors.InMemory;
78
using Microsoft.SemanticKernel.Connectors.OpenAI;
89
using Microsoft.SemanticKernel.Data;
9-
using Microsoft.SemanticKernel.Embeddings;
1010

1111
namespace GettingStartedWithTextSearch;
1212

@@ -15,7 +15,7 @@ namespace GettingStartedWithTextSearch;
1515
/// </summary>
1616
public class InMemoryVectorStoreFixture : IAsyncLifetime
1717
{
18-
public ITextEmbeddingGenerationService TextEmbeddingGenerationService { get; private set; }
18+
public IEmbeddingGenerator<string, Embedding<float>> EmbeddingGenerator { get; private set; }
1919

2020
public InMemoryVectorStore InMemoryVectorStore { get; private set; }
2121

@@ -35,13 +35,13 @@ public InMemoryVectorStoreFixture()
3535
.Build();
3636
TestConfiguration.Initialize(configRoot);
3737

38-
// Create an InMemory vector store.
39-
this.InMemoryVectorStore = new InMemoryVectorStore();
40-
4138
// Create an embedding generation service.
42-
this.TextEmbeddingGenerationService = new OpenAITextEmbeddingGenerationService(
39+
this.EmbeddingGenerator = new OpenAIEmbeddingGenerator(
4340
TestConfiguration.OpenAI.EmbeddingModelId,
4441
TestConfiguration.OpenAI.ApiKey);
42+
43+
// Create an InMemory vector store.
44+
this.InMemoryVectorStore = new InMemoryVectorStore(new() { EmbeddingGenerator = this.EmbeddingGenerator });
4545
}
4646

4747
/// <inheritdoc/>
@@ -72,7 +72,6 @@ static DataModel CreateRecord(int index, string text, ReadOnlyMemory<float> embe
7272
Text = text,
7373
Link = $"guid://{guid}",
7474
Tag = index % 2 == 0 ? "Even" : "Odd",
75-
Embedding = embedding
7675
};
7776
}
7877

@@ -122,7 +121,7 @@ private async Task<IVectorStoreRecordCollection<TKey, TRecord>> CreateCollection
122121
// Create records and generate embeddings for them.
123122
var tasks = entries.Select((entry, i) => Task.Run(async () =>
124123
{
125-
var record = createRecord(i, entry, await this.TextEmbeddingGenerationService.GenerateEmbeddingAsync(entry).ConfigureAwait(false));
124+
var record = createRecord(i, entry, (await this.EmbeddingGenerator.GenerateAsync(entry).ConfigureAwait(false)).Vector);
126125
await collection.UpsertAsync(record).ConfigureAwait(false);
127126
}));
128127
await Task.WhenAll(tasks).ConfigureAwait(false);
@@ -155,7 +154,7 @@ public sealed class DataModel
155154
public required string Tag { get; init; }
156155

157156
[VectorStoreRecordVector(1536)]
158-
public ReadOnlyMemory<float> Embedding { get; init; }
157+
public string Embedding => Text;
159158
}
160159
#endregion
161160
}

dotnet/samples/GettingStartedWithTextSearch/Step4_Search_With_VectorStore.cs

+5-15
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,10 @@ public class Step4_Search_With_VectorStore(ITestOutputHelper output, InMemoryVec
2323
public async Task UsingInMemoryVectorStoreRecordTextSearchAsync()
2424
{
2525
// Use embedding generation service and record collection for the fixture.
26-
var textEmbeddingGeneration = fixture.TextEmbeddingGenerationService;
2726
var collection = fixture.VectorStoreRecordCollection;
2827

2928
// Create a text search instance using the InMemory vector store.
30-
// TODO: Once OpenAITextEmbeddingGenerationService implements MEAI's IEmbeddingGenerator (#10811), configure it with the collection
31-
#pragma warning disable CS0618 // VectorStoreTextSearch with ITextEmbeddingGenerationService is obsolete
32-
var textSearch = new VectorStoreTextSearch<DataModel>(collection, textEmbeddingGeneration);
33-
#pragma warning restore CS0618
29+
var textSearch = new VectorStoreTextSearch<DataModel>(collection);
3430

3531
// Search and return results as TextSearchResult items
3632
var query = "What is the Semantic Kernel?";
@@ -59,14 +55,11 @@ public async Task RagWithInMemoryVectorStoreTextSearchAsync()
5955
Kernel kernel = kernelBuilder.Build();
6056

6157
// Use embedding generation service and record collection for the fixture.
62-
var textEmbeddingGeneration = fixture.TextEmbeddingGenerationService;
58+
var embeddingGenerator = fixture.EmbeddingGenerator;
6359
var collection = fixture.VectorStoreRecordCollection;
6460

6561
// Create a text search instance using the InMemory vector store.
66-
// TODO: Once OpenAITextEmbeddingGenerationService implements MEAI's IEmbeddingGenerator (#10811), configure it with the collection
67-
#pragma warning disable CS0618 // VectorStoreTextSearch with ITextEmbeddingGenerationService is obsolete
68-
var textSearch = new VectorStoreTextSearch<DataModel>(collection, textEmbeddingGeneration);
69-
#pragma warning restore CS0618
62+
var textSearch = new VectorStoreTextSearch<DataModel>(collection);
7063

7164
// Build a text search plugin with vector store search and add to the kernel
7265
var searchPlugin = textSearch.CreateWithGetTextSearchResults("SearchPlugin");
@@ -113,14 +106,11 @@ public async Task FunctionCallingWithInMemoryVectorStoreTextSearchAsync()
113106
Kernel kernel = kernelBuilder.Build();
114107

115108
// Use embedding generation service and record collection for the fixture.
116-
var textEmbeddingGeneration = fixture.TextEmbeddingGenerationService;
109+
var embeddingGenerator = fixture.EmbeddingGenerator;
117110
var collection = fixture.VectorStoreRecordCollection;
118111

119112
// Create a text search instance using the InMemory vector store.
120-
// TODO: Once OpenAITextEmbeddingGenerationService implements MEAI's IEmbeddingGenerator (#10811), configure it with the collection
121-
#pragma warning disable CS0618 // VectorStoreTextSearch with ITextEmbeddingGenerationService is obsolete
122-
var textSearch = new VectorStoreTextSearch<DataModel>(collection, textEmbeddingGeneration);
123-
#pragma warning restore CS0618
113+
var textSearch = new VectorStoreTextSearch<DataModel>(collection);
124114

125115
// Build a text search plugin with vector store search and add to the kernel
126116
var searchPlugin = textSearch.CreateWithGetTextSearchResults("SearchPlugin");

dotnet/src/Connectors/Connectors.OpenAI.UnitTests/Extensions/KernelBuilderExtensionsTests.cs

+36-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
using System;
44
using System.ClientModel;
5+
using Microsoft.Extensions.AI;
56
using Microsoft.Extensions.DependencyInjection;
67
using Microsoft.SemanticKernel;
78
using Microsoft.SemanticKernel.AudioToText;
@@ -19,7 +20,10 @@ namespace SemanticKernel.Connectors.OpenAI.UnitTests.Extensions;
1920

2021
public class KernelBuilderExtensionsTests
2122
{
23+
private const string ObsoleteMessage = "This test is in a deprecated feature will be removed in a future version.";
24+
2225
[Fact]
26+
[Obsolete(ObsoleteMessage)]
2327
public void ItCanAddTextEmbeddingGenerationService()
2428
{
2529
// Arrange
@@ -35,6 +39,7 @@ public void ItCanAddTextEmbeddingGenerationService()
3539
}
3640

3741
[Fact]
42+
[Obsolete(ObsoleteMessage)]
3843
public void ItCanAddTextEmbeddingGenerationServiceWithOpenAIClient()
3944
{
4045
// Arrange
@@ -49,6 +54,36 @@ public void ItCanAddTextEmbeddingGenerationServiceWithOpenAIClient()
4954
Assert.Equal("model", service.Attributes[AIServiceExtensions.ModelIdKey]);
5055
}
5156

57+
[Fact]
58+
public void ItCanAddEmbeddingGenerator()
59+
{
60+
// Arrange
61+
var sut = Kernel.CreateBuilder();
62+
63+
// Act
64+
var service = sut.AddOpenAIEmbeddingGenerator("model", "key")
65+
.Build()
66+
.GetRequiredService<IEmbeddingGenerator<string, Embedding<float>>>();
67+
68+
// Assert
69+
Assert.Equal("model", service.GetService<EmbeddingGeneratorMetadata>()!.DefaultModelId);
70+
}
71+
72+
[Fact]
73+
public void ItCanAddEmbeddingGeneratorServiceWithOpenAIClient()
74+
{
75+
// Arrange
76+
var sut = Kernel.CreateBuilder();
77+
78+
// Act
79+
var service = sut.AddOpenAIEmbeddingGenerator("model", new OpenAIClient(new ApiKeyCredential("key")))
80+
.Build()
81+
.GetRequiredService<IEmbeddingGenerator<string, Embedding<float>>>();
82+
83+
// Assert
84+
Assert.Equal("model", service.GetService<EmbeddingGeneratorMetadata>()!.DefaultModelId);
85+
}
86+
5287
[Fact]
5388
public void ItCanAddTextToImageService()
5489
{
@@ -110,7 +145,7 @@ public void ItCanAddAudioToTextServiceWithOpenAIClient()
110145
}
111146

112147
[Fact]
113-
[Obsolete("This test is deprecated and will be removed in a future version.")]
148+
[Obsolete(ObsoleteMessage)]
114149
public void ItCanAddFileService()
115150
{
116151
// Arrange

0 commit comments

Comments
 (0)