From 0e2f9fb919b5811d79a9b22b422ddf2221c257ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patryk=20Kopyci=C5=84ski?= Date: Fri, 22 Nov 2024 17:58:24 +0100 Subject: [PATCH 1/2] [Security KB] Fix setup KB (#201175) ## Summary Fix an issue with auto-recovery of Knowledge Base setup. When the KB setup was initialized on an undersized cluster, the model failed to deploy correctly. This resulted in the KB ending up in a broken state, repeatedly displaying the Setup KB button. --------- Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com> (cherry picked from commit 1cb56d7196cf60b03cb539f32f6a466a17141e02) # Conflicts: # x-pack/plugins/elastic_assistant/server/ai_assistant_data_clients/knowledge_base/index.ts --- .../knowledge_base/index.ts | 52 +++++++++++++------ 1 file changed, 35 insertions(+), 17 deletions(-) diff --git a/x-pack/plugins/elastic_assistant/server/ai_assistant_data_clients/knowledge_base/index.ts b/x-pack/plugins/elastic_assistant/server/ai_assistant_data_clients/knowledge_base/index.ts index 50e124321fe6c..378b32672f8e2 100644 --- a/x-pack/plugins/elastic_assistant/server/ai_assistant_data_clients/knowledge_base/index.ts +++ b/x-pack/plugins/elastic_assistant/server/ai_assistant_data_clients/knowledge_base/index.ts @@ -225,27 +225,45 @@ export class AIAssistantKnowledgeBaseDataClient extends AIAssistantDataClient { public createInferenceEndpoint = async () => { const elserId = await this.options.getElserId(); this.options.logger.debug(`Deploying ELSER model '${elserId}'...`); - try { - const esClient = await this.options.elasticsearchClientPromise; - if (this.isV2KnowledgeBaseEnabled) { - await esClient.inference.put({ - task_type: 'sparse_embedding', + const esClient = await this.options.elasticsearchClientPromise; + + const inferenceEndpointExists = await this.isInferenceEndpointExists(); + + if (inferenceEndpointExists) { + try { + await esClient.inference.delete({ inference_id: ASSISTANT_ELSER_INFERENCE_ID, - inference_config: { - service: 'elasticsearch', - service_settings: { - adaptive_allocations: { - enabled: true, - min_number_of_allocations: 0, - max_number_of_allocations: 8, - }, - num_threads: 1, - model_id: elserId, + // it's being used in the mapping so we need to force delete + force: true, + }); + this.options.logger.debug(`Deleted existing inference endpoint for ELSER model '${elserId}'`); + } catch (error) { + this.options.logger.error( + `Error deleting inference endpoint for ELSER model '${elserId}':\n${error}` + ); + } + } + + try { + await esClient.inference.put({ + task_type: 'sparse_embedding', + inference_id: ASSISTANT_ELSER_INFERENCE_ID, + inference_config: { + service: 'elasticsearch', + service_settings: { + adaptive_allocations: { + enabled: true, + min_number_of_allocations: 0, + max_number_of_allocations: 8, }, task_settings: {}, }, - }); - } + task_settings: {}, + }, + }); + + // await for the model to be deployed + await this.isInferenceEndpointExists(); } catch (error) { this.options.logger.error( `Error creating inference endpoint for ELSER model '${elserId}':\n${error}` From dc36f53d9e213140aaf060ee1e8ba37da3946e72 Mon Sep 17 00:00:00 2001 From: kibanamachine <42973632+kibanamachine@users.noreply.github.com> Date: Sun, 1 Dec 2024 21:10:55 +0000 Subject: [PATCH 2/2] [CI] Auto-commit changed files from 'node scripts/eslint --no-cache --fix' --- .../server/ai_assistant_data_clients/knowledge_base/index.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/x-pack/plugins/elastic_assistant/server/ai_assistant_data_clients/knowledge_base/index.ts b/x-pack/plugins/elastic_assistant/server/ai_assistant_data_clients/knowledge_base/index.ts index 378b32672f8e2..49413fc7fa544 100644 --- a/x-pack/plugins/elastic_assistant/server/ai_assistant_data_clients/knowledge_base/index.ts +++ b/x-pack/plugins/elastic_assistant/server/ai_assistant_data_clients/knowledge_base/index.ts @@ -236,7 +236,9 @@ export class AIAssistantKnowledgeBaseDataClient extends AIAssistantDataClient { // it's being used in the mapping so we need to force delete force: true, }); - this.options.logger.debug(`Deleted existing inference endpoint for ELSER model '${elserId}'`); + this.options.logger.debug( + `Deleted existing inference endpoint for ELSER model '${elserId}'` + ); } catch (error) { this.options.logger.error( `Error deleting inference endpoint for ELSER model '${elserId}':\n${error}`