Cascade Bot commited on
Commit
bb8cb0c
·
1 Parent(s): 6cc7431

Added StrategyResult class and enhanced base ReasoningStrategy

Browse files
Files changed (1) hide show
  1. reasoning/base.py +81 -8
reasoning/base.py CHANGED
@@ -1,17 +1,90 @@
1
- """Base class for all reasoning strategies."""
2
 
3
- from typing import Dict, Any
 
 
 
4
 
5
- class ReasoningStrategy:
6
- """Base class for reasoning strategies."""
7
- async def reason(self, query: str, context: Dict[str, Any]) -> Dict[str, Any]:
8
- """Apply reasoning strategy to query with context.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
  Args:
11
  query: The query to reason about
12
  context: Additional context for reasoning
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
  Returns:
15
- Dictionary containing reasoning results
16
  """
17
- raise NotImplementedError
 
1
+ """Base classes and types for reasoning strategies."""
2
 
3
+ from abc import ABC, abstractmethod
4
+ from typing import Dict, Any, List, Optional
5
+ from dataclasses import dataclass, field
6
+ from datetime import datetime
7
 
8
+ @dataclass
9
+ class StrategyResult:
10
+ """Result from a reasoning strategy."""
11
+
12
+ strategy_type: str
13
+ success: bool
14
+ answer: Optional[str]
15
+ confidence: float
16
+ reasoning_trace: List[Dict[str, Any]]
17
+ metadata: Dict[str, Any]
18
+ performance_metrics: Dict[str, Any]
19
+ timestamp: str = field(default_factory=lambda: datetime.now().isoformat())
20
+
21
+ class ReasoningStrategy(ABC):
22
+ """Base class for all reasoning strategies."""
23
+
24
+ @abstractmethod
25
+ async def reason(
26
+ self,
27
+ query: str,
28
+ context: Dict[str, Any],
29
+ **kwargs
30
+ ) -> StrategyResult:
31
+ """
32
+ Reason about a query using this strategy.
33
 
34
  Args:
35
  query: The query to reason about
36
  context: Additional context for reasoning
37
+ **kwargs: Additional strategy-specific parameters
38
+
39
+ Returns:
40
+ StrategyResult containing the reasoning output and metadata
41
+ """
42
+ pass
43
+
44
+ def _prepare_messages(
45
+ self,
46
+ query: str,
47
+ context: Dict[str, Any]
48
+ ) -> List[Dict[str, str]]:
49
+ """
50
+ Prepare messages for the model.
51
+
52
+ Args:
53
+ query: The query to reason about
54
+ context: Additional context
55
+
56
+ Returns:
57
+ List of message dictionaries
58
+ """
59
+ messages = []
60
+
61
+ # Add system message if provided
62
+ if "system_message" in context:
63
+ messages.append({
64
+ "role": "system",
65
+ "content": context["system_message"]
66
+ })
67
+
68
+ # Add chat history if provided
69
+ if "chat_history" in context:
70
+ messages.extend(context["chat_history"])
71
+
72
+ # Add the current query
73
+ messages.append({
74
+ "role": "user",
75
+ "content": query
76
+ })
77
+
78
+ return messages
79
+
80
+ def _calculate_confidence(self, response: Any) -> float:
81
+ """
82
+ Calculate confidence score for a response.
83
+
84
+ Args:
85
+ response: Response from the model
86
 
87
  Returns:
88
+ Confidence score between 0 and 1
89
  """
90
+ return 0.8 # Default confidence