import os import json import re import datetime import random import string import requests from io import BytesIO # CRITICAL FIX: To handle PDF in-memory extraction safely from typing import List, Optional from dotenv import load_dotenv import PyPDF2 from fastapi import FastAPI, HTTPException, UploadFile, File from fastapi.responses import HTMLResponse from pydantic import BaseModel from services.search import LiveWebSearcher from openai import OpenAI load_dotenv() app = FastAPI(title="Vertex Engine", version="4.0") searcher = LiveWebSearcher() class NvidiaClientManager: def __init__(self): self.base_url = "https://integrate.api.nvidia.com/v1" raw_keys = os.getenv("NVIDIA_API_KEYS", "") self.api_keys = [k.strip() for k in raw_keys.split(",") if k.strip()] def get_completion(self, model: str, messages: list, temperature: float = 0.2): last_exception = None for index, api_key in enumerate(self.api_keys): try: client = OpenAI(base_url=self.base_url, api_key=api_key) response = client.chat.completions.create( model=model, messages=messages, temperature=temperature, max_tokens=8192 ) content = response.choices[0].message.content # CRITICAL FIX: Handle the case where the API returns None instead of text if content is None: finish_reason = getattr(response.choices[0], 'finish_reason', 'unknown') return f"API ALERT: The model returned an empty response. Finish reason: {finish_reason}" return content except Exception as e: last_exception = e continue raise RuntimeError(f"All NVIDIA keys exhausted. Last error: {str(last_exception)}") nvidia_manager = NvidiaClientManager() class ChatMessage(BaseModel): role: str content: str class QueryRequest(BaseModel): query: str chat_history: Optional[List[ChatMessage]] = [] mode: str = "web" class CodeExecutionRequest(BaseModel): language: str code: str @app.get("/", response_class=HTMLResponse) def read_root(): template_path = os.path.join("templates", "index.html") if not os.path.exists(template_path): return "

Vertex Engine Running

Template index.html not found, but API is operational.

