Skip to content

Commit 73d722f

Browse files
committed
docs: Add test script and documentation for EpistemicState and Cost-Benefit Logic
1. Created test script (scripts/test_epistemic_state.py) - 8 test cases covering all epistemic states - Tests KNOWN, UNCERTAIN, UNKNOWN scenarios - Tests edge cases (None values, fallback, critical failures) - All tests passing 2. Created documentation (docs/EPISTEMIC_STATE.md) - Overview of epistemic state classification - Classification rules and examples - Implementation details and configuration - Use cases and future enhancements 3. Created documentation (docs/COST_BENEFIT_REWRITE.md) - Overview of cost-benefit logic for RewriteLLM - Self-correction modes (off/light/aggressive) - Quality thresholds and rewrite decision logic - Configuration and logging 4. Updated README.md - Added Cost-Benefit Logic and EpistemicState to Features section - Added links to new documentation in Documentation section - Kept updates concise and professional 5. Fixed format string errors in epistemic_state.py - Fixed f-string conditional formatting issues - Fixed Unicode encoding in test script (Windows compatibility)
1 parent 575d4db commit 73d722f

5 files changed

Lines changed: 545 additions & 2 deletions

File tree

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,12 @@ See `env.example` for full list.
226226
- ✅ Post-Processing System - Quality enhancement and variation
227227
- Quality evaluator - Rule-based quality assessment (0 token cost)
228228
- Rewrite engine - LLM-based answer refinement with retry mechanism
229+
- Cost-benefit logic - Intelligent rewrite decisions (prevents unnecessary rewrites)
229230
- Style sanitizer - Removes anthropomorphic language
230231
- Honesty handler - Specialized processing for transparency questions
232+
- ✅ Epistemic State Classification - Response certainty indicators
233+
- KNOWN/UNCERTAIN/UNKNOWN states - Transparent knowledge classification
234+
- Rule-based classifier - Based on citations, confidence, and validation results
231235
- ✅ Philosophical Question Processor - 3-layer system for consciousness/emotion questions
232236
- Intent classification (consciousness, emotion, understanding, mixed)
233237
- Sub-type detection (paradox, epistemic, meta, definitional, direct)
@@ -417,6 +421,8 @@ We welcome contributions! See [CONTRIBUTING.md](CONTRIBUTING.md) for detailed se
417421
**Features:**
418422
- [`docs/SPICE_ARCHITECTURE.md`](docs/SPICE_ARCHITECTURE.md) - SPICE framework
419423
- [`docs/CONFIDENCE_AND_FALLBACK.md`](docs/CONFIDENCE_AND_FALLBACK.md) - Validation system
424+
- [`docs/COST_BENEFIT_REWRITE.md`](docs/COST_BENEFIT_REWRITE.md) - Cost-benefit logic for RewriteLLM
425+
- [`docs/EPISTEMIC_STATE.md`](docs/EPISTEMIC_STATE.md) - Epistemic state classification (KNOWN/UNCERTAIN/UNKNOWN)
420426

421427
## ⚠️ Known Limitations & Improvements
422428

backend/core/epistemic_state.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,11 @@ def calculate_epistemic_state(
162162
(conf_score is None or conf_score >= 0.7) and
163163
not has_warnings
164164
):
165+
conf_display = f"{conf_score:.2f}" if conf_score is not None else "N/A"
165166
logger.debug(
166167
f"EpistemicState: KNOWN "
167168
f"(passed={validation_passed}, citations={has_citations}, "
168-
f"ctx_docs={ctx_docs_count}, confidence={conf_score:.2f if conf_score else 'N/A'})"
169+
f"ctx_docs={ctx_docs_count}, confidence={conf_display})"
169170
)
170171
return EpistemicState.KNOWN
171172

@@ -189,10 +190,11 @@ def calculate_epistemic_state(
189190
(validation_passed and has_warnings) or
190191
(has_citations and ctx_docs_count == 0) # Citations but no context (general knowledge)
191192
):
193+
conf_display = f"{conf_score:.2f}" if conf_score is not None else "N/A"
192194
logger.debug(
193195
f"EpistemicState: UNCERTAIN "
194196
f"(citations={has_citations}, ctx_docs={ctx_docs_count}, "
195-
f"confidence={conf_score:.2f if conf_score else 'N/A'}, warnings={has_warnings})"
197+
f"confidence={conf_display}, warnings={has_warnings})"
196198
)
197199
return EpistemicState.UNCERTAIN
198200

