|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package org.opensearch.ml.engine.tools; |
| 7 | + |
| 8 | +import java.util.List; |
| 9 | +import java.util.Map; |
| 10 | + |
| 11 | +import org.opensearch.core.action.ActionListener; |
| 12 | +import org.opensearch.ml.common.output.model.ModelTensors; |
| 13 | +import org.opensearch.ml.common.spi.tools.Parser; |
| 14 | +import org.opensearch.ml.common.spi.tools.ToolAnnotation; |
| 15 | +import org.opensearch.ml.common.spi.tools.WithModelTool; |
| 16 | +import org.opensearch.ml.repackage.com.google.common.annotations.VisibleForTesting; |
| 17 | +import org.opensearch.transport.client.Client; |
| 18 | + |
| 19 | +import lombok.Getter; |
| 20 | +import lombok.Setter; |
| 21 | +import lombok.extern.log4j.Log4j2; |
| 22 | + |
| 23 | +/** |
| 24 | + * This tool supports different types of query planning, |
| 25 | + * llmGenerated, systemSearchTemplates or userSearchTemplates. |
| 26 | + * //TODO only support llmGenerated for now. |
| 27 | + * //TODO to add in systemSearchTemplates or userSearchTemplates when searchTemplatesTool is implemented. |
| 28 | + */ |
| 29 | +@Log4j2 |
| 30 | +@ToolAnnotation(QueryPlanningTool.TYPE) |
| 31 | +public class QueryPlanningTool implements WithModelTool { |
| 32 | + public static final String TYPE = "QueryPlanningTool"; |
| 33 | + public static final String MODEL_ID_FIELD = "model_id"; |
| 34 | + private final MLModelTool queryGenerationTool; |
| 35 | + public static final String PROMPT_FIELD = "prompt"; |
| 36 | + private static final String GENERATION_TYPE_FIELD = "generation_type"; |
| 37 | + private static final String LLM_GENERATED_TYPE_FIELD = "llmGenerated"; |
| 38 | + private final String generationType; |
| 39 | + @Setter |
| 40 | + @Getter |
| 41 | + private String name = TYPE; |
| 42 | + @Getter |
| 43 | + @Setter |
| 44 | + private Map<String, Object> attributes; |
| 45 | + @VisibleForTesting |
| 46 | + static String DEFAULT_DESCRIPTION = "Use this tool to generate query plans for a given query text."; |
| 47 | + @Getter |
| 48 | + @Setter |
| 49 | + private String description = DEFAULT_DESCRIPTION; |
| 50 | + private String defaultPrompt = |
| 51 | + "You are an OpenSearch Query DSL generation assistant; using the provided index mapping ${parameters.ListIndexTool.output:-}, specified fields ${parameters.fields:-}, and the given sample queries as examples, generate an OpenSearch Query DSL to retrieve the most relevant documents for the user provided natural language question: ${parameters.query_text}\n"; |
| 52 | + @Getter |
| 53 | + private Client client; |
| 54 | + @Getter |
| 55 | + private String modelId; |
| 56 | + @Setter |
| 57 | + @Getter |
| 58 | + @VisibleForTesting |
| 59 | + private Parser outputParser; |
| 60 | + @Setter |
| 61 | + @Getter |
| 62 | + private String responseField; |
| 63 | + |
| 64 | + public QueryPlanningTool(Client client, String modelId, String generationType, MLModelTool queryGenerationTool) { |
| 65 | + this.client = client; |
| 66 | + this.modelId = modelId; |
| 67 | + this.generationType = generationType; |
| 68 | + this.queryGenerationTool = queryGenerationTool; |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public <T> void run(Map<String, String> parameters, ActionListener<T> listener) { |
| 73 | + if (!parameters.containsKey(PROMPT_FIELD)) { |
| 74 | + parameters.put(PROMPT_FIELD, defaultPrompt); |
| 75 | + } |
| 76 | + ActionListener<List<ModelTensors>> modelListener = ActionListener.wrap(r -> { |
| 77 | + try { |
| 78 | + @SuppressWarnings("unchecked") |
| 79 | + T result = (T) outputParser.parse(r); |
| 80 | + listener.onResponse(result); |
| 81 | + } catch (Exception e) { |
| 82 | + listener.onFailure(e); |
| 83 | + } |
| 84 | + }, listener::onFailure); |
| 85 | + queryGenerationTool.run(parameters, modelListener); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public String getType() { |
| 90 | + return TYPE; |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public String getVersion() { |
| 95 | + return null; |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public String getName() { |
| 100 | + return this.name; |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public void setName(String s) { |
| 105 | + this.name = s; |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public boolean validate(Map<String, String> parameters) { |
| 110 | + if (parameters == null || parameters.size() == 0) { |
| 111 | + return false; |
| 112 | + } |
| 113 | + return true; |
| 114 | + } |
| 115 | + |
| 116 | + public static class Factory implements WithModelTool.Factory<QueryPlanningTool> { |
| 117 | + private Client client; |
| 118 | + |
| 119 | + private static Factory INSTANCE; |
| 120 | + |
| 121 | + public static Factory getInstance() { |
| 122 | + if (INSTANCE != null) { |
| 123 | + return INSTANCE; |
| 124 | + } |
| 125 | + synchronized (QueryPlanningTool.class) { |
| 126 | + if (INSTANCE != null) { |
| 127 | + return INSTANCE; |
| 128 | + } |
| 129 | + INSTANCE = new Factory(); |
| 130 | + return INSTANCE; |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + public void init(Client client) { |
| 135 | + this.client = client; |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public QueryPlanningTool create(Map<String, Object> map) { |
| 140 | + |
| 141 | + MLModelTool queryGenerationTool = MLModelTool.Factory.getInstance().create(map); |
| 142 | + |
| 143 | + String type = (String) map.get(GENERATION_TYPE_FIELD); |
| 144 | + if (type == null || type.isEmpty()) { |
| 145 | + type = LLM_GENERATED_TYPE_FIELD; |
| 146 | + } |
| 147 | + |
| 148 | + // TODO to add in , SYSTEM_SEARCH_TEMPLATES_TYPE_FIELD, USER_SEARCH_TEMPLATES_TYPE_FIELD when searchTemplatesTool is |
| 149 | + // implemented. |
| 150 | + if (!LLM_GENERATED_TYPE_FIELD.equals(type)) { |
| 151 | + throw new IllegalArgumentException("Invalid generation type: " + type + ". The current supported types are llmGenerated."); |
| 152 | + } |
| 153 | + return new QueryPlanningTool(client, (String) map.get(MODEL_ID_FIELD), type, queryGenerationTool); |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + public String getDefaultDescription() { |
| 158 | + return DEFAULT_DESCRIPTION; |
| 159 | + } |
| 160 | + |
| 161 | + @Override |
| 162 | + public String getDefaultType() { |
| 163 | + return TYPE; |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + public String getDefaultVersion() { |
| 168 | + return null; |
| 169 | + } |
| 170 | + |
| 171 | + @Override |
| 172 | + public List<String> getAllModelKeys() { |
| 173 | + return List.of(MODEL_ID_FIELD); |
| 174 | + } |
| 175 | + } |
| 176 | +} |
0 commit comments