LLMListwiseRerank Class — langchain Architecture
Architecture documentation for the LLMListwiseRerank class in listwise_rerank.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD f5dac976_7ac3_cb43_2244_72d962ef6afb["LLMListwiseRerank"] 56ee7e00_cbf2_37e6_b294_468dfe7f2941["BaseDocumentCompressor"] f5dac976_7ac3_cb43_2244_72d962ef6afb -->|extends| 56ee7e00_cbf2_37e6_b294_468dfe7f2941 b678cac6_c104_ceb0_605e_a8f9013f12c4["RankDocuments"] f5dac976_7ac3_cb43_2244_72d962ef6afb -->|extends| b678cac6_c104_ceb0_605e_a8f9013f12c4 4e632d3e_51a3_ce8f_2735_e8ab3d863e6b["listwise_rerank.py"] f5dac976_7ac3_cb43_2244_72d962ef6afb -->|defined in| 4e632d3e_51a3_ce8f_2735_e8ab3d863e6b c7a4ff3e_79cd_cb68_3f2c_658ca6510415["compress_documents()"] f5dac976_7ac3_cb43_2244_72d962ef6afb -->|method| c7a4ff3e_79cd_cb68_3f2c_658ca6510415 37782356_811e_e15b_6a04_79e18ff6f0fa["from_llm()"] f5dac976_7ac3_cb43_2244_72d962ef6afb -->|method| 37782356_811e_e15b_6a04_79e18ff6f0fa
Relationship Graph
Source Code
libs/langchain/langchain_classic/retrievers/document_compressors/listwise_rerank.py lines 40–146
class LLMListwiseRerank(BaseDocumentCompressor):
"""Document compressor that uses `Zero-Shot Listwise Document Reranking`.
Adapted from: https://arxiv.org/pdf/2305.02156.pdf
`LLMListwiseRerank` uses a language model to rerank a list of documents based on
their relevance to a query.
!!! note
Requires that underlying model implement `with_structured_output`.
Example usage:
```python
from langchain_classic.retrievers.document_compressors.listwise_rerank import (
LLMListwiseRerank,
)
from langchain_core.documents import Document
from langchain_openai import ChatOpenAI
documents = [
Document("Sally is my friend from school"),
Document("Steve is my friend from home"),
Document("I didn't always like yogurt"),
Document("I wonder why it's called football"),
Document("Where's waldo"),
]
reranker = LLMListwiseRerank.from_llm(
llm=ChatOpenAI(model="gpt-3.5-turbo"), top_n=3
)
compressed_docs = reranker.compress_documents(documents, "Who is steve")
assert len(compressed_docs) == 3
assert "Steve" in compressed_docs[0].page_content
```
"""
reranker: Runnable[dict, list[Document]]
"""LLM-based reranker to use for filtering documents. Expected to take in a dict
with 'documents: Sequence[Document]' and 'query: str' keys and output a
List[Document]."""
top_n: int = 3
"""Number of documents to return."""
model_config = ConfigDict(
arbitrary_types_allowed=True,
)
def compress_documents(
self,
documents: Sequence[Document],
query: str,
callbacks: Callbacks | None = None,
) -> Sequence[Document]:
"""Filter down documents based on their relevance to the query."""
results = self.reranker.invoke(
{"documents": documents, "query": query},
config={"callbacks": callbacks},
)
return results[: self.top_n]
@classmethod
def from_llm(
cls,
llm: BaseLanguageModel,
*,
prompt: BasePromptTemplate | None = None,
**kwargs: Any,
) -> "LLMListwiseRerank":
"""Create a LLMListwiseRerank document compressor from a language model.
Args:
llm: The language model to use for filtering. **Must implement
BaseLanguageModel.with_structured_output().**
prompt: The prompt to use for the filter.
kwargs: Additional arguments to pass to the constructor.
Returns:
A LLMListwiseRerank document compressor that uses the given language model.
"""
if type(llm).with_structured_output == BaseLanguageModel.with_structured_output:
Source
Frequently Asked Questions
What is the LLMListwiseRerank class?
LLMListwiseRerank is a class in the langchain codebase, defined in libs/langchain/langchain_classic/retrievers/document_compressors/listwise_rerank.py.
Where is LLMListwiseRerank defined?
LLMListwiseRerank is defined in libs/langchain/langchain_classic/retrievers/document_compressors/listwise_rerank.py at line 40.
What does LLMListwiseRerank extend?
LLMListwiseRerank extends BaseDocumentCompressor, RankDocuments.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free