[BUG] Remove hard coded variables and better class extraction from KB#11490
Conversation
|
🔒 Entelligence AI Vulnerability Scanner ✅ No security vulnerabilities found! Your code passed our comprehensive security analysis. 📊 Files Analyzed: 3 files |
Review Summary🏷️ Draft Comments (2)
|
Review Summary🏷️ Draft Comments (2)
|
Review Summary🏷️ Draft Comments (1)
|
Review Summary |
Review Summary🏷️ Draft Comments (1)
|
Increases the `DEFAULT_RERANKER_MAX_TOKENS` from 3 to 100. This allows the re-ranker to process longer text snippets for improved accuracy.
Review Summary🔍 Comments beyond diff scope (1)
|
Review Summary🏷️ Draft Comments (3)
🔍 Comments beyond diff scope (1)
|
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Review Summary🏷️ Draft Comments (1)
|
Review Summary🏷️ Draft Comments (2)
|
Description
This PR adds configurability for reranker parameters and fixes a critical bug that occurred when using higher
max_tokensvalues.Key Changes:
Made reranker parameters configurable: The variables
n,logprobs,top_logprobs, andmax_tokenscan now be configured via:MINDSDB_RERANKER_N,MINDSDB_RERANKER_LOGPROBS, etc.)default_reranking_modelsection)RerankerConfigclassFixed token extraction bug: Resolved an issue where increasing
max_tokensbeyond 4 would cause the reranker to incorrectly extract class probabilities from wrong tokens (quotes, newlines, end tokens) instead of the actual class number token.Before: Parameters were hardcoded as
n=1,logprobs=True,top_logprobs=4,max_tokens=3After: Parameters are configurable with the same defaults, and the system correctly extracts class probabilities regardless of
max_tokensvalue.Type of change
Verification Process
To ensure the changes are working as expected:
max_tokens=100(previously would return 0.0 relevance scores for all documents)Reranker selected_class_tokenlog shows the correct class number token being selectedAdditional Media:
max_tokens=4(working): Correctly extracts from class number tokenmax_tokens=100(was broken, now fixed): Previously extracted from wrong tokens, now correctly finds class number tokenChecklist:
Technical Details
Files Modified:
mindsdb/integrations/utilities/rag/settings.py: Added default constants and extendedRerankerConfigmindsdb/integrations/utilities/rag/rerankers/base_reranker.py: Added configurable parameters and fixed token extraction logicmindsdb/utilities/config.py: Added environment variable support for reranker parametersConfiguration Methods:
Appendix: The Token Extraction Bug: Detailed Examples
Problem: Wrong Token Selection with Higher
max_tokensThe issue was that the original code always took the last token to extract class probabilities, but with higher
max_tokensvalues, the model generates additional formatting tokens after the class number.Example 1: Working Case (
max_tokens=4)Token Sequence:
Old Logic:
Example 2: Broken Case (
max_tokens=100)Token Sequence from your logs:
Old Logic:
What happened:
top_logprobscontained things like'class_','class_ \n\n','class_ (''class_1','class_2', etc.Example 3: Real Log Analysis
Example:
Analysis:
class_2'class_', not'class_2''class_2'in the extracted probabilities → score = 0.0The Fix: Smart Token Selection
New Logic:
Applied to Example 2:
Before vs After Comparison
max_tokens=4max_tokens=100Real Results from Your Logs
Before Fix (
max_tokens=100):After Fix (
max_tokens=100):Why This Bug Was So Sneaky
max_tokensbecause there were fewer "distractor" tokensmax_tokenswas increased, making everything seem "not relevant"The fix ensures we always find the right token regardless of how many additional formatting tokens the model generates! 🎯