Skip to content

spike(v6): add factory methods to Vectorizer namespace #391

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: v6
Choose a base branch
from
Draft
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
5 changes: 2 additions & 3 deletions src/it/java/io/weaviate/integration/AggregationITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@
import io.weaviate.ConcurrentTest;
import io.weaviate.client6.v1.api.WeaviateClient;
import io.weaviate.client6.v1.api.collections.Property;
import io.weaviate.client6.v1.api.collections.Vectorizer;
import io.weaviate.client6.v1.api.collections.Vectors;
import io.weaviate.client6.v1.api.collections.aggregate.AggregateResponseGroup;
import io.weaviate.client6.v1.api.collections.aggregate.AggregateResponseGrouped;
import io.weaviate.client6.v1.api.collections.aggregate.Aggregation;
import io.weaviate.client6.v1.api.collections.aggregate.GroupBy;
import io.weaviate.client6.v1.api.collections.aggregate.GroupedBy;
import io.weaviate.client6.v1.api.collections.aggregate.IntegerAggregation;
import io.weaviate.client6.v1.api.collections.vectorindex.Hnsw;
import io.weaviate.client6.v1.api.collections.vectorizers.NoneVectorizer;
import io.weaviate.containers.Container;

public class AggregationITest extends ConcurrentTest {
Expand All @@ -36,7 +35,7 @@ public static void beforeAll() throws IOException {
.properties(
Property.text("category"),
Property.integer("price"))
.vector(Hnsw.of(new NoneVectorizer())));
.vectors(Vectorizer.none()));

var things = client.collections.use(COLLECTION);
for (var category : List.of("Shoes", "Hat", "Jacket")) {
Expand Down
3 changes: 2 additions & 1 deletion src/it/java/io/weaviate/integration/CollectionsITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import io.weaviate.client6.v1.api.WeaviateClient;
import io.weaviate.client6.v1.api.collections.Property;
import io.weaviate.client6.v1.api.collections.VectorIndex;
import io.weaviate.client6.v1.api.collections.Vectorizer;
import io.weaviate.client6.v1.api.collections.WeaviateCollection;
import io.weaviate.client6.v1.api.collections.vectorindex.Hnsw;
import io.weaviate.client6.v1.api.collections.vectorizers.NoneVectorizer;
Expand All @@ -24,7 +25,7 @@ public void testCreateGetDelete() throws IOException {
client.collections.create(collectionName,
col -> col
.properties(Property.text("username"), Property.integer("age"))
.vector(Hnsw.of(new NoneVectorizer())));
.vectors(Vectorizer.none()));

var thingsCollection = client.collections.getConfig(collectionName);

Expand Down
5 changes: 2 additions & 3 deletions src/it/java/io/weaviate/integration/DataITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@
import io.weaviate.ConcurrentTest;
import io.weaviate.client6.v1.api.WeaviateClient;
import io.weaviate.client6.v1.api.collections.Property;
import io.weaviate.client6.v1.api.collections.Vectorizer;
import io.weaviate.client6.v1.api.collections.Vectors;
import io.weaviate.client6.v1.api.collections.WeaviateObject;
import io.weaviate.client6.v1.api.collections.query.Metadata;
import io.weaviate.client6.v1.api.collections.vectorindex.Hnsw;
import io.weaviate.client6.v1.api.collections.vectorizers.NoneVectorizer;
import io.weaviate.containers.Container;

public class DataITest extends ConcurrentTest {
Expand Down Expand Up @@ -99,6 +98,6 @@ private static void createTestCollections() throws IOException {
Property.integer("age"))
.references(
Property.reference("hasAwards", awardsGrammy, awardsOscar))
.vectors(named -> named.vector(VECTOR_INDEX, Hnsw.of(new NoneVectorizer()))));
.vectors(Vectorizer.none(VECTOR_INDEX)));
}
}
20 changes: 8 additions & 12 deletions src/it/java/io/weaviate/integration/SearchITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,14 @@
import io.weaviate.ConcurrentTest;
import io.weaviate.client6.v1.api.WeaviateClient;
import io.weaviate.client6.v1.api.collections.Property;
import io.weaviate.client6.v1.api.collections.Vectorizer;
import io.weaviate.client6.v1.api.collections.Vectors;
import io.weaviate.client6.v1.api.collections.WeaviateObject;
import io.weaviate.client6.v1.api.collections.data.Reference;
import io.weaviate.client6.v1.api.collections.query.GroupBy;
import io.weaviate.client6.v1.api.collections.query.Metadata;
import io.weaviate.client6.v1.api.collections.query.QueryResponseGroup;
import io.weaviate.client6.v1.api.collections.query.Where;
import io.weaviate.client6.v1.api.collections.vectorindex.Hnsw;
import io.weaviate.client6.v1.api.collections.vectorizers.Img2VecNeuralVectorizer;
import io.weaviate.client6.v1.api.collections.vectorizers.NoneVectorizer;
import io.weaviate.client6.v1.api.collections.vectorizers.Text2VecContextionaryVectorizer;
import io.weaviate.containers.Container;
import io.weaviate.containers.Container.ContainerGroup;
import io.weaviate.containers.Contextionary;
Expand Down Expand Up @@ -130,7 +127,7 @@ private static Map<String, Float[]> populateTest(int n) throws IOException {
private static void createTestCollection() throws IOException {
client.collections.create(COLLECTION, cfg -> cfg
.properties(Property.text("category"))
.vector(VECTOR_INDEX, Hnsw.of(new NoneVectorizer())));
.vectors(Vectorizer.none(VECTOR_INDEX)));
}

