-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[8.x] [Security solution]
naturalLanguageToEsql
Tool added to defau…
…lt assistant graph (#192042) (#193364) # Backport This will backport the following commits from `main` to `8.x`: - [[Security solution] `naturalLanguageToEsql` Tool added to default assistant graph (#192042)](#192042) <!--- Backport version: 8.9.8 --> ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) <!--BACKPORT [{"author":{"name":"Steph Milovic","email":"stephanie.milovic@elastic.co"},"sourceCommit":{"committedDate":"2024-09-18T21:05:41Z","message":"[Security solution] `naturalLanguageToEsql` Tool added to default assistant graph (#192042)","sha":"798a26f93ce0501ed8fe72e6de94fd7454315d8e","branchLabelMapping":{"^v9.0.0$":"main","^v8.16.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:enhancement","v9.0.0","Team: SecuritySolution","Team:Security Generative AI","v8.16.0"],"number":192042,"url":"https://github.com/elastic/kibana/pull/192042","mergeCommit":{"message":"[Security solution] `naturalLanguageToEsql` Tool added to default assistant graph (#192042)","sha":"798a26f93ce0501ed8fe72e6de94fd7454315d8e"}},"sourceBranch":"main","suggestedTargetBranches":["8.x"],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","labelRegex":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/192042","number":192042,"mergeCommit":{"message":"[Security solution] `naturalLanguageToEsql` Tool added to default assistant graph (#192042)","sha":"798a26f93ce0501ed8fe72e6de94fd7454315d8e"}},{"branch":"8.x","label":"v8.16.0","labelRegex":"^v8.16.0$","isSourceBranch":false,"state":"NOT_CREATED"}]}] BACKPORT--> Co-authored-by: Garrett Spong <spong@users.noreply.github.com>
- Loading branch information
1 parent
21e02f7
commit e89dda0
Showing
19 changed files
with
129 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
"ml", | ||
"taskManager", | ||
"licensing", | ||
"inference", | ||
"spaces", | ||
"security" | ||
] | ||
|
This file contains 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 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 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 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 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 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 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 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 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 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 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 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 |
---|---|---|
|
@@ -48,6 +48,7 @@ | |
"@kbn/apm-utils", | ||
"@kbn/std", | ||
"@kbn/zod", | ||
"@kbn/inference-plugin" | ||
], | ||
"exclude": [ | ||
"target/**/*", | ||
|
This file contains 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
80 changes: 80 additions & 0 deletions
80
.../security_solution/server/assistant/tools/esql_language_knowledge_base/nl_to_esql_tool.ts
This file contains 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,80 @@ | ||
/* | ||
* 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 { DynamicStructuredTool } from '@langchain/core/tools'; | ||
import { z } from '@kbn/zod'; | ||
import type { AssistantTool, AssistantToolParams } from '@kbn/elastic-assistant-plugin/server'; | ||
import { lastValueFrom } from 'rxjs'; | ||
import { naturalLanguageToEsql } from '@kbn/inference-plugin/server'; | ||
import { APP_UI_ID } from '../../../../common'; | ||
|
||
export type ESQLToolParams = AssistantToolParams; | ||
|
||
const TOOL_NAME = 'NaturalLanguageESQLTool'; | ||
|
||
const toolDetails = { | ||
id: 'nl-to-esql-tool', | ||
name: TOOL_NAME, | ||
description: `You MUST use the "${TOOL_NAME}" function when the user wants to: | ||
- run any arbitrary query | ||
- breakdown or filter ES|QL queries that are displayed on the current page | ||
- convert queries from another language to ES|QL | ||
- asks general questions about ES|QL | ||
DO NOT UNDER ANY CIRCUMSTANCES generate ES|QL queries or explain anything about the ES|QL query language yourself. | ||
DO NOT UNDER ANY CIRCUMSTANCES try to correct an ES|QL query yourself - always use the "${TOOL_NAME}" function for this. | ||
Even if the "${TOOL_NAME}" function was used before that, follow it up with the "${TOOL_NAME}" function. If a query fails, do not attempt to correct it yourself. Again you should call the "${TOOL_NAME}" function, | ||
even if it has been called before.`, | ||
}; | ||
|
||
export const NL_TO_ESQL_TOOL: AssistantTool = { | ||
...toolDetails, | ||
sourceRegister: APP_UI_ID, | ||
isSupported: (params: ESQLToolParams): params is ESQLToolParams => { | ||
const { chain, isEnabledKnowledgeBase, modelExists } = params; | ||
return isEnabledKnowledgeBase && modelExists && chain != null; | ||
}, | ||
getTool(params: ESQLToolParams) { | ||
if (!this.isSupported(params)) return null; | ||
|
||
const { connectorId, inference, logger, request } = params as ESQLToolParams; | ||
if (inference == null || connectorId == null) return null; | ||
|
||
const callNaturalLanguageToEsql = async (question: string) => { | ||
return lastValueFrom( | ||
naturalLanguageToEsql({ | ||
client: inference.getClient({ request }), | ||
connectorId, | ||
input: question, | ||
logger: { | ||
debug: (source) => { | ||
logger.debug(typeof source === 'function' ? source() : source); | ||
}, | ||
}, | ||
}) | ||
); | ||
}; | ||
|
||
return new DynamicStructuredTool({ | ||
name: toolDetails.name, | ||
description: toolDetails.description, | ||
schema: z.object({ | ||
question: z.string().describe(`The user's exact question about ESQL`), | ||
}), | ||
func: async (input) => { | ||
const generateEvent = await callNaturalLanguageToEsql(input.question); | ||
const answer = generateEvent.content ?? 'An error occurred in the tool'; | ||
|
||
logger.debug(`Received response from NL to ESQL tool: ${answer}`); | ||
return answer; | ||
}, | ||
tags: ['esql', 'query-generation', 'knowledge-base'], | ||
// TODO: Remove after ZodAny is fixed https://github.com/langchain-ai/langchainjs/blob/main/langchain-core/src/tools.ts | ||
}) as unknown as DynamicStructuredTool; | ||
}, | ||
}; |
This file contains 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 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 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 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