Skip to content

Auto merging retriever

AutoMergingRetrieverPack #

Bases: BaseLlamaPack

自动合并检索器包。

从一组文档构建一个分层节点图,并运行我们的自动合并检索器。

Source code in llama_index/packs/auto_merging_retriever/base.py
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
class AutoMergingRetrieverPack(BaseLlamaPack):
    """自动合并检索器包。

从一组文档构建一个分层节点图,并运行我们的自动合并检索器。"""

    def __init__(
        self,
        docs: List[Document] = None,
        **kwargs: Any,
    ) -> None:
        """初始化参数。"""
        # create the sentence window node parser w/ default settings
        self.node_parser = HierarchicalNodeParser.from_defaults()
        nodes = self.node_parser.get_nodes_from_documents(docs)
        leaf_nodes = get_leaf_nodes(nodes)
        docstore = SimpleDocumentStore()

        # insert nodes into docstore
        docstore.add_documents(nodes)

        # define storage context (will include vector store by default too)
        storage_context = StorageContext.from_defaults(docstore=docstore)

        service_context = ServiceContext.from_defaults(
            llm=OpenAI(model="gpt-3.5-turbo")
        )
        self.base_index = VectorStoreIndex(
            leaf_nodes,
            storage_context=storage_context,
            service_context=service_context,
        )
        base_retriever = self.base_index.as_retriever(similarity_top_k=6)
        self.retriever = AutoMergingRetriever(
            base_retriever, storage_context, verbose=True
        )
        self.query_engine = RetrieverQueryEngine.from_args(self.retriever)

    def get_modules(self) -> Dict[str, Any]:
        """获取模块。"""
        return {
            "node_parser": self.node_parser,
            "retriever": self.retriever,
            "query_engine": self.query_engine,
        }

    def run(self, *args: Any, **kwargs: Any) -> Any:
        """运行流水线。"""
        return self.query_engine.query(*args, **kwargs)

get_modules #

get_modules() -> Dict[str, Any]

获取模块。

Source code in llama_index/packs/auto_merging_retriever/base.py
56
57
58
59
60
61
62
def get_modules(self) -> Dict[str, Any]:
    """获取模块。"""
    return {
        "node_parser": self.node_parser,
        "retriever": self.retriever,
        "query_engine": self.query_engine,
    }

run #

run(*args: Any, **kwargs: Any) -> Any

运行流水线。

Source code in llama_index/packs/auto_merging_retriever/base.py
64
65
66
def run(self, *args: Any, **kwargs: Any) -> Any:
    """运行流水线。"""
    return self.query_engine.query(*args, **kwargs)