|
22 | 22 | import org.elasticsearch.xpack.inference.services.custom.CustomTaskSettings;
|
23 | 23 | import org.elasticsearch.xpack.inference.services.custom.QueryParameters;
|
24 | 24 | import org.elasticsearch.xpack.inference.services.custom.response.ErrorResponseParser;
|
| 25 | +import org.elasticsearch.xpack.inference.services.custom.response.RerankResponseParser; |
25 | 26 | import org.elasticsearch.xpack.inference.services.custom.response.TextEmbeddingResponseParser;
|
26 | 27 | import org.elasticsearch.xpack.inference.services.settings.RateLimitSettings;
|
27 | 28 | import org.elasticsearch.xpack.inference.services.settings.SerializableSecureString;
|
@@ -87,6 +88,55 @@ public void testCreateRequest() throws IOException {
|
87 | 88 | assertThat(convertToString(httpPost.getEntity().getContent()), is(expectedBody));
|
88 | 89 | }
|
89 | 90 |
|
| 91 | + public void testCreateRequest_QueryParametersAreEscaped_AndEncoded() { |
| 92 | + var requestContentString = """ |
| 93 | + { |
| 94 | + "input": ${input} |
| 95 | + } |
| 96 | + """; |
| 97 | + |
| 98 | + var serviceSettings = new CustomServiceSettings( |
| 99 | + null, |
| 100 | + null, |
| 101 | + null, |
| 102 | + "http://www.elastic.co", |
| 103 | + null, |
| 104 | + // escaped characters retrieved from here: https://docs.microfocus.com/OMi/10.62/Content/OMi/ExtGuide/ExtApps/URL_encoding.htm |
| 105 | + new QueryParameters( |
| 106 | + List.of( |
| 107 | + new QueryParameters.Parameter("key", " <>#%+{}|\\^~[]`;/?:@=&$"), |
| 108 | + // unicode is a 😀 |
| 109 | + // Note: In the current version of the apache library (4.x) being used to do the encoding, spaces are converted to + |
| 110 | + // There's a bug fix here explaining that: https://issues.apache.org/jira/browse/HTTPCORE-628 |
| 111 | + new QueryParameters.Parameter("key", "Σ \uD83D\uDE00") |
| 112 | + ) |
| 113 | + ), |
| 114 | + requestContentString, |
| 115 | + new TextEmbeddingResponseParser("$.result.embeddings"), |
| 116 | + new RateLimitSettings(10_000), |
| 117 | + new ErrorResponseParser("$.error.message") |
| 118 | + ); |
| 119 | + |
| 120 | + var model = CustomModelTests.createModel( |
| 121 | + "service", |
| 122 | + TaskType.TEXT_EMBEDDING, |
| 123 | + serviceSettings, |
| 124 | + new CustomTaskSettings(Map.of("url", "https://www.elastic.com")), |
| 125 | + new CustomSecretSettings(Map.of("api_key", new SerializableSecureString("my-secret-key"))) |
| 126 | + ); |
| 127 | + |
| 128 | + var request = new CustomRequest(null, List.of("abc", "123"), model); |
| 129 | + var httpRequest = request.createHttpRequest(); |
| 130 | + assertThat(httpRequest.httpRequestBase(), instanceOf(HttpPost.class)); |
| 131 | + |
| 132 | + var httpPost = (HttpPost) httpRequest.httpRequestBase(); |
| 133 | + assertThat( |
| 134 | + httpPost.getURI().toString(), |
| 135 | + // To visually verify that this is correct, input the query parameters into here: https://www.urldecoder.org/ |
| 136 | + is("http://www.elastic.co?key=+%3C%3E%23%25%2B%7B%7D%7C%5C%5E%7E%5B%5D%60%3B%2F%3F%3A%40%3D%26%24&key=%CE%A3+%F0%9F%98%80") |
| 137 | + ); |
| 138 | + } |
| 139 | + |
90 | 140 | public void testCreateRequest_SecretsInTheJsonBody_AreEncodedCorrectly() throws IOException {
|
91 | 141 | var dims = 1536;
|
92 | 142 | var maxInputTokens = 512;
|
@@ -139,6 +189,116 @@ public void testCreateRequest_SecretsInTheJsonBody_AreEncodedCorrectly() throws
|
139 | 189 | assertThat(convertToString(httpPost.getEntity().getContent()), is(expectedBody));
|
140 | 190 | }
|
141 | 191 |
|
| 192 | + public void testCreateRequest_HandlesQuery() throws IOException { |
| 193 | + var requestContentString = """ |
| 194 | + { |
| 195 | + "input": ${input}, |
| 196 | + "query": ${query} |
| 197 | + } |
| 198 | + """; |
| 199 | + |
| 200 | + var serviceSettings = new CustomServiceSettings( |
| 201 | + null, |
| 202 | + null, |
| 203 | + null, |
| 204 | + "http://www.elastic.co", |
| 205 | + null, |
| 206 | + null, |
| 207 | + requestContentString, |
| 208 | + new RerankResponseParser("$.result.score"), |
| 209 | + new RateLimitSettings(10_000), |
| 210 | + new ErrorResponseParser("$.error.message") |
| 211 | + ); |
| 212 | + |
| 213 | + var model = CustomModelTests.createModel( |
| 214 | + "service", |
| 215 | + TaskType.RERANK, |
| 216 | + serviceSettings, |
| 217 | + new CustomTaskSettings(Map.of()), |
| 218 | + new CustomSecretSettings(Map.of("api_key", new SerializableSecureString("my-secret-key"))) |
| 219 | + ); |
| 220 | + |
| 221 | + var request = new CustomRequest("query string", List.of("abc", "123"), model); |
| 222 | + var httpRequest = request.createHttpRequest(); |
| 223 | + assertThat(httpRequest.httpRequestBase(), instanceOf(HttpPost.class)); |
| 224 | + |
| 225 | + var httpPost = (HttpPost) httpRequest.httpRequestBase(); |
| 226 | + |
| 227 | + var expectedBody = XContentHelper.stripWhitespace(""" |
| 228 | + { |
| 229 | + "input": ["abc", "123"], |
| 230 | + "query": "query string" |
| 231 | + } |
| 232 | + """); |
| 233 | + |
| 234 | + assertThat(convertToString(httpPost.getEntity().getContent()), is(expectedBody)); |
| 235 | + } |
| 236 | + |
| 237 | + public void testCreateRequest_IgnoresNonStringFields_ForStringParams() throws IOException { |
| 238 | + var requestContentString = """ |
| 239 | + { |
| 240 | + "input": ${input} |
| 241 | + } |
| 242 | + """; |
| 243 | + |
| 244 | + var serviceSettings = new CustomServiceSettings( |
| 245 | + null, |
| 246 | + null, |
| 247 | + null, |
| 248 | + "http://www.elastic.co", |
| 249 | + Map.of(HttpHeaders.ACCEPT, Strings.format("${task.key}")), |
| 250 | + null, |
| 251 | + requestContentString, |
| 252 | + new RerankResponseParser("$.result.score"), |
| 253 | + new RateLimitSettings(10_000), |
| 254 | + new ErrorResponseParser("$.error.message") |
| 255 | + ); |
| 256 | + |
| 257 | + var model = CustomModelTests.createModel( |
| 258 | + "service", |
| 259 | + TaskType.RERANK, |
| 260 | + serviceSettings, |
| 261 | + new CustomTaskSettings(Map.of("task.key", 100)), |
| 262 | + new CustomSecretSettings(Map.of("api_key", new SerializableSecureString("my-secret-key"))) |
| 263 | + ); |
| 264 | + |
| 265 | + var request = new CustomRequest(null, List.of("abc", "123"), model); |
| 266 | + var exception = expectThrows(IllegalStateException.class, request::createHttpRequest); |
| 267 | + assertThat(exception.getMessage(), is("Found placeholder [${task.key}] in field [header.Accept] after replacement call")); |
| 268 | + } |
| 269 | + |
| 270 | + public void testCreateRequest_ThrowsException_ForInvalidUrl() { |
| 271 | + var requestContentString = """ |
| 272 | + { |
| 273 | + "input": ${input} |
| 274 | + } |
| 275 | + """; |
| 276 | + |
| 277 | + var serviceSettings = new CustomServiceSettings( |
| 278 | + null, |
| 279 | + null, |
| 280 | + null, |
| 281 | + "${url}", |
| 282 | + Map.of(HttpHeaders.ACCEPT, Strings.format("${task.key}")), |
| 283 | + null, |
| 284 | + requestContentString, |
| 285 | + new RerankResponseParser("$.result.score"), |
| 286 | + new RateLimitSettings(10_000), |
| 287 | + new ErrorResponseParser("$.error.message") |
| 288 | + ); |
| 289 | + |
| 290 | + var model = CustomModelTests.createModel( |
| 291 | + "service", |
| 292 | + TaskType.RERANK, |
| 293 | + serviceSettings, |
| 294 | + new CustomTaskSettings(Map.of("url", "^")), |
| 295 | + new CustomSecretSettings(Map.of("api_key", new SerializableSecureString("my-secret-key"))) |
| 296 | + ); |
| 297 | + |
| 298 | + var exception = expectThrows(IllegalStateException.class, () -> new CustomRequest(null, List.of("abc", "123"), model)); |
| 299 | + assertThat(exception.getMessage(), is("Failed to build URI, error: Illegal character in path at index 0: ^")); |
| 300 | + } |
| 301 | + |
142 | 302 | private static String convertToString(InputStream inputStream) throws IOException {
|
143 | 303 | return XContentHelper.stripWhitespace(Streams.copyToString(new InputStreamReader(inputStream, StandardCharsets.UTF_8)));
|
144 | 304 | }
|
|
0 commit comments