Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.Net: Add DI registration helpers for collections and search #9007

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,69 @@ public void AddVectorStoreWithUriAndTokenCredsRegistersClass()
this.AssertVectorStoreCreated();
}

[Fact]
public void AddVectorStoreRecordCollectionRegistersClass()
{
// Arrange.
this._kernelBuilder.Services.AddSingleton<SearchIndexClient>(Mock.Of<SearchIndexClient>());

// Act.
this._kernelBuilder.AddAzureAISearchVectorStoreRecordCollection<TestRecord>("testcollection");

// Assert.
this.AssertVectorStoreRecordCollectionCreated();
}

[Fact]
public void AddVectorStoreRecordCollectionWithUriAndCredsRegistersClass()
{
// Act.
this._kernelBuilder.AddAzureAISearchVectorStoreRecordCollection<TestRecord>("testcollection", new Uri("https://localhost"), new AzureKeyCredential("fakeKey"));

// Assert.
this.AssertVectorStoreRecordCollectionCreated();
}

[Fact]
public void AddVectorStoreRecordCollectionWithUriAndTokenCredsRegistersClass()
{
// Act.
this._kernelBuilder.AddAzureAISearchVectorStoreRecordCollection<TestRecord>("testcollection", new Uri("https://localhost"), Mock.Of<TokenCredential>());

// Assert.
this.AssertVectorStoreRecordCollectionCreated();
}

private void AssertVectorStoreCreated()
{
var kernel = this._kernelBuilder.Build();
var vectorStore = kernel.Services.GetRequiredService<IVectorStore>();
Assert.NotNull(vectorStore);
Assert.IsType<AzureAISearchVectorStore>(vectorStore);
}

private void AssertVectorStoreRecordCollectionCreated()
{
var kernel = this._kernelBuilder.Build();

var collection = kernel.Services.GetRequiredService<IVectorStoreRecordCollection<string, TestRecord>>();
Assert.NotNull(collection);
Assert.IsType<AzureAISearchVectorStoreRecordCollection<TestRecord>>(collection);

var vectorizedSearch = kernel.Services.GetRequiredService<IVectorizedSearch<TestRecord>>();
Assert.NotNull(vectorizedSearch);
Assert.IsType<AzureAISearchVectorStoreRecordCollection<TestRecord>>(vectorizedSearch);

var vectorizableSearch = kernel.Services.GetRequiredService<IVectorizableTextSearch<TestRecord>>();
Assert.NotNull(vectorizableSearch);
Assert.IsType<AzureAISearchVectorStoreRecordCollection<TestRecord>>(vectorizableSearch);
}

#pragma warning disable CA1812 // Avoid uninstantiated internal classes
private sealed class TestRecord
#pragma warning restore CA1812 // Avoid uninstantiated internal classes
{
[VectorStoreRecordKey]
public string Id { get; set; } = string.Empty;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,69 @@ public void AddVectorStoreWithUriAndTokenCredsRegistersClass()
this.AssertVectorStoreCreated();
}

[Fact]
public void AddVectorStoreRecordCollectionRegistersClass()
{
// Arrange.
this._serviceCollection.AddSingleton<SearchIndexClient>(Mock.Of<SearchIndexClient>());

// Act.
this._serviceCollection.AddAzureAISearchVectorStoreRecordCollection<TestRecord>("testcollection");

// Assert.
this.AssertVectorStoreRecordCollectionCreated();
}

[Fact]
public void AddVectorStoreRecordCollectionWithUriAndCredsRegistersClass()
{
// Act.
this._serviceCollection.AddAzureAISearchVectorStoreRecordCollection<TestRecord>("testcollection", new Uri("https://localhost"), new AzureKeyCredential("fakeKey"));

// Assert.
this.AssertVectorStoreRecordCollectionCreated();
}

[Fact]
public void AddVectorStoreRecordCollectionWithUriAndTokenCredsRegistersClass()
{
// Act.
this._serviceCollection.AddAzureAISearchVectorStoreRecordCollection<TestRecord>("testcollection", new Uri("https://localhost"), Mock.Of<TokenCredential>());

// Assert.
this.AssertVectorStoreRecordCollectionCreated();
}

private void AssertVectorStoreCreated()
{
var serviceProvider = this._serviceCollection.BuildServiceProvider();
var vectorStore = serviceProvider.GetRequiredService<IVectorStore>();
Assert.NotNull(vectorStore);
Assert.IsType<AzureAISearchVectorStore>(vectorStore);
}