docs/COST_BENEFIT_REWRITE.md

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# Cost-Benefit Logic for RewriteLLM
2+
3+
## Overview
4+
5+
StillMe implements a **cost-benefit policy** for the RewriteLLM system to intelligently decide when to rewrite responses for quality improvement. This prevents unnecessary rewrites that waste tokens and increase latency while ensuring critical quality issues are addressed.
6+
7+
## Self-Correction Modes
8+
9+
The system supports three modes (configurable via `SELF_CORRECTION_MODE` environment variable):
10+
11+
- **`off`**: No rewrites (disable self-correction)
12+
- **`light`**: Conservative (max 1 rewrite for medium quality, max 2 for low quality)
13+
- **`aggressive`**: More rewrites (max 2 for medium quality, max 2 for low quality)
14+
15+
**Default**: `light` (balanced cost and quality)
16+
17+
## Quality Thresholds
18+
19+
### High Quality (>= 0.8)
20+
- **Action**: No rewrite
21+
- **Reason**: Quality is already good enough
22+
23+
### Medium Quality (0.5 - 0.8)
24+
- **Light mode**: Max 1 rewrite (only if critical issues present)
25+
- **Aggressive mode**: Max 2 rewrites
26+
- **Condition**: Only rewrite if critical issues or aggressive mode enabled
27+
28+
### Low Quality (< 0.5)
29+
- **Action**: Max 2 rewrites allowed
30+
- **Stop condition**: Stop if quality doesn't improve after rewrite
31+
32+
## Rewrite Decision Logic
33+
34+
### Initial Decision (`should_rewrite()`)
35+
36+
The system decides whether to rewrite based on:
37+
38+
1. **Quality score** from QualityEvaluator
39+
2. **Critical issues** (anthropomorphic language, missing citations, template-like responses, topic drift)
40+
3. **Current rewrite count** (prevents excessive rewrites)
41+
4. **Self-correction mode** (off/light/aggressive)
42+
43+
### Continue Decision (`should_continue_rewrite()`)
44+
45+
After each rewrite, the system decides whether to continue:
46+
47+
- **Continue if**: Quality improved significantly (>= 0.2) but still below threshold
48+
- **Stop if**:
49+
- Quality reached threshold (>= 0.8)
50+
- Max attempts reached
51+
- Quality not improving (< 0.1 improvement) and still low
52+
53+
## Configuration
54+
55+
### Environment Variable
56+
57+
```bash
58+
SELF_CORRECTION_MODE=light # Options: off, light, aggressive
59+
```
60+
61+
### Code Configuration
62+
63+
Thresholds can be adjusted in `RewriteDecisionPolicy.__init__()`:
64+
65+
```python
66+
self.high_quality_threshold = 0.8 # No rewrite if >= this
67+
self.medium_quality_threshold = 0.5 # Conditional rewrite if 0.5-0.8
68+
self.max_attempts_light_medium = 1 # Light mode: max 1 for medium
69+
self.max_attempts_aggressive_medium = 2 # Aggressive mode: max 2 for medium
70+
self.max_attempts_low_quality = 2 # Low quality: max 2 attempts
71+
```
72+
73+
## Logging
74+
75+
The system logs detailed metrics for monitoring:
76+
77+
```
78+
🔄 Cost-Benefit: Rewrite decision - {reason}, quality_before={score:.2f}, rewrite_count={count}/{max}, mode={mode}
79+
📊 Rewrite metrics (attempt {n}): quality_before={before:.2f}, quality_after={after:.2f}, improvement={improvement:+.2f}
80+
✅ Rewrite loop complete: initial_quality={init:.2f}, final_quality={final:.2f}, total_rewrites={count}
81+
```
82+
83+
## Benefits
84+
85+
1. **Cost Reduction**: Prevents unnecessary rewrites for already-good responses
86+
2. **Latency Optimization**: Reduces response time by skipping redundant rewrites
87+
3. **Quality Assurance**: Still addresses critical quality issues when needed
88+
4. **Configurable**: Easy to adjust thresholds and modes based on requirements
89+
90+
## Architecture
91+
92+
- **Module**: `backend/postprocessing/rewrite_decision_policy.py`
93+
- **Integration**: Used by `PostProcessingOptimizer.should_rewrite()`
94+
- **Pipeline**: Integrated into `chat_router.py` rewrite loop
95+
96+
## Future Enhancements
97+
98+
- **ML-based Classifier**: Upgrade from rule-based to ML-based for more nuanced decisions
99+
- **Token Cost Tracking**: Track actual token costs for each rewrite
100+
- **Adaptive Thresholds**: Automatically adjust thresholds based on performance metrics
101+
102+
## See Also
103+
104+
- [Epistemic State Classification](EPISTEMIC_STATE.md)
105+
- [Validation Chain Documentation](VALIDATION_CHAIN.md)
106+
- [StillMe Architecture](../README.md)
107+

