This guide explains how to configure the enhanced LLM fallback logic in AircBot. The fallback system determines when the bot should switch from a local LLM to a remote LLM (like OpenAI) based on response quality.
The fallback system analyzes local LLM responses using multiple criteria:
- Response Length - Detects very short or incomplete responses
- "I Don't Know" Patterns - Identifies when the local LLM admits uncertainty
- Relevance Scoring - Checks if responses match the question topic (keyword-based)
- Semantic Coherence - Detects repetitive or broken responses
- Response Type Matching - Ensures responses match question types (code, explanations, etc.)
- Semantic Similarity - Uses AI-powered embeddings to understand response relevance beyond keywords (optional)
All fallback settings are configurable via environment variables. Here are the available settings with their default values and explanations:
What it does: Minimum number of characters required before considering a response valid. Effect of changing:
- Lower values (1-2): More permissive, accepts single-letter responses
- Higher values (5-10): More strict, requires longer responses
- Recommended: 3-5 for IRC environments, 5-10 for formal environments
# Very permissive - accepts "Y", "N", "OK"
export FALLBACK_MIN_RESPONSE_LENGTH=1
# Strict - requires at least 10 characters
export FALLBACK_MIN_RESPONSE_LENGTH=10What it does: Minimum word count for responses containing "I don't know" patterns before checking for additional context. Effect of changing:
- Lower values (5-10): More likely to fallback on uncertain responses
- Higher values (20-30): More forgiving of uncertain responses that provide context
- Recommended: 10-15 for technical discussions, 15-25 for general chat
# Fallback quickly on uncertainty
export FALLBACK_DONT_KNOW_CONTEXT_MIN_WORDS=10
# Be more forgiving of uncertain but helpful responses
export FALLBACK_DONT_KNOW_CONTEXT_MIN_WORDS=25What it does: Minimum ratio of question keywords that must appear in the response. Effect of changing:
- Lower values (0.01-0.03): More permissive, accepts responses with few matching keywords
- Higher values (0.1-0.3): More strict, requires more keyword overlap
- Recommended: 0.05-0.1 for technical discussions, 0.1-0.2 for structured Q&A
# Very permissive keyword matching
export FALLBACK_RELEVANCE_MIN_RATIO=0.01
# Strict keyword matching
export FALLBACK_RELEVANCE_MIN_RATIO=0.2What it does: Minimum number of meaningful words in a question before applying relevance scoring. Effect of changing:
- Lower values (1-2): Apply relevance scoring to very short questions
- Higher values (5-8): Only apply relevance scoring to longer questions
- Recommended: 3-5 for most environments
What it does: Minimum keyword overlap required when response type doesn't match question type. Effect of changing:
- Lower values (0.05): More forgiving of type mismatches
- Higher values (0.3): Stricter type matching requirements
- Recommended: 0.1-0.2 for balanced operation
What it does: Minimum question length before applying type mismatch penalties. Effect of changing:
- Lower values (3-4): Apply type checking to shorter questions
- Higher values (7-10): Only apply type checking to longer questions
- Recommended: 5-7 for most environments
What it does: Maximum word count for responses that start with generic phrases. Effect of changing:
- Lower values (10-15): More aggressive at catching generic responses
- Higher values (30-50): More forgiving of responses that start generically but provide value
- Recommended: 20-30 for balanced operation
What it does: Maximum ratio of any single word's occurrence in a response. Effect of changing:
- Lower values (0.2): More aggressive at catching repetitive responses
- Higher values (0.5): More forgiving of repetitive but valid responses
- Recommended: 0.3-0.4 for most environments
What it does: Minimum word length to consider when detecting repetition. Effect of changing:
- Lower values (2): Check repetition of shorter words like "is", "of"
- Higher values (4-5): Only check repetition of longer, more meaningful words
- Recommended: 3-4 for most environments
What it does: Minimum word count for responses to explanation-type questions. Effect of changing:
- Lower values (5-6): Accept shorter explanations
- Higher values (12-20): Require more detailed explanations
- Recommended: 8-12 for technical topics, 5-8 for simple questions
What it does: Maximum word count for short procedural responses (bypasses detailed step requirements). Effect of changing:
- Lower values (10): Require more detailed procedural responses
- Higher values (25): Accept longer responses without step-by-step format
- Recommended: 10-20 for most environments
What it does: Maximum word count for short code responses (bypasses code format requirements). Effect of changing:
- Lower values (5): Require more detailed code responses
- Higher values (20): Accept longer responses without code format
- Recommended: 5-15 for most environments
The bot supports advanced semantic similarity scoring using AI-powered sentence embeddings. This feature provides more intelligent fallback decisions beyond simple keyword matching.
Requirements: pip install sentence-transformers
What it does: Enables semantic similarity scoring for fallback decisions. Effect of changing:
true: Activates AI-powered relevance scoring (requires sentence-transformers)false: Uses only traditional keyword-based scoring (default for performance)- Recommended:
truefor technical discussions,falsefor casual chat or resource-constrained environments
What it does: Minimum similarity score (0.0-1.0) required between question and response. Effect of changing:
- Lower values (0.1-0.2): More permissive, accepts responses with lower semantic similarity
- Higher values (0.4-0.6): More strict, requires higher semantic similarity
- Recommended: 0.3-0.4 for balanced operation, 0.4-0.5 for technical accuracy
What it does: Weight of semantic similarity in combined scoring (0.0-1.0). Effect of changing:
- Lower values (0.2-0.3): Less influence of semantic scoring
- Higher values (0.5-0.7): More influence of semantic scoring
- Recommended: 0.4-0.5 for balanced operation
What it does: Specifies which sentence transformer model to use. Effect of changing:
all-MiniLM-L6-v2: Lightweight (22MB), fast, good for general useall-mpnet-base-v2: Larger (420MB), more accurate, better for technical contentall-distilroberta-v1: Medium (290MB), good balance of speed and accuracy- Recommended: Keep default unless you have specific accuracy requirements
What it does: Specifies processing device for the semantic model. Effect of changing:
cpu: Uses CPU processing (slower but widely compatible)cuda: Uses GPU processing (faster but requires CUDA-compatible GPU)- Recommended:
cpufor most setups,cudaif you have a compatible GPU
What it does: Number of embeddings to cache for performance. Effect of changing:
- Lower values (25-50): Less memory usage, more recomputation
- Higher values (200-500): More memory usage, better performance
- Recommended: 100-200 for most environments
What it does: Whether to consider conversation context in semantic scoring. Effect of changing:
true: Uses conversation history for better relevance (recommended)false: Only considers question-response similarity- Recommended:
truefor most environments
What it does: Weight of context similarity in combined scoring (0.0-1.0). Effect of changing:
- Lower values (0.1): Less influence of conversation context
- Higher values (0.3-0.4): More influence of conversation context
- Recommended: 0.2-0.3 for most environments
What it does: Boost factor for responses that address technical keywords. Effect of changing:
- Lower values (1.0-1.1): Less boost for technical accuracy
- Higher values (1.3-1.5): More boost for technical accuracy
- Recommended: 1.2-1.3 for technical discussions
What it does: Comma-separated list of technical keywords that receive boost scoring. Effect of changing:
- Add domain-specific terms: Better scoring for your specific technical domain
- Remove irrelevant terms: Cleaner scoring for non-technical environments
- Recommended: Customize for your specific use case
For casual IRC channels where short responses are common:
export FALLBACK_MIN_RESPONSE_LENGTH=2
export FALLBACK_RELEVANCE_MIN_RATIO=0.03
export FALLBACK_DONT_KNOW_CONTEXT_MIN_WORDS=10
export FALLBACK_GENERIC_RESPONSE_MAX_WORDS=20
export FALLBACK_EXPLANATION_MIN_WORDS=5
# Disable semantic similarity for performance
export SEMANTIC_SIMILARITY_ENABLED=falseFor technical support channels requiring detailed responses:
export FALLBACK_MIN_RESPONSE_LENGTH=5
export FALLBACK_RELEVANCE_MIN_RATIO=0.1
export FALLBACK_DONT_KNOW_CONTEXT_MIN_WORDS=20
export FALLBACK_GENERIC_RESPONSE_MAX_WORDS=30
export FALLBACK_EXPLANATION_MIN_WORDS=12
export FALLBACK_CODE_SHORT_ANSWER_MAX_WORDS=15
# Enable semantic similarity for technical accuracy
export SEMANTIC_SIMILARITY_ENABLED=true
export SEMANTIC_SIMILARITY_MIN_THRESHOLD=0.3
export SEMANTIC_ENTITY_BOOST=1.3For educational environments requiring comprehensive responses:
export FALLBACK_MIN_RESPONSE_LENGTH=10
export FALLBACK_RELEVANCE_MIN_RATIO=0.15
export FALLBACK_DONT_KNOW_CONTEXT_MIN_WORDS=25
export FALLBACK_GENERIC_RESPONSE_MAX_WORDS=35
export FALLBACK_EXPLANATION_MIN_WORDS=15
export FALLBACK_REPETITION_MAX_WORD_RATIO=0.25
# Strict semantic similarity for educational accuracy
export SEMANTIC_SIMILARITY_ENABLED=true
export SEMANTIC_SIMILARITY_MIN_THRESHOLD=0.4
export SEMANTIC_SIMILARITY_WEIGHT=0.5
export SEMANTIC_ENTITY_BOOST=1.2For development where you want to see more fallback behavior:
export FALLBACK_MIN_RESPONSE_LENGTH=5
export FALLBACK_RELEVANCE_MIN_RATIO=0.2
export FALLBACK_DONT_KNOW_CONTEXT_MIN_WORDS=30
export FALLBACK_GENERIC_RESPONSE_MAX_WORDS=40
export FALLBACK_EXPLANATION_MIN_WORDS=20
export FALLBACK_REPETITION_MAX_WORD_RATIO=0.2
# Enable semantic similarity for testing
export SEMANTIC_SIMILARITY_ENABLED=true
export SEMANTIC_SIMILARITY_MIN_THRESHOLD=0.5
export SEMANTIC_CACHE_SIZE=50For environments with limited CPU/memory resources:
export FALLBACK_MIN_RESPONSE_LENGTH=3
export FALLBACK_RELEVANCE_MIN_RATIO=0.05
export FALLBACK_DONT_KNOW_CONTEXT_MIN_WORDS=15
export FALLBACK_GENERIC_RESPONSE_MAX_WORDS=25
export FALLBACK_EXPLANATION_MIN_WORDS=8
# Disable semantic similarity to save resources
export SEMANTIC_SIMILARITY_ENABLED=falseUse the provided test suite to validate your configuration:
# Run the fallback logic tests
python tests/test_fallback_logic.py
# Run with your custom configuration
FALLBACK_MIN_RESPONSE_LENGTH=5 python tests/test_fallback_logic.py
# Test semantic similarity functionality
python tests/test_semantic_similarity.py
# Test with semantic similarity enabled
SEMANTIC_SIMILARITY_ENABLED=true python tests/test_semantic_similarity.pyThe semantic similarity feature requires additional testing considerations:
# Test basic functionality (works without sentence-transformers)
python tests/test_semantic_similarity.py
# Test with sentence-transformers installed
pip install sentence-transformers
SEMANTIC_SIMILARITY_ENABLED=true python tests/test_semantic_similarity.py
# Test different models
SEMANTIC_MODEL_NAME=all-mpnet-base-v2 python tests/test_semantic_similarity.py-
Fallback Rate: Percentage of responses that trigger fallback
- Target: 10-20% in normal operation
- Too high (>30%): Configuration may be too strict
- Too low (<5%): Configuration may be too permissive
-
Response Quality: User satisfaction with responses
- Monitor user feedback and complaints
- Track response relevance and completeness
-
Cost Impact: OpenAI API usage from fallbacks
- Monitor API costs and request volume
- Balance quality vs. cost based on your budget
- Start with defaults and monitor for 1-2 weeks
- Identify problem areas (too many/few fallbacks)
- Adjust one parameter at a time
- Test with the test suite before deploying
- Monitor results for another week
- Repeat until satisfied
- Increase
FALLBACK_MIN_RESPONSE_LENGTH(lower the threshold) - Decrease
FALLBACK_RELEVANCE_MIN_RATIO(be more permissive) - Increase
FALLBACK_GENERIC_RESPONSE_MAX_WORDS(accept longer generic responses) - Decrease
FALLBACK_EXPLANATION_MIN_WORDS(accept shorter explanations)
- Decrease
FALLBACK_MIN_RESPONSE_LENGTH(raise the threshold) - Increase
FALLBACK_RELEVANCE_MIN_RATIO(be more strict) - Decrease
FALLBACK_GENERIC_RESPONSE_MAX_WORDS(reject generic responses faster) - Increase
FALLBACK_EXPLANATION_MIN_WORDS(require more detailed explanations)
- For code-heavy environments: Increase
FALLBACK_CODE_SHORT_ANSWER_MAX_WORDS - For procedure-heavy environments: Increase
FALLBACK_PROCEDURAL_SHORT_ANSWER_MAX_WORDS - For casual chat: Decrease most thresholds to be more permissive
- For formal discussions: Increase most thresholds to be more strict
- Start conservative with default values
- Change one setting at a time to understand impact
- Test thoroughly with the test suite
- Monitor continuously after deployment
- Document your changes and reasoning
- Regular reviews of effectiveness (monthly/quarterly)
- Ensure environment variables are set before starting the bot
- Check that variable names are spelled correctly
- Verify values are within expected ranges (no negative numbers)
- Enable debug logging to see fallback decisions
- Use the test suite to validate specific scenarios
- Check for typos in environment variable names
- Very low thresholds may cause excessive processing
- Consider impact on response time vs. quality trade-offs
- Monitor system resource usage
For advanced users, consider:
- Dynamic thresholds based on time of day or channel activity
- User-specific configurations for different user groups
- Topic-specific configurations for different discussion areas
- A/B testing different configurations for optimization
Remember that the goal is to balance response quality, cost, and user experience. The "perfect" configuration depends on your specific use case and user expectations.