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 execute field #146

Merged
merged 2 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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 @@ -171,6 +174,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 @@ -255,7 +262,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
Loading