Skip to content

Commit

Permalink
test: Added more Databases Integration tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
HavenDV committed Apr 23, 2024
1 parent 0421187 commit e031aec
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 79 deletions.
75 changes: 0 additions & 75 deletions src/Databases/Chroma/test/ChromaTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,81 +161,6 @@ public async Task DeleteDocuments_Ok()
actualSecond.Should().BeNull();
}

[Test]
public async Task SimilaritySearch_Ok()
{
using var httpClient = new HttpClient();
var embeddingsMock = CreateFakeEmbeddings();
var collectionName = GenerateCollectionName();
var chroma = new ChromaVectorStore(httpClient, "http://localhost:8000", collectionName);

await chroma.AddTextsAsync(embeddingsMock.Object, EmbeddingsDict.Keys);

var similar = await chroma.SearchAsync(
embeddingsMock.Object,
embeddingRequest: "lemon",
searchSettings: new VectorSearchSettings
{
Type = VectorSearchType.Similarity,
NumberOfResults = 5,
});
similar.Items.Should().HaveCount(5);

var similarTexts = similar.Items.Select(s => s.Text).ToArray();

similarTexts[0].Should().BeEquivalentTo("lemon");
similarTexts.Should().Contain("orange");
similarTexts.Should().Contain("peach");
similarTexts.Should().Contain("banana");
similarTexts.Should().Contain("apple");
}

[Test]
public async Task SimilaritySearchByVector_Ok()
{
using var httpClient = new HttpClient();
var embeddingsMock = CreateFakeEmbeddings();
var collectionName = GenerateCollectionName();
var chroma = new ChromaVectorStore(httpClient, "http://localhost:8000", collectionName);

await chroma.AddTextsAsync(embeddingsMock.Object, EmbeddingsDict.Keys);

var similar = await chroma.SearchAsync(EmbeddingsDict["lemon"], new VectorSearchSettings
{
NumberOfResults = 5,
});
similar.Items.Should().HaveCount(5);

var similarTexts = similar.Items.Select(s => s.Text).ToArray();

similarTexts[0].Should().BeEquivalentTo("lemon");
similarTexts.Should().Contain("orange");
similarTexts.Should().Contain("peach");
similarTexts.Should().Contain("banana");
similarTexts.Should().Contain("apple");
}

[Test]
public async Task SimilaritySearchWithScores_Ok()
{
using var httpClient = new HttpClient();
var embeddingsMock = CreateFakeEmbeddings();
var collectionName = GenerateCollectionName();
var chroma = new ChromaVectorStore(httpClient, "http://localhost:8000", collectionName);

await chroma.AddTextsAsync(embeddingsMock.Object, EmbeddingsDict.Keys);

var similar = await chroma.SearchAsync(embeddingsMock.Object, "lemon", searchSettings: new VectorSearchSettings
{
NumberOfResults = 5,
});
similar.Items.Should().HaveCount(5);

var first = similar.Items.First();

first.Text.Should().BeEquivalentTo("lemon");
first.Distance.Should().BeGreaterOrEqualTo(1f);
}

