From 0c0cb2afc352dc12991db61fd2edffdd7b709f7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patryk=20Kopyci=C5=84ski?= Date: Tue, 3 Dec 2024 18:50:39 +0100 Subject: [PATCH] [8.16] [Security KB] Fix setup KB (#201175) (#202392) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Backport This will backport the following commits from `main` to `8.16`: - [[Security KB] Fix setup KB (#201175)](https://github.com/elastic/kibana/pull/201175) ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) \n\n### Questions ?\nPlease refer to the [Backport tool\ndocumentation](https://github.com/sqren/backport)\n\n\n\nCo-authored-by: Patryk Kopyciński "}},{"branch":"8.x","label":"v8.18.0","labelRegex":"^v8.18.0$","isSourceBranch":false,"url":"https://github.com/elastic/kibana/pull/201437","number":201437,"state":"MERGED","mergeCommit":{"sha":"7d6c034e25e6d9158edd6c5b1ab905265abb2bdb","message":"[8.x] [Security KB] Fix setup KB (#201175) (#201437)\n\n# Backport\n\nThis will backport the following commits from `main` to `8.x`:\n- [[Security KB] Fix setup KB\n(#201175)](https://github.com/elastic/kibana/pull/201175)\n\n\n\n### Questions ?\nPlease refer to the [Backport tool\ndocumentation](https://github.com/sqren/backport)\n\n\n\nCo-authored-by: Patryk Kopyciński "}},{"branch":"8.16","label":"v8.16.2","labelRegex":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"}]}] BACKPORT--> --------- Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com> --- .../knowledge_base/index.ts | 54 +++++++++++++------ 1 file changed, 37 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..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 @@ -225,27 +225,47 @@ 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}`