|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +package org.elasticsearch.xpack.inference.services.ai21.request; |
| 9 | + |
| 10 | +import org.apache.http.client.methods.HttpPost; |
| 11 | +import org.elasticsearch.core.Nullable; |
| 12 | +import org.elasticsearch.test.ESTestCase; |
| 13 | +import org.elasticsearch.xpack.inference.external.http.sender.UnifiedChatInput; |
| 14 | +import org.elasticsearch.xpack.inference.services.ai21.completion.Ai21ChatCompletionModel; |
| 15 | +import org.elasticsearch.xpack.inference.services.ai21.completion.Ai21ChatCompletionModelTests; |
| 16 | + |
| 17 | +import java.io.IOException; |
| 18 | +import java.util.List; |
| 19 | +import java.util.Map; |
| 20 | + |
| 21 | +import static org.elasticsearch.xpack.inference.external.http.Utils.entityAsMap; |
| 22 | +import static org.hamcrest.Matchers.aMapWithSize; |
| 23 | +import static org.hamcrest.Matchers.instanceOf; |
| 24 | +import static org.hamcrest.Matchers.is; |
| 25 | + |
| 26 | +public class Ai21ChatCompletionRequestTests extends ESTestCase { |
| 27 | + |
| 28 | + public void testCreateRequest_WithStreaming() throws IOException { |
| 29 | + var request = createRequest("secret", randomAlphaOfLength(15), "model", true); |
| 30 | + var httpRequest = request.createHttpRequest(); |
| 31 | + |
| 32 | + assertThat(httpRequest.httpRequestBase(), instanceOf(HttpPost.class)); |
| 33 | + var httpPost = (HttpPost) httpRequest.httpRequestBase(); |
| 34 | + |
| 35 | + var requestMap = entityAsMap(httpPost.getEntity().getContent()); |
| 36 | + assertThat(requestMap.get("stream"), is(true)); |
| 37 | + } |
| 38 | + |
| 39 | + public void testTruncate_DoesNotReduceInputTextSize() throws IOException { |
| 40 | + String input = randomAlphaOfLength(5); |
| 41 | + var request = createRequest("secret", input, "model", true); |
| 42 | + var truncatedRequest = request.truncate(); |
| 43 | + assertThat(request.getURI().toString(), is(Ai21ChatCompletionModel.API_COMPLETIONS_PATH)); |
| 44 | + |
| 45 | + var httpRequest = truncatedRequest.createHttpRequest(); |
| 46 | + assertThat(httpRequest.httpRequestBase(), instanceOf(HttpPost.class)); |
| 47 | + |
| 48 | + var httpPost = (HttpPost) httpRequest.httpRequestBase(); |
| 49 | + var requestMap = entityAsMap(httpPost.getEntity().getContent()); |
| 50 | + assertThat(requestMap, aMapWithSize(5)); |
| 51 | + |
| 52 | + // We do not truncate for AI21 chat completions |
| 53 | + assertThat(requestMap.get("messages"), is(List.of(Map.of("role", "user", "content", input)))); |
| 54 | + assertThat(requestMap.get("model"), is("model")); |
| 55 | + assertThat(requestMap.get("n"), is(1)); |
| 56 | + assertTrue((Boolean) requestMap.get("stream")); |
| 57 | + assertThat(requestMap.get("stream_options"), is(Map.of("include_usage", true))); |
| 58 | + } |
| 59 | + |
| 60 | + public void testTruncationInfo_ReturnsNull() { |
| 61 | + var request = createRequest("secret", randomAlphaOfLength(5), "model", true); |
| 62 | + assertNull(request.getTruncationInfo()); |
| 63 | + } |
| 64 | + |
| 65 | + public static Ai21ChatCompletionRequest createRequest(String apiKey, String input, @Nullable String model) { |
| 66 | + return createRequest(apiKey, input, model, false); |
| 67 | + } |
| 68 | + |
| 69 | + public static Ai21ChatCompletionRequest createRequest(String apiKey, String input, @Nullable String model, boolean stream) { |
| 70 | + var chatCompletionModel = Ai21ChatCompletionModelTests.createCompletionModel(apiKey, model); |
| 71 | + return new Ai21ChatCompletionRequest(new UnifiedChatInput(List.of(input), "user", stream), chatCompletionModel); |
| 72 | + } |
| 73 | + |
| 74 | +} |
0 commit comments