Skip to content
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

add config field in MLToolSpec for static parameters #2977

Merged
merged 5 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
import java.io.IOException;
import java.util.Map;

import org.opensearch.Version;
import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.core.common.io.stream.StreamOutput;
import org.opensearch.core.xcontent.ToXContentObject;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.ml.common.CommonValue;

import lombok.Builder;
import lombok.EqualsAndHashCode;
Expand All @@ -24,20 +26,31 @@
@EqualsAndHashCode
@Getter
public class MLToolSpec implements ToXContentObject {
public static final Version MINIMAL_SUPPORTED_VERSION_FOR_TOOL_CONFIG = CommonValue.VERSION_2_17_0;
jngz-es marked this conversation as resolved.
Show resolved Hide resolved

public static final String TOOL_TYPE_FIELD = "type";
public static final String TOOL_NAME_FIELD = "name";
public static final String DESCRIPTION_FIELD = "description";
public static final String PARAMETERS_FIELD = "parameters";
public static final String INCLUDE_OUTPUT_IN_AGENT_RESPONSE = "include_output_in_agent_response";
public static final String CONFIG_FIELD = "config";

private String type;
private String name;
private String description;
private Map<String, String> parameters;
private boolean includeOutputInAgentResponse;
private Map<String, String> configMap;

@Builder(toBuilder = true)
public MLToolSpec(String type, String name, String description, Map<String, String> parameters, boolean includeOutputInAgentResponse) {
public MLToolSpec(
String type,
String name,
String description,
Map<String, String> parameters,
boolean includeOutputInAgentResponse,
Map<String, String> configMap
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it valid that all the configs are Strings? Is it better to use Map<String, Object> here?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 on this

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-1 on that. The parameters map is already a Map<String, String>. In the initial PR comment an example is given of a much more complex "object" but escaped as a String.

Trying to parse anything else from Flow Framework will get a lot more complex without typed objects to construct from the JSON.

) {
if (type == null) {
throw new IllegalArgumentException("tool type is null");
}
Expand All @@ -46,6 +59,7 @@ public MLToolSpec(String type, String name, String description, Map<String, Stri
this.description = description;
this.parameters = parameters;
this.includeOutputInAgentResponse = includeOutputInAgentResponse;
this.configMap = configMap;
}

public MLToolSpec(StreamInput input) throws IOException {
Expand All @@ -56,6 +70,9 @@ public MLToolSpec(StreamInput input) throws IOException {
parameters = input.readMap(StreamInput::readString, StreamInput::readOptionalString);
}
includeOutputInAgentResponse = input.readBoolean();
if (input.getVersion().onOrAfter(MINIMAL_SUPPORTED_VERSION_FOR_TOOL_CONFIG) && input.readBoolean()) {
configMap = input.readMap(StreamInput::readOptionalString, StreamInput::readOptionalString);
jngz-es marked this conversation as resolved.
Show resolved Hide resolved
}
}

public void writeTo(StreamOutput out) throws IOException {
Expand All @@ -69,6 +86,14 @@ public void writeTo(StreamOutput out) throws IOException {
out.writeBoolean(false);
}
out.writeBoolean(includeOutputInAgentResponse);
if (out.getVersion().onOrAfter(MINIMAL_SUPPORTED_VERSION_FOR_TOOL_CONFIG)) {
if (configMap != null) {
out.writeBoolean(true);
out.writeMap(configMap, StreamOutput::writeOptionalString, StreamOutput::writeOptionalString);
} else {
out.writeBoolean(false);
}
}
}

@Override
Expand All @@ -87,6 +112,9 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
builder.field(PARAMETERS_FIELD, parameters);
}
builder.field(INCLUDE_OUTPUT_IN_AGENT_RESPONSE, includeOutputInAgentResponse);
if (configMap != null && configMap.size() > 0) {
jngz-es marked this conversation as resolved.
Show resolved Hide resolved
builder.field(CONFIG_FIELD, configMap);
}
builder.endObject();
return builder;
}
Expand All @@ -97,6 +125,7 @@ public static MLToolSpec parse(XContentParser parser) throws IOException {
String description = null;
Map<String, String> parameters = null;
boolean includeOutputInAgentResponse = false;
Map<String, String> configMap = null;

ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.currentToken(), parser);
while (parser.nextToken() != XContentParser.Token.END_OBJECT) {
Expand All @@ -119,6 +148,9 @@ public static MLToolSpec parse(XContentParser parser) throws IOException {
case INCLUDE_OUTPUT_IN_AGENT_RESPONSE:
includeOutputInAgentResponse = parser.booleanValue();
break;
case CONFIG_FIELD:
configMap = getParameterMap(parser.map());
jngz-es marked this conversation as resolved.
Show resolved Hide resolved
break;
default:
parser.skipChildren();
break;
Expand All @@ -131,6 +163,7 @@ public static MLToolSpec parse(XContentParser parser) throws IOException {
.description(description)
.parameters(parameters)
.includeOutputInAgentResponse(includeOutputInAgentResponse)
.configMap(configMap)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void constructor_NullName() {
MLAgentType.CONVERSATIONAL.name(),
"test",
new LLMSpec("test_model", Map.of("test_key", "test_value")),
List.of(new MLToolSpec("test", "test", "test", Collections.EMPTY_MAP, false)),
List.of(new MLToolSpec("test", "test", "test", Collections.EMPTY_MAP, false, Collections.EMPTY_MAP)),
jngz-es marked this conversation as resolved.
Show resolved Hide resolved
null,
null,
Instant.EPOCH,
Expand All @@ -66,7 +66,7 @@ public void constructor_NullType() {
null,
"test",
new LLMSpec("test_model", Map.of("test_key", "test_value")),
List.of(new MLToolSpec("test", "test", "test", Collections.EMPTY_MAP, false)),
List.of(new MLToolSpec("test", "test", "test", Collections.EMPTY_MAP, false, Collections.EMPTY_MAP)),
null,
null,
Instant.EPOCH,
Expand All @@ -86,7 +86,7 @@ public void constructor_NullLLMSpec() {
MLAgentType.CONVERSATIONAL.name(),
"test",
null,
List.of(new MLToolSpec("test", "test", "test", Collections.EMPTY_MAP, false)),
List.of(new MLToolSpec("test", "test", "test", Collections.EMPTY_MAP, false, Collections.EMPTY_MAP)),
null,
null,
Instant.EPOCH,
Expand All @@ -100,7 +100,14 @@ public void constructor_NullLLMSpec() {
public void constructor_DuplicateTool() {
exceptionRule.expect(IllegalArgumentException.class);
exceptionRule.expectMessage("Duplicate tool defined: test_tool_name");
MLToolSpec mlToolSpec = new MLToolSpec("test_tool_type", "test_tool_name", "test", Collections.EMPTY_MAP, false);
MLToolSpec mlToolSpec = new MLToolSpec(
"test_tool_type",
"test_tool_name",
"test",
Collections.EMPTY_MAP,
false,
Collections.EMPTY_MAP
);
MLAgent agent = new MLAgent(
"test_name",
MLAgentType.CONVERSATIONAL.name(),
Expand All @@ -123,7 +130,7 @@ public void writeTo() throws IOException {
"CONVERSATIONAL",
"test",
new LLMSpec("test_model", Map.of("test_key", "test_value")),
List.of(new MLToolSpec("test", "test", "test", Collections.EMPTY_MAP, false)),
List.of(new MLToolSpec("test", "test", "test", Collections.EMPTY_MAP, false, Collections.EMPTY_MAP)),
Map.of("test", "test"),
new MLMemorySpec("test", "123", 0),
Instant.EPOCH,
Expand All @@ -150,7 +157,7 @@ public void writeTo_NullLLM() throws IOException {
"FLOW",
"test",
null,
List.of(new MLToolSpec("test", "test", "test", Collections.EMPTY_MAP, false)),
List.of(new MLToolSpec("test", "test", "test", Collections.EMPTY_MAP, false, Collections.EMPTY_MAP)),
Map.of("test", "test"),
new MLMemorySpec("test", "123", 0),
Instant.EPOCH,
Expand Down Expand Up @@ -194,7 +201,7 @@ public void writeTo_NullParameters() throws IOException {
MLAgentType.CONVERSATIONAL.name(),
"test",
new LLMSpec("test_model", Map.of("test_key", "test_value")),
List.of(new MLToolSpec("test", "test", "test", Collections.EMPTY_MAP, false)),
List.of(new MLToolSpec("test", "test", "test", Collections.EMPTY_MAP, false, Collections.EMPTY_MAP)),
null,
new MLMemorySpec("test", "123", 0),
Instant.EPOCH,
Expand All @@ -216,7 +223,7 @@ public void writeTo_NullMemory() throws IOException {
"CONVERSATIONAL",
"test",
new LLMSpec("test_model", Map.of("test_key", "test_value")),
List.of(new MLToolSpec("test", "test", "test", Collections.EMPTY_MAP, false)),
List.of(new MLToolSpec("test", "test", "test", Collections.EMPTY_MAP, false, Collections.EMPTY_MAP)),
Map.of("test", "test"),
null,
Instant.EPOCH,
Expand All @@ -238,7 +245,7 @@ public void toXContent() throws IOException {
"CONVERSATIONAL",
"test",
new LLMSpec("test_model", Map.of("test_key", "test_value")),
List.of(new MLToolSpec("test", "test", "test", Map.of("test", "test"), false)),
List.of(new MLToolSpec("test", "test", "test", Map.of("test", "test"), false, Collections.EMPTY_MAP)),
Map.of("test", "test"),
new MLMemorySpec("test", "123", 0),
Instant.EPOCH,
Expand Down Expand Up @@ -294,7 +301,7 @@ public void fromStream() throws IOException {
MLAgentType.CONVERSATIONAL.name(),
"test",
new LLMSpec("test_model", Map.of("test_key", "test_value")),
List.of(new MLToolSpec("test", "test", "test", Collections.EMPTY_MAP, false)),
List.of(new MLToolSpec("test", "test", "test", Collections.EMPTY_MAP, false, Collections.EMPTY_MAP)),
Map.of("test", "test"),
new MLMemorySpec("test", "123", 0),
Instant.EPOCH,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class MLToolSpecTest {

@Test
public void writeTo() throws IOException {
MLToolSpec spec = new MLToolSpec("test", "test", "test", Map.of("test", "test"), false);
MLToolSpec spec = new MLToolSpec("test", "test", "test", Map.of("test", "test"), false, Map.of("configKey", "configValue"));
jngz-es marked this conversation as resolved.
Show resolved Hide resolved
BytesStreamOutput output = new BytesStreamOutput();
spec.writeTo(output);
MLToolSpec spec1 = new MLToolSpec(output.bytes().streamInput());
Expand All @@ -32,11 +32,70 @@ public void writeTo() throws IOException {
Assert.assertEquals(spec.getParameters(), spec1.getParameters());
Assert.assertEquals(spec.getDescription(), spec1.getDescription());
Assert.assertEquals(spec.isIncludeOutputInAgentResponse(), spec1.isIncludeOutputInAgentResponse());
Assert.assertEquals(spec.getConfigMap(), spec1.getConfigMap());
}

@Test
public void writeToEmptyConfigMap() throws IOException {
MLToolSpec spec = new MLToolSpec("test", "test", "test", Map.of("test", "test"), false, Collections.EMPTY_MAP);
BytesStreamOutput output = new BytesStreamOutput();
spec.writeTo(output);
MLToolSpec spec1 = new MLToolSpec(output.bytes().streamInput());

Assert.assertEquals(spec.getType(), spec1.getType());
Assert.assertEquals(spec.getName(), spec1.getName());
Assert.assertEquals(spec.getParameters(), spec1.getParameters());
Assert.assertEquals(spec.getDescription(), spec1.getDescription());
Assert.assertEquals(spec.isIncludeOutputInAgentResponse(), spec1.isIncludeOutputInAgentResponse());
Assert.assertEquals(spec.getConfigMap(), spec1.getConfigMap());
}

@Test
public void writeToNullConfigMap() throws IOException {
MLToolSpec spec = new MLToolSpec("test", "test", "test", Map.of("test", "test"), false, null);
BytesStreamOutput output = new BytesStreamOutput();
spec.writeTo(output);
MLToolSpec spec1 = new MLToolSpec(output.bytes().streamInput());

Assert.assertEquals(spec.getType(), spec1.getType());
Assert.assertEquals(spec.getName(), spec1.getName());
Assert.assertEquals(spec.getParameters(), spec1.getParameters());
Assert.assertEquals(spec.getDescription(), spec1.getDescription());
Assert.assertEquals(spec.isIncludeOutputInAgentResponse(), spec1.isIncludeOutputInAgentResponse());
Assert.assertNull(spec1.getConfigMap());
}

@Test
public void toXContent() throws IOException {
MLToolSpec spec = new MLToolSpec("test", "test", "test", Map.of("test", "test"), false);
MLToolSpec spec = new MLToolSpec("test", "test", "test", Map.of("test", "test"), false, Map.of("configKey", "configValue"));
XContentBuilder builder = XContentBuilder.builder(XContentType.JSON.xContent());
spec.toXContent(builder, ToXContent.EMPTY_PARAMS);
String content = TestHelper.xContentBuilderToString(builder);

Assert
.assertEquals(
"{\"type\":\"test\",\"name\":\"test\",\"description\":\"test\",\"parameters\":{\"test\":\"test\"},\"include_output_in_agent_response\":false,\"config\":{\"configKey\":\"configValue\"}}",
jngz-es marked this conversation as resolved.
Show resolved Hide resolved
content
);
}

@Test
public void toXContentEmptyConfigMap() throws IOException {
MLToolSpec spec = new MLToolSpec("test", "test", "test", Map.of("test", "test"), false, Collections.EMPTY_MAP);
XContentBuilder builder = XContentBuilder.builder(XContentType.JSON.xContent());
spec.toXContent(builder, ToXContent.EMPTY_PARAMS);
String content = TestHelper.xContentBuilderToString(builder);

Assert
.assertEquals(
"{\"type\":\"test\",\"name\":\"test\",\"description\":\"test\",\"parameters\":{\"test\":\"test\"},\"include_output_in_agent_response\":false}",
content
);
}

@Test
public void toXContentNullConfigMap() throws IOException {
MLToolSpec spec = new MLToolSpec("test", "test", "test", Map.of("test", "test"), false, null);
XContentBuilder builder = XContentBuilder.builder(XContentType.JSON.xContent());
spec.toXContent(builder, ToXContent.EMPTY_PARAMS);
String content = TestHelper.xContentBuilderToString(builder);
Expand All @@ -50,6 +109,28 @@ public void toXContent() throws IOException {

@Test
public void parse() throws IOException {
String jsonStr =
"{\"type\":\"test\",\"name\":\"test\",\"description\":\"test\",\"parameters\":{\"test\":\"test\"},\"include_output_in_agent_response\":false,\"config\":{\"configKey\":\"configValue\"}}";
XContentParser parser = XContentType.JSON
.xContent()
.createParser(
new NamedXContentRegistry(new SearchModule(Settings.EMPTY, Collections.emptyList()).getNamedXContents()),
null,
jsonStr
);
parser.nextToken();
MLToolSpec spec = MLToolSpec.parse(parser);

Assert.assertEquals(spec.getType(), "test");
Assert.assertEquals(spec.getName(), "test");
Assert.assertEquals(spec.getDescription(), "test");
jngz-es marked this conversation as resolved.
Show resolved Hide resolved
Assert.assertEquals(spec.getParameters(), Map.of("test", "test"));
Assert.assertEquals(spec.isIncludeOutputInAgentResponse(), false);
Assert.assertEquals(spec.getConfigMap(), Map.of("configKey", "configValue"));
}

@Test
public void parseEmptyConfigMap() throws IOException {
String jsonStr =
"{\"type\":\"test\",\"name\":\"test\",\"description\":\"test\",\"parameters\":{\"test\":\"test\"},\"include_output_in_agent_response\":false}";
XContentParser parser = XContentType.JSON
Expand All @@ -67,11 +148,42 @@ public void parse() throws IOException {
Assert.assertEquals(spec.getDescription(), "test");
Assert.assertEquals(spec.getParameters(), Map.of("test", "test"));
Assert.assertEquals(spec.isIncludeOutputInAgentResponse(), false);
Assert.assertEquals(spec.getConfigMap(), null);
}

@Test
public void fromStream() throws IOException {
MLToolSpec spec = new MLToolSpec("test", "test", "test", Map.of("test", "test"), false);
MLToolSpec spec = new MLToolSpec("test", "test", "test", Map.of("test", "test"), false, Map.of("configKey", "configValue"));
BytesStreamOutput output = new BytesStreamOutput();
spec.writeTo(output);
MLToolSpec spec1 = MLToolSpec.fromStream(output.bytes().streamInput());

Assert.assertEquals(spec.getType(), spec1.getType());
Assert.assertEquals(spec.getName(), spec1.getName());
Assert.assertEquals(spec.getParameters(), spec1.getParameters());
Assert.assertEquals(spec.getDescription(), spec1.getDescription());
Assert.assertEquals(spec.isIncludeOutputInAgentResponse(), spec1.isIncludeOutputInAgentResponse());
Assert.assertEquals(spec.getConfigMap(), spec1.getConfigMap());
}

@Test
public void fromStreamEmptyConfigMap() throws IOException {
MLToolSpec spec = new MLToolSpec("test", "test", "test", Map.of("test", "test"), false, Collections.EMPTY_MAP);
BytesStreamOutput output = new BytesStreamOutput();
spec.writeTo(output);
MLToolSpec spec1 = MLToolSpec.fromStream(output.bytes().streamInput());

Assert.assertEquals(spec.getType(), spec1.getType());
Assert.assertEquals(spec.getName(), spec1.getName());
Assert.assertEquals(spec.getParameters(), spec1.getParameters());
Assert.assertEquals(spec.getDescription(), spec1.getDescription());
Assert.assertEquals(spec.isIncludeOutputInAgentResponse(), spec1.isIncludeOutputInAgentResponse());
Assert.assertEquals(spec.getConfigMap(), spec1.getConfigMap());
}

@Test
public void fromStreamNullConfigMap() throws IOException {
MLToolSpec spec = new MLToolSpec("test", "test", "test", Map.of("test", "test"), false, null);
BytesStreamOutput output = new BytesStreamOutput();
spec.writeTo(output);
MLToolSpec spec1 = MLToolSpec.fromStream(output.bytes().streamInput());
Expand All @@ -81,5 +193,6 @@ public void fromStream() throws IOException {
Assert.assertEquals(spec.getParameters(), spec1.getParameters());
Assert.assertEquals(spec.getDescription(), spec1.getDescription());
Assert.assertEquals(spec.isIncludeOutputInAgentResponse(), spec1.isIncludeOutputInAgentResponse());
Assert.assertEquals(spec.getConfigMap(), spec1.getConfigMap());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void writeTo() throws IOException {
MLAgentType.CONVERSATIONAL.name(),
"test",
new LLMSpec("test_model", Map.of("test_key", "test_value")),
List.of(new MLToolSpec("test", "test", "test", Collections.EMPTY_MAP, false)),
List.of(new MLToolSpec("test", "test", "test", Collections.EMPTY_MAP, false, Collections.EMPTY_MAP)),
Map.of("test", "test"),
new MLMemorySpec("test", "123", 0),
Instant.EPOCH,
Expand Down
Loading
Loading