File size: 9,686 Bytes
226ac39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
"""
Reasoning Module - Core Abstraction

Provides clean separation between:
- Deterministic data processing (tools)
- Non-deterministic reasoning (LLM)

Design Principles:
- NO RAW DATA ACCESS - Only summaries/metadata
- NO TRAINING DECISIONS - Only explanations
- STRUCTURED I/O - JSON in, JSON + text out
- CACHEABLE - Deterministic enough to cache
- REASONING ONLY - No execution, no side effects

Architecture:
    Tool → Generates Summary → Reasoning Module → Returns Explanation
    
    Tool: "Here's what I found: {stats}"
    Reasoning: "Based on these stats, this means..."
    
Usage:
    from reasoning import get_reasoner
    
    reasoner = get_reasoner()
    result = reasoner.explain_data(
        summary={"rows": 1000, "columns": 20, "missing": 50}
    )
"""

import os
from typing import Dict, Any, Optional, Union
from abc import ABC, abstractmethod


class ReasoningBackend(ABC):
    """Abstract base class for reasoning backends."""
    
    @abstractmethod
    def generate(
        self,
        prompt: str,
        system_prompt: Optional[str] = None,
        temperature: float = 0.1,
        max_tokens: int = 2048
    ) -> str:
        """Generate reasoning response."""
        pass
    
    @abstractmethod
    def generate_structured(
        self,
        prompt: str,
        schema: Dict[str, Any],
        system_prompt: Optional[str] = None
    ) -> Dict[str, Any]:
        """Generate structured JSON response."""
        pass


class GeminiBackend(ReasoningBackend):
    """Gemini reasoning backend."""
    
    def __init__(self, api_key: Optional[str] = None, model: str = "gemini-2.0-flash-exp"):
        try:
            import google.generativeai as genai
        except ImportError:
            raise ImportError(
                "google-generativeai not installed. "
                "Install with: pip install google-generativeai"
            )
        
        api_key = api_key or os.getenv("GOOGLE_API_KEY")
        if not api_key:
            raise ValueError(
                "Google API key required. Set GOOGLE_API_KEY env var or pass api_key"
            )
        
        genai.configure(api_key=api_key)
        self.model = genai.GenerativeModel(
            model,
            generation_config={"temperature": 0.1}
        )
        self.model_name = model
    
    def generate(
        self,
        prompt: str,
        system_prompt: Optional[str] = None,
        temperature: float = 0.1,
        max_tokens: int = 2048
    ) -> str:
        """Generate reasoning response."""
        # Combine system and user prompts
        full_prompt = prompt
        if system_prompt:
            full_prompt = f"{system_prompt}\n\n{prompt}"
        
        response = self.model.generate_content(
            full_prompt,
            generation_config={
                "temperature": temperature,
                "max_output_tokens": max_tokens
            }
        )
        
        return response.text
    
    def generate_structured(
        self,
        prompt: str,
        schema: Dict[str, Any],
        system_prompt: Optional[str] = None
    ) -> Dict[str, Any]:
        """Generate structured JSON response."""
        import json
        
        # Add schema instruction
        schema_str = json.dumps(schema, indent=2)
        structured_prompt = f"""{prompt}

Respond with valid JSON matching this schema:
{schema_str}

Your response must be valid JSON only, no other text."""
        
        response_text = self.generate(structured_prompt, system_prompt)
        
        # Extract JSON from response
        try:
            # Try direct parse
            return json.loads(response_text)
        except json.JSONDecodeError:
            # Try to extract JSON from markdown code blocks
            import re
            json_match = re.search(r'```json\s*\n(.*?)\n```', response_text, re.DOTALL)
            if json_match:
                return json.loads(json_match.group(1))
            
            # Try to extract any JSON object
            json_match = re.search(r'\{.*\}', response_text, re.DOTALL)
            if json_match:
                return json.loads(json_match.group(0))
            
            raise ValueError(f"Failed to extract JSON from response: {response_text[:200]}...")


