-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* tool interface Signed-off-by: Jing Zhang <[email protected]> * remove sync run method Signed-off-by: Jing Zhang <[email protected]> --------- Signed-off-by: Jing Zhang <[email protected]>
- Loading branch information
Showing
4 changed files
with
179 additions
and
0 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
spi/src/main/java/org/opensearch/ml/common/spi/MLCommonsExtension.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,22 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.ml.common.spi; | ||
|
||
import org.opensearch.ml.common.spi.tools.Tool; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* ml-commons extension interface. | ||
*/ | ||
public interface MLCommonsExtension { | ||
|
||
/** | ||
* Get tool factories. | ||
* @return A list of tool factories | ||
*/ | ||
List<Tool.Factory<? extends Tool>> getToolFactories(); | ||
} |
21 changes: 21 additions & 0 deletions
21
spi/src/main/java/org/opensearch/ml/common/spi/tools/Parser.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,21 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.ml.common.spi.tools; | ||
|
||
/** | ||
* General parser interface. | ||
* @param <S> The input type | ||
* @param <T> The return type | ||
*/ | ||
public interface Parser<S, T> { | ||
|
||
/** | ||
* Parse input. | ||
* @param input the parser input | ||
* @return output the parser output | ||
*/ | ||
T parse(S input); | ||
} |
119 changes: 119 additions & 0 deletions
119
spi/src/main/java/org/opensearch/ml/common/spi/tools/Tool.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,119 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.ml.common.spi.tools; | ||
|
||
import org.opensearch.core.action.ActionListener; | ||
import java.util.Map; | ||
|
||
/** | ||
* General tool interface. | ||
*/ | ||
public interface Tool { | ||
|
||
/** | ||
* Run tool and return response asynchronously. | ||
* @param parameters input parameters | ||
* @param listener an action listener for the response | ||
* @param <T> The output type | ||
*/ | ||
default <T> void run(Map<String, String> parameters, ActionListener<T> listener) {}; | ||
|
||
/** | ||
* Set input parser. | ||
* @param parser the parser to set | ||
*/ | ||
default void setInputParser(Parser<?, ?> parser) {}; | ||
|
||
/** | ||
* Set output parser. | ||
* @param parser the parser to set | ||
*/ | ||
default void setOutputParser(Parser<?, ?> parser) {}; | ||
|
||
/** | ||
* Get tool type. | ||
* Agent uses the type to find the tool. | ||
* @return the tool type | ||
*/ | ||
String getType(); | ||
|
||
/** | ||
* Get tool version. | ||
* @return the tool version | ||
*/ | ||
String getVersion(); | ||
|
||
/** | ||
* Get tool name which is displayed in prompt. | ||
* @return the tool name | ||
*/ | ||
String getName(); | ||
|
||
/** | ||
* Set tool name which is displayed in prompt. | ||
* @param name the tool name | ||
*/ | ||
void setName(String name); | ||
|
||
/** | ||
* Get tool description. | ||
* @return the tool description | ||
*/ | ||
String getDescription(); | ||
|
||
/** | ||
* Set tool description. | ||
* @param description the description to set | ||
*/ | ||
void setDescription(String description); | ||
|
||
/** | ||
* Validate if the input is good. | ||
* @param parameters input parameters | ||
* @return true if the input is valid | ||
*/ | ||
boolean validate(Map<String, String> parameters); | ||
|
||
/** | ||
* Check if should end the whole CoT immediately. | ||
* For example, if some critical error detected like high memory pressure, | ||
* the tool may end the whole CoT process by returning true. | ||
* @param input tool input string | ||
* @param toolParameters map of input parameters | ||
* @return true as a signal to CoT to end the chain, false to continue CoT | ||
*/ | ||
default boolean end(String input, Map<String, String> toolParameters) { | ||
return false; | ||
} | ||
|
||
/** | ||
* The tool runs against the original human input. | ||
* @return | ||
*/ | ||
default boolean useOriginalInput() { | ||
return false; | ||
} | ||
|
||
/** | ||
* Tool factory which can create instance of {@link Tool}. | ||
* @param <T> The subclass this factory produces | ||
*/ | ||
interface Factory<T extends Tool> { | ||
/** | ||
* Create an instance of this tool. | ||
* | ||
* @param params Parameters for the tool | ||
* @return an instance of this tool | ||
*/ | ||
T create(Map<String, Object> params); | ||
|
||
/** | ||
* Get the default description of this tool. | ||
* @return the default description | ||
*/ | ||
String getDefaultDescription(); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
spi/src/main/java/org/opensearch/ml/common/spi/tools/ToolAnnotation.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,17 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.ml.common.spi.tools; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.TYPE) | ||
public @interface ToolAnnotation { | ||
String value(); | ||
} |