Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Boosting give more consistent scores #406

Merged
merged 1 commit into from
Sep 6, 2023
Merged
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
11 changes: 9 additions & 2 deletions backend/danswer/search/semantic_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,26 @@ def semantic_reranking(
query: str,
chunks: list[InferenceChunk],
) -> list[InferenceChunk]:
model_max = 12 # These are just based on observations from model selection
model_min = -12
cross_encoders = get_default_reranking_model_ensemble()
sim_scores = [
encoder.predict([(query, chunk.content) for chunk in chunks]) # type: ignore
for encoder in cross_encoders
]

cross_models_min = numpy.min(sim_scores)

shifted_sim_scores = sum(
[enc_n_scores - numpy.min(enc_n_scores) for enc_n_scores in sim_scores]
[enc_n_scores - cross_models_min for enc_n_scores in sim_scores]
) / len(sim_scores)

boosts = [translate_boost_count_to_multiplier(chunk.boost) for chunk in chunks]
boosted_sim_scores = shifted_sim_scores * boosts
scored_results = list(zip(boosted_sim_scores, chunks))
normalized_b_s_scores = (boosted_sim_scores + cross_models_min - model_min) / (
model_max - model_min
)
scored_results = list(zip(normalized_b_s_scores, chunks))
scored_results.sort(key=lambda x: x[0], reverse=True)
ranked_sim_scores, ranked_chunks = zip(*scored_results)

Expand Down
Loading