private void AssertVectorStoreRecordCollectionCreated()
{
var serviceProvider = this._serviceCollection.BuildServiceProvider();

var collection = serviceProvider.GetRequiredService<IVectorStoreRecordCollection<string, TestRecord>>();
Assert.NotNull(collection);
Assert.IsType<AzureAISearchVectorStoreRecordCollection<TestRecord>>(collection);

var vectorizedSearch = serviceProvider.GetRequiredService<IVectorizedSearch<TestRecord>>();
Assert.NotNull(vectorizedSearch);
Assert.IsType<AzureAISearchVectorStoreRecordCollection<TestRecord>>(vectorizedSearch);

var vectorizableSearch = serviceProvider.GetRequiredService<IVectorizableTextSearch<TestRecord>>();
Assert.NotNull(vectorizableSearch);
Assert.IsType<AzureAISearchVectorStoreRecordCollection<TestRecord>>(vectorizableSearch);
}

#pragma warning disable CA1812 // Avoid uninstantiated internal classes
private sealed class TestRecord
#pragma warning restore CA1812 // Avoid uninstantiated internal classes
{
[VectorStoreRecordKey]
public string Id { get; set; } = string.Empty;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,48 @@ public void AddVectorStoreWithConnectionStringRegistersClass()
var database = (IMongoDatabase)vectorStore.GetType().GetField("_mongoDatabase", BindingFlags.NonPublic | BindingFlags.Instance)!.GetValue(vectorStore)!;
Assert.Equal(HttpHeaderConstant.Values.UserAgent, database.Client.Settings.ApplicationName);
}

[Fact]
public void AddVectorStoreRecordCollectionRegistersClass()
{
// Arrange
this._kernelBuilder.Services.AddSingleton<IMongoDatabase>(Mock.Of<IMongoDatabase>());

// Act
this._kernelBuilder.AddAzureCosmosDBMongoDBVectorStoreRecordCollection<TestRecord>("testcollection");

// Assert
this.AssertVectorStoreRecordCollectionCreated();
}

[Fact]
public void AddVectorStoreRecordCollectionWithConnectionStringRegistersClass()
{
// Act
this._kernelBuilder.AddAzureCosmosDBMongoDBVectorStoreRecordCollection<TestRecord>("testcollection", "mongodb://localhost:27017", "mydb");

// Assert
this.AssertVectorStoreRecordCollectionCreated();
}

private void AssertVectorStoreRecordCollectionCreated()
{
var kernel = this._kernelBuilder.Build();

var collection = kernel.Services.GetRequiredService<IVectorStoreRecordCollection<string, TestRecord>>();
Assert.NotNull(collection);
Assert.IsType<AzureCosmosDBMongoDBVectorStoreRecordCollection<TestRecord>>(collection);

var vectorizedSearch = kernel.Services.GetRequiredService<IVectorizedSearch<TestRecord>>();
Assert.NotNull(vectorizedSearch);
Assert.IsType<AzureCosmosDBMongoDBVectorStoreRecordCollection<TestRecord>>(vectorizedSearch);
}

#pragma warning disable CA1812 // Avoid uninstantiated internal classes
private sealed class TestRecord
#pragma warning restore CA1812 // Avoid uninstantiated internal classes
{
[VectorStoreRecordKey]
public string Id { get; set; } = string.Empty;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,48 @@ public void AddVectorStoreWithConnectionStringRegistersClass()
var database = (IMongoDatabase)vectorStore.GetType().GetField("_mongoDatabase", BindingFlags.NonPublic | BindingFlags.Instance)!.GetValue(vectorStore)!;
Assert.Equal(HttpHeaderConstant.Values.UserAgent, database.Client.Settings.ApplicationName);
}

[Fact]
public void AddVectorStoreRecordCollectionRegistersClass()
{
// Arrange
this._serviceCollection.AddSingleton<IMongoDatabase>(Mock.Of<IMongoDatabase>());

// Act
this._serviceCollection.AddAzureCosmosDBMongoDBVectorStoreRecordCollection<TestRecord>("testcollection");

// Assert
this.AssertVectorStoreRecordCollectionCreated();
}

[Fact]
public void AddVectorStoreRecordCollectionWithConnectionStringRegistersClass()
{
// Act
this._serviceCollection.AddAzureCosmosDBMongoDBVectorStoreRecordCollection<TestRecord>("testcollection", "mongodb://localhost:27017", "mydb");

// Assert
this.AssertVectorStoreRecordCollectionCreated();
}

private void AssertVectorStoreRecordCollectionCreated()
{
var serviceProvider = this._serviceCollection.BuildServiceProvider();

var collection = serviceProvider.GetRequiredService<IVectorStoreRecordCollection<string, TestRecord>>();
Assert.NotNull(collection);
Assert.IsType<AzureCosmosDBMongoDBVectorStoreRecordCollection<TestRecord>>(collection);

var vectorizedSearch = serviceProvider.GetRequiredService<IVectorizedSearch<TestRecord>>();
Assert.NotNull(vectorizedSearch);
Assert.IsType<AzureCosmosDBMongoDBVectorStoreRecordCollection<TestRecord>>(vectorizedSearch);
}

