From 7d3cf2fe7f492d9f5c72699376f283d2888824f6 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] [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> --- .../knowledge_base/index.ts | 18 +++++++++++++++++- 1 file changed, 17 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 231aa1c319da4..c3ce7fb1a43a0 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 @@ -178,9 +178,22 @@ export class AIAssistantKnowledgeBaseDataClient extends AIAssistantDataClient { public createInferenceEndpoint = async () => { const elserId = await this.options.getElserId(); this.options.logger.debug(`Deploying ELSER model '${elserId}'...`); + const esClient = await this.options.elasticsearchClientPromise; + try { - const esClient = await this.options.elasticsearchClientPromise; + await esClient.inference.delete({ + inference_id: ASSISTANT_ELSER_INFERENCE_ID, + // 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, @@ -198,6 +211,9 @@ export class AIAssistantKnowledgeBaseDataClient extends AIAssistantDataClient { 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}`