-
Notifications
You must be signed in to change notification settings - Fork 33
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
Support pass prompt to CreateAlertTool #452
Changes from 4 commits
2ff4650
bfe59b7
e76e848
a1c34f2
d08081d
585e6d5
f779fd9
da90a27
a49a868
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Locale; | ||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
import java.util.Optional; | ||
|
@@ -62,29 +63,53 @@ public class CreateAlertTool implements Tool { | |
private String description = DEFAULT_DESCRIPTION; | ||
|
||
private final Client client; | ||
@Getter | ||
private final String modelId; | ||
private final String TOOL_PROMPT_TEMPLATE; | ||
@Getter | ||
private final String toolPrompt; | ||
|
||
private static final String MODEL_ID = "model_id"; | ||
private static final String PROMPT_FILE_PATH = "CreateAlertDefaultPrompt.json"; | ||
private static final String DEFAULT_QUESTION = "Create an alert as your recommendation based on the context"; | ||
private static final Map<String, String> promptDict = ToolHelper.loadDefaultPromptDictFromFile(CreateAlertTool.class, PROMPT_FILE_PATH); | ||
|
||
public CreateAlertTool(Client client, String modelId, String modelType) { | ||
public enum ModelType { | ||
CLAUDE, | ||
OPENAI; | ||
|
||
public static ModelType from(String value) { | ||
if (value.isEmpty()) { | ||
return ModelType.CLAUDE; | ||
} | ||
try { | ||
return ModelType.valueOf(value.toUpperCase(Locale.ROOT)); | ||
} catch (Exception e) { | ||
log.error("Wrong Model type, should be CLAUDE or OPENAI"); | ||
return ModelType.CLAUDE; | ||
} | ||
} | ||
} | ||
|
||
public CreateAlertTool(Client client, String modelId, String modelType, String prompt) { | ||
this.client = client; | ||
this.modelId = modelId; | ||
if (!promptDict.containsKey(modelType)) { | ||
throw new IllegalArgumentException( | ||
LoggerMessageFormat | ||
.format( | ||
null, | ||
"Failed to find the right prompt for modelType: {}, this tool supports prompts for these models: [{}]", | ||
modelType, | ||
String.join(",", promptDict.keySet()) | ||
) | ||
); | ||
modelType = String.valueOf(ModelType.from(modelType)); | ||
if (prompt.isEmpty()) { | ||
if (!promptDict.containsKey(modelType)) { | ||
qianheng-aws marked this conversation as resolved.
Show resolved
Hide resolved
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. The model type is enumerated, do we need to judge whether promptDict contains the model type? 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. This is the right line for identify whether promptDict contains that model. It will throw exception if not having. |
||
throw new IllegalArgumentException( | ||
LoggerMessageFormat | ||
.format( | ||
null, | ||
"Failed to find the right prompt for modelType: {}, this tool supports prompts for these models: [{}]", | ||
modelType, | ||
String.join(",", promptDict.keySet()) | ||
) | ||
); | ||
} | ||
this.toolPrompt = promptDict.get(modelType); | ||
} else { | ||
this.toolPrompt = prompt; | ||
} | ||
TOOL_PROMPT_TEMPLATE = promptDict.get(modelType); | ||
} | ||
|
||
@Override | ||
|
@@ -205,7 +230,7 @@ private ActionRequest constructMLPredictRequest(Map<String, String> tmpParams, S | |
tmpParams.putIfAbsent("chat_history", ""); | ||
tmpParams.putIfAbsent("question", DEFAULT_QUESTION); // In case no question is provided, use a default question. | ||
StringSubstitutor substitute = new StringSubstitutor(tmpParams, "${parameters.", "}"); | ||
String finalToolPrompt = substitute.replace(TOOL_PROMPT_TEMPLATE); | ||
String finalToolPrompt = substitute.replace(toolPrompt); | ||
tmpParams.put("prompt", finalToolPrompt); | ||
|
||
RemoteInferenceInputDataSet inputDataSet = RemoteInferenceInputDataSet.builder().parameters(tmpParams).build(); | ||
|
@@ -279,7 +304,8 @@ public CreateAlertTool create(Map<String, Object> params) { | |
throw new IllegalArgumentException("model_id cannot be null or blank."); | ||
} | ||
String modelType = (String) params.getOrDefault("model_type", ModelType.CLAUDE.toString()); | ||
return new CreateAlertTool(client, modelId, modelType); | ||
String prompt = (String) params.getOrDefault("prompt", ""); | ||
return new CreateAlertTool(client, modelId, modelType, prompt); | ||
} | ||
|
||
@Override | ||
|
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.
Wait a bit. The code is the same as the ModelType in PPL tool. Can you extract it outside the function for code reusage?
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.
They are not exactly the same, this tool doesn't support
FINETUNE
model. If we want to reuse this method, we'd better refactor it as well and I think it should also applies to other tools likeCreateAnomalyDetectorTool
.So I don't think it's a blocker for this PR, we can do such refactor work in a separate PR.
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.
I see. Then the code is good.