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

[Backport 2.x] add truncate For PPL Tool #211

Merged
merged 1 commit into from
Feb 8, 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
22 changes: 21 additions & 1 deletion src/main/java/org/opensearch/agent/tools/PPLTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ public class PPLTool implements Tool {

private String previousToolKey;

private int head;

private static Gson gson = new Gson();

private static Map<String, String> DEFAULT_PROMPT_DICT;
Expand Down Expand Up @@ -147,7 +149,15 @@ public static PPLModelType from(String value) {

}

public PPLTool(Client client, String modelId, String contextPrompt, String pplModelType, String previousToolKey, boolean execute) {
public PPLTool(
Client client,
String modelId,
String contextPrompt,
String pplModelType,
String previousToolKey,
int head,
boolean execute
) {
this.client = client;
this.modelId = modelId;
this.pplModelType = PPLModelType.from(pplModelType);
Expand All @@ -157,6 +167,7 @@ public PPLTool(Client client, String modelId, String contextPrompt, String pplMo
this.contextPrompt = contextPrompt;
}
this.previousToolKey = previousToolKey;
this.head = head;
this.execute = execute;
}

Expand Down Expand Up @@ -307,6 +318,7 @@ public PPLTool create(Map<String, Object> map) {
(String) map.getOrDefault("prompt", ""),
(String) map.getOrDefault("model_type", ""),
(String) map.getOrDefault("previous_tool_name", ""),
Integer.valueOf((String) map.getOrDefault("head", "-1")),
Boolean.valueOf((String) map.getOrDefault("execute", "true"))
);
}
Expand Down Expand Up @@ -487,6 +499,14 @@ private String parseOutput(String llmOutput, String indexName) {
}
ppl = ppl.replace("`", "");
ppl = ppl.replaceAll("\\bSPAN\\(", "span(");
if (this.head > 0) {
String[] lists = llmOutput.split("\\|");
String lastCommand = lists[lists.length - 1].strip();
if (!lastCommand.toLowerCase(Locale.ROOT).startsWith("head")) // not handle cases source=...| ... | head 5 | head <head>
{
ppl = ppl + " | head " + this.head;
}
}
return ppl;
}

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

}

@Test
public void testTool_withHEADButIgnore() {
PPLTool tool = PPLTool.Factory.getInstance().create(ImmutableMap.of("model_id", "modelId", "prompt", "contextPrompt", "head", "5"));
assertEquals(PPLTool.TYPE, tool.getName());

tool.run(ImmutableMap.of("index", "demo", "question", "demo"), ActionListener.<String>wrap(executePPLResult -> {
Map<String, String> returnResults = gson.fromJson(executePPLResult, Map.class);
assertEquals("ppl result", returnResults.get("executionResult"));
assertEquals("source=demo| head 1", returnResults.get("ppl"));
}, e -> { log.info(e); }));

}

@Test
public void testTool_withHEAD() {
pplReturns = Collections.singletonMap("response", "source=demo");
modelTensor = new ModelTensor("tensor", new Number[0], new long[0], MLResultDataType.STRING, null, null, pplReturns);
initMLTensors();

PPLTool tool = PPLTool.Factory.getInstance().create(ImmutableMap.of("model_id", "modelId", "prompt", "contextPrompt", "head", "5"));
assertEquals(PPLTool.TYPE, tool.getName());

tool.run(ImmutableMap.of("index", "demo", "question", "demo"), ActionListener.<String>wrap(executePPLResult -> {
Map<String, String> returnResults = gson.fromJson(executePPLResult, Map.class);
assertEquals("ppl result", returnResults.get("executionResult"));
assertEquals("source=demo | head 5", returnResults.get("ppl"));
}, e -> { log.info(e); }));

}

@Test
public void testTool_with_WithoutExecution() {
PPLTool tool = PPLTool.Factory
Expand Down
Loading