|
| 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.llama.request.completion; |
| 9 | + |
| 10 | +import org.apache.http.client.methods.HttpPost; |
| 11 | +import org.elasticsearch.test.ESTestCase; |
| 12 | +import org.elasticsearch.xpack.inference.external.http.sender.UnifiedChatInput; |
| 13 | +import org.elasticsearch.xpack.inference.services.llama.completion.LlamaChatCompletionModelTests; |
| 14 | + |
| 15 | +import java.io.IOException; |
| 16 | +import java.util.List; |
| 17 | +import java.util.Map; |
| 18 | + |
| 19 | +import static org.elasticsearch.xpack.inference.external.http.Utils.entityAsMap; |
| 20 | +import static org.hamcrest.Matchers.instanceOf; |
| 21 | +import static org.hamcrest.Matchers.is; |
| 22 | + |
| 23 | +public class LlamaChatCompletionRequestTests extends ESTestCase { |
| 24 | + |
| 25 | + public void testCreateRequest_WithStreaming() throws IOException { |
| 26 | + String input = randomAlphaOfLength(15); |
| 27 | + var request = createRequest("model", "url", "secret", input, true); |
| 28 | + var httpRequest = request.createHttpRequest(); |
| 29 | + |
| 30 | + assertThat(httpRequest.httpRequestBase(), instanceOf(HttpPost.class)); |
| 31 | + var httpPost = (HttpPost) httpRequest.httpRequestBase(); |
| 32 | + |
| 33 | + var requestMap = entityAsMap(httpPost.getEntity().getContent()); |
| 34 | + assertThat(request.getURI().toString(), is("url")); |
| 35 | + assertThat(requestMap.get("stream"), is(true)); |
| 36 | + assertThat(requestMap.get("model"), is("model")); |
| 37 | + assertThat(requestMap.get("n"), is(1)); |
| 38 | + assertThat(requestMap.get("stream_options"), is(Map.of("include_usage", true))); |
| 39 | + assertThat(requestMap.get("messages"), is(List.of(Map.of("role", "user", "content", input)))); |
| 40 | + assertNotNull(httpPost.getHeaders("Authorization")); |
| 41 | + } |
| 42 | + |
| 43 | + public void testCreateRequest_NoStreaming_NoAuthorization() throws IOException { |
| 44 | + String input = randomAlphaOfLength(15); |
| 45 | + var request = createRequestWithNoAuth("model", "url", input, false); |
| 46 | + var httpRequest = request.createHttpRequest(); |
| 47 | + |
| 48 | + assertThat(httpRequest.httpRequestBase(), instanceOf(HttpPost.class)); |
| 49 | + var httpPost = (HttpPost) httpRequest.httpRequestBase(); |
| 50 | + |
| 51 | + var requestMap = entityAsMap(httpPost.getEntity().getContent()); |
| 52 | + assertThat(request.getURI().toString(), is("url")); |
| 53 | + assertThat(requestMap.get("stream"), is(false)); |
| 54 | + assertThat(requestMap.get("model"), is("model")); |
| 55 | + assertThat(requestMap.get("n"), is(1)); |
| 56 | + assertNull(requestMap.get("stream_options")); |
| 57 | + assertThat(requestMap.get("messages"), is(List.of(Map.of("role", "user", "content", input)))); |
| 58 | + assertNull(httpPost.getFirstHeader("Authorization")); |
| 59 | + } |
| 60 | + |
| 61 | + public void testTruncate_DoesNotReduceInputTextSize() { |
| 62 | + String input = randomAlphaOfLength(5); |
| 63 | + var request = createRequest("model", "url", "secret", input, true); |
| 64 | + assertThat(request.truncate(), is(request)); |
| 65 | + } |
| 66 | + |
| 67 | + public void testTruncationInfo_ReturnsNull() { |
| 68 | + var request = createRequest("model", "url", "secret", randomAlphaOfLength(5), true); |
| 69 | + assertNull(request.getTruncationInfo()); |
| 70 | + } |
| 71 | + |
| 72 | + public static LlamaChatCompletionRequest createRequest(String modelId, String url, String apiKey, String input, boolean stream) { |
| 73 | + var chatCompletionModel = LlamaChatCompletionModelTests.createChatCompletionModel(modelId, url, apiKey); |
| 74 | + return new LlamaChatCompletionRequest(new UnifiedChatInput(List.of(input), "user", stream), chatCompletionModel); |
| 75 | + } |
| 76 | + |
| 77 | + public static LlamaChatCompletionRequest createRequestWithNoAuth(String modelId, String url, String input, boolean stream) { |
| 78 | + var chatCompletionModel = LlamaChatCompletionModelTests.createChatCompletionModelNoAuth(modelId, url); |
| 79 | + return new LlamaChatCompletionRequest(new UnifiedChatInput(List.of(input), "user", stream), chatCompletionModel); |
| 80 | + } |
| 81 | +} |
0 commit comments