Skip to content

Commit

Permalink
.Net: Azure CosmosDB MongoDB vCore Memory Store Bug Fixes (#6177)
Browse files Browse the repository at this point in the history
### Motivation and Context
- Fixed some issues with config and memory store for Azure CosmosDB
MongoDB vCore.
- Fixed vectorSearch test cases.

<!-- Thank you for your contribution to the semantic-kernel repo!
Please help reviewers and future users, providing the following
information:
  1. Why is this change required?
  2. What problem does it solve?
  3. What scenario does it contribute to?
  4. If it fixes an open issue, please link to the issue here.
-->

### Description

<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->

### 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:/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https:/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 😄
  • Loading branch information
aayush3011 authored May 10, 2024
1 parent 7b4aba4 commit 89eb3c0
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class AzureCosmosDBMongoDBMemoryStore : IMemoryStore, IDisposable
private readonly MongoClient _mongoClient;
private readonly IMongoDatabase _mongoDatabase;
private readonly AzureCosmosDBMongoDBConfig _config;
private readonly bool _ownsMongoClient;

/// <summary>
/// Initiates a AzureCosmosDBMongoDBMemoryStore instance using a Azure CosmosDB Mongo vCore connection string
Expand All @@ -41,22 +42,21 @@ AzureCosmosDBMongoDBConfig config
settings.ApplicationName = this._config.ApplicationName;
this._mongoClient = new MongoClient(settings);
this._mongoDatabase = this._mongoClient.GetDatabase(databaseName);
this._ownsMongoClient = true;
}

/// <summary>
/// Initiates a AzureCosmosDBMongoDBMemoryStore instance using a Azure CosmosDB MongoDB client
/// and other properties required for vector search.
/// </summary>
public AzureCosmosDBMongoDBMemoryStore(
IMongoClient mongoClient,
MongoClient mongoClient,
string databaseName,
AzureCosmosDBMongoDBConfig config
)
{
MongoClientSettings settings = mongoClient.Settings;
this._config = config;
settings.ApplicationName = this._config.ApplicationName;
this._mongoClient = new MongoClient(settings);
this._mongoClient = mongoClient;
this._mongoDatabase = this._mongoClient.GetDatabase(databaseName);
}

Expand Down Expand Up @@ -318,7 +318,10 @@ protected virtual void Dispose(bool disposing)
{
if (disposing)
{
this._mongoClient.Cluster.Dispose();
if (this._ownsMongoClient)
{
this._mongoClient.Cluster.Dispose();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ public async Task ItCanBatchUpsertGetRemoveAsync(bool withEmbeddings)
var memoryStore = this._fixture.MemoryStore;
var records = DataHelper.CreateBatchRecords(Count);

await memoryStore.CreateCollectionAsync(collectionName);
var keys = await memoryStore.UpsertBatchAsync(collectionName, records).ToListAsync();
var actualRecords = await memoryStore
.GetBatchAsync(collectionName, keys, withEmbeddings: withEmbeddings)
Expand Down Expand Up @@ -86,6 +85,12 @@ public async Task ItCanGetNearestMatchesAsync(int limit, bool withEmbeddings)
var memoryStore = this._fixture.MemoryStore;
var searchEmbedding = DataHelper.VectorSearchTestEmbedding;
var nearestMatchesExpected = DataHelper.VectorSearchExpectedResults;
var records = DataHelper.VectorSearchTestRecords;

var keys = await memoryStore.UpsertBatchAsync(collectionName, records).ToListAsync();
var actualRecords = await memoryStore
.GetBatchAsync(collectionName, keys, withEmbeddings: withEmbeddings)
.ToListAsync();

var nearestMatchesActual = await memoryStore
.GetNearestMatchesAsync(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public AzureCosmosDBMongoDBMemoryStoreTestsFixture()
)
.AddEnvironmentVariables()
.Build();

var connectionString = GetSetting(configuration, "ConnectionString");
this.DatabaseName = "DotNetSKTestDB";
this.CollectionName = "DotNetSKTestCollection";
Expand All @@ -42,7 +41,6 @@ public AzureCosmosDBMongoDBMemoryStoreTestsFixture()
public async Task InitializeAsync()
{
await this.MemoryStore.CreateCollectionAsync(this.CollectionName);

await this
.MemoryStore.UpsertBatchAsync(this.CollectionName, DataHelper.VectorSearchTestRecords)
.ToListAsync();
Expand Down

0 comments on commit 89eb3c0

Please sign in to comment.