Skip to content

Commit 5413cff

Browse files
[8.x] [NL-to-ESQL] update internal documentation (#205853) (#205995)
# Backport This will backport the following commits from `main` to `8.x`: - [[NL-to-ESQL] update internal documentation (#205853)](#205853) <!--- Backport version: 9.4.3 --> ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) <!--BACKPORT [{"author":{"name":"Pierre Gayvallet","email":"[email protected]"},"sourceCommit":{"committedDate":"2025-01-09T07:04:29Z","message":"[NL-to-ESQL] update internal documentation (#205853)\n\n## Summary\r\n\r\nFix https://github.com/elastic/kibana/issues/205606\r\n\r\n- Re-generate the internal ES|QL documentation using the generation\r\nscript (+ human review)\r\n- Add more scenario to the NL-to-ESQL evaluation suite \r\n- Some prompt engineering\r\n - improving the system instructions / functions summary\r\n - add more examples to the summary\r\n - adapt a few opinionated examples for some specific functions \r\n\r\n## Evaluation\r\n\r\n- average based on 4 runs for each model/branch tuple\r\n- the new tests were locally added to main to run against the same suite\r\nand properly evaluate the difference\r\n\r\n| Model | before (main) | after (PR) | delta |\r\n| ------------- | ------------- | ------------- | ------------- |\r\n| GPT-4o | 90.9 | 97.74 | + 6.84 |\r\n| Claude 3.5 Sonnet v2 | 88.58 | 96.49 | +7.91 |\r\n| Gemini 1.5-pro-002 | 88.17 | 94.19 | +6.02 |\r\n\r\nOverall, the prompt engineering somewhat significantly improved the\r\ngeneration efficiency.","sha":"5b9691278165417d4cc12853f58de728aaeff011","branchLabelMapping":{"^v9.0.0$":"main","^v8.18.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","v9.0.0","backport:version","Team:AI Infra","v8.18.0"],"title":"[NL-to-ESQL] update internal documentation","number":205853,"url":"https://github.com/elastic/kibana/pull/205853","mergeCommit":{"message":"[NL-to-ESQL] update internal documentation (#205853)\n\n## Summary\r\n\r\nFix https://github.com/elastic/kibana/issues/205606\r\n\r\n- Re-generate the internal ES|QL documentation using the generation\r\nscript (+ human review)\r\n- Add more scenario to the NL-to-ESQL evaluation suite \r\n- Some prompt engineering\r\n - improving the system instructions / functions summary\r\n - add more examples to the summary\r\n - adapt a few opinionated examples for some specific functions \r\n\r\n## Evaluation\r\n\r\n- average based on 4 runs for each model/branch tuple\r\n- the new tests were locally added to main to run against the same suite\r\nand properly evaluate the difference\r\n\r\n| Model | before (main) | after (PR) | delta |\r\n| ------------- | ------------- | ------------- | ------------- |\r\n| GPT-4o | 90.9 | 97.74 | + 6.84 |\r\n| Claude 3.5 Sonnet v2 | 88.58 | 96.49 | +7.91 |\r\n| Gemini 1.5-pro-002 | 88.17 | 94.19 | +6.02 |\r\n\r\nOverall, the prompt engineering somewhat significantly improved the\r\ngeneration efficiency.","sha":"5b9691278165417d4cc12853f58de728aaeff011"}},"sourceBranch":"main","suggestedTargetBranches":["8.x"],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/205853","number":205853,"mergeCommit":{"message":"[NL-to-ESQL] update internal documentation (#205853)\n\n## Summary\r\n\r\nFix https://github.com/elastic/kibana/issues/205606\r\n\r\n- Re-generate the internal ES|QL documentation using the generation\r\nscript (+ human review)\r\n- Add more scenario to the NL-to-ESQL evaluation suite \r\n- Some prompt engineering\r\n - improving the system instructions / functions summary\r\n - add more examples to the summary\r\n - adapt a few opinionated examples for some specific functions \r\n\r\n## Evaluation\r\n\r\n- average based on 4 runs for each model/branch tuple\r\n- the new tests were locally added to main to run against the same suite\r\nand properly evaluate the difference\r\n\r\n| Model | before (main) | after (PR) | delta |\r\n| ------------- | ------------- | ------------- | ------------- |\r\n| GPT-4o | 90.9 | 97.74 | + 6.84 |\r\n| Claude 3.5 Sonnet v2 | 88.58 | 96.49 | +7.91 |\r\n| Gemini 1.5-pro-002 | 88.17 | 94.19 | +6.02 |\r\n\r\nOverall, the prompt engineering somewhat significantly improved the\r\ngeneration efficiency.","sha":"5b9691278165417d4cc12853f58de728aaeff011"}},{"branch":"8.x","label":"v8.18.0","branchLabelMappingKey":"^v8.18.0$","isSourceBranch":false,"state":"NOT_CREATED"}]}] BACKPORT--> Co-authored-by: Pierre Gayvallet <[email protected]>
1 parent c678d61 commit 5413cff

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+1307
-194
lines changed

x-pack/platform/plugins/shared/inference/scripts/evaluation/scenarios/esql/index.spec.ts

Lines changed: 453 additions & 9 deletions
Large diffs are not rendered by default.

x-pack/platform/plugins/shared/inference/scripts/load_esql_docs/load_esql_docs.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ yargs(process.argv.slice(2))
6969
});
7070
log.info(`Using connector ${connector.connectorId}`);
7171

