chore: pre-migration snapshot
Layer0, MCP servers, Terraform consolidation
This commit is contained in:
@@ -9,7 +9,11 @@ Separate from CLI/API wrapper for clean testability.
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
import os
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
from dataclasses import dataclass
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
@@ -92,12 +96,10 @@ class OracleAnswerTool:
|
||||
if self.use_local_only:
|
||||
return "Local-only mode: skipping NVIDIA API call"
|
||||
|
||||
if not httpx:
|
||||
raise ImportError("httpx not installed. Install with: pip install httpx")
|
||||
|
||||
headers = {
|
||||
"Authorization": f"Bearer {self.api_key}",
|
||||
"Accept": "application/json",
|
||||
"Content-Type": "application/json",
|
||||
}
|
||||
|
||||
payload = {
|
||||
@@ -108,18 +110,45 @@ class OracleAnswerTool:
|
||||
"max_tokens": 1024,
|
||||
}
|
||||
|
||||
try:
|
||||
async with httpx.AsyncClient() as client:
|
||||
response = await client.post(
|
||||
f"{self.NVIDIA_API_BASE}/chat/completions",
|
||||
json=payload,
|
||||
headers=headers,
|
||||
timeout=30.0,
|
||||
)
|
||||
response.raise_for_status()
|
||||
data = response.json()
|
||||
# Prefer httpx when available; otherwise fall back to stdlib urllib to avoid extra deps.
|
||||
if httpx:
|
||||
try:
|
||||
async with httpx.AsyncClient() as client:
|
||||
response = await client.post(
|
||||
f"{self.NVIDIA_API_BASE}/chat/completions",
|
||||
json=payload,
|
||||
headers=headers,
|
||||
timeout=30.0,
|
||||
)
|
||||
response.raise_for_status()
|
||||
data = response.json()
|
||||
return data["choices"][0]["message"]["content"]
|
||||
except Exception as e: # noqa: BLE001
|
||||
return f"(API Error: {str(e)}) Falling back to local analysis..."
|
||||
|
||||
def _urllib_post() -> str:
|
||||
req = urllib.request.Request(
|
||||
url=f"{self.NVIDIA_API_BASE}/chat/completions",
|
||||
method="POST",
|
||||
headers=headers,
|
||||
data=json.dumps(payload, ensure_ascii=False).encode("utf-8"),
|
||||
)
|
||||
try:
|
||||
with urllib.request.urlopen(req, timeout=30) as resp:
|
||||
raw = resp.read().decode("utf-8", "replace")
|
||||
data = json.loads(raw)
|
||||
return data["choices"][0]["message"]["content"]
|
||||
except Exception as e:
|
||||
except urllib.error.HTTPError as e:
|
||||
detail = ""
|
||||
try:
|
||||
detail = e.read().decode("utf-8", "replace")
|
||||
except Exception:
|
||||
detail = str(e)
|
||||
raise RuntimeError(f"HTTP {e.code}: {detail}") from e
|
||||
|
||||
try:
|
||||
return await asyncio.to_thread(_urllib_post)
|
||||
except Exception as e: # noqa: BLE001
|
||||
return f"(API Error: {str(e)}) Falling back to local analysis..."
|
||||
|
||||
async def answer(
|
||||
|
||||
Reference in New Issue
Block a user