-
Notifications
You must be signed in to change notification settings - Fork 8.6k
[Inference] Prompts: reasoning mode + ES|QL prompt #226616
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
Merged
dgieselaar
merged 9 commits into
elastic:main
from
dgieselaar:inference-prompt-reasoning
Aug 1, 2025
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
98f23d1
[Inference] Prompts: reasoning mode
dgieselaar ea79a80
[CI] Auto-commit changed files from 'node scripts/generate codeowners'
kibanamachine 1985ee0
Update import after changes
dgieselaar a7e91b6
Merge branch 'inference-prompt-reasoning' of github.com:dgieselaar/ki…
dgieselaar eccc031
Merge branch 'main' into inference-prompt-reasoning
dgieselaar 120a3e5
Merge branch 'main' into inference-prompt-reasoning
elasticmachine 7f1d792
Update x-pack/platform/packages/shared/kbn-ai-tools/src/tools/esql/es…
dgieselaar 733616d
Add clarifying comment
dgieselaar 3e0cbfd
Merge branch 'main' into inference-prompt-reasoning
elasticmachine File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
x-pack/platform/packages/shared/kbn-ai-tools-cli/recipes/esql.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
| import { runRecipe } from '@kbn/inference-cli'; | ||
| import { executeAsEsqlAgent } from '@kbn/ai-tools'; | ||
| import moment from 'moment'; | ||
| import { inspect } from 'util'; | ||
|
|
||
| runRecipe( | ||
| { | ||
| name: 'answer_esql', | ||
| flags: { | ||
| string: ['prompt'], | ||
| help: ` | ||
| --prompt The user prompt for generating ES|QL | ||
| `, | ||
| }, | ||
| }, | ||
| async ({ inferenceClient, kibanaClient, flags, esClient, logger, log, signal }) => { | ||
| const now = moment(); | ||
|
|
||
| const end = now.valueOf(); | ||
|
|
||
| const start = now.clone().subtract(1, 'days').valueOf(); | ||
|
|
||
| const response = await executeAsEsqlAgent({ | ||
| start, | ||
| end, | ||
| esClient, | ||
| inferenceClient, | ||
| logger, | ||
| prompt: String(flags.prompt), | ||
| signal, | ||
| }); | ||
|
|
||
| log.info(inspect(response, { depth: null })); | ||
| } | ||
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # @kbn/ai-tools | ||
|
|
||
| A collection of GenAI & ML related (broadly defined) tools, such `describeDataset` which summarizes a data stream or index by sampling data and pre-processing it. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| export { describeDataset } from './src/tools/describe_dataset'; | ||
| export { sortAndTruncateAnalyzedFields } from './src/tools/describe_dataset/sort_and_truncate_analyzed_fields'; | ||
| export { executeAsEsqlAgent } from './src/tools/esql'; |
12 changes: 12 additions & 0 deletions
12
x-pack/platform/packages/shared/kbn-ai-tools/jest.config.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| module.exports = { | ||
| preset: '@kbn/test/jest_node', | ||
| rootDir: '../../../../..', | ||
| roots: ['<rootDir>/x-pack/platform/packages/shared/kbn-ai-tools'], | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "type": "shared-server", | ||
| "id": "@kbn/ai-tools", | ||
| "owner": "@elastic/appex-ai-infra", | ||
| "group": "platform", | ||
| "visibility": "shared" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "name": "@kbn/ai-tools", | ||
| "private": true, | ||
| "version": "1.0.0", | ||
| "license": "Elastic License 2.0" | ||
| } |
24 changes: 24 additions & 0 deletions
24
x-pack/platform/packages/shared/kbn-ai-tools/src/tools/describe_dataset/document_analysis.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| export interface DocumentAnalysis { | ||
| total: number; | ||
| sampled: number; | ||
| fields: Array<{ | ||
| name: string; | ||
| types: string[]; | ||
| cardinality: number | null; | ||
| values: Array<string | number | boolean>; | ||
| empty: boolean; | ||
| }>; | ||
| } | ||
|
|
||
| export interface TruncatedDocumentAnalysis { | ||
| fields: string[]; | ||
| total: number; | ||
| sampled: number; | ||
| } |
75 changes: 75 additions & 0 deletions
75
.../platform/packages/shared/kbn-ai-tools/src/tools/describe_dataset/get_sample_documents.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| import { ElasticsearchClient } from '@kbn/core/server'; | ||
| import { QueryDslFieldAndFormat, SearchHit } from '@elastic/elasticsearch/lib/api/types'; | ||
| import { kqlQuery, rangeQuery } from './queries'; | ||
|
|
||
| export function getSampleDocuments({ | ||
| esClient, | ||
| index, | ||
| start, | ||
| end, | ||
| kql, | ||
| size = 1000, | ||
| fields = [ | ||
| { | ||
| field: '*', | ||
| include_unmapped: true, | ||
| }, | ||
| ], | ||
| _source = false, | ||
| timeout = '5s', | ||
| }: { | ||
| esClient: ElasticsearchClient; | ||
| index: string; | ||
| start: number; | ||
| end: number; | ||
| kql?: string; | ||
| size?: number; | ||
| fields?: Array<QueryDslFieldAndFormat | string>; | ||
| _source?: boolean; | ||
| timeout?: string; | ||
| }) { | ||
| return esClient | ||
| .search<Record<string, any>>({ | ||
| index, | ||
| size, | ||
| track_total_hits: true, | ||
| timeout, | ||
| query: { | ||
| bool: { | ||
| must: [...kqlQuery(kql), ...rangeQuery(start, end)], | ||
| should: [ | ||
| { | ||
| function_score: { | ||
| functions: [ | ||
| { | ||
| random_score: {}, | ||
| }, | ||
| ], | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| }, | ||
| sort: { | ||
| _score: { | ||
| order: 'desc', | ||
| }, | ||
| }, | ||
| _source, | ||
| fields, | ||
| }) | ||
| .then((response) => ({ | ||
| hits: response.hits.hits as Array<SearchHit<Record<string, any>>>, | ||
| total: | ||
| typeof response.hits.total === 'number' | ||
| ? response.hits.total! | ||
| : response.hits.total?.value!, | ||
| })); | ||
| } |
53 changes: 53 additions & 0 deletions
53
x-pack/platform/packages/shared/kbn-ai-tools/src/tools/describe_dataset/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
| import { ElasticsearchClient } from '@kbn/core-elasticsearch-server'; | ||
| import { getSampleDocuments } from './get_sample_documents'; | ||
| import { mergeSampleDocumentsWithFieldCaps } from './merge_sample_documents_with_field_caps'; | ||
| import { rangeQuery } from './queries'; | ||
|
|
||
| export async function describeDataset({ | ||
| esClient, | ||
| start, | ||
| end, | ||
| index, | ||
| kql, | ||
| }: { | ||
| esClient: ElasticsearchClient; | ||
| start: number; | ||
| end: number; | ||
| index: string; | ||
| kql?: string; | ||
| }) { | ||
| const [fieldCaps, hits] = await Promise.all([ | ||
| esClient.fieldCaps({ | ||
| index, | ||
| fields: '*', | ||
| index_filter: { | ||
| bool: { | ||
| filter: rangeQuery(start, end), | ||
| }, | ||
| }, | ||
| }), | ||
| getSampleDocuments({ | ||
| esClient, | ||
| index, | ||
| start, | ||
| end, | ||
| kql, | ||
| }), | ||
| ]); | ||
|
|
||
| const total = hits.total; | ||
|
|
||
| const analysis = mergeSampleDocumentsWithFieldCaps({ | ||
| hits: hits.hits, | ||
| total: total ?? 0, | ||
| fieldCaps, | ||
| }); | ||
|
|
||
| return analysis; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Wonder if it would be beneficial to restrict this query to certain data tiers, or exclude frozen tiers here.
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.
the rangeQuery will do that, based on time. It will only hit frozen/cold/warm if the data is within range - ES will pre-filter shards for data streams based on time range. It might still hit frozen if it queries a wide time range, but that's maybe more of a feature - otherwise it cannot query frozen even if the user asks for it.