Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions python/dify_plugin/interfaces/model/openai_compatible/rerank.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,11 @@ def _invoke(
rerank_documents = []
scores = [result["relevance_score"] for result in results["results"]]

# Min-Max Normalization: Normalize scores to 0 ~ 1.0 range
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
# Use sigmoid normalization to map scores to 0-1 range
import math
def sigmoid(x):
# Apply the sigmoid formula: 1 / (1 + e^(-x))
return 1.0 / (1.0 + math.exp(-x))

for result in results["results"]:
index = result["index"]
Expand All @@ -94,8 +95,8 @@ def _invoke(
elif isinstance(document, str):
text = document

# Normalize the score
normalized_score = (result["relevance_score"] - min_score) / score_range
# Normalize the score Apply sigmoid normalization to each score
normalized_scores = [sigmoid(score) for score in scores]

# Create RerankDocument object with normalized score
rerank_document = RerankDocument(
Expand Down