community[patch]: Add ID field back to Azure AI Search results (#25828)

Commandeering https://github.com/langchain-ai/langchain/pull/23243 as
maintainers don't have ability to modify that PR.

Fixes https://github.com/langchain-ai/langchain/issues/22827

---------

Co-authored-by: Ming Quah <fleetadmiralbutter@icloud.com>
This commit is contained in:
ccurme 2024-08-28 17:56:50 -04:00 committed by GitHub
parent 9fa172bc26
commit afe8ccaaa6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1283,6 +1283,11 @@ class AzureSearch(VectorStore):
Document(
page_content=result.pop(FIELDS_CONTENT),
metadata={
**(
{FIELDS_ID: result.pop(FIELDS_ID)}
if FIELDS_ID in result
else {}
),
**(
json.loads(result[FIELDS_METADATA])
if FIELDS_METADATA in result
@ -1362,6 +1367,11 @@ class AzureSearch(VectorStore):
Document(
page_content=result.pop(FIELDS_CONTENT),
metadata={
**(
{FIELDS_ID: result.pop(FIELDS_ID)}
if FIELDS_ID in result
else {}
),
**(
json.loads(result[FIELDS_METADATA])
if FIELDS_METADATA in result
@ -1752,16 +1762,26 @@ def _reorder_results_with_maximal_marginal_relevance(
def _result_to_document(result: Dict) -> Document:
# Fields metadata
if FIELDS_METADATA in result:
if isinstance(result[FIELDS_METADATA], dict):
fields_metadata = result[FIELDS_METADATA]
else:
fields_metadata = json.loads(result[FIELDS_METADATA])
else:
fields_metadata = {
key: value for key, value in result.items() if key != FIELDS_CONTENT_VECTOR
}
# IDs
if FIELDS_ID in result:
fields_id = {FIELDS_ID: result.pop(FIELDS_ID)}
else:
fields_id = {}
return Document(
page_content=result.pop(FIELDS_CONTENT),
metadata=(
result[FIELDS_METADATA]
if isinstance(result[FIELDS_METADATA], dict)
else json.loads(result[FIELDS_METADATA])
)
if FIELDS_METADATA in result
else {
key: value for key, value in result.items() if key != FIELDS_CONTENT_VECTOR
metadata={
**fields_id,
**fields_metadata,
},
)