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

Support for Add #28

Merged
merged 1 commit into from
Sep 16, 2024
Merged
Changes from all 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
Support for Add.
cincuranet committed Sep 6, 2024
commit 04fe6b31b836c4cfaf15a292aca5adfb8788e551
93 changes: 93 additions & 0 deletions ChromaDB.Client.Tests/ChromaDBCUDTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using ChromaDB.Client.Models;
using ChromaDB.Client.Models.Requests;
using ChromaDB.Client.Services.Implementations;
using NUnit.Framework;

namespace ChromaDB.Client.Tests;

[TestFixture]
public class ChromaDBCUDTests : ChromaDBTestsBase
{
[Test]
public async Task AddJustIds()
{
using var httpClient = new ChromaDBHttpClient(ConfigurationOptions);
var client = await Init(httpClient);
var result = await client.Add(new CollectionAddRequest()
{
Ids = [$"{Guid.NewGuid()}"],
});
Assert.That(result.Success, Is.True);
}

[Test]
public async Task AddWithEmbeddings()
{
using var httpClient = new ChromaDBHttpClient(ConfigurationOptions);
var client = await Init(httpClient);
var result = await client.Add(new CollectionAddRequest()
{
Ids = [$"{Guid.NewGuid()}"],
Embeddings = [[1f, 0.5f, 0f, -0.5f, -1f]],
});
Assert.That(result.Success, Is.True);
}

[Test]
public async Task AddWithMetadatas()
{
using var httpClient = new ChromaDBHttpClient(ConfigurationOptions);
var client = await Init(httpClient);
var result = await client.Add(new CollectionAddRequest()
{
Ids = [$"{Guid.NewGuid()}"],
Metadatas = [new Dictionary<string, object>
{
{ "key", "value" },
{ "key2", 10 },
}],
});
Assert.That(result.Success, Is.True);
}

[Test]
public async Task AddWithDocuments()
{
using var httpClient = new ChromaDBHttpClient(ConfigurationOptions);
var client = await Init(httpClient);
var result = await client.Add(new CollectionAddRequest()
{
Ids = [$"{Guid.NewGuid()}"],
Documents = ["test"],
});
Assert.That(result.Success, Is.True);
}

[Test]
public async Task AddWithAll()
{
using var httpClient = new ChromaDBHttpClient(ConfigurationOptions);
var client = await Init(httpClient);
var result = await client.Add(new CollectionAddRequest()
{
Ids = [$"{Guid.NewGuid()}"],
Embeddings = [[1f, 0.5f, 0f, -0.5f, -1f]],
Metadatas = [new Dictionary<string, object>
{
{ "key", "value" },
{ "key2", 10 },
}],
Documents = ["test"],
});
Assert.That(result.Success, Is.True);
}

async Task<ChromaCollectionClient> Init(ChromaDBHttpClient httpClient)
{
var client = new ChromaDBClient(ConfigurationOptions, httpClient);
var collectionResponse = await client.GetOrCreateCollection(new DBGetOrCreateCollectionRequest { Name = "cud_tests" });
Assert.That(collectionResponse.Success, Is.True);
var collection = collectionResponse.Data!;
return new ChromaCollectionClient(collection, httpClient);
}
}
25 changes: 23 additions & 2 deletions ChromaDB.Client/Common/Helpers/HttpClientHelpers.cs
Original file line number Diff line number Diff line change
@@ -48,6 +48,13 @@ public static async Task<BaseResponse<TResponse>> Get<TSource, TResponse>(this I
_ => throw new ChromaDBException(httpResponseMessage.ReasonPhrase, null, httpResponseMessage.StatusCode)
};

if (typeof(TResponse) == typeof(BaseResponse.None))
{
return new BaseResponse<TResponse>(
data: (TResponse)(object)BaseResponse.None.Instance,
statusCode: httpResponseMessage.StatusCode);
}

return new BaseResponse<TResponse>(
data: responseBody is not null and not []
? JsonSerializer.Deserialize<TResponse>(responseBody, DeserializerJsonSerializerOptions)
@@ -101,10 +108,17 @@ public static async Task<BaseResponse<TResponse>> Post<TSource, TInput, TRespons
_ => throw new ChromaDBException(httpResponseMessage.ReasonPhrase, null, httpResponseMessage.StatusCode)
};

if (typeof(TResponse) == typeof(BaseResponse.None))
{
return new BaseResponse<TResponse>(
data: (TResponse)(object)BaseResponse.None.Instance,
statusCode: httpResponseMessage.StatusCode);
}

return new BaseResponse<TResponse>(
data: responseBody is not null and not []
? JsonSerializer.Deserialize<TResponse>(responseBody, DeserializerJsonSerializerOptions)
: default,
? JsonSerializer.Deserialize<TResponse>(responseBody, DeserializerJsonSerializerOptions)
: default,
statusCode: httpResponseMessage.StatusCode);
}
catch (ChromaDBGeneralException ex)
@@ -146,6 +160,13 @@ public static async Task<BaseResponse<TResponse>> Delete<TSource, TResponse>(thi
_ => throw new ChromaDBException(httpResponseMessage.ReasonPhrase, null, httpResponseMessage.StatusCode)
};

if (typeof(TResponse) == typeof(BaseResponse.None))
{
return new BaseResponse<TResponse>(
data: (TResponse)(object)BaseResponse.None.Instance,
statusCode: httpResponseMessage.StatusCode);
}

return new BaseResponse<TResponse>(
data: responseBody is not null and not []
? JsonSerializer.Deserialize<TResponse>(responseBody, DeserializerJsonSerializerOptions)
2 changes: 2 additions & 0 deletions ChromaDB.Client/Models/Base/BaseResponse.cs
Original file line number Diff line number Diff line change
@@ -22,6 +22,8 @@ public static class BaseResponse
{
public class None
{
public static readonly None Instance = new None();

private None()
{ }
}
1 change: 1 addition & 0 deletions ChromaDB.Client/Models/Collection.cs
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@ namespace ChromaDB.Client.Models;
[ChromaPostRoute(Endpoint = "collections", Source = typeof(Collection), RequestType = typeof(DBCreateCollectionRequest), ResponseType = typeof(Collection))]
[ChromaPostRoute(Endpoint = "collections", Source = typeof(Collection), RequestType = typeof(DBGetOrCreateCollectionRequest), ResponseType = typeof(Collection))]
[ChromaDeleteRoute(Endpoint = "collections/{collectionName}?tenant={tenant}&database={database}", Source = typeof(Collection), ResponseType = typeof(BaseResponse.None))]
[ChromaPostRoute(Endpoint = "collections/{collection_id}/add", Source = typeof(Collection), RequestType = typeof(CollectionAddRequest), ResponseType = typeof(BaseResponse.None))]
public class Collection
{
[JsonPropertyName("id")]
18 changes: 18 additions & 0 deletions ChromaDB.Client/Models/Requests/CollectionAddRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Text.Json.Serialization;

namespace ChromaDB.Client.Models.Requests;

public class CollectionAddRequest
{
[JsonPropertyName("ids")]
public required List<string> Ids { get; init; }

[JsonPropertyName("embeddings")]
public List<List<float>>? Embeddings { get; init; }

[JsonPropertyName("metadatas")]
public List<IDictionary<string, object>>? Metadatas { get; init; }

[JsonPropertyName("documents")]
public List<string>? Documents { get; init; }
}
Original file line number Diff line number Diff line change
@@ -33,4 +33,11 @@ public async Task<BaseResponse<CollectionEntriesQueryResponse>> Query(Collection
.Add("{collection_id}", _collection.Id);
return await _httpClient.Post<Collection, CollectionQueryRequest, CollectionEntriesQueryResponse>(request, requestParams);
}

public async Task<BaseResponse<BaseResponse.None>> Add(CollectionAddRequest request)
cincuranet marked this conversation as resolved.
Show resolved Hide resolved
{
RequestQueryParams requestParams = new RequestQueryParams()
.Add("{collection_id}", _collection.Id);
return await _httpClient.Post<Collection, CollectionAddRequest, BaseResponse.None>(request, requestParams);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -8,4 +8,5 @@ public interface IChromaCollectionClient
{
Task<BaseResponse<List<CollectionEntry>>> Get(CollectionGetRequest request);
Task<BaseResponse<CollectionEntriesQueryResponse>> Query(CollectionQueryRequest request);
Task<BaseResponse<BaseResponse.None>> Add(CollectionAddRequest request);
}
2 changes: 1 addition & 1 deletion Samples/ChromaDB.Client.Sample/Program.cs
Original file line number Diff line number Diff line change
@@ -20,7 +20,7 @@

BaseResponse<Collection> collection1 = await client.GetCollection("string5", database: "test", tenant: "nedeljko");

IChromaCollectionClient string5Client = new ChromaCollectionFactory(configOptions).Create(collection1.Data!, httpClient);
IChromaCollectionClient string5Client = new ChromaCollectionClient(collection1.Data!, httpClient);

BaseResponse<List<CollectionEntry>> getResponse = await string5Client.Get(new CollectionGetRequest()
{