" with open(template_path, "r", encoding="utf-8") as f: return f.read() @app.post("/upload") async def upload_file(file: UploadFile = File(...)): try: content = "" file_bytes = await file.read() if file.filename and file.filename.endswith(".pdf"): pdf_file = BytesIO(file_bytes) pdf_reader = PyPDF2.PdfReader(pdf_file) for page in pdf_reader.pages: extracted_text = page.extract_text() if extracted_text: content += extracted_text + "\n" else: content = file_bytes.decode("utf-8", errors="ignore") if not content.strip(): raise HTTPException(status_code=400, detail="File is empty or unreadable.") searcher.ingest_personal_file(file.filename or "unknown_file.txt", content) return {"status": "success", "filename": file.filename, "message": "File indexed securely into local memory."} except HTTPException as he: raise he except Exception as e: raise HTTPException(status_code=500, detail=str(e)) @app.post("/execute") async def execute_code_sandbox(req: CodeExecutionRequest): """Securely executes code via the Piston Sandbox API.""" lang_map = { "py": ("python", "3.10.0"), "python": ("python", "3.10.0"), "js": ("javascript", "18.15.0"), "javascript": ("javascript", "18.15.0") } normalized_lang = req.language.lower() if normalized_lang not in lang_map: raise HTTPException(status_code=400, detail=f"Language '{req.language}' is not supported in the sandbox.") lang, version = lang_map[normalized_lang] try: response = requests.post( "https://emkc.org/api/v2/piston/execute", json={"language": lang, "version": version, "files": [{"content": req.code}]}, timeout=10 ) data = response.json() if "run" in data: return {"output": data["run"].get("output", "")} else: return {"output": f"Execution Error: {data.get('message', 'Unknown error')}"} except Exception as e: raise HTTPException(status_code=500, detail=f"Sandbox Connection Failed: {str(e)}") @app.post("/search") async def execute_search(request: QueryRequest): if not request.query: raise HTTPException(status_code=400, detail="Query cannot be empty") try: is_cached = True contexts = [] if request.mode == "personal": contexts = searcher.search_personal_files(request.query, limit=5) is_cached = False else: contexts = searcher.check_cache(request.query, limit=4) if not contexts: is_cached = False contexts = searcher.search_web(request.query, limit=4) if not contexts: fallback_msg = "No personal documents found matching that query." if request.mode == "personal" else "I could not reach the live internet or found no results for this query." return { "answer": fallback_msg, "confidence_score": 0.0, "accuracy_tier": "Index Empty", "sources": [], "widget": None, "engine_id": "CORE-ERROR" } valid_blocks = [] unique_sources = {} for c in contexts: if not isinstance(c, dict): continue snippet = str(c.get('snippet', '')) url = str(c.get('url', '')) title = str(c.get('title', 'Web Resource')) source_engine = str(c.get('source', 'Web')) if snippet and url: valid_blocks.append(f"[Title: {title}]\n[Source URL: {url}]: {snippet}") unique_sources[title + url] = {"title": title, "url": url, "snippet": snippet, "score": source_engine} context_text = "\n\n".join(valid_blocks) current_date = datetime.datetime.now().strftime("%B %d, %Y") query_lower = request.query.lower() # --- AUTO-ROUTER & OBFUSCATION --- if any(w in query_lower for w in ["code", "python", "javascript", "html", "script", "debug", "function"]): target_model = "google/gemma-4-31b-it" else: target_model = "nvidia/nemotron-3.5-super-vl-120b-a12b" random_hash = "".join(random.choices(string.ascii_uppercase + string.digits, k=6)) obfuscated_engine_id = f"CORE-{random_hash}" widget_instruction = "" if request.mode == "web" and any(k in query_lower for k in ["weather", "score", "cricket", "vs", "stock", "price", "ticker"]): widget_instruction = ( "\n\nEXTRA CRITICAL DIRECTIVE:\n" "You MUST append a structured JSON block at the absolute end of your response wrapped inside explicit tags:\n" "\n{\n" ' "type": "weather" | "sports" | "finance",\n' ' "title": "Main Subject Header",\n' ' "metric_primary": "Main reading",\n' ' "metric_secondary": "Sub-label status",\n' ' "data_points": {"Key1": "Val1", "Key2": "Val2"}\n' "}\n\n" ) system_prompt = ( "You are Vertex, an advanced real-time answer engine.\n" f"CRITICAL TEMPORAL CONTEXT: Today's exact date is {current_date}.\n\n" "CRITICAL FORMATTING INSTRUCTIONS:\n" "1. Synthesize a clear, direct, and authoritative answer based ONLY on the provided reference streams.\n" "2. You must explicitly cite facts using clean Markdown hyperlinks smoothly within sentences.\n" "3. If generating code, use standard markdown code blocks.\n" f"{widget_instruction}" ) messages_stack = [{"role": "system", "content": system_prompt}] if request.chat_history: for msg in request.chat_history[-6:]: messages_stack.append({"role": msg.role, "content": msg.content}) user_prompt = f"Reference Context Streams:\n{context_text}\n\nUser Search Query: {request.query}" messages_stack.append({"role": "user", "content": user_prompt}) raw_answer = nvidia_manager.get_completion( model=target_model, messages=messages_stack, temperature=0.1 ) # CRITICAL FIX: Guarantee raw_answer is a string to prevent regex crashes if not isinstance(raw_answer, str): raw_answer = str(raw_answer or "API ALERT: The model returned no data.") widget_payload = None clean_answer = raw_answer widget_match = re.search(r"(.*?)", raw_answer, re.DOTALL) if widget_match: try: widget_payload = json.loads(widget_match.group(1).strip()) clean_answer = re.sub(r".*?", "", raw_answer, flags=re.DOTALL).strip() except Exception: pass return { "answer": clean_answer, "confidence_score": 1.0, "accuracy_tier": "Personal Cloud Index" if request.mode == "personal" else ("Local Memory Cache" if is_cached else "Live Internet Data"), "sources": list(unique_sources.values()), "widget": widget_payload, "engine_id": obfuscated_engine_id } except Exception as e: print(f"CRITICAL LIVE SEARCH ERROR: {str(e)}") raise HTTPException(status_code=500, detail=str(e))