class GroqBackend(ReasoningBackend):
    """Groq reasoning backend."""
    
    def __init__(self, api_key: Optional[str] = None, model: str = "llama-3.3-70b-versatile"):
        try:
            from groq import Groq
        except ImportError:
            raise ImportError(
                "groq not installed. "
                "Install with: pip install groq"
            )
        
        api_key = api_key or os.getenv("GROQ_API_KEY")
        if not api_key:
            raise ValueError(
                "Groq API key required. Set GROQ_API_KEY env var or pass api_key"
            )
        
        self.client = Groq(api_key=api_key)
        self.model_name = model
    
    def generate(
        self,
        prompt: str,
        system_prompt: Optional[str] = None,
        temperature: float = 0.1,
        max_tokens: int = 2048
    ) -> str:
        """Generate reasoning response."""
        messages = []
        
        if system_prompt:
            messages.append({"role": "system", "content": system_prompt})
        
        messages.append({"role": "user", "content": prompt})
        
        response = self.client.chat.completions.create(
            model=self.model_name,
            messages=messages,
            temperature=temperature,
            max_tokens=max_tokens
        )
        
        return response.choices[0].message.content
    
    def generate_structured(
        self,
        prompt: str,
        schema: Dict[str, Any],
        system_prompt: Optional[str] = None
    ) -> Dict[str, Any]:
        """Generate structured JSON response."""
        import json
        
        # Add schema instruction
        schema_str = json.dumps(schema, indent=2)
        structured_prompt = f"""{prompt}

Respond with valid JSON matching this schema:
{schema_str}

Your response must be valid JSON only, no other text."""
        
        response_text = self.generate(structured_prompt, system_prompt)
        
        # Extract JSON from response
        try:
            return json.loads(response_text)
        except json.JSONDecodeError:
            # Try to extract JSON from markdown code blocks
            import re
            json_match = re.search(r'```json\s*\n(.*?)\n```', response_text, re.DOTALL)
            if json_match:
                return json.loads(json_match.group(1))
            
            # Try to extract any JSON object
            json_match = re.search(r'\{.*\}', response_text, re.DOTALL)
            if json_match:
                return json.loads(json_match.group(0))
            
            raise ValueError(f"Failed to extract JSON from response: {response_text[:200]}...")


class ReasoningEngine:
    """
    Main reasoning engine.
    
    Delegates to appropriate backend (Gemini, Groq, etc).
    Provides high-level reasoning capabilities.
    """
    
    def __init__(
        self,
        backend: Optional[ReasoningBackend] = None,
        provider: str = "gemini"
    ):
        """
        Initialize reasoning engine.
        
        Args:
            backend: Custom backend instance
            provider: 'gemini' or 'groq' (if backend not provided)
        """
        if backend:
            self.backend = backend
        else:
            provider = provider or os.getenv("LLM_PROVIDER", "gemini")
            
            if provider == "gemini":
                self.backend = GeminiBackend()
            elif provider == "groq":
                self.backend = GroqBackend()
            else:
                raise ValueError(f"Unsupported provider: {provider}")
        
        self.provider = provider
    
    def reason(
        self,
        prompt: str,
        system_prompt: Optional[str] = None,
        temperature: float = 0.1
    ) -> str:
        """
        General-purpose reasoning.
        
        Args:
            prompt: User prompt
            system_prompt: Optional system context
            temperature: Creativity (0.0 = deterministic, 1.0 = creative)
            
        Returns:
            Natural language response
        """
        return self.backend.generate(prompt, system_prompt, temperature)
    
    def reason_structured(
        self,
        prompt: str,
        schema: Dict[str, Any],
        system_prompt: Optional[str] = None
    ) -> Dict[str, Any]:
        """
        Structured reasoning with JSON output.
        
        Args:
            prompt: User prompt
            schema: Expected JSON schema
            system_prompt: Optional system context
            
        Returns:
            Parsed JSON response
        """
        return self.backend.generate_structured(prompt, schema, system_prompt)


# Singleton instance
_reasoning_engine: Optional[ReasoningEngine] = None


def get_reasoner(
    backend: Optional[ReasoningBackend] = None,
    provider: Optional[str] = None
) -> ReasoningEngine:
    """
    Get singleton reasoning engine.
    
    Args:
        backend: Custom backend instance
        provider: 'gemini' or 'groq'
        
    Returns:
        ReasoningEngine instance
    """
    global _reasoning_engine
    
    if _reasoning_engine is None or backend is not None:
        _reasoning_engine = ReasoningEngine(backend=backend, provider=provider)
    
    return _reasoning_engine


def reset_reasoner():
    """Reset singleton (for testing)."""
    global _reasoning_engine
    _reasoning_engine = None