Repository navigation
Fix search vector_similarity_weight - #15108
Conversation
📝 WalkthroughWalkthroughRemove vector weight inversion in the UI and propagate explicit ChangesVector similarity weight persistence & propagation
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested labels
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (2)
api/apps/restful_apis/search_api.py (1)
164-177:⚠️ Potential issue | 🟠 Major | ⚡ Quick winValidate and bound
vector_similarity_weightbefore persisting config.Line 167 merges
search_configwithout validatingvector_similarity_weight. A non-numeric or out-of-range value can later break retrieval math (1 - vector_similarity_weight) or produce invalid negative full-text weight.Suggested fix
current_config = search_app.search_config or {} new_config = req["search_config"] if not isinstance(new_config, dict): return get_data_error_result(message="search_config must be a JSON object") + if "vector_similarity_weight" in new_config: + try: + vsw = float(new_config["vector_similarity_weight"]) + except (TypeError, ValueError): + return get_data_error_result(message="vector_similarity_weight must be a number") + if vsw < 0 or vsw > 1: + return get_data_error_result(message="vector_similarity_weight must be between 0 and 1") + new_config["vector_similarity_weight"] = vsw req["search_config"] = {**current_config, **new_config}🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@api/apps/restful_apis/search_api.py` around lines 164 - 177, The merged search_config currently accepts any vector_similarity_weight value; validate and bound it before persisting: in the code around new_config handling in search_api.py, ensure new_config.get("vector_similarity_weight") is checked, coerce to a float (or return get_data_error_result if non-numeric), clamp it to a sensible range [0.0, 1.0] (or use a default like 0.3 if missing), then set req["search_config"]["vector_similarity_weight"] to that sanitized value prior to logging and calling _full_text_weight so downstream math (e.g., 1 - vector_similarity_weight) is safe and deterministic.api/db/services/dialog_service.py (1)
1564-1587:⚠️ Potential issue | 🟠 Major | ⚡ Quick winNormalize
vector_similarity_weightto a bounded float before retrieval.Line 1564 currently forwards raw config value. If it is a string/invalid type, retrieval can fail at runtime when weight arithmetic runs.
Suggested fix
- vector_similarity_weight = search_config.get("vector_similarity_weight", 0.3) - try: - full_text_weight = 1 - vector_similarity_weight - except TypeError: - full_text_weight = None + try: + vector_similarity_weight = float(search_config.get("vector_similarity_weight", 0.3)) + except (TypeError, ValueError): + vector_similarity_weight = 0.3 + vector_similarity_weight = min(max(vector_similarity_weight, 0.0), 1.0) + full_text_weight = 1 - vector_similarity_weight🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@api/db/services/dialog_service.py` around lines 1564 - 1587, The code forwards a raw config value for vector_similarity_weight (set where vector_similarity_weight = search_config.get(...)) which can be a string or invalid type and cause runtime errors; before computing full_text_weight and calling retriever.retrieval, coerce vector_similarity_weight to a float with a safe fallback (default 0.3), clamp it to the [0.0, 1.0] range, and then compute full_text_weight = 1.0 - vector_similarity_weight (or None only if coercion fails); update the debug log to reflect the normalized value and ensure the normalized vector_similarity_weight is what is passed into retriever.retrieval and related downstream calls (reference the vector_similarity_weight variable, full_text_weight computation, and the retriever.retrieval call).
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Outside diff comments:
In `@api/apps/restful_apis/search_api.py`:
- Around line 164-177: The merged search_config currently accepts any
vector_similarity_weight value; validate and bound it before persisting: in the
code around new_config handling in search_api.py, ensure
new_config.get("vector_similarity_weight") is checked, coerce to a float (or
return get_data_error_result if non-numeric), clamp it to a sensible range [0.0,
1.0] (or use a default like 0.3 if missing), then set
req["search_config"]["vector_similarity_weight"] to that sanitized value prior
to logging and calling _full_text_weight so downstream math (e.g., 1 -
vector_similarity_weight) is safe and deterministic.
In `@api/db/services/dialog_service.py`:
- Around line 1564-1587: The code forwards a raw config value for
vector_similarity_weight (set where vector_similarity_weight =
search_config.get(...)) which can be a string or invalid type and cause runtime
errors; before computing full_text_weight and calling retriever.retrieval,
coerce vector_similarity_weight to a float with a safe fallback (default 0.3),
clamp it to the [0.0, 1.0] range, and then compute full_text_weight = 1.0 -
vector_similarity_weight (or None only if coercion fails); update the debug log
to reflect the normalized value and ensure the normalized
vector_similarity_weight is what is passed into retriever.retrieval and related
downstream calls (reference the vector_similarity_weight variable,
full_text_weight computation, and the retriever.retrieval call).
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 7d7e8920-caec-43ae-8b67-a950d9ad22f5
📒 Files selected for processing (4)
api/apps/restful_apis/search_api.pyapi/apps/services/dataset_api_service.pyapi/db/services/dialog_service.pyrag/nlp/search.py
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #15108 +/- ##
=======================================
Coverage 93.12% 93.12%
=======================================
Files 10 10
Lines 713 713
Branches 116 116
=======================================
Hits 664 664
Misses 29 29
Partials 20 20 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
What problem does this PR solve?
Fix search vector_similarity_weight
Type of change