For further details, please visit https://help.aliyun.com/zh/open-search/search-platform/developer-reference/ranker-api-details.
Source code in llama-index-integrations/postprocessor/llama-index-postprocessor-alibabacloud-aisearch-rerank/llama_index/postprocessor/alibabacloud_aisearch_rerank/base.py
classAlibabaCloudAISearchRerank(BaseNodePostprocessor):""" For further details, please visit `https://help.aliyun.com/zh/open-search/search-platform/developer-reference/ranker-api-details`. """_client:Client=PrivateAttr()aisearch_api_key:str=Field(default=None,exclude=True)endpoint:str=Noneservice_id:str="ops-bge-reranker-larger"workspace_name:str="default"top_n:int=3batch_size:int=16def__init__(self,endpoint:str=None,aisearch_api_key:str=None,**kwargs:Any)->None:super().__init__(**kwargs)self.aisearch_api_key=get_from_param_or_env("aisearch_api_key",aisearch_api_key,"AISEARCH_API_KEY")self.endpoint=get_from_param_or_env("endpoint",endpoint,"AISEARCH_ENDPOINT")config=AISearchConfig(bearer_token=self.aisearch_api_key,endpoint=self.endpoint,protocol="http",)self._client=Client(config=config)@classmethoddefclass_name(cls)->str:return"AlibabaCloudAISearchRerank"@retry_decoratordef_rerank_one_batch(self,query:str,texts:List[str])->List[GetDocumentRankResponseBodyResultScores]:request=GetDocumentRankRequest(docs=texts,query=query)response:GetDocumentRankResponse=self._client.get_document_rank(workspace_name=self.workspace_name,service_id=self.service_id,request=request,)returnresponse.body.result.scoresdef_rerank(self,query:str,texts:List[str],top_n:int)->List[GetDocumentRankResponseBodyResultScores]:scores=[]foriinrange(0,len(texts),self.batch_size):batch_scores=self._rerank_one_batch(query,texts[i:i+self.batch_size])forscoreinbatch_scores:score.index=i+score.indexscores.extend(batch_scores)scores.sort(key=lambdax:x.score,reverse=True)returnscores[:top_n]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.service_id,))ifquery_bundleisNone:raiseValueError("Missing query bundle in extra info.")iflen(nodes)==0:return[]withself.callback_manager.event(CBEventType.RERANKING,payload={EventPayload.NODES:nodes,EventPayload.MODEL_NAME:self.service_id,EventPayload.QUERY_STR:query_bundle.query_str,EventPayload.TOP_K:self.top_n,},)asevent:texts=[node.node.get_content(metadata_mode=MetadataMode.EMBED)fornodeinnodes]results=self._rerank(query=query_bundle.query_str,texts=texts,top_n=self.top_n,)new_nodes=[]forresultinresults:new_node_with_score=NodeWithScore(node=nodes[result.index].node,score=result.score)new_nodes.append(new_node_with_score)event.on_end(payload={EventPayload.NODES:new_nodes})dispatcher.event(ReRankEndEvent(nodes=new_nodes))returnnew_nodes