aboutsummaryrefslogtreecommitdiffstats
path: root/model-evaluation/src/main/java/ai/vespa/models/handler/ModelsEvaluationHandler.java
blob: b0e2be26f8aae2d68b4c987c54d425c729b1be4f (plain) (blame)
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package ai.vespa.models.handler;

import ai.vespa.models.evaluation.FunctionEvaluator;
import ai.vespa.models.evaluation.Model;
import ai.vespa.models.evaluation.ModelsEvaluator;
import com.yahoo.container.jdisc.HttpRequest;
import com.yahoo.container.jdisc.HttpResponse;
import com.yahoo.container.jdisc.ThreadedHttpRequestHandler;
import com.yahoo.searchlib.rankingexpression.ExpressionFunction;
import com.yahoo.slime.Cursor;
import com.yahoo.slime.Slime;
import com.yahoo.tensor.Tensor;
import com.yahoo.tensor.TensorType;
import com.yahoo.tensor.serialization.JsonFormat;
import com.yahoo.yolean.Exceptions;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.Executor;

public class ModelsEvaluationHandler extends ThreadedHttpRequestHandler {

    /** A dash in this key ensures it does not collide with feature names */
    private static final String missingValueKey = "missing-value";

    public static final String API_ROOT = "model-evaluation";
    public static final String VERSION_V1 = "v1";
    public static final String EVALUATE = "eval";

    private final ModelsEvaluator modelsEvaluator;

    public ModelsEvaluationHandler(ModelsEvaluator modelsEvaluator, Executor executor) {
        super(executor);
        this.modelsEvaluator = modelsEvaluator;
    }

    @Override
    public HttpResponse handle(HttpRequest request) {
        Path path = new Path(request);
        Optional<String> apiName = path.segment(0);
        Optional<String> version = path.segment(1);
        Optional<String> modelName = path.segment(2);

        try {
            if ( apiName.isEmpty() || ! apiName.get().equalsIgnoreCase(API_ROOT))
                throw new IllegalArgumentException("unknown API");
            if ( version.isEmpty() || ! version.get().equalsIgnoreCase(VERSION_V1))
                throw new IllegalArgumentException("unknown API version");
            if ( modelName.isEmpty())
                return listAllModels(request);

            Model model = modelsEvaluator.requireModel(modelName.get());

            Optional<Integer> evalSegment = path.lastIndexOf(EVALUATE);
            String[] function = path.range(3, evalSegment);
            if (evalSegment.isPresent()) {
                return evaluateModel(request, model, function);
            }
            return listModelInformation(request, model, function);

        } catch (IllegalArgumentException e) {
            return new ErrorResponse(404, Exceptions.toMessageString(e));
        } catch (IllegalStateException e) { // On missing bindings
            return new ErrorResponse(400, Exceptions.toMessageString(e));
        }
    }

    private HttpResponse evaluateModel(HttpRequest request, Model model, String[] function)  {
        FunctionEvaluator evaluator = model.evaluatorOf(function);

        property(request, missingValueKey).ifPresent(missingValue -> evaluator.setMissingValue(Tensor.from(missingValue)));

        for (Map.Entry<String, TensorType> argument : evaluator.function().argumentTypes().entrySet()) {
            Optional<String> value = property(request, argument.getKey());
            if (value.isPresent()) {
                try {
                    evaluator.bind(argument.getKey(), Tensor.from(argument.getValue(), value.get()));
                } catch (IllegalArgumentException e) {
                    evaluator.bind(argument.getKey(), value.get());  // since we don't yet support tensors with string values
                }
            }
        }
        Tensor result = evaluator.evaluate();

        Optional<String> format = property(request, "format.tensors");
        if (format.isPresent() && format.get().equalsIgnoreCase("short")) {
            return new Response(200, JsonFormat.encodeShortForm(result));
        }
        else if (format.isPresent() && format.get().equalsIgnoreCase("string")) {
            return new Response(200, result.toString().getBytes(StandardCharsets.UTF_8));
        }
        return new Response(200, JsonFormat.encode(result));
    }

