Skip to content

Commit b318ae6

Browse files
authored
.Net: Delete CreateCollection, rename CreateIfNotExists and make EnsureExists thread safe. (#11937)
### Motivation and Context #11829 ### Description - DeleteCreateCollection - Rename CreateCollectionIfNotExists to EnsureCollectionExists - Make EnsureCollectionExists thread safe - Update tests and samples. ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [ ] The code builds clean without any errors or warnings - [ ] 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 - [ ] All unit tests pass, and I have added new tests where possible - [ ] I didn't break anyone 😄
1 parent d5d52f8 commit b318ae6

File tree

74 files changed

+553
-857
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+553
-857
lines changed

dotnet/samples/Concepts/Caching/SemanticCachingWithFilters.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ public async Task OnPromptRenderAsync(PromptRenderContext context, Func<PromptRe
196196
var promptEmbedding = await textEmbeddingGenerationService.GenerateEmbeddingAsync(prompt);
197197

198198
var collection = vectorStore.GetCollection<string, CacheRecord>(CollectionName);
199-
await collection.CreateCollectionIfNotExistsAsync();
199+
await collection.EnsureCollectionExistsAsync();
200200

201201
// Search for similar prompts in cache.
202202
var searchResult = (await collection.SearchEmbeddingAsync(promptEmbedding, top: 1, cancellationToken: context.CancellationToken)
@@ -241,7 +241,7 @@ public async Task OnFunctionInvocationAsync(FunctionInvocationContext context, F
241241

242242
// Cache rendered prompt and LLM result.
243243
var collection = vectorStore.GetCollection<string, CacheRecord>(CollectionName);
244-
await collection.CreateCollectionIfNotExistsAsync();
244+
await collection.EnsureCollectionExistsAsync();
245245

246246
var cacheRecord = new CacheRecord
247247
{

dotnet/samples/Concepts/Memory/VectorStoreExtensions.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ internal static async Task<VectorStoreCollection<TKey, TRecord>> CreateCollectio
4949
{
5050
// Get and create collection if it doesn't exist.
5151
var collection = vectorStore.GetCollection<TKey, TRecord>(collectionName);
52-
await collection.CreateCollectionIfNotExistsAsync().ConfigureAwait(false);
52+
await collection.EnsureCollectionExistsAsync().ConfigureAwait(false);
5353

5454
// Create records and generate embeddings for them.
5555
var tasks = entries.Select(entry => Task.Run(async () =>
@@ -85,7 +85,7 @@ internal static async Task<VectorStoreCollection<TKey, TRecord>> CreateCollectio
8585
{
8686
// Get and create collection if it doesn't exist.
8787
var collection = vectorStore.GetCollection<TKey, TRecord>(collectionName);
88-
await collection.CreateCollectionIfNotExistsAsync().ConfigureAwait(false);
88+
await collection.EnsureCollectionExistsAsync().ConfigureAwait(false);
8989

9090
// Create records and generate embeddings for them.
9191
var tasks = searchResults.Select(searchResult => Task.Run(async () =>

dotnet/samples/Concepts/Memory/VectorStore_ConsumeFromMemoryStore_AzureAISearch.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public async Task ConsumeExampleAsync()
4242

4343
// Use the VectorStore abstraction to connect to an existing collection which was previously created via the IMemoryStore abstraction
4444
var collection = vectorStore.GetCollection<string, VectorStoreRecord>("memorystorecollection");
45-
await collection.CreateCollectionIfNotExistsAsync();
45+
await collection.EnsureCollectionExistsAsync();
4646

4747
// Show that the data can be read using the VectorStore abstraction.
4848
// Note that AzureAISearchMemoryStore converts all keys to base64

dotnet/samples/Concepts/Memory/VectorStore_ConsumeFromMemoryStore_Qdrant.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public async Task ConsumeExampleAsync()
3737

3838
// Use the VectorStore abstraction to connect to an existing collection which was previously created via the IMemoryStore abstraction
3939
var collection = vectorStore.GetCollection<Guid, VectorStoreRecord>("memorystorecollection");
40-
await collection.CreateCollectionIfNotExistsAsync();
40+
await collection.EnsureCollectionExistsAsync();
4141

4242
// Show that the data can be read using the VectorStore abstraction.
4343
var record1 = await collection.GetAsync(new Guid("11111111-1111-1111-1111-111111111111"));

dotnet/samples/Concepts/Memory/VectorStore_ConsumeFromMemoryStore_Redis.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public async Task ConsumeExampleAsync()
3939

4040
// Connect to the same collection using the VectorStore abstraction.
4141
var collection = vectorStore.GetCollection<string, VectorStoreRecord>(MemoryStoreCollectionName);
42-
await collection.CreateCollectionIfNotExistsAsync();
42+
await collection.EnsureCollectionExistsAsync();
4343

4444
// Show that the data can be read using the VectorStore abstraction.
4545
var record1 = await collection.GetAsync("11111111-1111-1111-1111-111111111111");

dotnet/samples/Concepts/Memory/VectorStore_DataIngestion_MultiStore.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public async Task<IEnumerable<TKey>> ImportDataAsync<TKey>(Func<TKey> uniqueKeyG
173173
{
174174
// Get and create collection if it doesn't exist.
175175
var collection = vectorStore.GetCollection<TKey, Glossary<TKey>>("skglossary");
176-
await collection.CreateCollectionIfNotExistsAsync();
176+
await collection.EnsureCollectionExistsAsync();
177177

178178
// Create glossary entries and generate embeddings for them.
179179
var glossaryEntries = CreateGlossaryEntries(uniqueKeyGenerator).ToList();

dotnet/samples/Concepts/Memory/VectorStore_DataIngestion_Simple.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public async Task ExampleAsync()
4040

4141
// Get and create collection if it doesn't exist.
4242
var collection = vectorStore.GetCollection<ulong, Glossary>("skglossary");
43-
await collection.CreateCollectionIfNotExistsAsync();
43+
await collection.EnsureCollectionExistsAsync();
4444

4545
// Create glossary entries and generate embeddings for them.
4646
var glossaryEntries = CreateGlossaryEntries().ToList();

dotnet/samples/Concepts/Memory/VectorStore_DynamicDataModel_Interop.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public async Task UpsertWithDynamicRetrieveWithCustomAsync()
5050

5151
// Get and create collection if it doesn't exist using the dynamic data model and record definition that defines the schema.
5252
var dynamicDataModelCollection = vectorStore.GetCollection<object, Dictionary<string, object?>>("skglossary", s_vectorStoreRecordDefinition);
53-
await dynamicDataModelCollection.CreateCollectionIfNotExistsAsync();
53+
await dynamicDataModelCollection.EnsureCollectionExistsAsync();
5454

5555
// Create glossary entries and generate embeddings for them.
5656
var glossaryEntries = CreateDynamicGlossaryEntries().ToList();
@@ -88,7 +88,7 @@ public async Task UpsertWithCustomRetrieveWithDynamicAsync()
8888

8989
// Get and create collection if it doesn't exist using the custom data model.
9090
var customDataModelCollection = vectorStore.GetCollection<ulong, Glossary>("skglossary");
91-
await customDataModelCollection.CreateCollectionIfNotExistsAsync();
91+
await customDataModelCollection.EnsureCollectionExistsAsync();
9292

9393
// Create glossary entries and generate embeddings for them.
9494
var glossaryEntries = CreateCustomGlossaryEntries().ToList();

dotnet/samples/Concepts/Memory/VectorStore_HybridSearch_Simple_AzureAISearch.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public async Task IngestDataAndUseHybridSearch()
3838

3939
// Get and create collection if it doesn't exist.
4040
var collection = vectorStore.GetCollection<string, Glossary>("skglossary");
41-
await collection.CreateCollectionIfNotExistsAsync();
41+
await collection.EnsureCollectionExistsAsync();
4242
var hybridSearchCollection = (IKeywordHybridSearchable<Glossary>)collection;
4343

4444
// Create glossary entries and generate embeddings for them.

dotnet/samples/Concepts/Memory/VectorStore_MigrateFromMemoryStore_Redis.cs

-195
This file was deleted.

dotnet/samples/Concepts/Memory/VectorStore_VectorSearch_MultiStore_Common.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public async Task IngestDataAndSearchAsync<TKey>(string collectionName, Func<TKe
3434
{
3535
// Get and create collection if it doesn't exist.
3636
var collection = vectorStore.GetCollection<TKey, Glossary<TKey>>(collectionName);
37-
await collection.CreateCollectionIfNotExistsAsync();
37+
await collection.EnsureCollectionExistsAsync();
3838

3939
// Create glossary entries and generate embeddings for them.
4040
var glossaryEntries = CreateGlossaryEntries(uniqueKeyGenerator).ToList();

dotnet/samples/Concepts/Memory/VectorStore_VectorSearch_MultiVector.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public async Task VectorSearchWithMultiVectorRecordAsync()
3333

3434
// Get and create collection if it doesn't exist.
3535
var collection = vectorStore.GetCollection<int, Product>("skproducts");
36-
await collection.CreateCollectionIfNotExistsAsync();
36+
await collection.EnsureCollectionExistsAsync();
3737

3838
// Create product records and generate embeddings for them.
3939
var productRecords = CreateProductRecords().ToList();

dotnet/samples/Concepts/Memory/VectorStore_VectorSearch_Paging.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public async Task VectorSearchWithPagingAsync()
2323

2424
// Get and create collection if it doesn't exist.
2525
var collection = vectorStore.GetCollection<int, TextSnippet>("skglossary");
26-
await collection.CreateCollectionIfNotExistsAsync();
26+
await collection.EnsureCollectionExistsAsync();
2727

2828
// Create some test data entries.
2929
// We are not generating real embeddings here, just some random numbers

dotnet/samples/Concepts/Memory/VectorStore_VectorSearch_Simple.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public async Task ExampleAsync()
3333

3434
// Get and create collection if it doesn't exist.
3535
var collection = vectorStore.GetCollection<ulong, Glossary>("skglossary");
36-
await collection.CreateCollectionIfNotExistsAsync();
36+
await collection.EnsureCollectionExistsAsync();
3737

3838
// Create glossary entries and generate embeddings for them.
3939
var glossaryEntries = CreateGlossaryEntries().ToList();

dotnet/samples/Concepts/Optimization/FrugalGPTWithFilters.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ public async Task OnPromptRenderAsync(PromptRenderContext context, Func<PromptRe
208208
// Create collection and upsert all vector store records for search.
209209
// It's possible to do it only once and re-use the same examples for future requests.
210210
var collection = vectorStore.GetCollection<string, ExampleRecord>(CollectionName);
211-
await collection.CreateCollectionIfNotExistsAsync(context.CancellationToken);
211+
await collection.EnsureCollectionExistsAsync(context.CancellationToken);
212212

213213
await collection.UpsertAsync(exampleRecords, cancellationToken: context.CancellationToken);
214214

0 commit comments

Comments
 (0)