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`), };