docs/EPISTEMIC_STATE.md

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
# Epistemic State Classification
2+
3+
## Overview
4+
5+
StillMe classifies each response into one of three **epistemic states** to indicate the system's level of certainty about the information provided:
6+
7+
- **KNOWN**: Clear evidence, good citations, validators pass
8+
- **UNCERTAIN**: Some information but thin, or validators warn
9+
- **UNKNOWN**: System truly doesn't know / insufficient data
10+
11+
This classification helps users understand the reliability of StillMe's responses and aligns with StillMe's philosophy of transparency and anti-hallucination.
12+
13+
## Classification Rules
14+
15+
### KNOWN State
16+
17+
A response is classified as **KNOWN** when:
18+
19+
- ✅ Validation passed (`validation_passed = True`)
20+
- ✅ Has valid citations (e.g., `[1]`, `[2]`, `[general knowledge]`)
21+
- ✅ Has RAG context (`context_docs_count > 0`)
22+
- ✅ High confidence (`confidence_score >= 0.7`)
23+
- ✅ No critical warnings from validators
24+
25+
**Example:**
26+
```
27+
Response: "According to [1] and [2], the capital of France is Paris."
28+
- Citations: [1], [2]
29+
- Context docs: 3
30+
- Confidence: 0.85
31+
- Validation: Passed
32+
→ State: KNOWN
33+
```
34+
35+
### UNCERTAIN State
36+
37+
A response is classified as **UNCERTAIN** when:
38+
39+
- Has some information (citations or context) but:
40+
- Medium confidence (`0.4 <= confidence_score < 0.7`), or
41+
- Has warnings from validators (non-critical), or
42+
- Has citations but no RAG context (general knowledge only)
43+
44+
**Example:**
45+
```
46+
Response: "Based on [1], the answer might be X. However, there is some uncertainty."
47+
- Citations: [1]
48+
- Context docs: 1
49+
- Confidence: 0.55
50+
- Validation: Passed with warnings
51+
→ State: UNCERTAIN
52+
```
53+
54+
### UNKNOWN State
55+
56+
A response is classified as **UNKNOWN** when:
57+
58+
- ❌ Fallback triggered (`used_fallback = True`), or
59+
- ❌ No context and low confidence (`context_docs_count = 0` and `confidence_score < 0.5`), or
60+
- ❌ Critical validation failures (e.g., `factual_hallucination`, `missing_citation`, `explicit_fake_entity`)
61+
62+
**Example:**
63+
```
64+
Response: "I don't have sufficient information to answer this question accurately."
65+
- Citations: None
66+
- Context docs: 0
67+
- Confidence: 0.2
68+
- Fallback: True
69+
→ State: UNKNOWN
70+
```
71+
72+
## Implementation
73+
74+
### Location
75+
76+
- **Module**: `backend/core/epistemic_state.py`
77+
- **Function**: `calculate_epistemic_state()`
78+
- **Integration**: Calculated after validation, before returning `ChatResponse`
79+
80+
### API Response
81+
82+
The `epistemic_state` field is included in the `ChatResponse` JSON:
83+
84+
```json
85+
{
86+
"response": "...",
87+
"confidence_score": 0.85,
88+
"validation_info": {...},
89+
"epistemic_state": "KNOWN"
90+
}
91+
```
92+
93+
### Configuration
94+
95+
Thresholds can be adjusted in `calculate_epistemic_state()`:
96+
97+
- **KNOWN threshold**: `confidence_score >= 0.7` (line ~100)
98+
- **UNCERTAIN range**: `0.4 <= confidence_score < 0.7` (line ~120)
99+
- **Citation patterns**: Regex patterns for detecting citations (line ~60)
100+
- **Critical failures**: List of critical validation failure patterns (line ~75)
101+
102+
## Use Cases
103+
104+
1. **User Transparency**: Users can see at a glance how confident StillMe is about each response
105+
2. **Quality Monitoring**: Track the distribution of epistemic states across responses
106+
3. **Fallback Detection**: Identify when StillMe is using fallback responses
107+
4. **Citation Quality**: Distinguish between responses with RAG context vs. general knowledge
108+
109+
## Future Enhancements
110+
111+
- **ML-based Classification**: Upgrade from rule-based to ML-based classifier for more nuanced classification
112+
- **Citation Quality Scoring**: Consider citation relevance and source quality, not just presence
113+
- **Confidence Calibration**: Fine-tune confidence thresholds based on evaluation results
114+
- **Dashboard Integration**: Display epistemic state badges in the dashboard UI
115+
116+
## Related Features
117+
118+
- **Validation Chain**: Epistemic state relies on validation results from the validator chain
119+
- **Confidence Scoring**: Uses `confidence_score` calculated from context quality and validation results
120+
- **Fallback Mechanism**: UNKNOWN state is triggered when fallback responses are used
121+
- **Citation System**: KNOWN state requires valid citations from RAG context
122+
123+
## See Also
124+
125+
- [Validation Chain Documentation](VALIDATION_CHAIN.md)
126+
- [Cost-Benefit Logic for RewriteLLM](COST_BENEFIT_REWRITE.md)
127+
- [StillMe Architecture](../README.md)
128+

0 commit comments

Comments
 (0)