Skip to content

Commit

Permalink
feat: targetVectorFactory, VectorFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
Anush008 committed Dec 15, 2023
1 parent 00aa674 commit 77aecbf
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 2 deletions.
32 changes: 32 additions & 0 deletions src/main/java/io/qdrant/client/TargetVectorFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.qdrant.client;

import io.qdrant.client.grpc.Points.PointId;
import io.qdrant.client.grpc.Points.TargetVector;
import io.qdrant.client.grpc.Points.Vector;
import io.qdrant.client.grpc.Points.VectorExample;

/**
* Convenience methods for constructing {@link TargetVector}
*/
public class TargetVectorFactory {
private TargetVectorFactory() {
}

/**
* Creates a TargetVector from a point ID
* @param id The point ID to use
* @return A new instance of {@link TargetVector}
*/
public static TargetVector targetVector(PointId id) {
return TargetVector.newBuilder().setSingle(VectorExample.newBuilder().setId(id)).build();
}

/**
* Creates a TargetVector from a Vector
* @param vector The Vector value to use
* @return A new instance of {@link TargetVector}
*/
public static TargetVector targetVector(Vector vector) {
return TargetVector.newBuilder().setSingle(VectorExample.newBuilder().setVector(vector)).build();
}
}
52 changes: 52 additions & 0 deletions src/main/java/io/qdrant/client/VectorFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package io.qdrant.client;

import com.google.common.primitives.Floats;

import java.util.List;

import static io.qdrant.client.grpc.Points.SparseIndices;
import static io.qdrant.client.grpc.Points.Vector;

/**
* Convenience methods for constructing {@link Vector}
*/
public final class VectorFactory {
private VectorFactory() {
}

/**
* Creates a vector from a list of floats
*
* @param values A map of vector names to values
* @return A new instance of {@link Vector}
*/
public static Vector vector(List<Float> values) {
return Vector.newBuilder().addAllData(values).build();
}

/**
* Creates a vector from a list of floats
*
* @param values A list of values
* @return A new instance of {@link Vector}
*/
public static Vector vector(float... values) {
return Vector.newBuilder()
.addAllData(Floats.asList(values))
.build();
}

/**
* Creates a sparse vector from a list of floats and integers as indices
*
* @param vector The list of floats representing the vector.
* @param indices The list of integers representing the indices.
* @return A new instance of {@link Vector}
*/
public static Vector vector(List<Float> vector, List<Integer> indices) {
return Vector.newBuilder()
.addAllData(vector)
.setIndices(SparseIndices.newBuilder().addAllData(indices).build())
.build();
}
}
4 changes: 2 additions & 2 deletions src/main/java/io/qdrant/client/VectorsFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static Vectors namedVectors(Map<String, List<Float>> values) {
* @param values A list of values
* @return a new instance of {@link Vectors}
*/
public static Vectors vector(List<Float> values) {
public static Vectors vectors(List<Float> values) {
return Vectors.newBuilder()
.setVector(Vector.newBuilder()
.addAllData(values)
Expand All @@ -50,7 +50,7 @@ public static Vectors vector(List<Float> values) {
* @param values A list of values
* @return a new instance of {@link Vectors}
*/
public static Vectors vector(float... values) {
public static Vectors vectors(float... values) {
return Vectors.newBuilder()
.setVector(Vector.newBuilder()
.addAllData(Floats.asList(values))
Expand Down

0 comments on commit 77aecbf

Please sign in to comment.