private void PopulateEmbedding()
{
Expand Down
5 changes: 3 additions & 2 deletions src/Databases/IntegrationTests/Tests.Configure.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,18 @@ private static async Task<TestEnvironment> StartEnvironmentForAsync(SupportedDat
}
case SupportedDatabase.Chroma:
{
var port = Random.Shared.Next(49152, 65535);
var container = new ContainerBuilder()
.WithImage("chromadb/chroma")
.WithPortBinding(hostPort: 8000, containerPort: 8000)
.WithPortBinding(hostPort: port, containerPort: 8000)
.WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(8000))
.Build();

await container.StartAsync(cancellationToken);

return new TestEnvironment
{
VectorDatabase = new ChromaVectorStore(new HttpClient(), "http://localhost:8000",
VectorDatabase = new ChromaVectorStore(new HttpClient(), $"http://localhost:{port}",
GenerateCollectionName()),
};
}
Expand Down
60 changes: 58 additions & 2 deletions src/Databases/IntegrationTests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,38 @@ public partial class Tests
[TestCase(SupportedDatabase.InMemory)]
[TestCase(SupportedDatabase.Chroma)]
[TestCase(SupportedDatabase.SqLite)]
[TestCase(SupportedDatabase.OpenSearch, Explicit = true)] // #TODO: Fix OpenSearch tests
[TestCase(SupportedDatabase.Postgres, Explicit = true)] // #TODO: Fix Postgres tests
//[TestCase(SupportedDatabase.OpenSearch, Explicit = true)] // #TODO: Fix OpenSearch tests
//[TestCase(SupportedDatabase.Postgres, Explicit = true)] // #TODO: Fix Postgres tests
public async Task SimilaritySearch_Ok(SupportedDatabase database)
{
await using var environment = await StartEnvironmentForAsync(database);

await environment.VectorDatabase.AddTextsAsync(environment.EmbeddingModel, Embeddings.Keys);

var similar = await environment.VectorDatabase.SearchAsync(
environment.EmbeddingModel,
embeddingRequest: "lemon",
searchSettings: new VectorSearchSettings
{
Type = VectorSearchType.Similarity,
NumberOfResults = 5,
});
similar.Items.Should().HaveCount(5);

var similarTexts = similar.Items.Select(s => s.Text).ToArray();

similarTexts[0].Should().BeEquivalentTo("lemon");
similarTexts.Should().Contain("orange");
similarTexts.Should().Contain("peach");
similarTexts.Should().Contain("banana");
similarTexts.Should().Contain("apple");
}

[TestCase(SupportedDatabase.InMemory)]
[TestCase(SupportedDatabase.Chroma)]
[TestCase(SupportedDatabase.SqLite)]
//[TestCase(SupportedDatabase.OpenSearch, Explicit = true)] // #TODO: Fix OpenSearch tests
//[TestCase(SupportedDatabase.Postgres, Explicit = true)] // #TODO: Fix Postgres tests
public async Task SimilaritySearchByVector_Ok(SupportedDatabase database)
{
await using var environment = await StartEnvironmentForAsync(database);
Expand All @@ -28,4 +58,30 @@ public async Task SimilaritySearchByVector_Ok(SupportedDatabase database)
similarTexts.Should().Contain("banana");
similarTexts.Should().Contain("apple");
}

[TestCase(SupportedDatabase.InMemory)]
[TestCase(SupportedDatabase.Chroma)]
[TestCase(SupportedDatabase.SqLite)]
//[TestCase(SupportedDatabase.OpenSearch, Explicit = true)] // #TODO: Fix OpenSearch tests
//[TestCase(SupportedDatabase.Postgres, Explicit = true)] // #TODO: Fix Postgres tests
public async Task SimilaritySearchWithScores_Ok(SupportedDatabase database)
{
await using var environment = await StartEnvironmentForAsync(database);

await environment.VectorDatabase.AddTextsAsync(environment.EmbeddingModel, Embeddings.Keys);

var similar = await environment.VectorDatabase.SearchAsync(environment.EmbeddingModel, "lemon", searchSettings: new VectorSearchSettings
{
NumberOfResults = 5,
});
similar.Items.Should().HaveCount(5);

var first = similar.Items.First();

first.Text.Should().BeEquivalentTo("lemon");
if (database is SupportedDatabase.Chroma)
{
first.Distance.Should().BeGreaterOrEqualTo(1f);
}
}
}
1 change: 1 addition & 0 deletions src/Databases/Qdrant/src/LangChain.Databases.Qdrant.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.SemanticKernel.Connectors.Memory.Qdrant" />
<PackageReference Include="Qdrant.Client" />
</ItemGroup>

</Project>
1 change: 1 addition & 0 deletions src/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageVersion>
<PackageVersion Include="Qdrant.Client" Version="1.9.0" />
<PackageVersion Include="StackExchange.Redis" Version="2.7.20" />
<PackageVersion Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
<PackageVersion Include="System.CommandLine.NamingConventionBinder" Version="2.0.0-beta4.22272.1" />
Expand Down

0 comments on commit e031aec

Please sign in to comment.