mirror of
https://github.com/hwchase17/langchain.git
synced 2025-09-03 20:16:52 +00:00
Community: RankLLMRerank AttributeError (Handle list-based rerank results) (#29840)
# community: Fix AttributeError in RankLLMRerank (`list` object has no attribute `candidates`) ## **Description** This PR fixes an issue in `RankLLMRerank` where reranking fails with the following error: ``` AttributeError: 'list' object has no attribute 'candidates' ``` The issue arises because `rerank_batch()` returns a `List[Result]` instead of an object containing `.candidates`. ### **Changes Introduced** - Adjusted `compress_documents()` to support both: - Old API format: `rerank_results.candidates` - New API format: `rerank_results` as a list - Also fix wrong .txt location parsing while I was at it. --- ## **Issue** Fixes **AttributeError** in `RankLLMRerank` when using `compression_retriever.invoke()`. The issue is observed when `rerank_batch()` returns a list instead of an object with `.candidates`. **Relevant log:** ``` AttributeError: 'list' object has no attribute 'candidates' ``` ## **Dependencies** - No additional dependencies introduced. --- ## **Checklist** - [x] **Backward compatible** with previous API versions - [x] **Tested** locally with different RankLLM models - [x] **No new dependencies introduced** - [x] **Linted** with `make format && make lint` - [x] **Ready for review** --- ## **Testing** - Ran `compression_retriever.invoke(query)` ## **Reviewers** If no review within a few days, please **@mention** one of: - @baskaryan - @efriis - @eyurtsev - @ccurme - @vbarda - @hwchase17
This commit is contained in:
@@ -130,10 +130,17 @@ class RankLLMRerank(BaseDocumentCompressor):
|
||||
)
|
||||
|
||||
final_results = []
|
||||
for res in rerank_results.candidates:
|
||||
doc = documents[int(res.docid)]
|
||||
doc_copy = Document(doc.page_content, metadata=deepcopy(doc.metadata))
|
||||
final_results.append(doc_copy)
|
||||
if hasattr(rerank_results, "candidates"):
|
||||
# Old API format
|
||||
for res in rerank_results.candidates:
|
||||
doc = documents[int(res.docid)]
|
||||
doc_copy = Document(doc.page_content, metadata=deepcopy(doc.metadata))
|
||||
final_results.append(doc_copy)
|
||||
else:
|
||||
for res in rerank_results:
|
||||
doc = documents[int(res.docid)]
|
||||
doc_copy = Document(doc.page_content, metadata=deepcopy(doc.metadata))
|
||||
final_results.append(doc_copy)
|
||||
|
||||
return final_results[: self.top_n]
|
||||
|
||||
|
Reference in New Issue
Block a user