    private HttpResponse listAllModels(HttpRequest request) {
        Slime slime = new Slime();
        Cursor root = slime.setObject();
        for (String modelName: modelsEvaluator.models().keySet()) {
            root.setString(modelName, baseUrl(request) + modelName);
        }
        return new Response(200, com.yahoo.slime.JsonFormat.toJsonBytes(slime));
    }

    private HttpResponse listModelInformation(HttpRequest request, Model model, String[] function) {
        Slime slime = new Slime();
        Cursor root = slime.setObject();
        root.setString("model", model.name());
        if (function.length == 0) {
            listFunctions(request, model, root);
        } else {
            listFunctionDetails(request, model, function, root);
        }
        return new Response(200, com.yahoo.slime.JsonFormat.toJsonBytes(slime));
    }

    private void listFunctions(HttpRequest request, Model model, Cursor cursor) {
        Cursor functions = cursor.setArray("functions");
        for (ExpressionFunction func : model.functions()) {
            Cursor function = functions.addObject();
            listFunctionDetails(request, model, new String[] { func.getName() }, function);
        }
    }

    private void listFunctionDetails(HttpRequest request, Model model, String[] function, Cursor cursor) {
        String compactedFunction = String.join(".", function);
        FunctionEvaluator evaluator = model.evaluatorOf(function);
        cursor.setString("function", compactedFunction);
        cursor.setString("info", baseUrl(request) + model.name() + "/" + compactedFunction);
        cursor.setString("eval", baseUrl(request) + model.name() + "/" + compactedFunction + "/" + EVALUATE);
        Cursor bindings = cursor.setArray("arguments");
        for (Map.Entry<String, TensorType> argument : evaluator.function().argumentTypes().entrySet()) {
            Cursor binding = bindings.addObject();
            binding.setString("name", argument.getKey());
            binding.setString("type", argument.getValue().toString());
        }
    }

    private Optional<String> property(HttpRequest request, String name) {
        return Optional.ofNullable(request.getProperty(name));
    }

    private String baseUrl(HttpRequest request) {
       URI uri = request.getUri();
       StringBuilder sb = new StringBuilder();
       sb.append(uri.getScheme()).append("://");
       if (request.getHeader("Host") != null) {
           sb.append(request.getHeader("Host"));
       } else {
           sb.append(uri.getHost());
           if (uri.getPort() >= 0) {
               sb.append(":").append(uri.getPort());
           }
       }
       sb.append("/").append(API_ROOT).append("/").append(VERSION_V1).append("/");
       return sb.toString();
    }

    private static class Path {

        private final String[] segments;

        public Path(HttpRequest httpRequest) {
            segments = splitPath(httpRequest);
        }

        Optional<String> segment(int index) {
            return (index < 0 || index >= segments.length) ? Optional.empty() : Optional.of(segments[index]);
        }

        Optional<Integer> lastIndexOf(String segment) {
            for (int i = segments.length - 1; i >= 0; --i) {
                if (segments[i].equalsIgnoreCase(segment)) {
                    return Optional.of(i);
                }
            }
            return Optional.empty();
        }

        public String[] range(int start, Optional<Integer> end) {
            return Arrays.copyOfRange(segments, start, end.isPresent() ? end.get() : segments.length);
        }

        private static String[] splitPath(HttpRequest request) {
            String path = request.getUri().getPath().toLowerCase();
            if (path.startsWith("/")) {
                path = path.substring("/".length());
            }
            if (path.endsWith("/")) {
                path = path.substring(0, path.length() - 1);
            }
            return path.split("/");
        }

    }

    private static class Response extends HttpResponse {

        private final byte[] data;

        Response(int code, byte[] data) {
            super(code);
            this.data = data;
        }

        Response(int code, String data) {
            this(code, data.getBytes(Charset.forName(DEFAULT_CHARACTER_ENCODING)));
        }

        @Override
        public String getContentType() {
            return "application/json";
        }

        @Override
        public void render(OutputStream outputStream) throws IOException {
            outputStream.write(data);
        }
    }

    private static class ErrorResponse extends Response {
        ErrorResponse(int code, String data) {
            super(code, "{\"error\":\"" + data + "\"}");
        }
    }

}