File size: 10,276 Bytes
0b3ae6d
 
 
 
 
 
 
543800c
0b3ae6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5c6ea0a
0b3ae6d
 
 
 
 
13ae5fa
0b3ae6d
5c6ea0a
 
 
 
 
 
 
 
 
0b3ae6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
235d1b1
 
0b3ae6d
 
 
 
 
 
 
235d1b1
 
543800c
 
0b3ae6d
543800c
 
 
0b3ae6d
235d1b1
0b3ae6d
 
 
 
235d1b1
0b3ae6d
543800c
 
0b3ae6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
235d1b1
0b3ae6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
235d1b1
 
 
 
 
 
0b3ae6d
 
 
 
 
 
 
 
 
 
0fb3383
0b3ae6d
0fb3383
0b3ae6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5c6ea0a
235d1b1
5c6ea0a
235d1b1
0b3ae6d
 
 
 
 
 
 
 
235d1b1
 
0b3ae6d
 
 
 
 
 
 
 
 
 
 
 
 
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
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 "<html><body><h3>Vertex Engine Running</h3><p>Template index.html not found, but API is operational.</p></body></html>"
    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"
                "<widget_data>\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</widget_data>\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"<widget_data>(.*?)</widget_data>", raw_answer, re.DOTALL)
        if widget_match:
            try:
                widget_payload = json.loads(widget_match.group(1).strip())
                clean_answer = re.sub(r"<widget_data>.*?</widget_data>", "", 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))