#pragma warning disable CA1812 // Avoid uninstantiated internal classes
private sealed class TestRecord
#pragma warning restore CA1812 // Avoid uninstantiated internal classes
{
[VectorStoreRecordKey]
public string Id { get; set; } = string.Empty;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,47 @@ public void AddVectorStoreWithConnectionStringRegistersClass()
var database = (Database)vectorStore.GetType().GetField("_database", BindingFlags.NonPublic | BindingFlags.Instance)!.GetValue(vectorStore)!;
Assert.Equal(HttpHeaderConstant.Values.UserAgent, database.Client.ClientOptions.ApplicationName);
}
[Fact]
public void AddVectorStoreRecordCollectionRegistersClass()
{
// Arrange
this._kernelBuilder.Services.AddSingleton<Database>(Mock.Of<Database>());

// Act
this._kernelBuilder.AddAzureCosmosDBNoSQLVectorStoreRecordCollection<TestRecord>("testcollection");

// Assert
this.AssertVectorStoreRecordCollectionCreated();
}

[Fact]
public void AddVectorStoreRecordCollectionWithConnectionStringRegistersClass()
{
// Act
this._kernelBuilder.AddAzureCosmosDBNoSQLVectorStoreRecordCollection<TestRecord>("testcollection", "AccountEndpoint=https://test.documents.azure.com:443/;AccountKey=mock;", "mydb");

// Assert
this.AssertVectorStoreRecordCollectionCreated();
}

private void AssertVectorStoreRecordCollectionCreated()
{
var kernel = this._kernelBuilder.Build();

var collection = kernel.Services.GetRequiredService<IVectorStoreRecordCollection<string, TestRecord>>();
Assert.NotNull(collection);
Assert.IsType<AzureCosmosDBNoSQLVectorStoreRecordCollection<TestRecord>>(collection);

var vectorizedSearch = kernel.Services.GetRequiredService<IVectorizedSearch<TestRecord>>();
Assert.NotNull(vectorizedSearch);
Assert.IsType<AzureCosmosDBNoSQLVectorStoreRecordCollection<TestRecord>>(vectorizedSearch);
}

#pragma warning disable CA1812 // Avoid uninstantiated internal classes
westey-m marked this conversation as resolved.
Show resolved Hide resolved
private sealed class TestRecord
#pragma warning restore CA1812 // Avoid uninstantiated internal classes
{
[VectorStoreRecordKey]
public string Id { get; set; } = string.Empty;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,48 @@ public void AddVectorStoreWithConnectionStringRegistersClass()
var database = (Database)vectorStore.GetType().GetField("_database", BindingFlags.NonPublic | BindingFlags.Instance)!.GetValue(vectorStore)!;
Assert.Equal(HttpHeaderConstant.Values.UserAgent, database.Client.ClientOptions.ApplicationName);
}

[Fact]
public void AddVectorStoreRecordCollectionRegistersClass()
{
// Arrange
this._serviceCollection.AddSingleton<Database>(Mock.Of<Database>());

// Act
this._serviceCollection.AddAzureCosmosDBNoSQLVectorStoreRecordCollection<TestRecord>("testcollection");

// Assert
this.AssertVectorStoreRecordCollectionCreated();
}

[Fact]
public void AddVectorStoreRecordCollectionWithConnectionStringRegistersClass()
{
// Act
this._serviceCollection.AddAzureCosmosDBNoSQLVectorStoreRecordCollection<TestRecord>("testcollection", "AccountEndpoint=https://test.documents.azure.com:443/;AccountKey=mock;", "mydb");

// Assert
this.AssertVectorStoreRecordCollectionCreated();
}

private void AssertVectorStoreRecordCollectionCreated()
{
var serviceProvider = this._serviceCollection.BuildServiceProvider();

var collection = serviceProvider.GetRequiredService<IVectorStoreRecordCollection<string, TestRecord>>();
Assert.NotNull(collection);
Assert.IsType<AzureCosmosDBNoSQLVectorStoreRecordCollection<TestRecord>>(collection);

var vectorizedSearch = serviceProvider.GetRequiredService<IVectorizedSearch<TestRecord>>();
Assert.NotNull(vectorizedSearch);
Assert.IsType<AzureCosmosDBNoSQLVectorStoreRecordCollection<TestRecord>>(vectorizedSearch);
}

#pragma warning disable CA1812 // Avoid uninstantiated internal classes
private sealed class TestRecord
#pragma warning restore CA1812 // Avoid uninstantiated internal classes
{
[VectorStoreRecordKey]
public string Id { get; set; } = string.Empty;
}
}
Loading
Loading