-
Notifications
You must be signed in to change notification settings - Fork 148
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
support step size for embedding model which outputs less embeddings #1586
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,7 @@ | |
|
||
import java.io.IOException; | ||
import java.util.Arrays; | ||
import java.util.Map; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.mock; | ||
|
@@ -165,7 +166,7 @@ public void executePredict_TextDocsInput() throws IOException { | |
.postProcessFunction(MLPostProcessFunction.OPENAI_EMBEDDING) | ||
.requestBody("{\"input\": ${parameters.input}}") | ||
.build(); | ||
Connector connector = HttpConnector.builder().name("test connector").version("1").protocol("http").actions(Arrays.asList(predictAction)).build(); | ||
HttpConnector connector = HttpConnector.builder().name("test connector").version("1").protocol("http").actions(Arrays.asList(predictAction)).build(); | ||
HttpJsonConnectorExecutor executor = spy(new HttpJsonConnectorExecutor(connector)); | ||
executor.setScriptService(scriptService); | ||
when(httpClient.execute(any())).thenReturn(response); | ||
|
@@ -182,6 +183,7 @@ public void executePredict_TextDocsInput() throws IOException { | |
HttpEntity entity = new StringEntity(modelResponse); | ||
when(response.getEntity()).thenReturn(entity); | ||
when(executor.getHttpClient()).thenReturn(httpClient); | ||
when(executor.getConnector()).thenReturn(connector); | ||
MLInputDataset inputDataSet = TextDocsInputDataSet.builder().docs(Arrays.asList("test doc1", "test doc2")).build(); | ||
ModelTensorOutput modelTensorOutput = executor.executePredict(MLInput.builder().algorithm(FunctionName.REMOTE).inputDataset(inputDataSet).build()); | ||
Assert.assertEquals(1, modelTensorOutput.getMlModelOutputs().size()); | ||
|
@@ -190,4 +192,46 @@ public void executePredict_TextDocsInput() throws IOException { | |
Assert.assertArrayEquals(new Number[] {-0.014555434, -0.002135904, 0.0035105038}, modelTensorOutput.getMlModelOutputs().get(0).getMlModelTensors().get(0).getData()); | ||
Assert.assertArrayEquals(new Number[] {-0.014555434, -0.002135904, 0.0035105038}, modelTensorOutput.getMlModelOutputs().get(0).getMlModelTensors().get(1).getData()); | ||
} | ||
|
||
@Test | ||
public void executePredict_TextDocsInput_LessEmbeddingThanInputDocs() throws IOException { | ||
String preprocessResult1 = "{\"parameters\": { \"input\": \"test doc1\" } }"; | ||
String preprocessResult2 = "{\"parameters\": { \"input\": \"test doc2\" } }"; | ||
when(scriptService.compile(any(), any())) | ||
.then(invocation -> new TestTemplateService.MockTemplateScript.Factory(preprocessResult1)) | ||
.then(invocation -> new TestTemplateService.MockTemplateScript.Factory(preprocessResult2)); | ||
|
||
ConnectorAction predictAction = ConnectorAction.builder() | ||
.actionType(ConnectorAction.ActionType.PREDICT) | ||
.method("POST") | ||
.url("http://test.com/mock") | ||
.preProcessFunction(MLPreProcessFunction.TEXT_DOCS_TO_OPENAI_EMBEDDING_INPUT) | ||
.postProcessFunction(MLPostProcessFunction.OPENAI_EMBEDDING) | ||
.requestBody("{\"input\": ${parameters.input}}") | ||
.build(); | ||
Map<String, String> parameters = ImmutableMap.of("input_docs_processed_step_size", "2"); | ||
HttpConnector connector = HttpConnector.builder().name("test connector").version("1").protocol("http").parameters(parameters).actions(Arrays.asList(predictAction)).build(); | ||
HttpJsonConnectorExecutor executor = spy(new HttpJsonConnectorExecutor(connector)); | ||
executor.setScriptService(scriptService); | ||
when(httpClient.execute(any())).thenReturn(response); | ||
// model takes 2 input docs, but only output 1 embedding | ||
String modelResponse = "{\n" + " \"object\": \"list\",\n" + " \"data\": [\n" + " {\n" | ||
+ " \"object\": \"embedding\",\n" + " \"index\": 0,\n" + " \"embedding\": [\n" | ||
+ " -0.014555434,\n" + " -0.002135904,\n" + " 0.0035105038\n" + " ]\n" | ||
+ " } ],\n" | ||
+ " \"model\": \"text-embedding-ada-002-v2\",\n" + " \"usage\": {\n" + " \"prompt_tokens\": 5,\n" | ||
+ " \"total_tokens\": 5\n" + " }\n" + "}"; | ||
StatusLine statusLine = new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), 200, "OK"); | ||
when(response.getStatusLine()).thenReturn(statusLine); | ||
HttpEntity entity = new StringEntity(modelResponse); | ||
when(response.getEntity()).thenReturn(entity); | ||
when(executor.getHttpClient()).thenReturn(httpClient); | ||
when(executor.getConnector()).thenReturn(connector); | ||
MLInputDataset inputDataSet = TextDocsInputDataSet.builder().docs(Arrays.asList("test doc1", "test doc2")).build(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. May be we should provide 4 documents to see if we are getting 2 outputs? But we can do it later too. |
||
ModelTensorOutput modelTensorOutput = executor.executePredict(MLInput.builder().algorithm(FunctionName.REMOTE).inputDataset(inputDataSet).build()); | ||
Assert.assertEquals(1, modelTensorOutput.getMlModelOutputs().size()); | ||
Assert.assertEquals(1, modelTensorOutput.getMlModelOutputs().get(0).getMlModelTensors().size()); | ||
Assert.assertEquals("sentence_embedding", modelTensorOutput.getMlModelOutputs().get(0).getMlModelTensors().get(0).getName()); | ||
Assert.assertArrayEquals(new Number[] {-0.014555434, -0.002135904, 0.0035105038}, modelTensorOutput.getMlModelOutputs().get(0).getMlModelTensors().get(0).getData()); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
region
for AWS region andaccess_key
for AWS access key. All AI connector have to use this parameter to represent step size, just like they have to useaccess_key
to represent AWS access key.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed in this PR #1587