diff --git a/python/sglang/srt/entrypoints/openai/serving_rerank.py b/python/sglang/srt/entrypoints/openai/serving_rerank.py index f5d6e4743..7bf410a07 100644 --- a/python/sglang/srt/entrypoints/openai/serving_rerank.py +++ b/python/sglang/srt/entrypoints/openai/serving_rerank.py @@ -1,5 +1,6 @@ import heapq import logging +import math from typing import Any, Dict, List, Optional, Union from fastapi import Request @@ -533,8 +534,6 @@ class OpenAIServingRerank(OpenAIServingBase): def _extract_score_from_logprobs(self, ret: Dict[str, Any]) -> float: """Extract reranking score from generation response with logprobs.""" - import math - # Get logprobs from the response meta_info = ret.get("meta_info", {}) output_top_logprobs = meta_info.get("output_top_logprobs", []) @@ -546,13 +545,19 @@ class OpenAIServingRerank(OpenAIServingBase): # Format: list of tuples (logprob, token_id, token_text) p_yes = 0.0 p_no = 0.0 + found_yes = False + found_no = False for item in top_logprobs: logprob, token_id = item[0], item[1] if token_id == self._yes_token_id: p_yes = math.exp(logprob) + found_yes = True elif token_id == self._no_token_id: p_no = math.exp(logprob) + found_no = True + if found_yes and found_no: + break return _qwen3_rerank_score(p_yes, p_no)