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 "
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" "