Source code for langchain_community.document_transformers.long_context_reorder

"""重新排序文档"""
from typing import Any, List, Sequence

from langchain_core.documents import BaseDocumentTransformer, Document
from langchain_core.pydantic_v1 import BaseModel


def _litm_reordering(documents: List[Document]) -> List[Document]:
    """在中间重新排序:不太相关的文档将位于列表的中间,而更相关的元素将位于开头/结尾。
参见:https://arxiv.org/abs//2307.03172
"""

    documents.reverse()
    reordered_result = []
    for i, value in enumerate(documents):
        if i % 2 == 1:
            reordered_result.append(value)
        else:
            reordered_result.insert(0, value)
    return reordered_result


[docs]class LongContextReorder(BaseDocumentTransformer, BaseModel): """重新排序长上下文。 迷失在中间: 当模型必须访问长上下文中的相关信息时,性能会下降。 参见:https://arxiv.org/abs//2307.03172""" class Config: """此pydantic对象的配置。""" arbitrary_types_allowed = True
[docs] def transform_documents( self, documents: Sequence[Document], **kwargs: Any ) -> Sequence[Document]: """重新排序文档。""" return _litm_reordering(list(documents))
[docs] async def atransform_documents( self, documents: Sequence[Document], **kwargs: Any ) -> Sequence[Document]: raise NotImplementedError