From 5bed06c542ae200201e34342b73702b3272076b0 Mon Sep 17 00:00:00 2001 From: Hamed Hasani Date: Tue, 6 May 2025 18:22:52 +0330 Subject: [PATCH] update urls from api version 1 to 2 remove deleted data field from CollectionEntriesGetResponse and CollectionEntriesQueryResponse --- ChromaDB.Client.Tests/ChromaTestsBase.cs | 2 +- ChromaDB.Client/ChromaClient.cs | 18 ++-- ChromaDB.Client/ChromaCollectionClient.cs | 90 +++++++++++++++++-- ChromaDB.Client/ChromaCollectionEntry.cs | 1 - ChromaDB.Client/ChromaCollectionQueryEntry.cs | 1 - ChromaDB.Client/Common/ClientConstants.cs | 2 +- .../Common/CollectionEntryMapper.cs | 1 - .../Common/CollectionQueryEntryMapper.cs | 1 - .../Responses/CollectionEntriesGetResponse.cs | 3 - .../CollectionEntriesQueryResponse.cs | 3 - README.md | 2 +- Samples/ChromaDB.Client.Sample/Program.cs | 2 +- 12 files changed, 94 insertions(+), 32 deletions(-) diff --git a/ChromaDB.Client.Tests/ChromaTestsBase.cs b/ChromaDB.Client.Tests/ChromaTestsBase.cs index afd6089..339ae1f 100644 --- a/ChromaDB.Client.Tests/ChromaTestsBase.cs +++ b/ChromaDB.Client.Tests/ChromaTestsBase.cs @@ -15,7 +15,7 @@ public async Task OneTimeSetUp() { _container = ConfigureContainer(new ChromaDBBuilder()).Build(); await _container.StartAsync(); - _baseConfigurationOptions = new ChromaConfigurationOptions(uri: $"http://{_container.IpAddress}:{_container.GetMappedPublicPort(ChromaDBBuilder.ChromaDBPort)}/api/v1/"); + _baseConfigurationOptions = new ChromaConfigurationOptions(uri: $"http://{_container.IpAddress}:{_container.GetMappedPublicPort(ChromaDBBuilder.ChromaDBPort)}/api/v2/"); } [OneTimeTearDown] diff --git a/ChromaDB.Client/ChromaClient.cs b/ChromaDB.Client/ChromaClient.cs index d9ea4cc..1ba2c27 100644 --- a/ChromaDB.Client/ChromaClient.cs +++ b/ChromaDB.Client/ChromaClient.cs @@ -38,7 +38,7 @@ public async Task> ListCollections(string? tenant = null, var requestParams = new RequestQueryParams() .Insert("{tenant}", tenant) .Insert("{database}", database); - return await _httpClient.Get>("collections?tenant={tenant}&database={database}", requestParams); + return await _httpClient.Get>("tenants/{tenant}/databases/{database}/collections", requestParams); } public async Task GetCollection(string name, string? tenant = null, string? database = null) @@ -46,15 +46,15 @@ public async Task GetCollection(string name, string? tenant = tenant = tenant is not null and not [] ? tenant : _currentTenant.Name; database = database is not null and not [] ? database : _currentDatabase.Name; var requestParams = new RequestQueryParams() - .Insert("{collectionName}", name) + .Insert("{collection_id}", name) // it seems to be a chroma bug: https://github.com/chroma-core/chroma/issues/4456 .Insert("{tenant}", tenant) .Insert("{database}", database); - return await _httpClient.Get("collections/{collectionName}?tenant={tenant}&database={database}", requestParams); + return await _httpClient.Get("tenants/{tenant}/databases/{database}/collections/{collection_id}", requestParams); } public async Task Heartbeat() { - return await _httpClient.Get("", new RequestQueryParams()); + return await _httpClient.Get("heartbeat", new RequestQueryParams()); } public async Task CreateCollection(string name, Dictionary? metadata = null, string? tenant = null, string? database = null) @@ -69,7 +69,7 @@ public async Task CreateCollection(string name, Dictionary("collections?tenant={tenant}&database={database}", request, requestParams); + return await _httpClient.Post("tenants/{tenant}/databases/{database}/collections", request, requestParams); } public async Task GetOrCreateCollection(string name, Dictionary? metadata = null, string? tenant = null, string? database = null) @@ -84,7 +84,7 @@ public async Task GetOrCreateCollection(string name, Dictionar Name = name, Metadata = metadata }; - return await _httpClient.Post("collections?tenant={tenant}&database={database}", request, requestParams); + return await _httpClient.Post("tenants/{tenant}/databases/{database}/collections", request, requestParams); } public async Task DeleteCollection(string name, string? tenant = null, string? database = null) @@ -92,10 +92,10 @@ public async Task DeleteCollection(string name, string? tenant = null, string? d tenant = tenant is not null and not [] ? tenant : _currentTenant.Name; database = database is not null and not [] ? database : _currentDatabase.Name; var requestParams = new RequestQueryParams() - .Insert("{collectionName}", name) + .Insert("{collection_id}", name) .Insert("{tenant}", tenant) .Insert("{database}", database); - await _httpClient.Delete("collections/{collectionName}?tenant={tenant}&database={database}", requestParams); + await _httpClient.Delete("tenants/{tenant}/databases/{database}/collections/{collection_id}", requestParams); } public async Task GetVersion() @@ -115,6 +115,6 @@ public async Task CountCollections(string? tenant = null, string? database var requestParams = new RequestQueryParams() .Insert("{tenant}", tenant) .Insert("{database}", database); - return await _httpClient.Get("count_collections?tenant={tenant}&database={database}", requestParams); + return await _httpClient.Get("tenants/{tenant}/databases/{database}/collections_count", requestParams); } } diff --git a/ChromaDB.Client/ChromaCollectionClient.cs b/ChromaDB.Client/ChromaCollectionClient.cs index 60ee381..5189b8c 100644 --- a/ChromaDB.Client/ChromaCollectionClient.cs +++ b/ChromaDB.Client/ChromaCollectionClient.cs @@ -9,11 +9,19 @@ public class ChromaCollectionClient { private readonly ChromaCollection _collection; private readonly HttpClient _httpClient; + private readonly ChromaTenant _currentTenant; + private readonly ChromaDatabase _currentDatabase; public ChromaCollectionClient(ChromaCollection collection, ChromaConfigurationOptions options, HttpClient httpClient) { _collection = collection; _httpClient = httpClient; + _currentTenant = options.Tenant is not null and not [] + ? new ChromaTenant(options.Tenant) + : ClientConstants.DefaultTenant; + _currentDatabase = options.Database is not null and not [] + ? new ChromaDatabase(options.Database) + : ClientConstants.DefaultDatabase; if (_httpClient.BaseAddress != options.Uri) { @@ -29,7 +37,10 @@ public ChromaCollectionClient(ChromaCollection collection, ChromaConfigurationOp public async Task> Get(List? ids = null, ChromaWhereOperator? where = null, ChromaWhereDocumentOperator? whereDocument = null, int? limit = null, int? offset = null, ChromaGetInclude? include = null) { var requestParams = new RequestQueryParams() + .Insert("{tenant}", _currentTenant.Name) + .Insert("{database}", _currentDatabase.Name) .Insert("{collection_id}", _collection.Id); + var request = new CollectionGetRequest() { Ids = ids, @@ -39,7 +50,12 @@ public async Task> Get(List? ids = null, Chr Offset = offset, Include = (include ?? ChromaGetInclude.Metadatas | ChromaGetInclude.Documents).ToInclude(), }; - var response = await _httpClient.Post("collections/{collection_id}/get", request, requestParams); + + var response = await _httpClient.Post( + "tenants/{tenant}/databases/{database}/collections/{collection_id}/get", + request, + requestParams); + return response.Map() ?? []; } @@ -49,7 +65,10 @@ public async Task> Query(ReadOnlyMemory public async Task>> Query(List> queryEmbeddings, int nResults = 10, ChromaWhereOperator? where = null, ChromaWhereDocumentOperator? whereDocument = null, ChromaQueryInclude? include = null) { var requestParams = new RequestQueryParams() + .Insert("{tenant}", _currentTenant.Name) + .Insert("{database}", _currentDatabase.Name) .Insert("{collection_id}", _collection.Id); + var request = new CollectionQueryRequest() { QueryEmbeddings = queryEmbeddings, @@ -58,14 +77,22 @@ public async Task>> Query(List("collections/{collection_id}/query", request, requestParams); + + var response = await _httpClient.Post( + "tenants/{tenant}/databases/{database}/collections/{collection_id}/query", + request, + requestParams); + return response.Map() ?? []; } public async Task Add(List ids, List>? embeddings = null, List>? metadatas = null, List? documents = null) { var requestParams = new RequestQueryParams() + .Insert("{tenant}", _currentTenant.Name) + .Insert("{database}", _currentDatabase.Name) .Insert("{collection_id}", _collection.Id); + var request = new CollectionAddRequest() { Ids = ids, @@ -73,13 +100,20 @@ public async Task Add(List ids, List>? embeddings Metadatas = metadatas, Documents = documents, }; - await _httpClient.Post("collections/{collection_id}/add", request, requestParams); + + await _httpClient.Post( + "tenants/{tenant}/databases/{database}/collections/{collection_id}/add", + request, + requestParams); } public async Task Update(List ids, List>? embeddings = null, List>? metadatas = null, List? documents = null) { var requestParams = new RequestQueryParams() + .Insert("{tenant}", _currentTenant.Name) + .Insert("{database}", _currentDatabase.Name) .Insert("{collection_id}", _collection.Id); + var request = new CollectionUpdateRequest() { Ids = ids, @@ -87,13 +121,20 @@ public async Task Update(List ids, List>? embeddin Metadatas = metadatas, Documents = documents, }; - await _httpClient.Post("collections/{collection_id}/update", request, requestParams); + + await _httpClient.Post( + "tenants/{tenant}/databases/{database}/collections/{collection_id}/update", + request, + requestParams); } public async Task Upsert(List ids, List>? embeddings = null, List>? metadatas = null, List? documents = null) { var requestParams = new RequestQueryParams() + .Insert("{tenant}", _currentTenant.Name) + .Insert("{database}", _currentDatabase.Name) .Insert("{collection_id}", _collection.Id); + var request = new CollectionUpsertRequest() { Ids = ids, @@ -101,50 +142,81 @@ public async Task Upsert(List ids, List>? embeddin Metadatas = metadatas, Documents = documents, }; - await _httpClient.Post("collections/{collection_id}/upsert", request, requestParams); + + await _httpClient.Post( + "tenants/{tenant}/databases/{database}/collections/{collection_id}/upsert", + request, + requestParams); } public async Task Delete(List ids, ChromaWhereOperator? where = null, ChromaWhereDocumentOperator? whereDocument = null) { var requestParams = new RequestQueryParams() + .Insert("{tenant}", _currentTenant.Name) + .Insert("{database}", _currentDatabase.Name) .Insert("{collection_id}", _collection.Id); + var request = new CollectionDeleteRequest() { Ids = ids, Where = where?.ToWhere(), WhereDocument = whereDocument?.ToWhereDocument(), }; - await _httpClient.Post("collections/{collection_id}/delete", request, requestParams); + + await _httpClient.Post( + "tenants/{tenant}/databases/{database}/collections/{collection_id}/delete", + request, + requestParams); } public async Task Count() { var requestParams = new RequestQueryParams() + .Insert("{tenant}", _currentTenant.Name) + .Insert("{database}", _currentDatabase.Name) .Insert("{collection_id}", _collection.Id); - return await _httpClient.Get("collections/{collection_id}/count", requestParams); + + return await _httpClient.Get( + "tenants/{tenant}/databases/{database}/collections/{collection_id}/count", + requestParams); } public async Task> Peek(int limit = 10) { var requestParams = new RequestQueryParams() + .Insert("{tenant}", _currentTenant.Name) + .Insert("{database}", _currentDatabase.Name) .Insert("{collection_id}", _collection.Id); + var request = new CollectionPeekRequest() { Limit = limit, }; - var response = await _httpClient.Post("collections/{collection_id}/get", request, requestParams); + + var response = await _httpClient.Post( + "tenants/{tenant}/databases/{database}/collections/{collection_id}/get", + request, + requestParams); + return response.Map() ?? []; } public async Task Modify(string? name = null, Dictionary? metadata = null) { var requestParams = new RequestQueryParams() + .Insert("{tenant}", _currentTenant.Name) + .Insert("{database}", _currentDatabase.Name) .Insert("{collection_id}", _collection.Id); + var request = new CollectionModifyRequest() { Name = name, Metadata = metadata, }; - await _httpClient.Put("collections/{collection_id}", request, requestParams); + + await _httpClient.Put( + "tenants/{tenant}/databases/{database}/collections/{collection_id}", + request, + requestParams); } } diff --git a/ChromaDB.Client/ChromaCollectionEntry.cs b/ChromaDB.Client/ChromaCollectionEntry.cs index aa20823..948ea77 100644 --- a/ChromaDB.Client/ChromaCollectionEntry.cs +++ b/ChromaDB.Client/ChromaCollectionEntry.cs @@ -7,7 +7,6 @@ public class ChromaCollectionEntry public Dictionary? Metadata { get; init; } public string? Document { get; init; } public List? Uris { get; init; } - public dynamic? Data { get; init; } public ChromaCollectionEntry(string id) { diff --git a/ChromaDB.Client/ChromaCollectionQueryEntry.cs b/ChromaDB.Client/ChromaCollectionQueryEntry.cs index 1a0c444..a9eacfc 100644 --- a/ChromaDB.Client/ChromaCollectionQueryEntry.cs +++ b/ChromaDB.Client/ChromaCollectionQueryEntry.cs @@ -8,7 +8,6 @@ public class ChromaCollectionQueryEntry public ReadOnlyMemory? Embeddings { get; init; } public string? Document { get; init; } public List? Uris { get; init; } - public dynamic? Data { get; init; } public ChromaCollectionQueryEntry(string id) { diff --git a/ChromaDB.Client/Common/ClientConstants.cs b/ChromaDB.Client/Common/ClientConstants.cs index 4827194..9839a0a 100644 --- a/ChromaDB.Client/Common/ClientConstants.cs +++ b/ChromaDB.Client/Common/ClientConstants.cs @@ -6,7 +6,7 @@ internal static class ClientConstants { public const string DefaultTenantName = "default_tenant"; public const string DefaultDatabaseName = "default_database"; - public const string DefaultUri = "http://localhost:8000/api/v1/"; + public const string DefaultUri = "http://localhost:8000/api/v2/"; public const string ChromaTokenHeader = "X-Chroma-Token"; public static ChromaTenant DefaultTenant { get; } = new(DefaultTenantName); diff --git a/ChromaDB.Client/Common/CollectionEntryMapper.cs b/ChromaDB.Client/Common/CollectionEntryMapper.cs index 4925086..1d94e23 100644 --- a/ChromaDB.Client/Common/CollectionEntryMapper.cs +++ b/ChromaDB.Client/Common/CollectionEntryMapper.cs @@ -14,7 +14,6 @@ public static List Map(this CollectionEntriesGetResponse Metadata = response.Metadatas?[i], Document = response.Documents?[i], Uris = response.Uris?[i], - Data = response.Data, }) .ToList(); } diff --git a/ChromaDB.Client/Common/CollectionQueryEntryMapper.cs b/ChromaDB.Client/Common/CollectionQueryEntryMapper.cs index 34a55d0..a6c601c 100644 --- a/ChromaDB.Client/Common/CollectionQueryEntryMapper.cs +++ b/ChromaDB.Client/Common/CollectionQueryEntryMapper.cs @@ -16,7 +16,6 @@ public static List> Map(this CollectionEntriesQ Embeddings = response.Embeddings?[i][j], Document = response.Documents?[i][j], Uris = response.Uris?[i][j], - Data = response.Data, }) .ToList()) .ToList(); diff --git a/ChromaDB.Client/Models/Responses/CollectionEntriesGetResponse.cs b/ChromaDB.Client/Models/Responses/CollectionEntriesGetResponse.cs index cfb4506..a5f4c43 100644 --- a/ChromaDB.Client/Models/Responses/CollectionEntriesGetResponse.cs +++ b/ChromaDB.Client/Models/Responses/CollectionEntriesGetResponse.cs @@ -18,7 +18,4 @@ internal class CollectionEntriesGetResponse [JsonPropertyName("uris")] public required List?> Uris { get; init; } - - [JsonPropertyName("data")] - public required dynamic? Data { get; init; } } diff --git a/ChromaDB.Client/Models/Responses/CollectionEntriesQueryResponse.cs b/ChromaDB.Client/Models/Responses/CollectionEntriesQueryResponse.cs index c5880b0..d6b8774 100644 --- a/ChromaDB.Client/Models/Responses/CollectionEntriesQueryResponse.cs +++ b/ChromaDB.Client/Models/Responses/CollectionEntriesQueryResponse.cs @@ -21,7 +21,4 @@ internal class CollectionEntriesQueryResponse [JsonPropertyName("uris")] public required List>>? Uris { get; init; } - - [JsonPropertyName("data")] - public required dynamic? Data { get; init; } } diff --git a/README.md b/README.md index 70f3205..d2a8353 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ _ChromaDB.Client_ is a .NET SDK that offers a seamless connection to the Chroma ```csharp using ChromaDB.Client; -var configOptions = new ChromaConfigurationOptions(uri: "http://localhost:8000/api/v1/"); +var configOptions = new ChromaConfigurationOptions(uri: "http://localhost:8000/api/v2/"); using var httpClient = new HttpClient(); var client = new ChromaClient(configOptions, httpClient); diff --git a/Samples/ChromaDB.Client.Sample/Program.cs b/Samples/ChromaDB.Client.Sample/Program.cs index af935aa..8e8bb73 100644 --- a/Samples/ChromaDB.Client.Sample/Program.cs +++ b/Samples/ChromaDB.Client.Sample/Program.cs @@ -1,6 +1,6 @@ using ChromaDB.Client; -var configOptions = new ChromaConfigurationOptions(uri: "http://localhost:8000/api/v1/"); +var configOptions = new ChromaConfigurationOptions(uri: "http://localhost:8000/api/v2/"); using var httpClient = new HttpClient(); var client = new ChromaClient(configOptions, httpClient);