72-
const chatClient = kibanaClient.createInferenceClient({
72+
const inferenceClient = kibanaClient.createInferenceClient({
7373
connectorId: connector.connectorId,
7474
});
7575

@@ -84,14 +84,14 @@ yargs(process.argv.slice(2))
8484
log.info(`Retrieving and converting documentation from ${builtDocsDir}...`);
8585
const extraction = await extractDocEntries({
8686
builtDocsDir,
87-
inferenceClient: chatClient,
87+
inferenceClient,
8888
log,
8989
});
9090

9191
log.info(`Rewriting documentation...`);
9292
const docFiles = await generateDoc({
9393
extraction,
94-
inferenceClient: chatClient,
94+
inferenceClient,
9595
log,
9696
});
9797

x-pack/platform/plugins/shared/inference/scripts/util/kibana_client.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,10 @@ export class KibanaClient {
212212
return this.axios
213213
.post(
214214
this.getUrl({
215-
pathname: `/internal/inference/chat_complete/stream`,
215+
pathname: `/internal/inference/chat_complete`,
216216
}),
217217
body,
218-
{ responseType: 'stream', timeout: NaN }
218+
{ timeout: NaN }
219219
)
220220
.then((response) => {
221221
return response.data;

x-pack/platform/plugins/shared/inference/server/tasks/nl_to_esql/doc_base/suggestions.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ const suggestions: Suggestion[] = [
1313
return ['BUCKET'];
1414
}
1515
},
16+
(keywords) => {
17+
if (keywords.includes('TO_DATETIME')) {
18+
return ['DATE_PARSE'];
19+
}
20+
},
1621
];
1722

1823
/**
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# BIT_LENGTH
2+
3+
This function calculates the bit length of a given string.
4+
5+
## Syntax
6+
7+
`BIT_LENGTH(string)`
8+
9+
### Parameters
10+
11+
#### string
12+
13+
This is the string whose bit length you want to calculate. If `null` is provided, the function will return `null`.
14+
15+
**Note**: Strings are in UTF-8 format, which means a single character may occupy multiple bytes.
16+
17+
## Examples
18+
19+
```esql
20+
FROM airports
21+
| WHERE country == "India"
22+
| KEEP city
23+
| EVAL fn_length = LENGTH(city), fn_bit_length = BIT_LENGTH(city)
24+
```

x-pack/platform/plugins/shared/inference/server/tasks/nl_to_esql/esql_docs/esql-bucket.txt

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ BUCKET can operate in two modes:
3030
- one where the bucket size is computed based on a bucket count recommendation and a range,
3131
- and another where the bucket size is provided directly.
3232

33-
When the bucket size is provided directly for time interval,
34-
it is expressed as a *timespan literal*, e.g.
33+
When the bucket size is provided directly for time interval, it is expressed as a **timespan literal**, e.g.
3534
- GOOD: `BUCKET(@timestamp, 1 month)`
3635
- BAD: `BUCKET(@timestamp, "month")`
3736

@@ -74,29 +73,40 @@ FROM employees
7473

7574
More examples:
7675

76+
*Regrouping employees in buckets based on salary and counting them*
7777
```esql
7878
FROM employees
7979
| WHERE hire_date >= "1985-01-01T00:00:00Z" AND hire_date < "1986-01-01T00:00:00Z"
80-
| STATS c = COUNT(1) BY b = BUCKET(salary, 5000.)
80+
| STATS c = COUNT(*) BY b = BUCKET(salary, 5000.)
8181
| SORT b
8282
```
8383

84+
*Group data emitted over the last 24h into 25 buckets*
8485
```esql
8586
FROM sample_data
8687
| WHERE @timestamp >= NOW() - 1 day and @timestamp < NOW()
8788
| STATS COUNT(*) BY bucket = BUCKET(@timestamp, 25, NOW() - 1 day, NOW())
8889
```
8990

91+
*Similar to previous example but with fixed 1 hour bucket size*
92+
```esql
93+
FROM sample_data
94+
| WHERE @timestamp >= NOW() - 1 day and @timestamp < NOW()
95+
| STATS COUNT(*) BY bucket = BUCKET(@timestamp, 1 hour)
96+
```
97+
98+
*Group employees in 20 buckets based on their hire_date and then calculate the average salary for each bucket*
9099
```esql
91100
FROM employees
92101
| WHERE hire_date >= "1985-01-01T00:00:00Z" AND hire_date < "1986-01-01T00:00:00Z"
93102
| STATS AVG(salary) BY bucket = BUCKET(hire_date, 20, "1985-01-01T00:00:00Z", "1986-01-01T00:00:00Z")
94103
| SORT bucket
95104
```
96105

106+
*Similar to previous example but using fixed 1 month buckets size*
97107
```esql
98108
FROM employees
99-
| STATS s1 = BUCKET(salary / 1000 + 999, 50.) + 2 BY b1 = BUCKET(salary / 100 + 99, 50.), b2 = BUCKET(salary / 1000 + 999, 50.)
100-
| SORT b1, b2
101-
| KEEP b1, s1, b2
109+
| WHERE hire_date >= "1985-01-01T00:00:00Z" AND hire_date < "1986-01-01T00:00:00Z"
110+
| STATS AVG(salary) BY bucket = BUCKET(hire_date, 1 month)
111+
| SORT bucket
102112
```
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# BYTE_LENGTH
2+
3+
This function calculates the byte length of a given string.
4+
5+
## Syntax
6+
7+
`BYTE_LENGTH(string)`
8+
9+
### Parameters
10+
11+
#### string
12+
13+
The text string for which the byte length is to be determined. If `null` is provided, the function will return `null`.
14+
15+
## Examples
16+
17+
```esql
18+
FROM airports
19+
| WHERE country == "India"
20+
| KEEP city
21+
| EVAL fn_length = LENGTH(city), fn_byte_length = BYTE_LENGTH(city)
22+
```

x-pack/platform/plugins/shared/inference/server/tasks/nl_to_esql/esql_docs/esql-case.txt

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ The CASE function accepts pairs of conditions and values. It returns the value t
44

55
## Syntax
66

7-
`CASE(condition, trueValue)`
7+
`CASE(condition, trueValue, elseValue)`
88

99
### Parameters
1010

@@ -16,16 +16,20 @@ A condition to evaluate.
1616

1717
The value that is returned when the corresponding condition is the first to evaluate to `true`. If no condition matches, the default value is returned.
1818

19+
#### elseValue
20+
21+
The value that will be returned when no condition evaluates to `true`.
22+
1923
## Examples
2024

21-
Determine whether employees are monolingual, bilingual, or polyglot:
25+
In this example, employees are categorized as monolingual, bilingual, or polyglot depending on how many languages they speak:
2226

2327
```esql
2428
FROM employees
2529
| EVAL type = CASE(
2630
languages <= 1, "monolingual",
2731
languages <= 2, "bilingual",
28-
"polyglot")
32+
"polyglot")
2933
| KEEP emp_no, languages, type
3034
```
3135

@@ -46,6 +50,6 @@ Calculate an hourly error rate as a percentage of the total number of log messag
4650
FROM sample_data
4751
| EVAL error = CASE(message LIKE "*error*", 1, 0)
4852
| EVAL hour = DATE_TRUNC(1 hour, @timestamp)
49-
| STATS error_rate = AVG(error) by hour
53+
| STATS error_rate = AVG(error) BY hour
5054
| SORT hour
51-
```
55+
```
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# CATEGORIZE
2+
3+
The `CATEGORIZE` function organizes textual data into groups of similar format.
4+
5+
> **Note:** The `CATEGORIZE` function is currently in technical preview and may undergo changes or be removed in future releases.
6+
7+
## Syntax
8+
9+
`CATEGORIZE(field)`
10+
11+
### Parameters
12+
13+
#### field
14+
15+
The expression that is to be categorized.
16+
17+
## Examples
18+
19+
The following example demonstrates how to use `CATEGORIZE` to group server log messages into categories and then aggregate their counts.
20+
21+
```esql
22+
FROM sample_data
23+
| STATS count = COUNT() BY category=CATEGORIZE(message)
24+
```
25+
26+
## Limitations
27+
28+
- `CATEGORIZE` can't be used within other expressions
29+
- `CATEGORIZE` can't be used with multiple groupings
30+
- `CATEGORIZE` can't be used or referenced within aggregate functions

x-pack/platform/plugins/shared/inference/server/tasks/nl_to_esql/esql_docs/esql-date_extract.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ The DATE_EXTRACT function is used to extract specific parts of a date.
1010

1111
#### datePart
1212

13-
This is the part of the date you want to extract, such as "year", "month" or ""hour_of_day".
13+
This is the part of the date you want to extract, such as "year", "month" or "hour_of_day".
1414

1515
#### date
1616

0 commit comments

Comments
 (0)