flowmarg / mcp /optimized_engine.py
akash
Deploy to Hugging Face (source code only, no binary files)
2379054
Raw History Blame Contribute Delete
9.21 kB
"""
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))