Source code for langchain_community.document_transformers.long_context_reorder
"""Reorder documents"""fromtypingimportAny,List,Sequencefromlangchain_core.documentsimportBaseDocumentTransformer,DocumentfrompydanticimportBaseModel,ConfigDictdef_litm_reordering(documents:List[Document])->List[Document]:"""Lost in the middle reorder: the less relevant documents will be at the middle of the list and more relevant elements at beginning / end. See: https://arxiv.org/abs//2307.03172"""documents.reverse()reordered_result=[]fori,valueinenumerate(documents):ifi%2==1:reordered_result.append(value)else:reordered_result.insert(0,value)returnreordered_result
[docs]classLongContextReorder(BaseDocumentTransformer,BaseModel):"""Reorder long context. Lost in the middle: Performance degrades when models must access relevant information in the middle of long contexts. See: https://arxiv.org/abs//2307.03172"""model_config=ConfigDict(arbitrary_types_allowed=True,)