kibana/x-pack/packages/kbn-elastic-assistant/impl/assistant/api.tsx
Andrew Macri 3f0fa7d245
[Security Solution] [Elastic AI Assistant] Retrieval Augmented Generation (RAG) for Alerts (#172542)
## [Security Solution] [Elastic AI Assistant] Retrieval Augmented Generation (RAG) for Alerts

This PR implements _Retrieval Augmented Generation_ (RAG) for Alerts in the Security Solution. This feature enables users to ask the assistant questions about the latest and riskiest open alerts in their environment using natural language, for example:

- _How many alerts are currently open?_
- _Which alerts should I look at first?_
- _Did we have any alerts with suspicious activity on Windows machines?_

### More context

Previously, the assistant relied solely on the knowledge of the configured LLM and _singular_ alerts or events passed _by the client_ to the LLM as prompt context. This new feature:

- Enables _multiple_ alerts to be passed by the _server_ as context to the LLM, via [LangChain tools](https://github.com/elastic/kibana/pull/167097)
- Applies the user's [anonymization](https://github.com/elastic/kibana/pull/159857) settings to those alerts
  - Only fields allowed by the user will be sent as context to the LLM
  - Users may enable or disable anonymization for specific fields (via settings)
  - Click the conversation's `Show anonymized` toggle to see the anonymized values sent to / received from the LLM:
  ![show_anonymized](7db85f69-9352-4422-adbf-c97248ccb3dd)

### Settings

This feature is enabled and configured via the `Knowledge Base` > `Alerts` settings in the screenshot below:
![rag_on_alerts_setting](9161b6d4-b7c3-4f37-bcde-f032f5a02966)

- The `Alerts` toggle enables or disables the feature
- The slider has a range of `10` - `100` alerts (default: `20`)

When the setting above is enabled, up to `n` alerts (as determined by the slider) that meet the following criteria will be returned:

- the `kibana.alert.workflow_status` must be `open`
- the alert must have been generated in the last `24 hours`
- the alert must NOT be a `kibana.alert.building_block_type` alert
- the `n` alerts are ordered by `kibana.alert.risk_score`, to prioritize the riskiest alerts

### Feature flag

To use this feature:

1) Add the `assistantRagOnAlerts` feature flag to the `xpack.securitySolution.enableExperimental` setting in `config/kibana.yml` (or `config/kibana.dev.yml` in local development environments), per the example below:

```
xpack.securitySolution.enableExperimental: ['assistantRagOnAlerts']
```

2) Enable the `Alerts` toggle in the Assistant's `Knowledge Base` settings, per the screenshot below:

![alerts_toggle](07f241ea-af4a-43a4-bd19-0dc6337db167)

## How it works

- When the `Alerts` settings toggle is enabled, http `POST` requests to the `/internal/elastic_assistant/actions/connector/{id}/_execute` route include the following new (optional) parameters:
  - `alertsIndexPattern`, the alerts index for the current Kibana Space, e.g. `.alerts-security.alerts-default`
  - `allow`, the user's `Allowed` fields in the `Anonymization` settings, e.g.  `["@timestamp", "cloud.availability_zone", "file.name", "user.name", ...]`
  - `allowReplacement`, the user's `Anonymized` fields in the `Anonymization` settings, e.g. `["cloud.availability_zone", "host.name", "user.name", ...]`
  - `replacements`, a `Record<string, string>` of replacements (generated on the server) that starts empty for a new conversation, and accumulates anonymized values until the conversation is cleared, e.g.

```json
"replacements": {
    "e4f935c0-5a80-47b2-ac7f-816610790364": "Host-itk8qh4tjm",
    "cf61f946-d643-4b15-899f-6ffe3fd36097": "rpwmjvuuia",
    "7f80b092-fb1a-48a2-a634-3abc61b32157": "6astve9g6s",
    "f979c0d5-db1b-4506-b425-500821d00813": "Host-odqbow6tmc",
    // ...
},
```

- `size`, the numeric value set by the slider in the user's `Knowledge Base > Alerts` setting, e.g. `20`

- The `postActionsConnectorExecuteRoute` function in `x-pack/plugins/elastic_assistant/server/routes/post_actions_connector_execute.ts` was updated to accept the new optional parameters, and to return an updated `replacements` with every response. (Every new request that is processed on the server may add additional anonymized values to the `replacements` returned in the response.)

