Skip to content

Commit 7d00b02

Browse files
bartovalstratoula
andauthored
[ES|QL] Supports autocomplete for RERANK (elastic#234385)
## Summary part of elastic#217285 **pre** To test the **Rerank** command, we need to change the flag _hidden_ to **false** , here src/platform/packages/shared/kbn-esql-ast/src/commands_registry/commands/rerank/index.ts <img width="824" height="115" alt="1" src="https://github.com/user-attachments/assets/56dfb873-064f-441f-b1bc-ae949ac838a9" /> <img width="905" height="132" alt="2" src="https://github.com/user-attachments/assets/271a3552-6f40-47be-b223-14c3f6fc0369" /> <img width="1037" height="104" alt="3" src="https://github.com/user-attachments/assets/9a863525-1abe-4c08-9a8c-a466efb80067" /> <img width="1130" height="356" alt="4" src="https://github.com/user-attachments/assets/d6be76d2-d485-4ae4-9223-a06a1f3257f0" /> <img width="1140" height="260" alt="5" src="https://github.com/user-attachments/assets/75d320c6-8dac-482d-abcd-777130a04382" /> <img width="1342" height="248" alt="7" src="https://github.com/user-attachments/assets/a46e5747-9ecb-40f5-9581-6ce3ce47b6ff" /> <img width="1207" height="393" alt="6" src="https://github.com/user-attachments/assets/7e4895e8-218d-4469-95ea-ee11fde876cf" /> <img width="1461" height="232" alt="8" src="https://github.com/user-attachments/assets/7a3bb6f6-ec80-47e3-b8c9-cd992a1531ae" /> --------- Co-authored-by: Stratou <efstratia.kalafateli@elastic.co>
1 parent 7e64538 commit 7d00b02

18 files changed

Lines changed: 1155 additions & 68 deletions

File tree

src/platform/packages/shared/kbn-esql-ast/scripts/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export const defaultScalarFunctionLocations: Location[] = [
3030
Location.STATS_WHERE,
3131
Location.STATS_TIMESERIES,
3232
Location.COMPLETION,
33+
Location.RERANK,
3334
];
3435

3536
export const defaultAggFunctionLocations: Location[] = [Location.STATS];

src/platform/packages/shared/kbn-esql-ast/scripts/generate_function_definitions.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ const enrichOperators = (
180180
Location.STATS_WHERE,
181181
Location.STATS_BY,
182182
Location.COMPLETION,
183+
Location.RERANK,
183184
]);
184185
// Adding comparison operator signatures for ip and version types
185186
signatures.push(...comparisonOperatorSignatures);
@@ -195,6 +196,7 @@ const enrichOperators = (
195196
Location.STATS_WHERE,
196197
Location.STATS_BY,
197198
Location.COMPLETION,
199+
Location.RERANK,
198200
]);
199201

