-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Obs AI Assistant] Extract
recallFromConnectors
from KB service (#1…
…86796) Minor refactor to extract `recallFromConnectors` from the KB service. This change is made to make it easier to unit test parts of it, and reduce the size of the KB service.
- Loading branch information
Showing
5 changed files
with
138 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
...bservability_ai_assistant/server/service/knowledge_base_service/recall_from_connectors.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { ElasticsearchClient } from '@kbn/core-elasticsearch-server'; | ||
import { IUiSettingsClient } from '@kbn/core-ui-settings-server'; | ||
import { isEmpty } from 'lodash'; | ||
import { RecalledEntry } from '.'; | ||
import { aiAssistantSearchConnectorIndexPattern } from '../../../common'; | ||
|
||
export async function recallFromConnectors({ | ||
queries, | ||
esClient, | ||
uiSettingsClient, | ||
modelId, | ||
}: { | ||
queries: Array<{ text: string; boost?: number }>; | ||
esClient: { asCurrentUser: ElasticsearchClient }; | ||
uiSettingsClient: IUiSettingsClient; | ||
modelId: string; | ||
}): Promise<RecalledEntry[]> { | ||
const ML_INFERENCE_PREFIX = 'ml.inference.'; | ||
|
||
const connectorIndices = await getConnectorIndices(esClient, uiSettingsClient); | ||
|
||
const fieldCaps = await esClient.asCurrentUser.fieldCaps({ | ||
index: connectorIndices, | ||
fields: `${ML_INFERENCE_PREFIX}*`, | ||
allow_no_indices: true, | ||
types: ['sparse_vector'], | ||
filters: '-metadata,-parent', | ||
}); | ||
|
||
const fieldsWithVectors = Object.keys(fieldCaps.fields).map((field) => | ||
field.replace('_expanded.predicted_value', '').replace(ML_INFERENCE_PREFIX, '') | ||
); | ||
|
||
if (!fieldsWithVectors.length) { | ||
return []; | ||
} | ||
|
||
const esQueries = fieldsWithVectors.flatMap((field) => { | ||
const vectorField = `${ML_INFERENCE_PREFIX}${field}_expanded.predicted_value`; | ||
const modelField = `${ML_INFERENCE_PREFIX}${field}_expanded.model_id`; | ||
|
||
return queries.map(({ text, boost = 1 }) => { | ||
return { | ||
bool: { | ||
should: [ | ||
{ | ||
text_expansion: { | ||
[vectorField]: { | ||
model_text: text, | ||
model_id: modelId, | ||
boost, | ||
}, | ||
}, | ||
}, | ||
], | ||
filter: [ | ||
{ | ||
term: { | ||
[modelField]: modelId, | ||
}, | ||
}, | ||
], | ||
}, | ||
}; | ||
}); | ||
}); | ||
|
||
const response = await esClient.asCurrentUser.search<unknown>({ | ||
index: connectorIndices, | ||
query: { | ||
bool: { | ||
should: esQueries, | ||
}, | ||
}, | ||
size: 20, | ||
_source: { | ||
exclude: ['_*', 'ml*'], | ||
}, | ||
}); | ||
|
||
const results = response.hits.hits.map((hit) => ({ | ||
text: JSON.stringify(hit._source), | ||
score: hit._score!, | ||
is_correction: false, | ||
id: hit._id, | ||
})); | ||
|
||
return results; | ||
} | ||
|
||
async function getConnectorIndices( | ||
esClient: { asCurrentUser: ElasticsearchClient }, | ||
uiSettingsClient: IUiSettingsClient | ||
) { | ||
// improve performance by running this in parallel with the `uiSettingsClient` request | ||
const responsePromise = esClient.asCurrentUser.transport.request({ | ||
method: 'GET', | ||
path: '_connector', | ||
querystring: { | ||
filter_path: 'results.index_name', | ||
}, | ||
}); | ||
|
||
const customSearchConnectorIndex = await uiSettingsClient.get<string>( | ||
aiAssistantSearchConnectorIndexPattern | ||
); | ||
|
||
if (customSearchConnectorIndex) { | ||
return customSearchConnectorIndex.split(','); | ||
} | ||
|
||
const response = (await responsePromise) as { results?: Array<{ index_name: string }> }; | ||
const connectorIndices = response.results?.map((result) => result.index_name); | ||
|
||
// preserve backwards compatibility with 8.14 (may not be needed in the future) | ||
if (isEmpty(connectorIndices)) { | ||
return ['search-*']; | ||
} | ||
|
||
return connectorIndices; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters