diff --git a/ChromaDB.Client/ChromaDBClient.cs b/ChromaDB.Client/ChromaDBClient.cs index 93c7c8a..0adeea9 100644 --- a/ChromaDB.Client/ChromaDBClient.cs +++ b/ChromaDB.Client/ChromaDBClient.cs @@ -52,7 +52,7 @@ public async Task> Heartbeat() return await _httpClient.Get("", new RequestQueryParams()); } - public async Task> CreateCollection(string name, IDictionary? metadata = null, string? tenant = null, string? database = null) + public async Task> CreateCollection(string name, Dictionary? metadata = null, string? tenant = null, string? database = null) { tenant = tenant is not null and not [] ? tenant : _currentTenant.Name; database = database is not null and not [] ? database : _currentDatabase.Name; @@ -67,7 +67,7 @@ public async Task> CreateCollection(string name, IDictionar return await _httpClient.Post("collections?tenant={tenant}&database={database}", request, requestParams); } - public async Task> GetOrCreateCollection(string name, IDictionary? metadata = null, string? tenant = null, string? database = null) + public async Task> GetOrCreateCollection(string name, Dictionary? metadata = null, string? tenant = null, string? database = null) { tenant = tenant is not null and not [] ? tenant : _currentTenant.Name; database = database is not null and not [] ? database : _currentDatabase.Name; diff --git a/ChromaDB.Client/ChromaDBCollectionClient.cs b/ChromaDB.Client/ChromaDBCollectionClient.cs index 87fc5cc..5623120 100644 --- a/ChromaDB.Client/ChromaDBCollectionClient.cs +++ b/ChromaDB.Client/ChromaDBCollectionClient.cs @@ -19,7 +19,7 @@ public ChromaDBCollectionClient(Collection collection, IChromaDBHttpClient httpC public Collection Collection => _collection; - public async Task>> Get(List? ids = null, IDictionary? where = null, IDictionary? whereDocument = null, int? limit = null, int? offset = null, List? include = null) + public async Task>> Get(List? ids = null, Dictionary? where = null, Dictionary? whereDocument = null, int? limit = null, int? offset = null, List? include = null) { RequestQueryParams requestParams = new RequestQueryParams() .Insert("{collection_id}", _collection.Id); @@ -37,7 +37,7 @@ public async Task>> Get(List? ids = null, return new Response>(response.StatusCode, entries, response.ErrorMessage); } - public async Task>>> Query(List> queryEmbeddings, int nResults = 10, IDictionary? where = null, IDictionary? whereDocument = null, List? include = null) + public async Task>>> Query(List> queryEmbeddings, int nResults = 10, Dictionary? where = null, Dictionary? whereDocument = null, List? include = null) { RequestQueryParams requestParams = new RequestQueryParams() .Insert("{collection_id}", _collection.Id); @@ -54,7 +54,7 @@ public async Task>>> Query(List>>(response.StatusCode, entries, response.ErrorMessage); } - public async Task> Add(List ids, List>? embeddings = null, List>? metadatas = null, List? documents = null) + public async Task> Add(List ids, List>? embeddings = null, List>? metadatas = null, List? documents = null) { RequestQueryParams requestParams = new RequestQueryParams() .Insert("{collection_id}", _collection.Id); @@ -68,7 +68,7 @@ public async Task>>> Query(List("collections/{collection_id}/add", request, requestParams); } - public async Task> Update(List ids, List>? embeddings = null, List>? metadatas = null, List? documents = null) + public async Task> Update(List ids, List>? embeddings = null, List>? metadatas = null, List? documents = null) { RequestQueryParams requestParams = new RequestQueryParams() .Insert("{collection_id}", _collection.Id); @@ -82,7 +82,7 @@ public async Task>>> Query(List("collections/{collection_id}/update", request, requestParams); } - public async Task> Upsert(List ids, List>? embeddings = null, List>? metadatas = null, List? documents = null) + public async Task> Upsert(List ids, List>? embeddings = null, List>? metadatas = null, List? documents = null) { RequestQueryParams requestParams = new RequestQueryParams() .Insert("{collection_id}", _collection.Id); @@ -96,7 +96,7 @@ public async Task>>> Query(List("collections/{collection_id}/upsert", request, requestParams); } - public async Task> Delete(List ids, IDictionary? where = null, IDictionary? whereDocument = null) + public async Task> Delete(List ids, Dictionary? where = null, Dictionary? whereDocument = null) { RequestQueryParams requestParams = new RequestQueryParams() .Insert("{collection_id}", _collection.Id); @@ -129,7 +129,7 @@ public async Task>> Peek(int limit = 10) return new Response>(response.StatusCode, entries, response.ErrorMessage); } - public async Task> Modify(string? name = null, IDictionary? metadata = null) + public async Task> Modify(string? name = null, Dictionary? metadata = null) { RequestQueryParams requestParams = new RequestQueryParams() .Insert("{collection_id}", _collection.Id); diff --git a/ChromaDB.Client/Common/Helpers/HttpClientHelpers.cs b/ChromaDB.Client/Common/Helpers/HttpClientHelpers.cs index 184a8b9..1f0b0b2 100644 --- a/ChromaDB.Client/Common/Helpers/HttpClientHelpers.cs +++ b/ChromaDB.Client/Common/Helpers/HttpClientHelpers.cs @@ -148,10 +148,10 @@ private static string ValidateAndPrepareEndpoint(string endpoint, RequestQueryPa private static string FormatRequestUri(string endpoint, RequestQueryParams queryParams) { string formattedEndpoint = endpoint; - foreach (KeyValuePair entry in queryParams.Build()) + foreach (var (key, value) in queryParams) { - string urlEncodedQueryParam = Uri.EscapeDataString(entry.Value); - formattedEndpoint = formattedEndpoint.Replace(entry.Key, urlEncodedQueryParam); + string urlEncodedQueryParam = Uri.EscapeDataString(value); + formattedEndpoint = formattedEndpoint.Replace(key, urlEncodedQueryParam); } return formattedEndpoint; } diff --git a/ChromaDB.Client/IChromaDBClient.cs b/ChromaDB.Client/IChromaDBClient.cs index 67bd441..b94ec5f 100644 --- a/ChromaDB.Client/IChromaDBClient.cs +++ b/ChromaDB.Client/IChromaDBClient.cs @@ -7,8 +7,8 @@ public interface IChromaDBClient Task> GetCollection(string name, string? tenant = null, string? database = null); Task>> ListCollections(string? tenant = null, string? database = null); Task> Heartbeat(); - Task> CreateCollection(string name, IDictionary? metadata = null, string? tenant = null, string? database = null); - Task> GetOrCreateCollection(string name, IDictionary? metadata = null, string? tenant = null, string? database = null); + Task> CreateCollection(string name, Dictionary? metadata = null, string? tenant = null, string? database = null); + Task> GetOrCreateCollection(string name, Dictionary? metadata = null, string? tenant = null, string? database = null); Task> DeleteCollection(string name, string? tenant = null, string? database = null); Task> GetVersion(); Task> Reset(); diff --git a/ChromaDB.Client/IChromaDBCollectionClient.cs b/ChromaDB.Client/IChromaDBCollectionClient.cs index c0bf8fe..057e258 100644 --- a/ChromaDB.Client/IChromaDBCollectionClient.cs +++ b/ChromaDB.Client/IChromaDBCollectionClient.cs @@ -6,13 +6,13 @@ public interface IChromaDBCollectionClient { Collection Collection { get; } - Task>> Get(List? ids = null, IDictionary? where = null, IDictionary? whereDocument = null, int? limit = null, int? offset = null, List? include = null); - Task>>> Query(List> queryEmbeddings, int nResults = 10, IDictionary? where = null, IDictionary? whereDocument = null, List? include = null); - Task> Add(List ids, List>? embeddings = null, List>? metadatas = null, List? documents = null); - Task> Update(List ids, List>? embeddings = null, List>? metadatas = null, List? documents = null); - Task> Upsert(List ids, List>? embeddings = null, List>? metadatas = null, List? documents = null); - Task> Delete(List ids, IDictionary? where = null, IDictionary? whereDocument = null); + Task>> Get(List? ids = null, Dictionary? where = null, Dictionary? whereDocument = null, int? limit = null, int? offset = null, List? include = null); + Task>>> Query(List> queryEmbeddings, int nResults = 10, Dictionary? where = null, Dictionary? whereDocument = null, List? include = null); + Task> Add(List ids, List>? embeddings = null, List>? metadatas = null, List? documents = null); + Task> Update(List ids, List>? embeddings = null, List>? metadatas = null, List? documents = null); + Task> Upsert(List ids, List>? embeddings = null, List>? metadatas = null, List? documents = null); + Task> Delete(List ids, Dictionary? where = null, Dictionary? whereDocument = null); Task> Count(); Task>> Peek(int limit = 10); - Task> Modify(string? name = null, IDictionary? metadata = null); + Task> Modify(string? name = null, Dictionary? metadata = null); } diff --git a/ChromaDB.Client/Models/Collection.cs b/ChromaDB.Client/Models/Collection.cs index 773a07d..f0aba23 100644 --- a/ChromaDB.Client/Models/Collection.cs +++ b/ChromaDB.Client/Models/Collection.cs @@ -11,7 +11,7 @@ public class Collection public string Name { get; } [JsonPropertyName("metadata")] - public IDictionary? Metadata { get; init; } + public Dictionary? Metadata { get; init; } [JsonPropertyName("tenant")] public string? Tenant { get; init; } diff --git a/ChromaDB.Client/Models/Requests/CollectionAddRequest.cs b/ChromaDB.Client/Models/Requests/CollectionAddRequest.cs index 69253e7..04dc8c4 100644 --- a/ChromaDB.Client/Models/Requests/CollectionAddRequest.cs +++ b/ChromaDB.Client/Models/Requests/CollectionAddRequest.cs @@ -11,7 +11,7 @@ internal class CollectionAddRequest public List>? Embeddings { get; init; } [JsonPropertyName("metadatas")] - public List>? Metadatas { get; init; } + public List>? Metadatas { get; init; } [JsonPropertyName("documents")] public List? Documents { get; init; } diff --git a/ChromaDB.Client/Models/Requests/CollectionDeleteRequest.cs b/ChromaDB.Client/Models/Requests/CollectionDeleteRequest.cs index 865b0ee..707ec6e 100644 --- a/ChromaDB.Client/Models/Requests/CollectionDeleteRequest.cs +++ b/ChromaDB.Client/Models/Requests/CollectionDeleteRequest.cs @@ -8,8 +8,8 @@ internal class CollectionDeleteRequest public required List Ids { get; init; } [JsonPropertyName("where")] - public IDictionary? Where { get; init; } + public Dictionary? Where { get; init; } [JsonPropertyName("where_document")] - public IDictionary? WhereDocument { get; init; } + public Dictionary? WhereDocument { get; init; } } diff --git a/ChromaDB.Client/Models/Requests/CollectionGetRequest.cs b/ChromaDB.Client/Models/Requests/CollectionGetRequest.cs index 5c9e4f2..44ef224 100644 --- a/ChromaDB.Client/Models/Requests/CollectionGetRequest.cs +++ b/ChromaDB.Client/Models/Requests/CollectionGetRequest.cs @@ -8,10 +8,10 @@ internal class CollectionGetRequest public List? Ids { get; init; } [JsonPropertyName("where")] - public IDictionary? Where { get; init; } + public Dictionary? Where { get; init; } [JsonPropertyName("where_document")] - public IDictionary? WhereDocument { get; init; } + public Dictionary? WhereDocument { get; init; } [JsonPropertyName("limit")] public int? Limit { get; init; } diff --git a/ChromaDB.Client/Models/Requests/CollectionModifyRequest.cs b/ChromaDB.Client/Models/Requests/CollectionModifyRequest.cs index 1f098d7..cd632b2 100644 --- a/ChromaDB.Client/Models/Requests/CollectionModifyRequest.cs +++ b/ChromaDB.Client/Models/Requests/CollectionModifyRequest.cs @@ -8,5 +8,5 @@ internal class CollectionModifyRequest public string? Name { get; init; } [JsonPropertyName("metadata")] - public IDictionary? Metadata { get; init; } + public Dictionary? Metadata { get; init; } } diff --git a/ChromaDB.Client/Models/Requests/CollectionQueryRequest.cs b/ChromaDB.Client/Models/Requests/CollectionQueryRequest.cs index cba6296..4869803 100644 --- a/ChromaDB.Client/Models/Requests/CollectionQueryRequest.cs +++ b/ChromaDB.Client/Models/Requests/CollectionQueryRequest.cs @@ -11,10 +11,10 @@ internal class CollectionQueryRequest public int NResults { get; init; } = 10; [JsonPropertyName("where")] - public IDictionary? Where { get; init; } + public Dictionary? Where { get; init; } [JsonPropertyName("where_document")] - public IDictionary? WhereDocument { get; init; } + public Dictionary? WhereDocument { get; init; } [JsonPropertyName("include")] public required List Include { get; init; } diff --git a/ChromaDB.Client/Models/Requests/CollectionUpdateRequest.cs b/ChromaDB.Client/Models/Requests/CollectionUpdateRequest.cs index d78a9f8..bc2c9c6 100644 --- a/ChromaDB.Client/Models/Requests/CollectionUpdateRequest.cs +++ b/ChromaDB.Client/Models/Requests/CollectionUpdateRequest.cs @@ -11,7 +11,7 @@ internal class CollectionUpdateRequest public List>? Embeddings { get; init; } [JsonPropertyName("metadatas")] - public List>? Metadatas { get; init; } + public List>? Metadatas { get; init; } [JsonPropertyName("documents")] public List? Documents { get; init; } diff --git a/ChromaDB.Client/Models/Requests/CollectionUpsertRequest.cs b/ChromaDB.Client/Models/Requests/CollectionUpsertRequest.cs index d31cd1f..4c5ea37 100644 --- a/ChromaDB.Client/Models/Requests/CollectionUpsertRequest.cs +++ b/ChromaDB.Client/Models/Requests/CollectionUpsertRequest.cs @@ -11,7 +11,7 @@ internal class CollectionUpsertRequest public List>? Embeddings { get; init; } [JsonPropertyName("metadatas")] - public List>? Metadatas { get; init; } + public List>? Metadatas { get; init; } [JsonPropertyName("documents")] public List? Documents { get; init; } diff --git a/ChromaDB.Client/Models/Requests/GetOrCreateCollectionRequestBase.cs b/ChromaDB.Client/Models/Requests/GetOrCreateCollectionRequestBase.cs index 2573172..0c248d4 100644 --- a/ChromaDB.Client/Models/Requests/GetOrCreateCollectionRequestBase.cs +++ b/ChromaDB.Client/Models/Requests/GetOrCreateCollectionRequestBase.cs @@ -8,7 +8,7 @@ internal abstract class GetOrCreateCollectionRequestBase public required string Name { get; init; } [JsonPropertyName("metadata")] - public IDictionary? Metadata { get; init; } + public Dictionary? Metadata { get; init; } [JsonInclude] [JsonPropertyName("get_or_create")] diff --git a/ChromaDB.Client/Models/Requests/RequestQueryParams.cs b/ChromaDB.Client/Models/Requests/RequestQueryParams.cs index 9022dd4..f9d4cdb 100644 --- a/ChromaDB.Client/Models/Requests/RequestQueryParams.cs +++ b/ChromaDB.Client/Models/Requests/RequestQueryParams.cs @@ -1,8 +1,9 @@ -using System.Globalization; +using System.Collections; +using System.Globalization; namespace ChromaDB.Client.Models.Requests; -internal class RequestQueryParams +internal class RequestQueryParams : IEnumerable<(string key, string value)> { private Dictionary _queryParams; @@ -11,12 +12,18 @@ public RequestQueryParams() _queryParams = new Dictionary(StringComparer.Ordinal); } + public IEnumerator<(string key, string value)> GetEnumerator() + => _queryParams + .Select(kvp => (kvp.Key, kvp.Value)) + .GetEnumerator(); + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + public RequestQueryParams Insert(string key, string value) { _queryParams[key] = value; return this; } - public RequestQueryParams Insert(string key, IFormattable value) => Insert(key, value.ToString(null, CultureInfo.InvariantCulture)); + public RequestQueryParams Insert(string key, IFormattable value) + => Insert(key, value.ToString(null, CultureInfo.InvariantCulture)); - public IDictionary Build() => _queryParams; }