2025-02-23 16:38:56 +08:00
|
|
|
import os
|
|
|
|
|
import json
|
|
|
|
|
import requests
|
|
|
|
|
import numpy as np
|
|
|
|
|
|
2025-04-06 20:33:15 +08:00
|
|
|
from src import config
|
2025-06-25 02:12:19 +08:00
|
|
|
from src.utils import logger, get_docker_safe_url
|
2025-02-23 16:38:56 +08:00
|
|
|
|
|
|
|
|
def sigmoid(x):
|
|
|
|
|
return 1 / (1 + np.exp(-x))
|
|
|
|
|
|
2025-07-02 02:38:36 +08:00
|
|
|
class OnlineReranker:
|
|
|
|
|
def __init__(self, model_name, api_key, base_url, **kwargs):
|
|
|
|
|
self.url = get_docker_safe_url(base_url)
|
|
|
|
|
self.model = model_name
|
|
|
|
|
self.api_key = api_key
|
2025-02-23 16:38:56 +08:00
|
|
|
self.headers = {
|
|
|
|
|
"Authorization": f"Bearer {api_key}",
|
|
|
|
|
"Content-Type": "application/json"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def compute_score(self, sentence_pairs, batch_size = 256, max_length = 512, normalize = False):
|
|
|
|
|
# TODO 还没实现 batch_size
|
|
|
|
|
query, sentences = sentence_pairs[0], sentence_pairs[1]
|
|
|
|
|
payload = self.build_payload(query, sentences, max_length)
|
|
|
|
|
response = requests.request("POST", self.url, json=payload, headers=self.headers)
|
|
|
|
|
response = json.loads(response.text)
|
2025-03-20 19:51:46 +08:00
|
|
|
# logger.debug(f"SiliconFlow Reranker response: {response}")
|
2025-02-23 16:38:56 +08:00
|
|
|
|
|
|
|
|
results = sorted(response["results"], key=lambda x: x["index"])
|
|
|
|
|
all_scores = [result["relevance_score"] for result in results]
|
|
|
|
|
|
|
|
|
|
if normalize:
|
|
|
|
|
all_scores = [sigmoid(score) for score in all_scores]
|
|
|
|
|
|
|
|
|
|
return all_scores
|
|
|
|
|
|
|
|
|
|
def build_payload(self, query, sentences, max_length = 512):
|
|
|
|
|
return {
|
|
|
|
|
"model": self.model,
|
|
|
|
|
"query": query,
|
|
|
|
|
"documents": sentences,
|
|
|
|
|
"max_chunks_per_doc": max_length,
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-02 02:38:36 +08:00
|
|
|
def get_reranker(model_id, **kwargs):
|
2025-05-24 11:29:45 +08:00
|
|
|
support_rerankers = config.reranker_names.keys()
|
2025-07-02 02:38:36 +08:00
|
|
|
assert model_id in support_rerankers, f"Unsupported Reranker: {model_id}, only support {support_rerankers}"
|
|
|
|
|
|
|
|
|
|
model_info = config.reranker_names[model_id]
|
|
|
|
|
base_url = model_info["base_url"]
|
|
|
|
|
api_key = os.getenv(model_info["api_key"], model_info["api_key"])
|
|
|
|
|
assert api_key, f"{model_info['name']} api_key is required"
|
|
|
|
|
return OnlineReranker(
|
|
|
|
|
model_name=model_info["name"],
|
|
|
|
|
api_key=api_key,
|
|
|
|
|
base_url=base_url,
|
|
|
|
|
**kwargs
|
|
|
|
|
)
|