Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* add execute field

* apply spotless

---------

(cherry picked from commit 16a26ce)

Signed-off-by: xinyual <[email protected]>
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Signed-off-by: yuye-aws <[email protected]>
  • Loading branch information
2 people authored and yuye-aws committed Apr 26, 2024
1 parent f1945a9 commit c0c9e38
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/main/java/org/opensearch/agent/tools/PPLTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ public class PPLTool implements Tool {

private String contextPrompt;

private Boolean execute;

private PPLModelType pplModelType;

private static Gson gson = new Gson();
Expand Down Expand Up @@ -120,7 +122,7 @@ public static PPLModelType from(String value) {

}

public PPLTool(Client client, String modelId, String contextPrompt, String pplModelType) {
public PPLTool(Client client, String modelId, String contextPrompt, String pplModelType, boolean execute) {
this.client = client;
this.modelId = modelId;
this.pplModelType = PPLModelType.from(pplModelType);
Expand All @@ -129,6 +131,7 @@ public PPLTool(Client client, String modelId, String contextPrompt, String pplMo
} else {
this.contextPrompt = contextPrompt;
}
this.execute = execute;
}

@Override
Expand Down Expand Up @@ -169,6 +172,10 @@ public <T> void run(Map<String, String> parameters, ActionListener<T> listener)
ModelTensor modelTensor = modelTensors.getMlModelTensors().get(0);
Map<String, String> dataAsMap = (Map<String, String>) modelTensor.getDataAsMap();
String ppl = parseOutput(dataAsMap.get("response"), indexName);
if (!this.execute) {
listener.onResponse((T) ppl);
return;
}
JSONObject jsonContent = new JSONObject(ImmutableMap.of("query", ppl));
PPLQueryRequest pplQueryRequest = new PPLQueryRequest(ppl, jsonContent, null, "jdbc");
TransportPPLQueryRequest transportPPLQueryRequest = new TransportPPLQueryRequest(pplQueryRequest);
Expand Down Expand Up @@ -253,7 +260,8 @@ public PPLTool create(Map<String, Object> map) {
client,
(String) map.get("model_id"),
(String) map.getOrDefault("prompt", ""),
(String) map.getOrDefault("model_type", "")
(String) map.getOrDefault("model_type", ""),
(boolean) map.getOrDefault("execute", true)
);
}

Expand Down
13 changes: 13 additions & 0 deletions src/test/java/org/opensearch/agent/tools/PPLToolTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,19 @@ public void testTool() {

}

@Test
public void testTool_with_WithoutExecution() {
PPLTool tool = PPLTool.Factory
.getInstance()
.create(ImmutableMap.of("model_id", "modelId", "model_type", "claude", "execute", false));
assertEquals(PPLTool.TYPE, tool.getName());

tool.run(ImmutableMap.of("index", "demo", "question", "demo"), ActionListener.<String>wrap(executePPLResult -> {
assertEquals("source=demo| head 1", executePPLResult);
}, e -> { log.info(e); }));

}

@Test
public void testTool_with_DefaultPrompt() {
PPLTool tool = PPLTool.Factory.getInstance().create(ImmutableMap.of("model_id", "modelId", "model_type", "claude"));
Expand Down

0 comments on commit c0c9e38

Please sign in to comment.