-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathMySearcher.java
54 lines (38 loc) · 1.85 KB
/
MySearcher.java
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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package ai.vespa.example;
import ai.vespa.models.evaluation.FunctionEvaluator;
import ai.vespa.models.evaluation.ModelsEvaluator;
import com.yahoo.prelude.query.WordItem;
import com.yahoo.search.Query;
import com.yahoo.search.Result;
import com.yahoo.search.Searcher;
import com.yahoo.search.searchchain.Execution;
import com.yahoo.tensor.Tensor;
import com.yahoo.tensor.TensorType;
public class MySearcher extends Searcher {
private final ModelsEvaluator modelsEvaluator;
public MySearcher(ModelsEvaluator modelsEvaluator) {
this.modelsEvaluator = modelsEvaluator;
}
@Override
public Result search(Query query, Execution execution) {
// Create evaluator
FunctionEvaluator evaluator = modelsEvaluator.evaluatorOf("transformer");
// Get input
String inputString = query.properties().getString("input");
// Convert to a Vespa tensor - and reshape to model expectations
Tensor input = Tensor.from(TensorType.fromSpec("tensor<int8>(x[])"), inputString);
input = Util.renameDimension(input, "x", "d1");
input = Util.addDimension(input, "d0");
// Evaluate model - output has 3 dimensions: d0,d1, and d2
Tensor output = evaluator.bind("input", input).evaluate();
// Retrieve embedding (values in the d2 dimension) of the first token
Tensor embedding = Util.renameDimension(Util.slice(output, "d0:0,d1:0"), "d2", "x");
// Add this tensor to query
query.getRanking().getFeatures().put("query(embedding)", embedding);
// Add a query to match all documents of type "mydoc"
query.getModel().getQueryTree().setRoot(new WordItem("mydoc", "sddocname"));
// Continue processing
return execution.search(query);
}
}