Skip to content
Open
Show file tree
Hide file tree
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
319 changes: 158 additions & 161 deletions README.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
/**
* ChromaDB Client
*/
public class Client {
public class ClientImpl {
final ApiClient apiClient = new ApiClient();
private int timeout = 60;
DefaultApi api;

public Client(String basePath) {
public ClientImpl(String basePath) {
apiClient.setBasePath(basePath);
api = new DefaultApi(apiClient);
apiClient.setHttpClient(apiClient.getHttpClient().newBuilder()
Expand Down Expand Up @@ -52,15 +52,15 @@ public void setDefaultHeaders(Map<String, String> headers) {
}
}

public Collection getCollection(String collectionName, EmbeddingFunction embeddingFunction) throws ApiException {
return new Collection(api, collectionName, embeddingFunction).fetch();
public CollectionImpl getCollection(String collectionName, EmbeddingFunction embeddingFunction) throws ApiException {
return new CollectionImpl(api, collectionName, embeddingFunction).fetch();
}

public Map<String, BigDecimal> heartbeat() throws ApiException {
return api.heartbeat();
}

public Collection createCollection(String collectionName, Map<String, String> metadata, Boolean createOrGet, EmbeddingFunction embeddingFunction) throws ApiException {
public CollectionImpl createCollection(String collectionName, Map<String, String> metadata, Boolean createOrGet, EmbeddingFunction embeddingFunction) throws ApiException {
CreateCollection req = new CreateCollection();
req.setName(collectionName);
Map<String, String> _metadata = metadata;
Expand All @@ -71,17 +71,17 @@ public Collection createCollection(String collectionName, Map<String, String> me
req.setMetadata(_metadata);
req.setGetOrCreate(createOrGet);
LinkedTreeMap resp = (LinkedTreeMap) api.createCollection(req);
return new Collection(api, (String) resp.get("name"), embeddingFunction).fetch();
return new CollectionImpl(api, (String) resp.get("name"), embeddingFunction).fetch();
}

public Collection deleteCollection(String collectionName) throws ApiException {
Collection collection = Collection.getInstance(api, collectionName);
public CollectionImpl deleteCollection(String collectionName) throws ApiException {
CollectionImpl collection = CollectionImpl.getInstance(api, collectionName);
api.deleteCollection(collectionName);
return collection;
}

public Collection upsert(String collectionName, EmbeddingFunction ef) throws ApiException {
Collection collection = getCollection(collectionName, ef);
public CollectionImpl upsert(String collectionName, EmbeddingFunction ef) throws ApiException {
CollectionImpl collection = getCollection(collectionName, ef);
// collection.upsert();
return collection;
}
Expand All @@ -90,7 +90,7 @@ public Boolean reset() throws ApiException {
return api.reset();
}

public List<Collection> listCollections() throws ApiException {
public List<CollectionImpl> listCollections() throws ApiException {
List<LinkedTreeMap> apiResponse = (List<LinkedTreeMap>) api.listCollections();
return apiResponse.stream().map((LinkedTreeMap m) -> {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import static java.lang.Thread.sleep;

public class Collection {
public class CollectionImpl {
static Gson gson = new Gson();
DefaultApi api;
String collectionName;
Expand All @@ -25,7 +25,7 @@ public class Collection {

private EmbeddingFunction embeddingFunction;

public Collection(DefaultApi api, String collectionName, EmbeddingFunction embeddingFunction) {
public CollectionImpl(DefaultApi api, String collectionName, EmbeddingFunction embeddingFunction) {
this.api = api;
this.collectionName = collectionName;
this.embeddingFunction = embeddingFunction;
Expand All @@ -44,7 +44,7 @@ public Map<String, Object> getMetadata() {
return metadata;
}

public Collection fetch() throws ApiException {
public CollectionImpl fetch() throws ApiException {
try {
LinkedTreeMap<String, ?> resp = (LinkedTreeMap<String, ?>) api.getCollection(collectionName);
this.collectionName = resp.get("name").toString();
Expand All @@ -56,8 +56,8 @@ public Collection fetch() throws ApiException {
}
}

public static Collection getInstance(DefaultApi api, String collectionName) throws ApiException {
return new Collection(api, collectionName, null);
public static CollectionImpl getInstance(DefaultApi api, String collectionName) throws ApiException {
return new CollectionImpl(api, collectionName, null);
}

@Override
Expand Down
41 changes: 41 additions & 0 deletions src/main/java/tech/amikos/chromadb/WithParam.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package tech.amikos.chromadb;

import java.util.Map;

public abstract class WithParam {
public abstract void apply(Map<String, Object> params) throws ChromaException;

public static WithParam baseAPI(String baseApi) {
return new WithBaseAPI(baseApi);
}
}


class WithBaseAPI extends WithParam {
private final String baseAPI;

public WithBaseAPI(String baseAPI) {
this.baseAPI = baseAPI;
}

@Override
public void apply(Map<String, Object> params) {
params.put(Constants.EF_PARAMS_BASE_API, baseAPI);
}
}

class WithTimeout extends WithParam {
private final int timeout;

public WithTimeout(int timeout) {
this.timeout = timeout;
}

@Override
public void apply(Map<String, Object> params) throws ChromaException {
if (timeout < 0) {
throw new ChromaException("Timeout must be a positive integer");
}
// params.put(Constants.EF_PARAMS_TIMEOUT, timeout);
}
}
14 changes: 14 additions & 0 deletions src/main/java/tech/amikos/chromadb/api/AddRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package tech.amikos.chromadb.api;

public class AddRequest implements RequestValidator {

@Override
public void validate() throws Exception {
//TODO
}

public static class Builder {

client.createCollection(name, WithMetadata.W
}
}
29 changes: 29 additions & 0 deletions src/main/java/tech/amikos/chromadb/api/ApiClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package tech.amikos.chromadb.api;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;

public class ApiClient {
private OkHttpClient httpClient;
private boolean debugging = false;
private HttpLoggingInterceptor loggingInterceptor;

public ApiClient() {
this.httpClient = new OkHttpClient();
}

public ApiClient setDebugging(boolean debugging) {
if (debugging != this.debugging) {
if (debugging) {
loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
httpClient.interceptors().add(loggingInterceptor);
} else {
httpClient.interceptors().remove(loggingInterceptor);
loggingInterceptor = null;
}
}
this.debugging = debugging;
return this;
}
}
4 changes: 4 additions & 0 deletions src/main/java/tech/amikos/chromadb/api/AuthProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package tech.amikos.chromadb.api;

public interface AuthProvider {
}
4 changes: 4 additions & 0 deletions src/main/java/tech/amikos/chromadb/api/Client.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package tech.amikos.chromadb.api;

public interface Client {
}
80 changes: 80 additions & 0 deletions src/main/java/tech/amikos/chromadb/api/Collection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package tech.amikos.chromadb.api;

public interface Collection {
/**
* The name of the collection.
* @return the name of the collection
*/
String name();

/**
* The unique identifier of the collection.
* @return the unique identifier of the collection
*/
String id();

/**
* The name of the tenant that owns the collection.
* @return the name of the tenant that owns the collection
*/
String tenant();

/**
* The name of the database that contains the collection.
* @return the name of the database that contains the collection
*/
String database();

/**
* The metadata of the collection.
* @return the metadata of the collection
*/
CollectionMetadata metadata();

/**
* The configuration of the collection.
* @return the configuration of the collection
*/
CollectionConfiguration configuration();

/**
* Adds a record to the collection.
*/
void add();

/**
* Upserts a record in the collection.
*/
void upsert();

/**
* Updates a record in the collection.
*/
void update();

/**
* Deletes a record from the collection.
*/
void delete();

/**
* Counts the number of records in the collection.
* @return the number of records in the collection
*/
long count();

/**
* Renames the collection.
* @param newName the new name of the collection
*/
void rename(String newName);

/**
* Updates the metadata of the collection.
* @param newMetadata the new metadata of the collection
*/
void updateMetadata(CollectionMetadata newMetadata);

void get();
void query();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package tech.amikos.chromadb.api;

public interface CollectionConfiguration {
}
96 changes: 96 additions & 0 deletions src/main/java/tech/amikos/chromadb/api/CollectionMetadata.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package tech.amikos.chromadb.api;

import java.util.HashMap;
import java.util.Map;

public interface CollectionMetadata {
String getString(String key);
Integer getInt(String key);
Float getFloat(String key);
Boolean getBoolean(String key);
Map<String,Object> getAsMap(); //TODO immutable map or clone of the original

class CollectionMetadataImpl implements CollectionMetadata{
private final Map<String, Object> metadata;

CollectionMetadataImpl() {
this.metadata = new HashMap<>();
}

void put(String key, String value){
metadata.put(key, value);
}

void put(String key, int value){
metadata.put(key, value);
}

void put(String key, float value){
metadata.put(key, value);
}

void put(String key, boolean value){
metadata.put(key, value);
}

@Override
public String getString(String key) {
return (String) metadata.get(key);
}

@Override
public Integer getInt(String key) {
return (Integer) metadata.get(key);
}

@Override
public Float getFloat(String key) {
return (Float) metadata.get(key);
}

@Override
public Boolean getBoolean(String key) {
return (Boolean) metadata.get(key);
}

@Override
public Map<String, Object> getAsMap() {
return metadata;
}
}

class Builder{
private final CollectionMetadataImpl metadata;

private Builder(){
this.metadata = new CollectionMetadataImpl();
}
public static Builder New() {
return new Builder();
}

public Builder put(String key, String value){
this.metadata.put(key, value);
return this;
}

public Builder put(String key, int value){
this.metadata.put(key, value);
return this;
}

public Builder put(String key, float value){
this.metadata.put(key, value);
return this;
}

public Builder put(String key, boolean value){
this.metadata.put(key, value);
return this;
}

public CollectionMetadata build(){
return metadata;
}
}
}
4 changes: 4 additions & 0 deletions src/main/java/tech/amikos/chromadb/api/Document.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package tech.amikos.chromadb.api;

public interface Document {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package tech.amikos.chromadb.api;

public interface DocumentMetadata {
}
Loading
Loading