You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _ml-commons-plugin/agents-tools/tools/query-planning-tool.md
+14-46Lines changed: 14 additions & 46 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -17,42 +17,9 @@ Introduced in 3.2
17
17
{: .label .label-purple }
18
18
<!-- vale on -->
19
19
20
-
This is an experimental feature and is not recommended for use in a production environment. For updates on the progress of the feature or if you want to leave feedback, join the discussion on the [OpenSearch forum](https://forum.opensearch.org/).
21
-
{: .warning}
22
-
23
20
The `QueryPlanningTool` generates an OpenSearch query domain-specific language (DSL) query from a natural language question.
24
21
25
-
## Step 1: Enable the agentic search feature flag
26
-
27
-
To use the `QueryPlanningTool`, you must first enable the `agentic_search_enabled` setting:
The following request registers a remote model from Amazon Bedrock and deploys it to your cluster. The API call creates the connector and model in one step.
58
25
@@ -143,12 +110,13 @@ OpenSearch responds with the model ID:
143
110
}
144
111
```
145
112
146
-
## Step 3: Register an agent
113
+
## Step 2: Register an agent
147
114
148
115
You can use any [OpenSearch agent type](https://opensearch.org/docs/latest/ml-commons-plugin/agents-tools/agents/) to run the `QueryPlanningTool`. The following example uses a `flow` agent, which runs a sequence of tools in order and returns the last tool's output.
149
116
150
117
When registering the agent, you can override parameters set during model registration, such as `query_planner_system_prompt` and `query_planner_user_prompt`. The following example registers an agent with a default `llmGenerated` generation_type.
151
118
119
+
### LLM Generated
152
120
```json
153
121
POST /_plugins/_ml/agents/_register
154
122
{
@@ -160,16 +128,17 @@ POST /_plugins/_ml/agents/_register
160
128
"type": "QueryPlanningTool",
161
129
"description": "A general tool to answer any question.",
You can also register an agent with a `user_templates` generation_type which provides an LLM with an array of [OpenSearch Search Templates](https://docs.opensearch.org/latest/api-reference/search-apis/search-template/index/). These templates are provided to the LLM as additional context to assist the LLM in OpenSearch DSL generation. The following example creates a few search templates and registers an agent with a `user_template` generation_type.
139
+
### User Templates
140
+
141
+
You can also register an agent with a `user_templates` generation_type which provides an LLM with an array of [OpenSearch Search Templates]({{site.url}}{{site.baseurl}}/api-reference/search-apis/search-template/index/). These templates are provided to the LLM as additional context to assist the LLM in OpenSearch DSL generation. The following example creates a few search templates and registers an agent with a `user_template` generation_type.
173
142
174
143
Creating search templates :
175
144
```json
@@ -221,7 +190,6 @@ POST /_plugins/_ml/agents/_register
221
190
"description": "A general tool to answer any question",
@@ -240,7 +208,7 @@ POST /_plugins/_ml/agents/_register
240
208
}
241
209
```
242
210
243
-
Each `search_templates`array entry must have a `template_id` and `template_description`. The LLM will use the description as additional context to help it choose the best template to use when generating an OpenSearch DSL query based on the uses provided `query_text`. If the LLM determines that the provided search templates are not applicable to the given `query_text`, then the LLM will use a default match all query search template. It is important to note that the LLM will not directly render the final Opensearch DSL query with the chosen template, but rather use the template source as additional context to help it form the query.
211
+
Each `search_templates`list entry must have a `template_id` and `template_description`. The LLM will use the description as additional context to help it choose the best template to use when generating an OpenSearch DSL query based on the uses provided `question`. If the LLM determines that the provided search templates are not applicable to the given `question`, then the LLM will use a default match all query. It is important to note that the LLM will not directly render the final OpenSearch DSL query with the chosen template, but rather use the template source as additional context to help it form the query.
244
212
245
213
For parameter descriptions, see [Register parameters](#register-parameters).
246
214
@@ -252,16 +220,16 @@ OpenSearch responds with an agent ID:
252
220
}
253
221
```
254
222
255
-
## Step 4: Execute the agent
223
+
## Step 3: Execute the agent
256
224
257
225
Execute the agent by sending the following request:
258
226
259
227
```json
260
228
POST /_plugins/_ml/agents/RNjQi5gBOh0h20Y9-RX1/_execute
261
229
{
262
230
"parameters": {
263
-
"query_planner_user_prompt": "You are an OpenSearch Query DSL generation assistant, generate an OpenSearch Query DSL to retrieve the most relevant documents for the user provided natural language question: ${parameters.query_text}, please return the query dsl only, no other texts. Please don't use size:0, because that would limit the query to return no result. please return a query to find the most relevant documents related to users question. For example: {\"query\":{\"match\":{\"species\":\"setosa\"}}} \n",
264
-
"query_text": "How many iris flowers of type setosa are there?"
231
+
"query_planner_user_prompt": "You are an OpenSearch Query DSL generation assistant, generate an OpenSearch Query DSL to retrieve the most relevant documents for the user provided natural language question: ${parameters.question}, please return the query dsl only, no other texts. Please don't use size:0, because that would limit the query to return no result. please return a query to find the most relevant documents related to users question. For example: {\"query\":{\"match\":{\"species\":\"setosa\"}}} \n",
232
+
"question": "How many iris flowers of type setosa are there?"
`response_filter` | String | Optional | A JSONPath expression used to extract the generated query from the LLM's response.
295
263
`generation_type` | String | Optional | The type of query generation. Currently, only `llmGenerated` and `user_templates` are supported. Defaults to `llmGenerated`.
296
264
`query_planner_system_prompt` | String | Optional | A system prompt that provides high-level instructions to the LLM. Defaults to "You are an OpenSearch Query DSL generation assistant, translating natural language questions to OpenSeach DSL Queries".
297
-
`query_planner_user_prompt` | String | Optional | A user prompt template for the LLM. It can contain placeholders for execution-time parameters like `${parameters.query_text}`.
265
+
`query_planner_user_prompt` | String | Optional | A user prompt template for the LLM. It can contain placeholders for execution-time parameters like `${parameters.question}`.
298
266
`search_templates` | Array | Optional | Applicable only for `user_templates` generation_type. A list of search template IDs and descriptions which an LLM will use as context to create an OpenSearch DSL query. Each entry within the `search_templates` array must include a `template_id` and a `template_description` which provides the LLM with additional context on the contents of the search template
299
267
300
268
## Execute parameters
@@ -307,7 +275,7 @@ There are three layers of parameters to consider:
307
275
308
276
2.**Tool parameters**: The `QueryPlanningTool` uses its own set of parameters, such as `query_planner_user_prompt` and `query_planner_system_prompt`, to construct the final string that will be passed to the connector. The tool takes the `query_planner_user_prompt` string and resolves any variables within it, and the resulting string is then used to fill the appropriate variable (for example, `${parameters.query_planner_user_prompt}`) in the connector's `request_body`.
309
277
310
-
3.**Prompt variables**: These are the variables inside the `query_planner_user_prompt`, which have the format `${parameters.your_variable_name}`. These must be provided in the `_execute` API call. For example, if your `user_prompt` is "Generate a query for: ${parameters.query_text}", then you must provide a `query_text` parameter when you run the agent.
278
+
3.**Prompt variables**: These are the variables inside the `query_planner_user_prompt`, which have the format `${parameters.your_variable_name}`. These must be provided in the `_execute` API call. For example, if your `user_prompt` is "Generate a query for: ${parameters.question}", then you must provide a `question` parameter when you run the agent.
311
279
312
280
In summary, the required parameters for an `_execute` call are the **prompt variables**. The tool's own parameters (like `query_planner_user_prompt`) can be overridden at execution time to change how the final prompt is constructed.
313
281
@@ -351,7 +319,7 @@ POST /_plugins/_ml/agents/your_agent_id/_execute
351
319
352
320
## Next steps
353
321
354
-
This is an experimental feature. See the following GitHub issues for information about future enhancements:
322
+
See the following GitHub issues for information about future enhancements:
355
323
356
324
-[[RFC] Design for Agentic Search #1479](https://github.com/opensearch-project/neural-search/issues/1479)
357
325
-[[RFC] Agentic Search in OpenSearch #4005](https://github.com/opensearch-project/ml-commons/issues/4005)
0 commit comments