Skip to content

Commit

Permalink
feat(chromadb)!: Multi-modal and tenants support (#210)
Browse files Browse the repository at this point in the history
New features:
- tenants and databases support
- global headers (e.g. for authentication)
- multi-modal support
  • Loading branch information
davidmigloz committed Nov 12, 2023
1 parent ae5b8bc commit bfb0d89
Show file tree
Hide file tree
Showing 31 changed files with 5,215 additions and 1,619 deletions.
3 changes: 3 additions & 0 deletions packages/chromadb/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ targets:
- prefer_final_parameters
- require_trailing_commas
- non_constant_identifier_names
json_serializable:
options:
explicit_to_json: true
4 changes: 3 additions & 1 deletion packages/chromadb/lib/chromadb.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ library;

export 'src/chroma_client.dart';
export 'src/collection.dart';
export 'src/embeddings/embedding_function.dart';
export 'src/generated/client.dart' show ChromaApiClientException;
export 'src/generated/schema/schema.dart'
show CollectionType, GetEmbedding, GetResponse, QueryResponse;
show CollectionType, GetResponse, QueryResponse;
export 'src/loaders/data_loader.dart';
81 changes: 64 additions & 17 deletions packages/chromadb/lib/src/chroma_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,67 @@ import 'collection.dart';
import 'embeddings/embedding_function.dart';
import 'generated/client.dart';
import 'generated/schema/schema.dart';
import 'loaders/data_loader.dart';

/// {@template chroma_client}
/// Chroma open-source embedding database API client.
/// {@endtemplate}
class ChromaClient {
/// {@macro chroma_client}
ChromaClient({
final String host = 'http://localhost:8000',
this.tenant = 'default_tenant',
this.database = 'default_database',
final String baseUrl = 'http://localhost:8000',
final Map<String, String>? headers,
final http.Client? client,
}) : _api = ChromaApiClient(host: host, client: client);
}) : _api = ChromaApiClient(
baseUrl: baseUrl,
headers: headers ?? const {},
client: client,
);

/// The API client.
final ChromaApiClient _api;

/// Resets the state of the object by making an API call to the reset
/// endpoint.
Future<bool> reset() {
return _api.reset();
}
/// The tenant name.
final String tenant;

/// Returns the version of the Chroma API.
Future<String> version() {
return _api.version();
}
/// The database name.
final String database;

/// Returns a heartbeat from the Chroma API.
/// Get the current time in nanoseconds since epoch.
/// Used to check if the server is alive.
/// Returns the current time in nanoseconds since epoch.
Future<int> heartbeat() async {
final res = await _api.heartbeat();
return res['nanosecond heartbeat'] ?? 0;
}

/// Lists all collections.
Future<List<CollectionType>> listCollections() {
return _api.listCollections(
tenant: tenant,
database: database,
);
}

/// Creates a new collection with the specified properties.
///
/// - [name] - The name of the collection.
/// - [metadata] - Optional metadata associated with the collection.
/// - [embeddingFunction] - Optional custom embedding function for the
/// collection.
/// - [dataLoader] - Optional data loader for the collection. It is used to
/// load the data from a stored URI when needed.
Future<Collection> createCollection({
required final String name,
final Map<String, dynamic>? metadata,
final EmbeddingFunction? embeddingFunction,
final DataLoader<Loadable>? dataLoader,
}) async {
final res = await _api.createCollection(
tenant: tenant,
database: database,
request: CreateCollection(
name: name,
metadata: metadata,
Expand All @@ -56,7 +74,10 @@ class ChromaClient {
name: res.name,
id: res.id,
metadata: res.metadata,
tenant: tenant,
database: database,
embeddingFunction: embeddingFunction,
dataLoader: dataLoader,
api: _api,
);
}
Expand All @@ -66,12 +87,18 @@ class ChromaClient {
/// - [name] - The name of the collection.
/// - [metadata] - Optional metadata associated with the collection.
/// - [embeddingFunction] - Optional custom embedding function for the
/// collection.
/// - [dataLoader] - Optional data loader for the collection. It is used to
/// load the data from a stored URI when needed.
Future<Collection> getOrCreateCollection({
required final String name,
final Map<String, dynamic>? metadata,
final EmbeddingFunction? embeddingFunction,
final DataLoader<Loadable>? dataLoader,
}) async {
final res = await _api.createCollection(
tenant: tenant,
database: database,
request: CreateCollection(
name: name,
metadata: metadata,
Expand All @@ -82,33 +109,39 @@ class ChromaClient {
name: res.name,
id: res.id,
metadata: res.metadata,
tenant: tenant,
database: database,
embeddingFunction: embeddingFunction,
dataLoader: dataLoader,
api: _api,
);
}

/// Lists all collections.
Future<List<CollectionType>> listCollections() {
return _api.listCollections();
}

/// Gets a collection with the specified name.
///
/// - [name] - The name of the collection.
/// - [embeddingFunction] - Optional custom embedding function for the
/// collection.
/// - [dataLoader] - Optional data loader for the collection. It is used to
/// load the data from a stored URI when needed.
Future<Collection> getCollection({
required final String name,
final EmbeddingFunction? embeddingFunction,
final DataLoader<Loadable>? dataLoader,
}) async {
final res = await _api.getCollection(
collectionName: name,
tenant: tenant,
database: database,
);
return Collection(
name: res.name,
id: res.id,
metadata: res.metadata,
tenant: tenant,
database: database,
embeddingFunction: embeddingFunction,
dataLoader: dataLoader,
api: _api,
);
}
Expand All @@ -121,6 +154,20 @@ class ChromaClient {
}) async {
await _api.deleteCollection(
collectionName: name,
tenant: tenant,
database: database,
);
}

/// Resets the database. This will delete all collections and entries.
/// Returns true if the database was reset successfully.
Future<bool> reset() {
return _api.reset();
}

/// Returns the version of the Chroma API.
Future<String> version() async {
final version = await _api.version();
return version.replaceAll('"', '');
}
}

0 comments on commit bfb0d89

Please sign in to comment.