Some checks failed
Create and publish Docker images with specific build args / build-main-image (linux/amd64, ubuntu-latest) (push) Has been cancelled
Create and publish Docker images with specific build args / build-main-image (linux/arm64, ubuntu-24.04-arm) (push) Has been cancelled
Create and publish Docker images with specific build args / build-cuda-image (linux/amd64, ubuntu-latest) (push) Has been cancelled
Create and publish Docker images with specific build args / build-cuda-image (linux/arm64, ubuntu-24.04-arm) (push) Has been cancelled
Create and publish Docker images with specific build args / build-cuda126-image (linux/amd64, ubuntu-latest) (push) Has been cancelled
Create and publish Docker images with specific build args / build-cuda126-image (linux/arm64, ubuntu-24.04-arm) (push) Has been cancelled
Create and publish Docker images with specific build args / build-ollama-image (linux/amd64, ubuntu-latest) (push) Has been cancelled
Create and publish Docker images with specific build args / build-ollama-image (linux/arm64, ubuntu-24.04-arm) (push) Has been cancelled
Create and publish Docker images with specific build args / build-slim-image (linux/amd64, ubuntu-latest) (push) Has been cancelled
Create and publish Docker images with specific build args / build-slim-image (linux/arm64, ubuntu-24.04-arm) (push) Has been cancelled
Create and publish Docker images with specific build args / merge-main-images (push) Has been cancelled
Create and publish Docker images with specific build args / merge-cuda-images (push) Has been cancelled
Create and publish Docker images with specific build args / merge-cuda126-images (push) Has been cancelled
Create and publish Docker images with specific build args / merge-ollama-images (push) Has been cancelled
Create and publish Docker images with specific build args / merge-slim-images (push) Has been cancelled
Python CI / Format Backend (3.11.x) (push) Has been cancelled
Python CI / Format Backend (3.12.x) (push) Has been cancelled
Frontend Build / Format & Build Frontend (push) Has been cancelled
Frontend Build / Frontend Unit Tests (push) Has been cancelled
Close inactive issues / close-issues (push) Has been cancelled
73 lines
2.1 KiB
Python
73 lines
2.1 KiB
Python
import logging
|
|
import requests
|
|
from typing import Optional, List, Tuple
|
|
from urllib.parse import quote
|
|
|
|
|
|
from open_webui.env import ENABLE_FORWARD_USER_INFO_HEADERS
|
|
from open_webui.retrieval.models.base_reranker import BaseReranker
|
|
from open_webui.utils.headers import include_user_info_headers
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
class ExternalReranker(BaseReranker):
|
|
def __init__(
|
|
self,
|
|
api_key: str,
|
|
url: str = "http://localhost:8080/v1/rerank",
|
|
model: str = "reranker",
|
|
timeout: Optional[int] = None,
|
|
):
|
|
self.api_key = api_key
|
|
self.url = url
|
|
self.model = model
|
|
self.timeout = timeout
|
|
|
|
def predict(
|
|
self, sentences: List[Tuple[str, str]], user=None
|
|
) -> Optional[List[float]]:
|
|
query = sentences[0][0]
|
|
docs = [i[1] for i in sentences]
|
|
|
|
payload = {
|
|
"model": self.model,
|
|
"query": query,
|
|
"documents": docs,
|
|
"top_n": len(docs),
|
|
}
|
|
|
|
try:
|
|
log.info(f"ExternalReranker:predict:model {self.model}")
|
|
log.info(f"ExternalReranker:predict:query {query}")
|
|
|
|
headers = {
|
|
"Content-Type": "application/json",
|
|
"Authorization": f"Bearer {self.api_key}",
|
|
}
|
|
|
|
if ENABLE_FORWARD_USER_INFO_HEADERS and user:
|
|
headers = include_user_info_headers(headers, user)
|
|
|
|
r = requests.post(
|
|
f"{self.url}",
|
|
headers=headers,
|
|
json=payload,
|
|
timeout=self.timeout,
|
|
)
|
|
|
|
r.raise_for_status()
|
|
data = r.json()
|
|
|
|
if "results" in data:
|
|
sorted_results = sorted(data["results"], key=lambda x: x["index"])
|
|
return [result["relevance_score"] for result in sorted_results]
|
|
else:
|
|
log.error("No results found in external reranking response")
|
|
return None
|
|
|
|
except Exception as e:
|
|
log.exception(f"Error in external reranking: {e}")
|
|
return None
|