@Test
Expand All @@ -139,7 +136,7 @@ public void testNearText() throws IOException {
client.collections.create(nsSongs,
col -> col
.properties(Property.text("title"))
.vector(Hnsw.of(Text2VecContextionaryVectorizer.of())));
.vectors(Vectorizer.text2VecContextionary()));

var songs = client.collections.use(nsSongs);
var submarine = songs.data.insert(Map.of("title", "Yellow Submarine"));
Expand All @@ -161,13 +158,13 @@ public void testNearText() throws IOException {

@Test
public void testNearText_groupBy() throws IOException {
var vectorIndex = Hnsw.of(Text2VecContextionaryVectorizer.of());
var vectorIndex = Vectorizer.text2VecContextionary();

var nsArtists = ns("Artists");
client.collections.create(nsArtists,
col -> col
.properties(Property.text("name"))
.vector(vectorIndex));
.vectors(vectorIndex));

var artists = client.collections.use(nsArtists);
var beatles = artists.data.insert(Map.of("name", "Beatles"));
Expand All @@ -178,7 +175,7 @@ public void testNearText_groupBy() throws IOException {
col -> col
.properties(Property.text("title"))
.references(Property.reference("performedBy", nsArtists))
.vector(vectorIndex));
.vectors(vectorIndex));

var songs = client.collections.use(nsSongs);
songs.data.insert(Map.of("title", "Yellow Submarine"),
Expand All @@ -205,9 +202,8 @@ public void testNearImage() throws IOException {
.properties(
Property.text("breed"),
Property.blob("img"))
.vector(Hnsw.of(
Img2VecNeuralVectorizer.of(
i2v -> i2v.imageFields("img")))));
.vectors(Vectorizer.img2vecNeural(
i2v -> i2v.imageFields("img"))));

