File size: 9,212 Bytes
2379054
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
OPTIMIZED ENGINE - User Selectable: Gemini / Local / Hybrid
"""

import os
import sys
from pathlib import Path
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

try:
    from dotenv import load_dotenv
    load_dotenv()
except Exception as e:
    pass

from openai import OpenAI
from typing import List, Dict
import chromadb
from sentence_transformers import SentenceTransformer

from mcp.cache_manager import _cache
from mcp.token_optimizer import _optimizer

class OptimizedEngine:
    def __init__(self, engine_type: str = "hybrid", model_name: str = "gemini-3.5-flash"):
        """
        Initialize optimized engine
        
        Args:
            engine_type: "gemini", "local", or "hybrid"
            model_name: Gemini model name
        """
        self.engine_type = engine_type
        self.model_name = model_name
        
        self.api_key = os.environ.get("GEMINI_API_KEY", "")
        self.use_gemini = False
        
        # Setup Gemini if available
        if self.api_key:
            try:
                self.client = OpenAI(
                    api_key=self.api_key,
                    base_url="https://generativelanguage.googleapis.com/v1beta/openai/"
                )
                self.use_gemini = True
                print("βœ… Gemini API connected")
            except Exception as e:
                print(f"⚠️ Gemini connection failed: {e}")
                self.use_gemini = False
        else:
            if engine_type in ["gemini", "hybrid"]:
                print("⚠️ GEMINI_API_KEY not set. Falling back to local.")
                self.engine_type = "local"
        
        # Initialize RAG
        self.embedding_model = SentenceTransformer('all-MiniLM-L6-v2')
        self.chroma_client = chromadb.PersistentClient(path="knowledge/chroma_db")
        
        try:
            self.collection = self.chroma_client.get_collection("track_design_knowledge")
            print("βœ… Connected to knowledge base")
        except Exception as e:
            self.collection = None
            print("⚠️ Knowledge base not found")
        
        # Show active engine
        if engine_type == "gemini" and self.use_gemini:
            print("πŸš€ Engine: Gemini (Cloud) - Fast, Accurate")
        elif engine_type == "local":
            print("πŸš€ Engine: Local LLM (Offline) - Free, Works without internet")
        elif engine_type == "hybrid":
            if self.use_gemini:
                print("πŸš€ Engine: Hybrid (Gemini + Local Fallback)")
            else:
                print("πŸš€ Engine: Hybrid (Local only - no API key)")
    
    def search(self, question: str, top_k: int = 3) -> List[Dict]:
        if not self.collection:
            return []
        
        q_embed = self.embedding_model.encode([question])
        results = self.collection.query(
            query_embeddings=q_embed.tolist(),
            n_results=top_k * 2
        )
        
        formatted = []
        if results['documents'] and results['documents'][0]:
            for i in range(len(results['documents'][0])):
                formatted.append({
                    "content": results['documents'][0][i],
                    "metadata": results['metadatas'][0][i],
                    "score": 1 - results['distances'][0][i] if results['distances'] else 0
                })
        
        return _optimizer.select_best_chunks(formatted, question)
    
    def ask(self, question: str, top_k: int = 3, force_refresh: bool = False) -> Dict:
        # 1. Check cache
        if not force_refresh:
            cached = _cache.get(question, top_k)
            if cached:
                return {
                    "answer": cached.get('answer', ''),
                    "sources": cached.get('sources', []),
                    "confidence": cached.get('confidence', 0),
                    "cached": True,
                    "model_used": cached.get('model_used', 'cache')
                }
        
        # 2. Search and optimize
        results = self.search(question, top_k)
        
        if not results:
            return {
                "answer": "No relevant information found.",
                "sources": [],
                "confidence": 0,
                "cached": False
            }
        
        # 3. Condense context
        context = _optimizer.condense_context(results, max_chars=2000)
        
        # 4. Get answer based on engine type
        answer, confidence, model_used = self._get_answer(question, context)
        
        # 5. Extract sources
        sources = []
        for r in results[:3]:
            sources.append({
                "source": r['metadata'].get('source', 'Unknown'),
                "type": r['metadata'].get('type', 'Unknown'),
                "score": round(r['score'], 3)
            })
        
        result = {
            "answer": answer,
            "sources": sources,
            "confidence": confidence,
            "cached": False,
            "model_used": model_used
        }
        
        _cache.set(question, result, top_k)
        return result
    
    def _get_answer(self, question: str, context: str) -> tuple:
        """Get answer based on engine type"""
        
        # HYBRID: Try Gemini first, fallback to local
        if self.engine_type == "hybrid" or self.engine_type == "gemini":
            if self.use_gemini:
                answer, confidence = self._ask_gemini(question, context)
                if "ERROR" not in answer and "Error" not in answer:
                    return answer, confidence, "gemini-3.5-flash"
        
        # LOCAL or HYBRID fallback
        return self._ask_local(question, context)
    
    def _ask_gemini(self, question: str, context: str) -> tuple:
        if not self.use_gemini or not self.api_key:
            return "ERROR: Gemini not available", 0.0
        
        prompt = f"""
