-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring around
Query
and tests for it.
- Loading branch information
1 parent
dabb4e3
commit 2c1f1a4
Showing
11 changed files
with
184 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
using ChromaDB.Client.Models.Requests; | ||
using ChromaDB.Client.Services.Implementations; | ||
using NUnit.Framework; | ||
|
||
namespace ChromaDB.Client.Tests; | ||
|
||
[TestFixture] | ||
public class CollectionClientQueryTests : ChromaDBTestsBase | ||
{ | ||
[Test] | ||
public async Task SimpleQuerySingle() | ||
{ | ||
using var httpClient = new ChromaDBHttpClient(ConfigurationOptions); | ||
var client = await Init(httpClient); | ||
var result = await client.Query(new CollectionQueryRequest() | ||
{ | ||
QueryEmbeddings = [Embeddings1], | ||
Include = ["distances", "embeddings"], | ||
}); | ||
Assert.That(result.Success, Is.True); | ||
Assert.That(result.Data!.Count, Is.EqualTo(1)); | ||
Assert.That(result.Data![0].Count, Is.EqualTo(2)); | ||
Assert.That(result.Data![0].Select(x => x.Distance), Has.Some.Not.EqualTo(0)); | ||
Assert.That(result.Data![0][0].Embeddings, Is.Not.Null.And.Not.Empty); | ||
Assert.That(result.Data![0][1].Embeddings, Is.Not.Null.And.Not.Empty); | ||
} | ||
|
||
[Test] | ||
public async Task SimpleQueryMultiple() | ||
{ | ||
using var httpClient = new ChromaDBHttpClient(ConfigurationOptions); | ||
var client = await Init(httpClient); | ||
var result = await client.Query(new CollectionQueryRequest() | ||
{ | ||
QueryEmbeddings = [Embeddings1, Embeddings2], | ||
Include = ["distances", "embeddings"], | ||
}); | ||
Assert.That(result.Success, Is.True); | ||
Assert.That(result.Data!.Count, Is.EqualTo(2)); | ||
Assert.That(result.Data![0].Count, Is.EqualTo(2)); | ||
Assert.That(result.Data![0].Select(x => x.Distance), Has.Some.Not.EqualTo(0)); | ||
Assert.That(result.Data![0][0].Embeddings, Is.Not.Null.And.Not.Empty); | ||
Assert.That(result.Data![0][1].Embeddings, Is.Not.Null.And.Not.Empty); | ||
Assert.That(result.Data![1].Count, Is.EqualTo(2)); | ||
Assert.That(result.Data![1].Select(x => x.Distance), Has.Some.Not.EqualTo(0)); | ||
Assert.That(result.Data![1][0].Embeddings, Is.Not.Null.And.Not.Empty); | ||
Assert.That(result.Data![1][1].Embeddings, Is.Not.Null.And.Not.Empty); | ||
} | ||
|
||
[Test] | ||
public async Task QuerySingleNResults1() | ||
{ | ||
using var httpClient = new ChromaDBHttpClient(ConfigurationOptions); | ||
var client = await Init(httpClient); | ||
var result = await client.Query(new CollectionQueryRequest() | ||
{ | ||
QueryEmbeddings = [Embeddings1], | ||
Include = ["distances", "embeddings"], | ||
NResults = 1, | ||
}); | ||
Assert.That(result.Success, Is.True); | ||
Assert.That(result.Data!.Count, Is.EqualTo(1)); | ||
Assert.That(result.Data![0].Count, Is.EqualTo(1)); | ||
} | ||
|
||
static readonly string Id1 = "id1"; | ||
static readonly string Id2 = "id2"; | ||
static readonly List<float> Embeddings1 = [1, 2, 3]; | ||
static readonly List<float> Embeddings2 = [1.4f, 1.5f, 99.33f]; | ||
static readonly string MetadataKey1 = "key1"; | ||
static readonly string MetadataKey2 = "key2"; | ||
static readonly Dictionary<string, object> Metadata1 = new() | ||
{ | ||
{ MetadataKey1, "1" }, | ||
{ MetadataKey2, 1 }, | ||
}; | ||
static readonly Dictionary<string, object> Metadata2 = new() | ||
{ | ||
{ MetadataKey1, "2" }, | ||
{ MetadataKey2, 2 }, | ||
}; | ||
static readonly string Doc1 = "Doc1"; | ||
static readonly string Doc2 = "Doc2"; | ||
|
||
async Task<ChromaDBCollectionClient> Init(ChromaDBHttpClient httpClient) | ||
{ | ||
var name = $"collection{Random.Shared.Next()}"; | ||
var client = new ChromaDBClient(ConfigurationOptions, httpClient); | ||
var collectionResponse = await client.CreateCollection(new CreateCollectionRequest { Name = name }); | ||
Assert.That(collectionResponse.Success, Is.True); | ||
var collection = collectionResponse.Data!; | ||
var collectionClient = new ChromaDBCollectionClient(collection, httpClient); | ||
var addResponse = await collectionClient.Add(new CollectionAddRequest() | ||
{ | ||
Ids = [Id1, Id2], | ||
Embeddings = [Embeddings1, Embeddings2], | ||
Metadatas = [Metadata1, Metadata2], | ||
Documents = [Doc1, Doc2], | ||
}); | ||
Assert.That(addResponse.Success, Is.True); | ||
return collectionClient; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
ChromaDB.Client/Common/Mappers/CollectionQueryEntryMapper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using ChromaDB.Client.Models; | ||
using ChromaDB.Client.Models.Responses; | ||
|
||
namespace ChromaDB.Client.Common.Mappers; | ||
|
||
public static class CollectionQueryEntryMapper | ||
{ | ||
public static List<List<CollectionQueryEntry>> Map(this CollectionEntriesQueryResponse response) | ||
{ | ||
return response.Ids | ||
.Select((_, i) => response.Ids[i] | ||
.Select((id, j) => new CollectionQueryEntry(id) | ||
{ | ||
Distance = response.Distances[i][j], | ||
Metadata = response.Metadatas?[i][j], | ||
Embeddings = response.Embeddings?[i][j], | ||
Document = response.Documents?[i][j], | ||
Uris = response.Uris?[i][j], | ||
Data = response.Data, | ||
}) | ||
.ToList()) | ||
.ToList(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
namespace ChromaDB.Client.Models; | ||
|
||
public class CollectionQueryEntry | ||
{ | ||
public string Id { get; } | ||
public float Distance { get; init; } | ||
public Dictionary<string, object>? Metadata { get; init; } | ||
public List<float>? Embeddings { get; init; } | ||
public string? Document { get; init; } | ||
public List<string?>? Uris { get; init; } | ||
public dynamic? Data { get; init; } | ||
|
||
public CollectionQueryEntry(string id) | ||
{ | ||
Id = id; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters