From 28eb07b8fc3735f3a5918abafef95c2c0102cbb9 Mon Sep 17 00:00:00 2001 From: zdw <452202586@qq.com> Date: Fri, 11 Apr 2025 11:48:27 +0800 Subject: [PATCH] fix: normalize rerank score to 0-1 range when only one document --- .../dify_plugin/interfaces/model/openai_compatible/rerank.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/python/dify_plugin/interfaces/model/openai_compatible/rerank.py b/python/dify_plugin/interfaces/model/openai_compatible/rerank.py index 91b783f7..a31baa03 100644 --- a/python/dify_plugin/interfaces/model/openai_compatible/rerank.py +++ b/python/dify_plugin/interfaces/model/openai_compatible/rerank.py @@ -79,6 +79,9 @@ def _invoke( scores = [result["relevance_score"] for result in results["results"]] # Min-Max Normalization: Normalize scores to 0 ~ 1.0 range + if len(scores) == 1: + scores.append(0) + min_score = min(scores) max_score = max(scores) score_range = max_score - min_score if max_score != min_score else 1.0 # Avoid division by zero