Skip to content

Siliconflow rerank

SiliconFlowRerank #

Bases: BaseNodePostprocessor

Source code in llama-index-integrations/postprocessor/llama-index-postprocessor-siliconflow-rerank/llama_index/postprocessor/siliconflow_rerank/base.py
 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
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
class SiliconFlowRerank(BaseNodePostprocessor):
    model: str = Field(
        default="BAAI/bge-reranker-v2-m3",
        description="Specifies the model to be used.",
    )
    base_url: str = Field(
        default=DEFAULT_SILICONFLOW_API_URL,
        description="The URL of the SiliconFlow Rerank API.",
    )
    api_key: str = Field(default=None, description="The SiliconFlow API key.")

    top_n: int = Field(
        description="Number of most relevant documents or indices to return."
    )
    return_documents: bool = Field(
        default=True,
        description="Specify whether the response should include the document text.",
    )
    max_chunks_per_doc: int = Field(
        default=1024,
        description="""\
            Maximum number of chunks generated from within a document.
            Long documents are divided into multiple chunks for calculation,
            and the highest score among the chunks is taken as the document's score.
        """,
    )
    overlap_tokens: int = Field(
        default=80,
        description="Number of token overlaps between adjacent chunks when documents are chunked.",
    )

    _session: Any = PrivateAttr()

    def __init__(
        self,
        model: str = "BAAI/bge-reranker-v2-m3",
        base_url: str = DEFAULT_SILICONFLOW_API_URL,
        api_key: Optional[str] = None,
        top_n: int = 4,
        return_documents: bool = True,
        max_chunks_per_doc: int = 1024,
        overlap_tokens: int = 80,
    ):
        super().__init__(
            model=model,
            base_url=base_url,
            api_key=api_key,
            top_n=top_n,
            return_documents=return_documents,
            max_chunks_per_doc=max_chunks_per_doc,
            overlap_tokens=overlap_tokens,
        )
        self._session: requests.Session = requests.Session()
        self._session.headers.update(
            {
                "Authorization": f"Bearer {self.api_key}",
                "Content-Type": "application/json",
            }
        )

    @classmethod
    def class_name(cls) -> str:
        return "SiliconFlowRerank"

    @property
    def _model_kwargs(self) -> Dict[str, Any]:
        return {
            "return_documents": self.return_documents,
            "max_chunks_per_doc": self.max_chunks_per_doc,
            "overlap_tokens": self.overlap_tokens,
        }

    def _postprocess_nodes(
        self,
        nodes: List[NodeWithScore],
        query_bundle: Optional[QueryBundle] = None,
    ) -> List[NodeWithScore]:
        dispatcher.event(
            ReRankStartEvent(
                query=query_bundle,
                nodes=nodes,
                top_n=self.top_n,
                model_name=self.model,
            )
        )

        if query_bundle is None:
            raise ValueError("Missing query bundle in extra info.")
        if len(nodes) == 0:
            return []

        with self.callback_manager.event(
            CBEventType.RERANKING,
            payload={
                EventPayload.NODES: nodes,
                EventPayload.MODEL_NAME: self.model,
                EventPayload.QUERY_STR: query_bundle.query_str,
                EventPayload.TOP_K: self.top_n,
            },
        ) as event:
            texts = [
                node.node.get_content(metadata_mode=MetadataMode.EMBED)
                for node in nodes
            ]
            response = self._session.post(
                self.base_url,
                json={
                    "model": self.model,
                    "query": query_bundle.query_str,
                    "documents": texts,
                    "top_n": self.top_n,
                    **self._model_kwargs,
                },
            ).json()
            if "results" not in response:
                raise RuntimeError(response)

            new_nodes = []
            for result in response["results"]:
                new_node_with_score = NodeWithScore(
                    node=nodes[result["index"]].node, score=result["relevance_score"]
                )
                new_nodes.append(new_node_with_score)
            event.on_end(payload={EventPayload.NODES: new_nodes})

        dispatcher.event(ReRankEndEvent(nodes=new_nodes))
        return new_nodes