forked from opensearch-project/ml-commons
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding mlmodeltool and agent tool with tests (opensearch-project#1768)
* adding mlmodeltool and agent tool with tests Signed-off-by: Dhrubo Saha <[email protected]> * updating tests Signed-off-by: Dhrubo Saha <[email protected]> * removed connector Signed-off-by: Dhrubo Saha <[email protected]> --------- Signed-off-by: Dhrubo Saha <[email protected]>
- Loading branch information
Showing
9 changed files
with
730 additions
and
2 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
75 changes: 75 additions & 0 deletions
75
common/src/main/java/org/opensearch/ml/common/input/execute/agent/AgentMLInput.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,75 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.ml.common.input.execute.agent; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
import org.opensearch.core.common.io.stream.StreamInput; | ||
import org.opensearch.core.common.io.stream.StreamOutput; | ||
import org.opensearch.core.xcontent.XContentParser; | ||
import org.opensearch.ml.common.FunctionName; | ||
import org.opensearch.ml.common.dataset.MLInputDataset; | ||
import org.opensearch.ml.common.dataset.remote.RemoteInferenceInputDataSet; | ||
import org.opensearch.ml.common.input.MLInput; | ||
import org.opensearch.ml.common.utils.StringUtils; | ||
|
||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
import static org.opensearch.core.xcontent.XContentParserUtils.ensureExpectedToken; | ||
|
||
|
||
@org.opensearch.ml.common.annotation.MLInput(functionNames = {FunctionName.AGENT}) | ||
public class AgentMLInput extends MLInput { | ||
public static final String AGENT_ID_FIELD = "agent_id"; | ||
public static final String PARAMETERS_FIELD = "parameters"; | ||
|
||
@Getter @Setter | ||
private String agentId; | ||
|
||
@Builder(builderMethodName = "AgentMLInputBuilder") | ||
public AgentMLInput(String agentId, FunctionName functionName, MLInputDataset inputDataset) { | ||
this.agentId = agentId; | ||
this.algorithm = functionName; | ||
this.inputDataset = inputDataset; | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
super.writeTo(out); | ||
out.writeString(agentId); | ||
} | ||
|
||
public AgentMLInput(StreamInput in) throws IOException { | ||
super(in); | ||
this.agentId = in.readString(); | ||
} | ||
|
||
public AgentMLInput(XContentParser parser, FunctionName functionName) throws IOException { | ||
super(); | ||
this.algorithm = functionName; | ||
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.currentToken(), parser); | ||
while (parser.nextToken() != XContentParser.Token.END_OBJECT) { | ||
String fieldName = parser.currentName(); | ||
parser.nextToken(); | ||
|
||
switch (fieldName) { | ||
case AGENT_ID_FIELD: | ||
agentId = parser.text(); | ||
break; | ||
case PARAMETERS_FIELD: | ||
Map<String, String> parameters = StringUtils.getParameterMap(parser.map()); | ||
inputDataset = new RemoteInferenceInputDataSet(parameters); | ||
break; | ||
default: | ||
parser.skipChildren(); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
} |
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
112 changes: 112 additions & 0 deletions
112
common/src/test/java/org/opensearch/ml/common/input/execute/agent/AgentMLInputTests.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,112 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.ml.common.input.execute.agent; | ||
|
||
import org.junit.Test; | ||
import org.opensearch.core.common.io.stream.StreamInput; | ||
import org.opensearch.core.common.io.stream.StreamOutput; | ||
import org.opensearch.core.xcontent.XContentParser; | ||
import org.opensearch.ml.common.FunctionName; | ||
import org.opensearch.ml.common.dataset.MLInputDataset; | ||
import org.opensearch.ml.common.dataset.remote.RemoteInferenceInputDataSet; | ||
|
||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class AgentMLInputTests { | ||
|
||
@Test | ||
public void testConstructorWithAgentIdFunctionNameAndDataset() { | ||
// Arrange | ||
String agentId = "testAgentId"; | ||
FunctionName functionName = FunctionName.AGENT; // Assuming FunctionName is an enum or similar | ||
MLInputDataset dataset = mock(MLInputDataset.class); // Mock the MLInputDataset | ||
|
||
// Act | ||
AgentMLInput input = new AgentMLInput(agentId, functionName, dataset); | ||
|
||
// Assert | ||
assertEquals(agentId, input.getAgentId()); | ||
assertEquals(functionName, input.getAlgorithm()); | ||
assertEquals(dataset, input.getInputDataset()); | ||
} | ||
|
||
@Test | ||
public void testWriteTo() throws IOException { | ||
// Arrange | ||
String agentId = "testAgentId"; | ||
AgentMLInput input = new AgentMLInput(agentId, FunctionName.AGENT, null); | ||
StreamOutput out = mock(StreamOutput.class); | ||
|
||
// Act | ||
input.writeTo(out); | ||
|
||
// Assert | ||
verify(out).writeString(agentId); | ||
} | ||
|
||
@Test | ||
public void testConstructorWithStreamInput() throws IOException { | ||
// Arrange | ||
String agentId = "testAgentId"; | ||
StreamInput in = mock(StreamInput.class); | ||
when(in.readString()).thenReturn(agentId); | ||
|
||
// Act | ||
AgentMLInput input = new AgentMLInput(in); | ||
|
||
// Assert | ||
assertEquals(agentId, input.getAgentId()); | ||
} | ||
|
||
@Test | ||
public void testConstructorWithXContentParser() throws IOException { | ||
// Arrange | ||
XContentParser parser = mock(XContentParser.class); | ||
|
||
// Simulate parser behavior for START_OBJECT token | ||
when(parser.currentToken()).thenReturn(XContentParser.Token.START_OBJECT); | ||
when(parser.nextToken()).thenReturn(XContentParser.Token.FIELD_NAME) | ||
.thenReturn(XContentParser.Token.VALUE_STRING) | ||
.thenReturn(XContentParser.Token.FIELD_NAME) // For PARAMETERS_FIELD | ||
.thenReturn(XContentParser.Token.START_OBJECT) // Start of PARAMETERS_FIELD map | ||
.thenReturn(XContentParser.Token.FIELD_NAME) // Key in PARAMETERS_FIELD map | ||
.thenReturn(XContentParser.Token.VALUE_STRING) // Value in PARAMETERS_FIELD map | ||
.thenReturn(XContentParser.Token.END_OBJECT) // End of PARAMETERS_FIELD map | ||
.thenReturn(XContentParser.Token.END_OBJECT); // End of the main object | ||
|
||
// Simulate parser behavior for agent_id | ||
when(parser.currentName()).thenReturn("agent_id") | ||
.thenReturn("parameters") | ||
.thenReturn("paramKey"); | ||
when(parser.text()).thenReturn("testAgentId") | ||
.thenReturn("paramValue"); | ||
|
||
// Simulate parser behavior for parameters | ||
Map<String, Object> paramMap = new HashMap<>(); | ||
paramMap.put("paramKey", "paramValue"); | ||
when(parser.map()).thenReturn(paramMap); | ||
|
||
// Act | ||
AgentMLInput input = new AgentMLInput(parser, FunctionName.AGENT); | ||
|
||
// Assert | ||
assertEquals("testAgentId", input.getAgentId()); | ||
assertNotNull(input.getInputDataset()); | ||
assertTrue(input.getInputDataset() instanceof RemoteInferenceInputDataSet); | ||
// Additional assertions for RemoteInferenceInputDataSet | ||
RemoteInferenceInputDataSet dataset = (RemoteInferenceInputDataSet) input.getInputDataset(); | ||
assertEquals("paramValue", dataset.getParameters().get("paramKey")); | ||
} | ||
} |
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
Oops, something went wrong.