Skip to content

Commit

Permalink
community[patch]: Add ID field back to Azure AI Search results (#25828)
Browse files Browse the repository at this point in the history
Commandeering #23243 as
maintainers don't have ability to modify that PR.

Fixes #22827

---------

Co-authored-by: Ming Quah <fleetadmiralbutter@icloud.com>
  • Loading branch information
ccurme and FleetAdmiralButter authored Aug 28, 2024
1 parent 9fa172b commit afe8cca
Showing 1 changed file with 28 additions and 8 deletions.
36 changes: 28 additions & 8 deletions libs/community/langchain_community/vectorstores/azuresearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -1283,6 +1283,11 @@ def semantic_hybrid_search_with_score_and_rerank(
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
Expand Down Expand Up @@ -1362,6 +1367,11 @@ async def asemantic_hybrid_search_with_score_and_rerank(
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
Expand Down Expand Up @@ -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,
},
)

Expand Down

0 comments on commit afe8cca

Please sign in to comment.