- The `callAgentExecutor` function in `x-pack/plugins/elastic_assistant/server/lib/langchain/execute_custom_llm_chain/index.ts` previously used a hard-coded array of LangChain tools that had just one entry, for the `ESQLKnowledgeBaseTool` tool. That hard-coded array was replaced in this PR with a call to the (new) `getApplicableTools` function:

```typescript
  const tools: Tool[] = getApplicableTools({
    allow,
    allowReplacement,
    alertsIndexPattern,
    assistantLangChain,
    chain,
    esClient,
    modelExists,
    onNewReplacements,
    replacements,
    request,
    size,
  });
```

- The `getApplicableTools` function in `x-pack/plugins/elastic_assistant/server/lib/langchain/tools/index.ts` examines the parameters in the `KibanaRequest` and only returns a filtered set of LangChain tools. If the request doesn't contain all the parameters required by a tool, it will NOT be returned by `getApplicableTools`. For example, if the required anonymization parameters are not included in the request, the `open-alerts` tool will not be returned.

- The new `alert-counts` LangChain tool returned by the `getAlertCountsTool` function in `x-pack/plugins/elastic_assistant/server/lib/langchain/tools/alert_counts/get_alert_counts_tool.ts` provides the LLM the results of an aggregation on the last `24` hours of alerts (in the current Kibana Space), grouped by `kibana.alert.severity`. See the `getAlertsCountQuery` function in `x-pack/plugins/elastic_assistant/server/lib/langchain/tools/alert_counts/get_alert_counts_query.ts` for details

- The new `open-alerts` LangChain tool returned by the `getOpenAlertsTool` function in `x-pack/plugins/elastic_assistant/server/lib/langchain/tools/open_alerts/get_open_alerts_tool.ts` provides the LLM up to `size` non-building-block alerts generated in the last `24` hours  (in the current Kibana Space) with an `open` workflow status, ordered by `kibana.alert.risk_score` to prioritize the riskiest alerts. See the `getOpenAlertsQuery` function in `x-pack/plugins/elastic_assistant/server/lib/langchain/tools/open_alerts/get_open_alerts_query.ts` for details.

- On the client, a conversation continues to accumulate additional `replacements` (and send them in subsequent requests) until the conversation is cleared

- Anonymization functions that were only invoked by the browser were moved from the (browser) `kbn-elastic-assistant` package in `x-pack/packages/kbn-elastic-assistant/` to a new common package: `x-pack/packages/kbn-elastic-assistant-common`
  - The new `kbn-elastic-assistant-common` package is also consumed by the `elastic_assistant` (server) plugin: `x-pack/plugins/elastic_assistant`
2023-12-06 00:56:04 -05:00

397 lines
11 KiB
TypeScript

