Skip to content

Commit

Permalink
.Net: Added metadata specifying connection stems from MSK code (#5269)
Browse files Browse the repository at this point in the history
### Motivation and Context
### Description

MongoDB drivers are used in various flavors and languages. Making sure
we exercise our due diligence in identifying the "origin" of the library
calls makes it best to understand how our Atlas servers get accessed.
Similar to [Python
PR](#3419).

### 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
- [x] I didn't break anyone 😄

Co-authored-by: Mark Wallace <127216156+markwallace-microsoft@users.noreply.github.com>
Co-authored-by: Dmytro Struk <13853051+dmytrostruk@users.noreply.github.com>
  • Loading branch information
3 people committed May 7, 2024
1 parent db46d34 commit 45f3d56
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Threading.Tasks;
using Microsoft.SemanticKernel.Memory;
using MongoDB.Driver;
using MongoDB.Driver.Core.Configuration;

namespace Microsoft.SemanticKernel.Connectors.MongoDB;

Expand All @@ -22,7 +23,7 @@ public class MongoDBMemoryStore : IMemoryStore, IDisposable
/// <param name="databaseName">Database name.</param>
/// <param name="indexName">Name of the search index. If no value is provided default index will be used.</param>
public MongoDBMemoryStore(string connectionString, string databaseName, string? indexName = default) :
this(new MongoClient(connectionString), databaseName, indexName)
this(new MongoClient(GetMongoClientSettings(connectionString)), databaseName, indexName)
{
}

Expand Down Expand Up @@ -219,6 +220,14 @@ protected virtual void Dispose(bool disposing)
private static FilterDefinition<MongoDBMemoryEntry> GetFilterByIds(IEnumerable<string> ids) =>
Builders<MongoDBMemoryEntry>.Filter.In(m => m.Id, ids);

private static MongoClientSettings GetMongoClientSettings(string connectionString)
{
var settings = MongoClientSettings.FromConnectionString(connectionString);
var skVersion = typeof(IMemoryStore).Assembly.GetName().Version.ToString();
settings.LibraryInfo = new LibraryInfo("Microsoft Semantic Kernel", skVersion);
return settings;
}

private Task<IAsyncCursor<MongoDBMemoryEntry>> VectorSearch(
string collectionName,
ReadOnlyMemory<float> embedding,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.SemanticKernel.Connectors.MongoDB;
using Microsoft.SemanticKernel.Memory;
using MongoDB.Driver;
using MongoDB.Driver.Core.Configuration;
using Xunit;

namespace SemanticKernel.IntegrationTests.Connectors.MongoDB;
Expand Down Expand Up @@ -39,8 +41,10 @@ public MongoDBMemoryStoreTestsFixture()
var vectorSearchCollectionNamespace = CollectionNamespace.FromFullName(vectorSearchCollection);
this.VectorSearchCollectionName = vectorSearchCollectionNamespace.CollectionName;

var skVersion = typeof(IMemoryStore).Assembly?.GetName()?.Version?.ToString();
var mongoClientSettings = MongoClientSettings.FromConnectionString(connectionString);
mongoClientSettings.ApplicationName = GetRandomName();
mongoClientSettings.LibraryInfo = new LibraryInfo("Microsoft Semantic Kernel", skVersion);

this.DatabaseTestName = "dotnetMSKIntegrationTests1";
this.ListCollectionsDatabaseTestName = "dotnetMSKIntegrationTests2";
Expand Down

0 comments on commit 45f3d56

Please sign in to comment.