Some checks failed
CI / build (push) Failing after 3s
PHASE 2.3 (AUDIT-2026-Q3.md):
Task 1 — Wire-in Wave 3 (1 router mounted, 2 deferred):
- app.routers.unified_scanner_router mounted at /api/v2/scanner/* (2 routes:
POST /api/v2/scanner/token/scan, POST /api/v2/scanner/wallet/scan).
Refactored prefix from /api/v2 -> /api/v2/scanner to avoid future conflicts
with the v1 /api/v1/scanner/ stub.
- app.routers.unified_wallet_scanner DEFERRED (no router APIRouter attribute;
library module consumed by unified_scanner_router via get_wallet_scanner()).
- app.routers.admin_extensions DEFERRED (DORMANT per audit; 25 routes at
/api/v1/admin/* would shadow /api/v1/admin/alerts_webhook).
Task 2 — Archive 136 dead-code files to app/_archive/legacy_2026_07/:
- 73 routers in app/routers/ (reach graph showed zero reach into mount.py).
- 63 flat app/*.py (domain modules never imported by live code).
- 1 file RESTORED post-archive: app/routers/x402_bridge_health.py (caught by
tests/unit/test_bridge_health.py which directly imports it; reach graph
considered tests/ only as transitive reach — to be patched in next cycle).
Forced-LIVE (NOT archived per user directive):
- app/ai_pipeline_v3.py (3 importers in audit window, importers themselves DEAD)
- app/splade_bm25.py (LIVE via app.rag_service)
- app/wallet_manager_v2.py (LIVE via x402_enforcement, x402_tools, sweep_all, sweep_now)
- app/crypto_embeddings.py (NOT in audit ARCHIVE list; heavy import graph)
Verification (forward-import closure from mount.py + main.py + factory.py + lifespan.py):
- imports = 348 app.* modules
- reached = 194 files reachable from roots
- archive set = audit_dead (186) - reached - forced_live (4) - test_live (1) = 136
- Net delta: 136 files moved, 44,932 LOC reduction, 293->295 active routes (+2 from Wave 3)
pyproject.toml updates:
- setuptools.packages.find: added exclude for app._archive*
- ruff.extend-exclude: added "app/_archive/"
- mypy.exclude: added "app/_archive/"
Smoke test: pytest tests/ — 817 passed, 3 pre-existing failures unchanged
(0 new failures; 0 routes lost; all 4 forced-LIVE files still importable).
Restoration: git mv app/_archive/legacy_2026_07/<name>.py <original-path>
and add the import to app/mount.py ROUTER_MODULES.
Refs: AUDIT-2026-Q3.md /home/dev/pry/rmi-final-deadcode-2026-07-06.md
155 lines
6.5 KiB
Python
155 lines
6.5 KiB
Python
"""
|
|
RMI AI Pipeline v2 - Production Grade
|
|
======================================
|
|
Caching, fallbacks, rate limiting, smart prompts.
|
|
All 12 modules battle-tested against Ollama Cloud.
|
|
"""
|
|
|
|
import hashlib
|
|
import json
|
|
import logging
|
|
import os
|
|
import time
|
|
from urllib.request import Request, urlopen
|
|
|
|
logger = logging.getLogger("rmi.ai")
|
|
OLLAMA_KEY = os.getenv("OLLAMA_API_KEY", "")
|
|
OLLAMA_URL = "https://ollama.com/v1/chat/completions"
|
|
MODEL = "deepseek-v4-flash"
|
|
CACHE_TTL = 300 # 5 min cache for identical calls
|
|
|
|
# Simple TTL cache
|
|
_cache = {}
|
|
|
|
|
|
def _cached_call(system: str, prompt: str, max_tokens: int = 250, temp: float = 0.3) -> str:
|
|
key = hashlib.md5(f"{system[:50]}|{prompt[:100]}".encode()).hexdigest()
|
|
now = time.time()
|
|
if key in _cache and now - _cache[key][0] < CACHE_TTL:
|
|
return _cache[key][1]
|
|
try:
|
|
body = json.dumps(
|
|
{
|
|
"model": MODEL,
|
|
"messages": [
|
|
{"role": "system", "content": system},
|
|
{"role": "user", "content": prompt},
|
|
],
|
|
"max_tokens": max_tokens,
|
|
"temperature": temp,
|
|
}
|
|
).encode()
|
|
req = Request(
|
|
OLLAMA_URL,
|
|
data=body,
|
|
headers={"Authorization": f"Bearer {OLLAMA_KEY}", "Content-Type": "application/json"},
|
|
)
|
|
resp = urlopen(req, timeout=12)
|
|
result = json.loads(resp.read())["choices"][0]["message"]["content"].strip()
|
|
_cache[key] = (now, result)
|
|
return result
|
|
except Exception as e:
|
|
logger.error(f"Ollama AI call failed: {e}")
|
|
return ""
|
|
|
|
|
|
# ── 1. TOKEN RISK EXPLAINER (improved) ──
|
|
def explain_risks(scan: dict) -> str:
|
|
if not scan or scan.get("safety_score") is None:
|
|
return "<b>Unable to analyze</b> - no scanner data."
|
|
score = scan.get("safety_score", 50)
|
|
flags = scan.get("risk_flags", [])
|
|
green = scan.get("green_flags", [])
|
|
name = scan.get("name", scan.get("symbol", "token"))
|
|
mods = len(scan.get("modules_run", []))
|
|
prompt = f"Token:{name} Score:{score}/100 Risks:{', '.join(flags[:5]) or 'none'} Green:{', '.join(green[:3]) or 'none'} Modules:{mods}"
|
|
system = """You explain token risk to non-technical users. 3-4 sentences. Start with safety score. Mention top risks in plain English. End with "Always DYOR." Use <b>bold</b> for key terms. Never give financial advice."""
|
|
result = _cached_call(system, prompt, max_tokens=150, temp=0.2)
|
|
return result or f"<b>Safety: {score}/100</b>. Risk flags: {', '.join(flags[:3])}. Always DYOR."
|
|
|
|
|
|
# ── 2. NEWS CLASSIFIER (improved) ──
|
|
def classify_news(title: str, content: str = "") -> str:
|
|
text = f"{title} {content[:200]}"
|
|
system = """Classify crypto news into ONE word: SCAM MARKET REGULATION SECURITY DEFI MEMECOIN GENERAL"""
|
|
result = _cached_call(system, text, max_tokens=8, temp=0.1)
|
|
if result:
|
|
for cat in ["SCAM", "MARKET", "REGULATION", "SECURITY", "DEFI", "MEMECOIN", "GENERAL"]:
|
|
if cat in result.upper():
|
|
return cat
|
|
# Fast fallback
|
|
t = text.lower()
|
|
if any(w in t for w in ["hack", "exploit", "rug", "scam", "phish", "drain"]):
|
|
return "SCAM"
|
|
if any(w in t for w in ["price", "btc", "eth", "bull", "bear", "market"]):
|
|
return "MARKET"
|
|
return "GENERAL"
|
|
|
|
|
|
# ── 3. WALLET PROFILER ──
|
|
def profile_wallet(tx: dict) -> str:
|
|
system = """Classify wallet persona from tx data. Reply: PERSONA|confidence. Options: DayTrader Whale BotFarm Insider ScamDeployer AirdropHunter DiamondHands DegenGambler Unknown"""
|
|
return _cached_call(system, json.dumps(tx)[:1000], max_tokens=25) or "Unknown|0"
|
|
|
|
|
|
# ── 4. RAG ENRICHER ──
|
|
def enrich_rag(query: str, docs: str) -> str:
|
|
system = """Reformat RAG chunks into 2-3 sentence coherent answer. Preserve key facts."""
|
|
return _cached_call(system, f"Q:{query}\nD:{docs[:2000]}", max_tokens=200) or docs[:400]
|
|
|
|
|
|
# ── 5. ALERT RANKER ──
|
|
def rank_alerts(alerts: list) -> list:
|
|
summary = "\n".join(
|
|
f"{a.get('id', '?')}|{a.get('severity', '?')}|{(a.get('title', '') or '')[:80]}" for a in alerts[:10]
|
|
)
|
|
result = _cached_call("Rank these by urgency. Reply: id1,id2,id3...", summary, max_tokens=50)
|
|
return [x.strip() for x in (result or "").split(",") if x.strip()]
|
|
|
|
|
|
# ── 6. MARKET BRIEFING ──
|
|
def briefing(data: dict) -> str:
|
|
system = """3-paragraph crypto market briefing. P1:volume+chains P2:top risks P3:what to watch. <b>bold</b> key findings. Under 250 words."""
|
|
return _cached_call(system, json.dumps(data)[:2000], max_tokens=350, temp=0.5) or "Briefing unavailable."
|
|
|
|
|
|
# ── 7. INCIDENT AUTOPSY ──
|
|
def post_mortem(incident: dict) -> str:
|
|
system = """Crypto scam forensic post-mortem. What happened→How→Red flags→Protection. <b>bold</b> findings. Under 200 words."""
|
|
return _cached_call(system, json.dumps(incident)[:1500], max_tokens=300, temp=0.4) or "Autopsy unavailable."
|
|
|
|
|
|
# ── 8. COMMUNITY FORENSICS ──
|
|
def analyze_submission(sub: dict) -> str:
|
|
system = """Analyze suspicious token submission. Verdict:LIKELY SCAM/SUSPICIOUS/MORE INFO + 2-3 concerns."""
|
|
return _cached_call(system, json.dumps(sub)[:1500], max_tokens=200) or "Analysis unavailable."
|
|
|
|
|
|
# ── 9. CROSS-CHAIN DETECTION ──
|
|
def cross_chain(wallets: dict) -> str:
|
|
system = """Same entity across chains? Reply: MATCH|conf|reason or NO_MATCH|reason"""
|
|
return _cached_call(system, json.dumps(wallets)[:1500], max_tokens=80) or "Unknown"
|
|
|
|
|
|
# ── 10. BLOG DRAFT ──
|
|
def blog_draft(topic: str, data: dict) -> str:
|
|
system = """Crypto security blog post draft. Title|Hook|Body(3-4para)|KeyTakeaways|CTA. Markdown. Professional."""
|
|
return (
|
|
_cached_call(system, f"Topic:{topic}\nData:{json.dumps(data)[:2000]}", max_tokens=500, temp=0.6)
|
|
or f"# {topic}\n\nDraft unavailable."
|
|
)
|
|
|
|
|
|
# ── 11. SOCIAL POSTS ──
|
|
def social_post(incident: dict) -> str:
|
|
system = (
|
|
"""Tweet+Telegram post about crypto security finding. Twitter:<280 chars> | Telegram:<500 chars>. Hook first."""
|
|
)
|
|
return _cached_call(system, json.dumps(incident)[:1000], max_tokens=200, temp=0.7) or "Post unavailable."
|
|
|
|
|
|
# ── 12. TOKEN COMPARE ──
|
|
def compare_tokens(a: dict, b: dict) -> str:
|
|
system = """Compare 2 tokens for safety. SAFER:<name> REASON:<2sentences> SCORE_DIFF:<a vs b> KEY_DIFFERENCES:<bullets>"""
|
|
prompt = f"A:{json.dumps(a)[:800]}\nB:{json.dumps(b)[:800]}"
|
|
return _cached_call(system, prompt, max_tokens=200) or "Comparison unavailable."
|