Skip to content

Commit

Permalink
Implement GetOrCreateCollection. (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
cincuranet authored Jul 12, 2024
1 parent 2730ca3 commit f23dabc
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 10 deletions.
40 changes: 40 additions & 0 deletions ChromaDB.Client.Tests/ChromaDBCollectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,44 @@ await client.CreateCollection(new DBCreateCollectionRequest()
Assert.That(result.Success, Is.False);
Assert.That(result.ReasonPhrase, Is.Not.Null.And.Not.Empty);
}

[Test]
public async Task GetOrCreateCollectionDoesNotExist()
{
var name = $"collection{Random.Shared.Next()}";

using var httpClient = new ChromaDBHttpClient(ConfigurationOptions);
var client = new ChromaDBClient(ConfigurationOptions, httpClient);
var result = await client.GetOrCreateCollection(new DBGetOrCreateCollectionRequest()
{
Name = name,
});
Assert.That(result.Success, Is.True);
Assert.That(result.Data, Is.Not.Null);
Assert.That(result.Data.Name, Is.EqualTo(name));
}

[Test]
public async Task GetOrCreateCollectionDoesExist()
{
var name = $"collection{Random.Shared.Next()}";

using var httpClient = new ChromaDBHttpClient(ConfigurationOptions);
var client = new ChromaDBClient(ConfigurationOptions, httpClient);
var result1 = await client.GetOrCreateCollection(new DBGetOrCreateCollectionRequest()
{
Name = name,
});
var result2 = await client.GetOrCreateCollection(new DBGetOrCreateCollectionRequest()
{
Name = name,
});
Assert.That(result1.Success, Is.True);
Assert.That(result1.Data, Is.Not.Null);
Assert.That(result1.Data.Name, Is.EqualTo(name));
Assert.That(result2.Success, Is.True);
Assert.That(result2.Data, Is.Not.Null);
Assert.That(result2.Data.Name, Is.EqualTo(name));
Assert.That(result1.Data.Id, Is.EqualTo(result2.Data.Id));
}
}
1 change: 1 addition & 0 deletions ChromaDB.Client/Models/Collection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace ChromaDB.Client.Models;
[ChromaPostRoute(Endpoint = "collections/{collection_id}/get", Source = typeof(Collection), RequestType = typeof(CollectionGetRequest), ResponseType = typeof(CollectionEntriesResponse))]
[ChromaPostRoute(Endpoint = "collections/{collection_id}/query", Source = typeof(Collection), RequestType = typeof(CollectionQueryRequest), ResponseType = typeof(CollectionEntriesQueryResponse))]
[ChromaPostRoute(Endpoint = "collections", Source = typeof(Collection), RequestType = typeof(DBCreateCollectionRequest), ResponseType = typeof(Collection))]
[ChromaPostRoute(Endpoint = "collections", Source = typeof(Collection), RequestType = typeof(DBGetOrCreateCollectionRequest), ResponseType = typeof(Collection))]
public class Collection
{
[JsonPropertyName("id")]
Expand Down
12 changes: 3 additions & 9 deletions ChromaDB.Client/Models/Requests/DBCreateCollectionRequest.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
using System.Text.Json.Serialization;
namespace ChromaDB.Client.Models.Requests;

namespace ChromaDB.Client.Models.Requests;

public class DBCreateCollectionRequest
public class DBCreateCollectionRequest : DBGetOrCreateCollectionRequestBase
{
[JsonPropertyName("name")]
public required string Name { get; init; }

[JsonPropertyName("metadata")]
public IDictionary<string, object>? Metadata { get; init; }
protected override bool GetOrCreate { get; } = false;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace ChromaDB.Client.Models.Requests;

public class DBGetOrCreateCollectionRequest : DBGetOrCreateCollectionRequestBase
{
protected override bool GetOrCreate { get; } = true;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Text.Json.Serialization;

namespace ChromaDB.Client.Models.Requests;

public abstract class DBGetOrCreateCollectionRequestBase
{
[JsonPropertyName("name")]
public required string Name { get; init; }

[JsonPropertyName("metadata")]
public IDictionary<string, object>? Metadata { get; init; }

[JsonInclude]
[JsonPropertyName("get_or_create")]
protected abstract bool GetOrCreate { get; }
}
10 changes: 10 additions & 0 deletions ChromaDB.Client/Services/Implementations/ChromaDBClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,14 @@ public async Task<BaseResponse<Collection>> CreateCollection(DBCreateCollectionR
.Add("{database}", database);
return await _httpClient.Post<Collection, DBCreateCollectionRequest, Collection>(request, requestParams);
}

public async Task<BaseResponse<Collection>> GetOrCreateCollection(DBGetOrCreateCollectionRequest request, 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;
RequestQueryParams requestParams = new RequestQueryParams()
.Add("{tenant}", tenant)
.Add("{database}", database);
return await _httpClient.Post<Collection, DBGetOrCreateCollectionRequest, Collection>(request, requestParams);
}
}
1 change: 1 addition & 0 deletions ChromaDB.Client/Services/Interfaces/IChromaDBClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ public interface IChromaDBClient
Task<BaseResponse<List<Collection>>> GetCollections(string? tenant = null, string? database = null);
Task<BaseResponse<Heartbeat>> Heartbeat();
Task<BaseResponse<Collection>> CreateCollection(DBCreateCollectionRequest request, string? tenant = null, string? database = null);
Task<BaseResponse<Collection>> GetOrCreateCollection(DBGetOrCreateCollectionRequest request, string? tenant = null, string? database = null);
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ With ChromaDB.Client, you can easily connect to a ChromaDB instance, create and
## Features

- [x] Basic connection and authentication (partially done)
- [x] Collection creation (partially done)
- [x] Collection creation
- [ ] Collection deletion
- [x] Collection retrieval and modification (partially done)
- [ ] Document insertion, deletion, and update
Expand Down

0 comments on commit f23dabc

Please sign in to comment.