-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathVectorFactory.java
More file actions
117 lines (107 loc) · 3.71 KB
/
Copy pathVectorFactory.java
File metadata and controls
117 lines (107 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package io.qdrant.client;
import com.google.common.primitives.Floats;
import io.qdrant.client.grpc.Points.DenseVector;
import io.qdrant.client.grpc.Points.Document;
import io.qdrant.client.grpc.Points.Image;
import io.qdrant.client.grpc.Points.InferenceObject;
import io.qdrant.client.grpc.Points.MultiDenseVector;
import io.qdrant.client.grpc.Points.SparseVector;
import io.qdrant.client.grpc.Points.Vector;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/** 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()
.setDense(DenseVector.newBuilder().addAllData(values).build())
.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()
.setDense(DenseVector.newBuilder().addAllData(Floats.asList(values)).build())
.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()
.setSparse(SparseVector.newBuilder().addAllValues(vector).addAllIndices(indices).build())
.build();
}
/**
* Creates a vector from a document for cloud inference.
*
* @param document The document to vectorize.
* @return A new instance of {@link Vector}
*/
public static Vector vector(Document document) {
return Vector.newBuilder().setDocument(document).build();
}
/**
* Creates a vector from an image for cloud inference.
*
* @param image The image to vectorize.
* @return A new instance of {@link Vector}
*/
public static Vector vector(Image image) {
return Vector.newBuilder().setImage(image).build();
}
/**
* Creates a vector from an inference object.
*
* @param object The inference object to vectorize.
* @return A new instance of {@link Vector}
*/
public static Vector vector(InferenceObject object) {
return Vector.newBuilder().setObject(object).build();
}
/**
* Creates a multi vector from a nested list of floats
*
* @param vectors The nested list of floats representing the multi vector.
* @return A new instance of {@link Vector}
*/
public static Vector multiVector(List<List<Float>> vectors) {
List<DenseVector> denseVectors =
vectors.stream()
.map(v -> DenseVector.newBuilder().addAllData(v).build())
.collect(Collectors.toList());
return Vector.newBuilder()
.setMultiDense(MultiDenseVector.newBuilder().addAllVectors(denseVectors).build())
.build();
}
/**
* Creates a multi vector from a nested array of floats
*
* @param vectors The nested array of floats representing the multi vector.
* @return A new instance of {@link Vector}
*/
public static Vector multiVector(float[][] vectors) {
List<DenseVector> denseVectors = new ArrayList<>();
for (float[] vector : vectors) {
denseVectors.add(DenseVector.newBuilder().addAllData(Floats.asList(vector)).build());
}
return Vector.newBuilder()
.setMultiDense(MultiDenseVector.newBuilder().addAllVectors(denseVectors).build())
.build();
}
}