From fa5276475b17fd2156397fc9f2f4e52de36ef416 Mon Sep 17 00:00:00 2001 From: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> Date: Tue, 29 Oct 2024 07:55:02 +1100 Subject: [PATCH] [8.16] [Security GenAI][BUG] KB index entry created via pdf upload does not give the right response (#198020) (#198075) # Backport This will backport the following commits from `main` to `8.16`: - [[Security GenAI][BUG] KB index entry created via pdf upload does not give the right response (#198020)](https://github.com/elastic/kibana/pull/198020) ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) Co-authored-by: Ievgen Sorokopud --- .../knowledge_base/helpers.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/x-pack/plugins/elastic_assistant/server/ai_assistant_data_clients/knowledge_base/helpers.ts b/x-pack/plugins/elastic_assistant/server/ai_assistant_data_clients/knowledge_base/helpers.ts index de76a38135f0b..59816b0b0c264 100644 --- a/x-pack/plugins/elastic_assistant/server/ai_assistant_data_clients/knowledge_base/helpers.ts +++ b/x-pack/plugins/elastic_assistant/server/ai_assistant_data_clients/knowledge_base/helpers.ts @@ -220,6 +220,17 @@ export const getStructuredToolForIndexEntry = ({ return { ...prev, [field]: hit._source[field] }; }, {}); } + + // We want to send relevant inner hits (chunks) to the LLM as a context + const innerHitPath = `${indexEntry.name}.${indexEntry.field}`; + if (hit.inner_hits?.[innerHitPath]) { + return { + text: hit.inner_hits[innerHitPath].hits.hits + .map((innerHit) => innerHit._source.text) + .join('\n --- \n'), + }; + } + return { text: get(hit._source, `${indexEntry.field}.inference.chunks[0].text`), };