var cats = client.collections.use(nsCats);
cats.data.insert(Map.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.IOException;
import java.util.EnumMap;
import java.util.Map;
import java.util.function.Function;

import com.google.gson.Gson;
import com.google.gson.JsonParser;
Expand All @@ -18,6 +19,7 @@
import io.weaviate.client6.v1.internal.json.JsonEnum;

public interface VectorIndex {
static final Function<Vectorizer, VectorIndex> DEFAULT_VECTOR_INDEX = Hnsw::of;
static final String DEFAULT_VECTOR_NAME = "default";

public enum Kind implements JsonEnum<Kind> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.IOException;
import java.util.EnumMap;
import java.util.Map;
import java.util.function.Function;

import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
Expand All @@ -17,6 +18,7 @@
import io.weaviate.client6.v1.api.collections.vectorizers.NoneVectorizer;
import io.weaviate.client6.v1.api.collections.vectorizers.Text2VecContextionaryVectorizer;
import io.weaviate.client6.v1.api.collections.vectorizers.Text2VecWeaviateVectorizer;
import io.weaviate.client6.v1.internal.ObjectBuilder;
import io.weaviate.client6.v1.internal.json.JsonEnum;

public interface Vectorizer {
Expand Down Expand Up @@ -48,6 +50,84 @@ public static Kind valueOfJson(String jsonValue) {

Object _self();

public static Map.Entry<String, VectorIndex> none() {
return Map.entry(VectorIndex.DEFAULT_VECTOR_NAME,
VectorIndex.DEFAULT_VECTOR_INDEX.apply(new NoneVectorizer()));
}

public static Map.Entry<String, VectorIndex> none(String vectorName) {
return Map.entry(vectorName,
VectorIndex.DEFAULT_VECTOR_INDEX.apply(new NoneVectorizer()));
}

public static Map.Entry<String, VectorIndex> text2vecWeaviate() {
return Map.entry(VectorIndex.DEFAULT_VECTOR_NAME,
VectorIndex.DEFAULT_VECTOR_INDEX.apply(Text2VecWeaviateVectorizer.of()));
}

public static Map.Entry<String, VectorIndex> text2vecWeaviate(
Function<Text2VecWeaviateVectorizer.Builder, ObjectBuilder<Text2VecWeaviateVectorizer>> fn) {
return Map.entry(VectorIndex.DEFAULT_VECTOR_NAME,
VectorIndex.DEFAULT_VECTOR_INDEX.apply(Text2VecWeaviateVectorizer.of(fn)));
}

public static Map.Entry<String, VectorIndex> text2vecWeaviate(String vectorName,
Function<Text2VecWeaviateVectorizer.Builder, ObjectBuilder<Text2VecWeaviateVectorizer>> fn) {
return Map.entry(vectorName,
VectorIndex.DEFAULT_VECTOR_INDEX.apply(Text2VecWeaviateVectorizer.of(fn)));
}

public static Map.Entry<String, VectorIndex> text2VecContextionary() {
return Map.entry(VectorIndex.DEFAULT_VECTOR_NAME,
VectorIndex.DEFAULT_VECTOR_INDEX.apply(Text2VecContextionaryVectorizer.of()));
}

public static Map.Entry<String, VectorIndex> text2VecContextionary(
Function<Text2VecContextionaryVectorizer.Builder, ObjectBuilder<Text2VecContextionaryVectorizer>> fn) {
return Map.entry(VectorIndex.DEFAULT_VECTOR_NAME,
VectorIndex.DEFAULT_VECTOR_INDEX.apply(Text2VecContextionaryVectorizer.of(fn)));
}

public static Map.Entry<String, VectorIndex> text2VecContextionary(String vectorName,
Function<Text2VecContextionaryVectorizer.Builder, ObjectBuilder<Text2VecContextionaryVectorizer>> fn) {
return Map.entry(vectorName,
VectorIndex.DEFAULT_VECTOR_INDEX.apply(Text2VecContextionaryVectorizer.of(fn)));
}

public static Map.Entry<String, VectorIndex> multi2vecClip() {
return Map.entry(VectorIndex.DEFAULT_VECTOR_NAME,
VectorIndex.DEFAULT_VECTOR_INDEX.apply(Multi2VecClipVectorizer.of()));
}

public static Map.Entry<String, VectorIndex> multi2vecClip(
Function<Multi2VecClipVectorizer.Builder, ObjectBuilder<Multi2VecClipVectorizer>> fn) {
return Map.entry(VectorIndex.DEFAULT_VECTOR_NAME,
VectorIndex.DEFAULT_VECTOR_INDEX.apply(Multi2VecClipVectorizer.of(fn)));
}

public static Map.Entry<String, VectorIndex> multi2vecClip(String vectorName,
Function<Multi2VecClipVectorizer.Builder, ObjectBuilder<Multi2VecClipVectorizer>> fn) {
return Map.entry(vectorName,
VectorIndex.DEFAULT_VECTOR_INDEX.apply(Multi2VecClipVectorizer.of(fn)));
}

public static Map.Entry<String, VectorIndex> img2vecNeural() {
return Map.entry(VectorIndex.DEFAULT_VECTOR_NAME,
VectorIndex.DEFAULT_VECTOR_INDEX.apply(Img2VecNeuralVectorizer.of()));
}

public static Map.Entry<String, VectorIndex> img2vecNeural(
Function<Img2VecNeuralVectorizer.Builder, ObjectBuilder<Img2VecNeuralVectorizer>> fn) {
return Map.entry(VectorIndex.DEFAULT_VECTOR_NAME,
VectorIndex.DEFAULT_VECTOR_INDEX.apply(Img2VecNeuralVectorizer.of(fn)));
}

public static Map.Entry<String, VectorIndex> img2vecNeural(String vectorName,
Function<Img2VecNeuralVectorizer.Builder, ObjectBuilder<Img2VecNeuralVectorizer>> fn) {
return Map.entry(vectorName,
VectorIndex.DEFAULT_VECTOR_INDEX.apply(Img2VecNeuralVectorizer.of(fn)));
}

public static enum CustomTypeAdapterFactory implements TypeAdapterFactory {
INSTANCE;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,20 +81,29 @@ public Builder references(List<ReferenceProperty> references) {
return this;
}

public Builder vector(VectorIndex vector) {
this.vectors.put(VectorIndex.DEFAULT_VECTOR_NAME, vector);
return this;
}

public Builder vector(String name, VectorIndex vector) {
this.vectors.put(name, vector);
@SafeVarargs
public final Builder vectors(Map.Entry<String, VectorIndex>... vectors) {
for (final var vector : vectors) {
this.vectors.put(vector.getKey(), vector.getValue());
}
return this;
}

public Builder vectors(Function<VectorsBuilder, ObjectBuilder<Map<String, VectorIndex>>> fn) {
this.vectors = fn.apply(new VectorsBuilder()).build();
return this;
}
// public Builder vector(VectorIndex vector) {
// this.vectors.put(VectorIndex.DEFAULT_VECTOR_NAME, vector);
// return this;
// }
//
// public Builder vector(String name, VectorIndex vector) {
// this.vectors.put(name, vector);
// return this;
// }
//
// public Builder vectors(Function<VectorsBuilder, ObjectBuilder<Map<String,
// VectorIndex>>> fn) {
// this.vectors = fn.apply(new VectorsBuilder()).build();
// return this;
// }

public static class VectorsBuilder implements ObjectBuilder<Map<String, VectorIndex>> {
private Map<String, VectorIndex> vectors = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,8 @@ public static Object[][] testCases() {
Property.integer("size"))
.references(
Property.reference("owner", "Person", "Company"))
.vectors(named -> named
.vector("v-shape", Hnsw.of(Img2VecNeuralVectorizer.of(
i2v -> i2v.imageFields("img")))))),
.vectors(Vectorizer.img2vecNeural("v-shape",
i2v -> i2v.imageFields("img")))),
"""
{
"class": "Things",
Expand Down Expand Up @@ -229,7 +228,7 @@ public static Object[][] testCases() {
"{\"beacon\": \"weaviate://localhost/Doodlebops/id-1\"}",
},

// WeaviateObject.CustomTypeAdapterFactory.INSTANCE
// WeaviateObject.CustomTypeAdapterFactory
{
new TypeToken<WeaviateObject<Map<String, Object>, Reference, ObjectMetadata>>() {
},
Expand Down