200202
// taking care the `...EVAL col = @timestamp + 1 year` cases
@@ -210,6 +212,7 @@ const enrichOperators = (
210212
Location.ROW,
211213
Location.STATS_WHERE,
212214
Location.COMPLETION,
215+
Location.RERANK,
213216
];
214217
}
215218
if (isInOperator) {

src/platform/packages/shared/kbn-esql-ast/src/commands_registry/commands/completion/autocomplete.ts

Lines changed: 28 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
*/
99
import { i18n } from '@kbn/i18n';
1010
import { uniqBy } from 'lodash';
11-
import type { InferenceEndpointAutocompleteItem } from '@kbn/esql-types';
1211
import type * as ast from '../../../types';
1312
import { getCommandMapExpressionSuggestions } from '../../../definitions/utils/autocomplete/map_expression';
1413
import { EDITOR_MARKER } from '../../../definitions/constants';
@@ -17,13 +16,16 @@ import {
1716
pipeCompleteItem,
1817
assignCompletionItem,
1918
getNewUserDefinedColumnSuggestion,
19+
withCompleteItem,
2020
} from '../../complete_items';
2121
import {
2222
getFieldsOrFunctionsSuggestions,
2323
findFinalWord,
2424
handleFragment,
2525
columnExists,
26+
createInferenceEndpointToCompletionItem,
2627
} from '../../../definitions/utils/autocomplete/helpers';
28+
import { buildConstantsDefinitions } from '../../../definitions/utils/literals';
2729
import {
2830
type ISuggestionItem,
2931
Location,
@@ -84,41 +86,8 @@ function getPosition(
8486

8587
return undefined;
8688
}
87-
88-
const defaultPrompt: ISuggestionItem = {
89-
detail: '',
90-
kind: 'Constant',
91-
asSnippet: true,
92-
label: 'Your prompt to the LLM',
93-
sortText: '1',
94-
text: '"${0:Your prompt to the LLM.}"',
95-
};
96-
97-
const withCompletionItem: ISuggestionItem = {
98-
detail: i18n.translate('kbn-esql-ast.esql.definitions.completionWithDoc', {
99-
defaultMessage: 'Provide additional parameters for the LLM prompt.',
100-
}),
101-
kind: 'Reference',
102-
label: 'WITH',
103-
sortText: '1',
104-
asSnippet: true,
105-
text: 'WITH { $0 }',
106-
command: TRIGGER_SUGGESTION_COMMAND,
107-
};
108-
109-
function inferenceEndpointToCompletionItem(
110-
inferenceEndpoint: InferenceEndpointAutocompleteItem
111-
): ISuggestionItem {
112-
return {
113-
detail: i18n.translate('kbn-esql-ast.esql.definitions.completionInferenceIdDoc', {
114-
defaultMessage: 'Inference endpoint used for the completion',
115-
}),
116-
kind: 'Reference',
117-
label: inferenceEndpoint.inference_id,
118-
sortText: '1',
119-
text: inferenceEndpoint.inference_id,
120-
};
121-
}
89+
const promptText = 'Your prompt to the LLM.';
90+
const promptSnippetText = `"$\{0:${promptText}}"`;
12291

12392
export async function autocomplete(
12493
query: string,
@@ -130,6 +99,7 @@ export async function autocomplete(
13099
if (!callbacks?.getByType) {
131100
return [];
132101
}
102+
133103
const innerText = query.substring(0, cursorPosition);
134104
const { prompt } = command as ESQLAstCompletionCommand;
135105
const position = getPosition(innerText, command, context);
@@ -185,7 +155,11 @@ export async function autocomplete(
185155
const lastWord = findFinalWord(innerText);
186156

187157
if (!lastWord) {
188-
suggestions.push(defaultPrompt);
158+
suggestions.push({
159+
...buildConstantsDefinitions([promptSnippetText], '', '1')[0],
160+
label: promptText,
161+
asSnippet: true,
162+
});
189163
}
190164

191165
if (position !== CompletionPosition.AFTER_TARGET_ID) {
@@ -198,7 +172,14 @@ export async function autocomplete(
198172
}
199173

200174
case CompletionPosition.AFTER_PROMPT:
201-
return [withCompletionItem];
175+
return [
176+
{
177+
...withCompleteItem,
178+
detail: i18n.translate('kbn-esql-ast.esql.definitions.completionWithDoc', {
179+
defaultMessage: 'Provide additional parameters for the LLM prompt.',
180+
}),
181+
},
182+
];
202183

203184
case CompletionPosition.AFTER_PROMPT_OR_TARGET: {
204185
const lastWord = findFinalWord(query);
@@ -211,12 +192,19 @@ export async function autocomplete(
211192
return [assignCompletionItem];
212193
}
213194

214-
return [withCompletionItem];
195+
return [
196+
{
197+
...withCompleteItem,
198+
detail: i18n.translate('kbn-esql-ast.esql.definitions.completionWithDoc', {
199+
defaultMessage: 'Provide additional parameters for the LLM prompt.',
200+
}),
201+
},
202+
];
215203
}
216204

217205
case CompletionPosition.WITHIN_MAP_EXPRESSION:
218206
const availableParameters = {
219-
inference_id: endpoints?.map(inferenceEndpointToCompletionItem) || [],
207+
inference_id: endpoints?.map(createInferenceEndpointToCompletionItem) || [],
220208
};
221209

222210
return getCommandMapExpressionSuggestions(innerText, availableParameters);

0 commit comments

Comments
 (0)