/*
* 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 { OpenAiProviderType } from '@kbn/stack-connectors-plugin/public/common';
import { HttpSetup, IHttpFetchError } from '@kbn/core-http-browser';
import type { Conversation, Message } from '../assistant_context/types';
import { API_ERROR } from './translations';
import { MODEL_GPT_3_5_TURBO } from '../connectorland/models/model_selector/model_selector';
import {
getFormattedMessageContent,
getOptionalRequestParams,
hasParsableResponse,
} from './helpers';
import { PerformEvaluationParams } from './settings/evaluation_settings/use_perform_evaluation';
export interface FetchConnectorExecuteAction {
alerts: boolean;
alertsIndexPattern?: string;
allow?: string[];
allowReplacement?: string[];
assistantLangChain: boolean;
assistantStreamingEnabled: boolean;
apiConfig: Conversation['apiConfig'];
http: HttpSetup;
messages: Message[];
onNewReplacements: (newReplacements: Record<string, string>) => void;
ragOnAlerts: boolean;
replacements?: Record<string, string>;
signal?: AbortSignal | undefined;
size?: number;
}
export interface FetchConnectorExecuteResponse {
response: string | ReadableStreamDefaultReader<Uint8Array>;
isError: boolean;
isStream: boolean;
traceData?: {
transactionId: string;
traceId: string;
};
}
export const fetchConnectorExecuteAction = async ({
alerts,
alertsIndexPattern,
allow,
allowReplacement,
assistantLangChain,
assistantStreamingEnabled,
http,
messages,
onNewReplacements,
ragOnAlerts,
replacements,
apiConfig,
signal,
size,
}: FetchConnectorExecuteAction): Promise<FetchConnectorExecuteResponse> => {
const outboundMessages = messages.map((msg) => ({
role: msg.role,
content: msg.content,
}));
const body =
apiConfig?.provider === OpenAiProviderType.OpenAi
? {
model: apiConfig.model ?? MODEL_GPT_3_5_TURBO,
messages: outboundMessages,
n: 1,
stop: null,
temperature: 0.2,
}
: {
// Azure OpenAI and Bedrock invokeAI both expect this body format
messages: outboundMessages,
};
// TODO: Remove in part 3 of streaming work for security solution
// tracked here: https://github.com/elastic/security-team/issues/7363
// In part 3 I will make enhancements to langchain to introduce streaming
// Once implemented, invokeAI can be removed
const isStream = assistantStreamingEnabled && !assistantLangChain;
const optionalRequestParams = getOptionalRequestParams({
alerts,
alertsIndexPattern,
allow,
allowReplacement,
ragOnAlerts,
replacements,
size,
});
const requestBody = isStream
? {
params: {
subActionParams: body,
subAction: 'invokeStream',
},
assistantLangChain,
...optionalRequestParams,
}
: {
params: {
subActionParams: body,
subAction: 'invokeAI',
},
assistantLangChain,
...optionalRequestParams,
};
try {
if (isStream) {
const response = await http.fetch(
`/internal/elastic_assistant/actions/connector/${apiConfig?.connectorId}/_execute`,
{
method: 'POST',
body: JSON.stringify(requestBody),
signal,
asResponse: isStream,
rawResponse: isStream,
}
);
const reader = response?.response?.body?.getReader();
if (!reader) {
return {
response: `${API_ERROR}\n\nCould not get reader from response`,
isError: true,
isStream: false,
};
}
return {
response: reader,
isStream: true,
isError: false,
};
}
// TODO: Remove in part 3 of streaming work for security solution
// tracked here: https://github.com/elastic/security-team/issues/7363
// This is a temporary code to support the non-streaming API
const response = await http.fetch<{
connector_id: string;
status: string;
data: string;
replacements?: Record<string, string>;
service_message?: string;
trace_data?: {
transaction_id: string;
trace_id: string;
};
}>(`/internal/elastic_assistant/actions/connector/${apiConfig?.connectorId}/_execute`, {
method: 'POST',
body: JSON.stringify(requestBody),
headers: { 'Content-Type': 'application/json' },
signal,
});
if (response.status !== 'ok' || !response.data) {
if (response.service_message) {
return {
response: `${API_ERROR}\n\n${response.service_message}`,
isError: true,
isStream: false,
};
}
return {
response: API_ERROR,
isError: true,
isStream: false,
};
}
// Only add traceData if it exists in the response
const traceData =
response.trace_data?.trace_id != null && response.trace_data?.transaction_id != null
? {
traceId: response.trace_data?.trace_id,
transactionId: response.trace_data?.transaction_id,
}
: undefined;
onNewReplacements(response.replacements ?? {});
return {
response: hasParsableResponse({
alerts,
assistantLangChain,
ragOnAlerts,
})
? getFormattedMessageContent(response.data)
: response.data,
isError: false,
isStream: false,
traceData,
};
} catch (error) {
const getReader = error?.response?.body?.getReader;
const reader =
isStream && typeof getReader === 'function' ? getReader.call(error.response.body) : null;
if (!reader) {
return {
response: `${API_ERROR}\n\n${error?.body?.message ?? error?.message}`,
isError: true,
isStream: false,
};
}
return {
response: reader,
isStream: true,
isError: true,
};
}
};
export interface GetKnowledgeBaseStatusParams {
http: HttpSetup;
resource?: string;
signal?: AbortSignal | undefined;
}
export interface GetKnowledgeBaseStatusResponse {
elser_exists: boolean;
esql_exists?: boolean;
index_exists: boolean;
pipeline_exists: boolean;
}
/**
* API call for getting the status of the Knowledge Base. Provide
* a resource to include the status of that specific resource.
*
* @param {Object} options - The options object.
* @param {HttpSetup} options.http - HttpSetup
* @param {string} [options.resource] - Resource to get the status of, otherwise status of overall KB
* @param {AbortSignal} [options.signal] - AbortSignal
*
* @returns {Promise<GetKnowledgeBaseStatusResponse | IHttpFetchError>}
*/
export const getKnowledgeBaseStatus = async ({
http,
resource,
signal,
}: GetKnowledgeBaseStatusParams): Promise<GetKnowledgeBaseStatusResponse | IHttpFetchError> => {
try {
const path = `/internal/elastic_assistant/knowledge_base/${resource || ''}`;
const response = await http.fetch(path, {
method: 'GET',
signal,
});
return response as GetKnowledgeBaseStatusResponse;
} catch (error) {
return error as IHttpFetchError;
}
};
export interface PostKnowledgeBaseParams {
http: HttpSetup;
resource?: string;
signal?: AbortSignal | undefined;
}
export interface PostKnowledgeBaseResponse {
success: boolean;
}
/**
* API call for setting up the Knowledge Base. Provide a resource to set up a specific resource.
*
* @param {Object} options - The options object.
* @param {HttpSetup} options.http - HttpSetup
* @param {string} [options.resource] - Resource to be added to the KB, otherwise sets up the base KB
* @param {AbortSignal} [options.signal] - AbortSignal
*
* @returns {Promise<PostKnowledgeBaseResponse | IHttpFetchError>}
*/
export const postKnowledgeBase = async ({
http,
resource,
signal,
}: PostKnowledgeBaseParams): Promise<PostKnowledgeBaseResponse | IHttpFetchError> => {
try {
const path = `/internal/elastic_assistant/knowledge_base/${resource || ''}`;
const response = await http.fetch(path, {
method: 'POST',
signal,
});
return response as PostKnowledgeBaseResponse;
} catch (error) {
return error as IHttpFetchError;
}
};
export interface DeleteKnowledgeBaseParams {
http: HttpSetup;
resource?: string;
signal?: AbortSignal | undefined;
}
export interface DeleteKnowledgeBaseResponse {
success: boolean;
}
/**
* API call for deleting the Knowledge Base. Provide a resource to delete that specific resource.
*
* @param {Object} options - The options object.
* @param {HttpSetup} options.http - HttpSetup
* @param {string} [options.resource] - Resource to be deleted from the KB, otherwise delete the entire KB
* @param {AbortSignal} [options.signal] - AbortSignal
*
* @returns {Promise<DeleteKnowledgeBaseResponse | IHttpFetchError>}
*/
export const deleteKnowledgeBase = async ({
http,
resource,
signal,
}: DeleteKnowledgeBaseParams): Promise<DeleteKnowledgeBaseResponse | IHttpFetchError> => {
try {
const path = `/internal/elastic_assistant/knowledge_base/${resource || ''}`;
const response = await http.fetch(path, {
method: 'DELETE',
signal,
});
return response as DeleteKnowledgeBaseResponse;
} catch (error) {
return error as IHttpFetchError;
}
};
export interface PostEvaluationParams {
http: HttpSetup;
evalParams?: PerformEvaluationParams;
signal?: AbortSignal | undefined;
}
export interface PostEvaluationResponse {
evaluationId: string;
success: boolean;
}
/**
* API call for evaluating models.
*
* @param {Object} options - The options object.
* @param {HttpSetup} options.http - HttpSetup
* @param {string} [options.evalParams] - Params necessary for evaluation
* @param {AbortSignal} [options.signal] - AbortSignal
*
* @returns {Promise<PostEvaluationResponse | IHttpFetchError>}
*/
export const postEvaluation = async ({
http,
evalParams,
signal,
}: PostEvaluationParams): Promise<PostEvaluationResponse | IHttpFetchError> => {
try {
const path = `/internal/elastic_assistant/evaluate`;
const query = {
agents: evalParams?.agents.sort()?.join(','),
datasetName: evalParams?.datasetName,
evaluationType: evalParams?.evaluationType.sort()?.join(','),
evalModel: evalParams?.evalModel.sort()?.join(','),
outputIndex: evalParams?.outputIndex,
models: evalParams?.models.sort()?.join(','),
projectName: evalParams?.projectName,
runName: evalParams?.runName,
};
const response = await http.fetch(path, {
method: 'POST',
body: JSON.stringify({
dataset: JSON.parse(evalParams?.dataset ?? '[]'),
evalPrompt: evalParams?.evalPrompt ?? '',
}),
headers: {
'Content-Type': 'application/json',
},
query,
signal,
});
return response as PostEvaluationResponse;
} catch (error) {
return error as IHttpFetchError;
}
};