-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: HenryL27 <[email protected]>
- Loading branch information
Showing
11 changed files
with
256 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
...test/java/org/opensearch/neuralsearch/processor/rerank/CrossEncoderRerankProcessorIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
/* | ||
* Copyright 2023 Aryn | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.opensearch.neuralsearch.processor.rerank; | ||
|
||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import lombok.SneakyThrows; | ||
import lombok.extern.log4j.Log4j2; | ||
|
||
import org.apache.hc.core5.http.HttpHeaders; | ||
import org.apache.hc.core5.http.io.entity.EntityUtils; | ||
import org.apache.hc.core5.http.message.BasicHeader; | ||
import org.junit.After; | ||
import org.opensearch.client.Request; | ||
import org.opensearch.client.Response; | ||
import org.opensearch.common.xcontent.XContentHelper; | ||
import org.opensearch.common.xcontent.XContentType; | ||
import org.opensearch.core.rest.RestStatus; | ||
import org.opensearch.neuralsearch.common.BaseNeuralSearchIT; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
|
||
@Log4j2 | ||
public class CrossEncoderRerankProcessorIT extends BaseNeuralSearchIT { | ||
|
||
final static String PIPELINE_NAME = "rerank-ce-pipeline"; | ||
final static String INDEX_NAME = "rerank-test"; | ||
final static String TEXT_REP_1 = "Jacques loves fish. Fish make Jacques happy"; | ||
final static String TEXT_REP_2 = "Fish like to eat plankton"; | ||
final static String INDEX_CONFIG = "{\"mappings\": {\"properties\": {\"text_representation\": {\"type\": \"text\"}}}}"; | ||
|
||
@After | ||
@SneakyThrows | ||
public void tearDown() { | ||
super.tearDown(); | ||
/* this is required to minimize chance of model not being deployed due to open memory CB, | ||
* this happens in case we leave model from previous test case. We use new model for every test, and old model | ||
* can be undeployed and deleted to free resources after each test case execution. | ||
*/ | ||
deleteSearchPipeline(PIPELINE_NAME); | ||
findDeployedModels().forEach(this::deleteModel); | ||
deleteIndex(INDEX_NAME); | ||
} | ||
|
||
public void testCrossEncoderRerankProcessor() throws Exception { | ||
String modelId = uploadCrossEncoderModel(); | ||
loadModel(modelId); | ||
createSearchPipelineViaConfig(modelId, PIPELINE_NAME, "processor/CrossEncoderRerankPipelineConfiguration.json"); | ||
setupIndex(); | ||
runQueries(); | ||
} | ||
|
||
private String uploadCrossEncoderModel() throws Exception { | ||
String requestBody = Files.readString( | ||
Path.of(classLoader.getResource("processor/UploadCrossEncoderModelRequestBody.json").toURI()) | ||
); | ||
return uploadModel(requestBody); | ||
} | ||
|
||
private void setupIndex() throws Exception { | ||
createIndexWithConfiguration(INDEX_NAME, INDEX_CONFIG, PIPELINE_NAME); | ||
Response response1 = makeRequest( | ||
client(), | ||
"POST", | ||
INDEX_NAME + "/_doc?refresh", | ||
null, | ||
toHttpEntity(String.format(LOCALE, "{\"text_representation\": \"%s\"}", TEXT_REP_1)), | ||
ImmutableList.of(new BasicHeader(HttpHeaders.USER_AGENT, DEFAULT_USER_AGENT)) | ||
); | ||
Response response2 = makeRequest( | ||
client(), | ||
"POST", | ||
INDEX_NAME + "/_doc?refresh", | ||
null, | ||
toHttpEntity(String.format(LOCALE, "{\"text_representation\": \"%s\"}", TEXT_REP_2)), | ||
ImmutableList.of(new BasicHeader(HttpHeaders.USER_AGENT, DEFAULT_USER_AGENT)) | ||
); | ||
Map<String, Object> map = XContentHelper.convertToMap( | ||
XContentType.JSON.xContent(), | ||
EntityUtils.toString(response1.getEntity()), | ||
false | ||
); | ||
assertEquals("created", map.get("result")); | ||
map = XContentHelper.convertToMap(XContentType.JSON.xContent(), EntityUtils.toString(response2.getEntity()), false); | ||
assertEquals("created", map.get("result")); | ||
} | ||
|
||
private void runQueries() throws Exception { | ||
Map<String, Object> response1 = search("What do fish eat?"); | ||
@SuppressWarnings("unchecked") | ||
List<Map<String, ?>> hits = (List<Map<String, ?>>) ((Map<String, ?>) response1.get("hits")).get("hits"); | ||
@SuppressWarnings("unchecked") | ||
Map<String, String> hit0Source = (Map<String, String>) hits.get(0).get("_source"); | ||
assert ((String) hit0Source.get("text_representation")).equals(TEXT_REP_2); | ||
@SuppressWarnings("unchecked") | ||
Map<String, String> hit1Source = (Map<String, String>) hits.get(1).get("_source"); | ||
assert ((String) hit1Source.get("text_representation")).equals(TEXT_REP_1); | ||
|
||
Map<String, Object> response2 = search("Who loves fish?"); | ||
@SuppressWarnings("unchecked") | ||
List<Map<String, ?>> hits2 = (List<Map<String, ?>>) ((Map<String, ?>) response2.get("hits")).get("hits"); | ||
@SuppressWarnings("unchecked") | ||
Map<String, String> hit2Source = (Map<String, String>) hits2.get(0).get("_source"); | ||
assert ((String) hit2Source.get("text_representation")).equals(TEXT_REP_1); | ||
@SuppressWarnings("unchecked") | ||
Map<String, String> hit3Source = (Map<String, String>) hits2.get(1).get("_source"); | ||
assert ((String) hit3Source.get("text_representation")).equals(TEXT_REP_2); | ||
} | ||
|
||
private Map<String, Object> search(String queryText) throws Exception { | ||
String jsonQueryFrame = "{\"query\":{\"match_all\":{}},\"ext\":{\"rerank\":{\"query_text\":\"%s\"}}}"; | ||
String jsonQuery = String.format(LOCALE, jsonQueryFrame, queryText); | ||
log.info(jsonQuery); | ||
Request request = new Request("POST", "/" + INDEX_NAME + "/_search"); | ||
request.addParameter("search_pipeline", PIPELINE_NAME); | ||
request.setJsonEntity(jsonQuery); | ||
|
||
Response response = client().performRequest(request); | ||
assertEquals(request.getEndpoint() + ": failed", RestStatus.OK, RestStatus.fromCode(response.getStatusLine().getStatusCode())); | ||
|
||
String responseBody = EntityUtils.toString(response.getEntity()); | ||
|
||
return XContentHelper.convertToMap(XContentType.JSON.xContent(), responseBody, false); | ||
} | ||
} |
Oops, something went wrong.