-
Notifications
You must be signed in to change notification settings - Fork 138
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
Changes from all commits
Commits
Show all changes
5 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 |
---|---|---|
|
@@ -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; | ||
|
@@ -24,20 +26,31 @@ | |
@EqualsAndHashCode | ||
@Getter | ||
public class MLToolSpec implements ToXContentObject { | ||
public static final Version MINIMAL_SUPPORTED_VERSION_FOR_TOOL_CONFIG = CommonValue.VERSION_2_18_0; | ||
|
||
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 | ||
) { | ||
if (type == null) { | ||
throw new IllegalArgumentException("tool type is null"); | ||
} | ||
|
@@ -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 { | ||
|
@@ -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::readString, StreamInput::readOptionalString); | ||
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. why 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. Yes, this allows you to override a value with a non-value. |
||
} | ||
} | ||
|
||
public void writeTo(StreamOutput out) throws IOException { | ||
|
@@ -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::writeString, StreamOutput::writeOptionalString); | ||
} else { | ||
out.writeBoolean(false); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
|
@@ -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.isEmpty()) { | ||
builder.field(CONFIG_FIELD, configMap); | ||
} | ||
builder.endObject(); | ||
return builder; | ||
} | ||
|
@@ -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) { | ||
|
@@ -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; | ||
|
@@ -131,6 +163,7 @@ public static MLToolSpec parse(XContentParser parser) throws IOException { | |
.description(description) | ||
.parameters(parameters) | ||
.includeOutputInAgentResponse(includeOutputInAgentResponse) | ||
.configMap(configMap) | ||
.build(); | ||
} | ||
|
||
|
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.
Oops, something went wrong.
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.
Is it valid that all the configs are Strings? Is it better to use Map<String, Object> here?
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.
+1 on this
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.
-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.