You are a Track Design Engineer. Extract exact values from the context.

QUESTION: {question}

CONTEXT:
{context}

RULES:
1. Extract EXACT numbers with units
2. If formula exists, use it
3. Cite source: "From [source]: [value]"
4. If not found, say "NOT FOUND"

ANSWER:
"""
        
        try:
            response = self.client.chat.completions.create(
                model=self.model_name,
                messages=[
                    {"role": "system", "content": "You are a Track Design Engineer. Be concise and accurate."},
                    {"role": "user", "content": prompt}
                ],
                temperature=0.1,
                max_tokens=300
            )
            answer = response.choices[0].message.content
            confidence = 0.85
        except Exception as e:
            answer = f"ERROR: {e}"
            confidence = 0.0
        
        return answer, confidence
    
    def _ask_local(self, question: str, context: str) -> tuple:
        try:
            import ollama
            
            prompt = f"""
Q: {question}

Context: {context}

Answer with specific numbers. Cite sources.

A:"""
            
            response = ollama.chat(
                model="qwen2:1.5b",
                messages=[{"role": "user", "content": prompt}],
                options={
                    "num_predict": 250,
                    "temperature": 0.1,
                    "top_k": 20,
                }
            )
            answer = response['message']['content']
            confidence = 0.60
        except Exception as e:
            answer = f"ERROR: {e}"
            confidence = 0.0
        
        return answer, confidence, "qwen2:1.5b"
    
    def ask_with_sources(self, question: str, top_k: int = 3, force_refresh: bool = False) -> str:
        result = self.ask(question, top_k, force_refresh)
        
        output = f"""
{'='*60}
🎯 OPTIMIZED ENGINE
{'='*60}

πŸ“ {question}

πŸ’‘ {result['answer']}

πŸ“š Sources:
"""
        for s in result['sources']:
            output += f"   - {s['source']} ({s['type']}, {s['score']:.2%})\n"
        
        output += f"""
⚑ Model: {result.get('model_used', 'Unknown')}
{'='*60}
"""
        return output


_engine = None

def get_engine(engine_type: str = "hybrid") -> OptimizedEngine:
    global _engine
    if _engine is None:
        _engine = OptimizedEngine(engine_type=engine_type)
    return _engine


def ask(question: str, top_k: int = 3) -> str:
    engine = get_engine()
    return engine.ask_with_sources(question, top_k)


if __name__ == "__main__":
    print("=" * 60)
    print("  πŸš€ OPTIMIZED ENGINE")
    print("=" * 60)
    print("")
    print("  Select engine:")
    print("  1: Gemini (Cloud)")
    print("  2: Local (Offline)")
    print("  3: Hybrid (Gemini + Fallback)")
    choice = input("  Enter (1, 2, 3): ").strip()
    
    engine_map = {"1": "gemini", "2": "local", "3": "hybrid"}
    engine_type = engine_map.get(choice, "hybrid")
    
    engine = OptimizedEngine(engine_type=engine_type)
    
    while True:
        q = input("\nπŸ” Ask: ").strip()
        if q.lower() in ['exit', 'quit']:
            break
        if not q:
            continue
        print("\n⏳ Processing...")
        print(engine.ask_with_sources(q))