rmi-backend/app/_archive/legacy_2026_07/ai_pipeline.py
cryptorugmunch 628c1d2a10
Some checks failed
CI / build (push) Failing after 3s
refactor(rmi-backend,audit): mount Wave 3 + archive 136 dead-code files (P2.3)
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
2026-07-06 20:52:31 +02:00

113 lines
4.3 KiB
Python

#!/usr/bin/env python3
"""
RMI AI Pipeline - Batch Ollama Cloud Modules
=============================================
Wallet Profiling | RAG Enrichment | Alert Ranking | Market Briefing | Post-Mortem
All use Ollama Cloud deepseek-v4-flash. ~$0.001 per operation.
"""
import json
import logging
import os
from urllib.request import Request, urlopen
logger = logging.getLogger("rmi.ai_pipeline")
OLLAMA_KEY = os.getenv("OLLAMA_API_KEY", os.getenv("DEEPSEEK_API_KEY", ""))
OLLAMA_URL = "https://ollama.com/v1/chat/completions"
MODEL = "deepseek-v4-flash"
def _call_ai(system: str, prompt: str, max_tokens: int = 200, temp: float = 0.3) -> str:
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=15)
return json.loads(resp.read())["choices"][0]["message"]["content"].strip()
except Exception as e:
logger.error(f"AI call failed: {e}")
return ""
# ── 7. WALLET BEHAVIORAL PROFILING ──
WALLET_SYSTEM = """Classify a crypto wallet into a persona based on transaction patterns.
Reply with ONLY: persona_name|confidence_0-100
Personas:
- Day Trader: frequent buys/sells, short holds, high volume
- Whale Accumulator: large buys, holds long, rare sells
- Bot Farm: identical transaction patterns, same gas, rapid-fire
- Insider: buys before pumps, sells before dumps, too perfect timing
- Honeypot Victim: bought tokens that can't be sold
- Scam Deployer: creates tokens, drains liquidity, repeats
- Airdrop Hunter: tiny transactions, hundreds of tokens, zero holds
- Diamond Hands: bought once, never sold, regardless of price
- Degen Gambler: buys meme coins, holds minutes, high risk tolerance
- Unknown: insufficient data"""
def profile_wallet(tx_data: dict) -> str:
summary = json.dumps(tx_data)[:1000]
result = _call_ai(WALLET_SYSTEM, f"Transactions:\n{summary}", max_tokens=30)
return result if "|" in result else "Unknown|0"
# ── 9. RAG QUERY ENRICHMENT ──
RAG_SYSTEM = """You reformat raw RAG search results into a coherent, readable answer.
Keep it under 150 words. Preserve key facts. Add a 1-line summary at the end."""
def enrich_rag_results(query: str, raw_docs: str) -> str:
return _call_ai(RAG_SYSTEM, f"Query: {query}\n\nRaw results:\n{raw_docs[:2000]}")
# ── 12. ALERT PRIORITIZATION ──
ALERT_SYSTEM = """Rank these crypto security alerts by urgency. Reply ONLY with the alert IDs in priority order, comma-separated.
Priority rules: CRITICAL (immediate rug/hack) > HIGH (likely scam) > MEDIUM (suspicious) > LOW (noise)."""
def rank_alerts(alerts: list) -> list:
summary = "\n".join(
f"ID:{a.get('id', '?')} | {a.get('severity', '?')} | {a.get('title', '?')[:100]}" for a in alerts[:20]
)
result = _call_ai(ALERT_SYSTEM, summary, max_tokens=50)
return [x.strip() for x in result.split(",") if x.strip()]
# ── 6. DAILY MARKET BRIEFING ──
MARKET_SYSTEM = """Write a 3-paragraph daily crypto market briefing from scanner data.
Para 1: Market overview (most scanned chains, scan volume)
Para 2: Top risks (worst tokens found today, emerging patterns)
Para 3: What to watch (trending scam types, new threat vectors)
Use Telegram HTML formatting. Keep it under 250 words. Professional but direct tone."""
def generate_market_briefing(scan_summary: dict) -> str:
return _call_ai(MARKET_SYSTEM, json.dumps(scan_summary)[:2000], max_tokens=350, temp=0.5)
# ── 15. INCIDENT POST-MORTEM ──
AUTOPSY_SYSTEM = """Write a forensic post-mortem of a crypto scam incident.
Structure:
1. What happened (1 sentence)
2. How it worked (the mechanics, 2-3 sentences)
3. Red flags that were visible beforehand
4. How to protect against similar scams
Keep it under 200 words. Use <b>bold</b> for key findings. Professional forensic tone."""
def write_post_mortem(incident: dict) -> str:
return _call_ai(AUTOPSY_SYSTEM, json.dumps(incident)[:1500], max_tokens=300, temp=0.4)