Spaces:
Runtime error
Runtime error
Upload folder using huggingface_hub
Browse files- reasoning/specialized.py +113 -0
reasoning/specialized.py
CHANGED
|
@@ -11,6 +11,119 @@ from collections import defaultdict
|
|
| 11 |
|
| 12 |
from .base import ReasoningStrategy
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
class CodeRewriteStrategy(ReasoningStrategy):
|
| 15 |
"""
|
| 16 |
Advanced code rewriting strategy that:
|
|
|
|
| 11 |
|
| 12 |
from .base import ReasoningStrategy
|
| 13 |
|
| 14 |
+
class SpecializedReasoning(ReasoningStrategy):
|
| 15 |
+
"""
|
| 16 |
+
A composite reasoning strategy that combines multiple specialized strategies
|
| 17 |
+
for different domains and tasks.
|
| 18 |
+
"""
|
| 19 |
+
|
| 20 |
+
def __init__(self, config: Optional[Dict[str, Any]] = None):
|
| 21 |
+
"""Initialize specialized reasoning with component strategies."""
|
| 22 |
+
super().__init__()
|
| 23 |
+
self.config = config or {}
|
| 24 |
+
|
| 25 |
+
# Initialize component strategies
|
| 26 |
+
self.strategies = {
|
| 27 |
+
'code_rewrite': CodeRewriteStrategy(),
|
| 28 |
+
'security_audit': SecurityAuditStrategy(),
|
| 29 |
+
'performance': PerformanceOptimizationStrategy(),
|
| 30 |
+
'testing': TestGenerationStrategy(),
|
| 31 |
+
'documentation': DocumentationStrategy(),
|
| 32 |
+
'api_design': APIDesignStrategy(),
|
| 33 |
+
'dependencies': DependencyManagementStrategy(),
|
| 34 |
+
'code_review': CodeReviewStrategy()
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
async def reason(self, query: str, context: Dict[str, Any]) -> Dict[str, Any]:
|
| 38 |
+
"""
|
| 39 |
+
Apply specialized reasoning by selecting and combining appropriate
|
| 40 |
+
strategies based on the query and context.
|
| 41 |
+
|
| 42 |
+
Args:
|
| 43 |
+
query: The input query to reason about
|
| 44 |
+
context: Additional context and parameters
|
| 45 |
+
|
| 46 |
+
Returns:
|
| 47 |
+
Dict containing reasoning results and confidence scores
|
| 48 |
+
"""
|
| 49 |
+
try:
|
| 50 |
+
# Determine which strategies to use based on context
|
| 51 |
+
selected_strategies = await self._select_strategies(query, context)
|
| 52 |
+
|
| 53 |
+
# Get results from each selected strategy
|
| 54 |
+
results = {}
|
| 55 |
+
for strategy_name in selected_strategies:
|
| 56 |
+
strategy = self.strategies[strategy_name]
|
| 57 |
+
results[strategy_name] = await strategy.reason(query, context)
|
| 58 |
+
|
| 59 |
+
# Combine results
|
| 60 |
+
combined_result = await self._combine_results(results, context)
|
| 61 |
+
|
| 62 |
+
return {
|
| 63 |
+
'answer': combined_result.get('answer', ''),
|
| 64 |
+
'confidence': combined_result.get('confidence', 0.0),
|
| 65 |
+
'reasoning_path': {
|
| 66 |
+
'selected_strategies': selected_strategies,
|
| 67 |
+
'individual_results': results,
|
| 68 |
+
'combination_method': combined_result.get('method', '')
|
| 69 |
+
}
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
except Exception as e:
|
| 73 |
+
logging.error(f"Specialized reasoning failed: {str(e)}")
|
| 74 |
+
return {
|
| 75 |
+
'error': f"Specialized reasoning failed: {str(e)}",
|
| 76 |
+
'confidence': 0.0
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
async def _select_strategies(self, query: str, context: Dict[str, Any]) -> List[str]:
|
| 80 |
+
"""Select appropriate strategies based on query and context."""
|
| 81 |
+
selected = []
|
| 82 |
+
|
| 83 |
+
# Simple keyword-based selection for now
|
| 84 |
+
keywords = {
|
| 85 |
+
'code_rewrite': ['rewrite', 'refactor', 'improve'],
|
| 86 |
+
'security_audit': ['security', 'vulnerability', 'audit'],
|
| 87 |
+
'performance': ['performance', 'optimize', 'speed'],
|
| 88 |
+
'testing': ['test', 'coverage', 'verify'],
|
| 89 |
+
'documentation': ['document', 'explain', 'describe'],
|
| 90 |
+
'api_design': ['api', 'interface', 'endpoint'],
|
| 91 |
+
'dependencies': ['dependency', 'package', 'version'],
|
| 92 |
+
'code_review': ['review', 'quality', 'check']
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
query_lower = query.lower()
|
| 96 |
+
for strategy, terms in keywords.items():
|
| 97 |
+
if any(term in query_lower for term in terms):
|
| 98 |
+
selected.append(strategy)
|
| 99 |
+
|
| 100 |
+
# If no specific strategies selected, use code review as default
|
| 101 |
+
if not selected:
|
| 102 |
+
selected = ['code_review']
|
| 103 |
+
|
| 104 |
+
return selected
|
| 105 |
+
|
| 106 |
+
async def _combine_results(
|
| 107 |
+
self,
|
| 108 |
+
results: Dict[str, Dict[str, Any]],
|
| 109 |
+
context: Dict[str, Any]
|
| 110 |
+
) -> Dict[str, Any]:
|
| 111 |
+
"""Combine results from multiple strategies."""
|
| 112 |
+
if not results:
|
| 113 |
+
return {'answer': '', 'confidence': 0.0, 'method': 'none'}
|
| 114 |
+
|
| 115 |
+
# For now, use the highest confidence result
|
| 116 |
+
best_result = max(
|
| 117 |
+
results.items(),
|
| 118 |
+
key=lambda x: x[1].get('confidence', 0)
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
return {
|
| 122 |
+
'answer': best_result[1].get('answer', ''),
|
| 123 |
+
'confidence': best_result[1].get('confidence', 0.0),
|
| 124 |
+
'method': 'highest_confidence'
|
| 125 |
+
}
|
| 126 |
+
|
| 127 |
class CodeRewriteStrategy(ReasoningStrategy):
|
| 128 |
"""
|
| 129 |
Advanced code rewriting strategy that:
|