File size: 6,396 Bytes
6993919 | 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 | #!/usr/bin/env python3
"""
RMI AI Risk Explainer β Ollama Cloud Powered
=============================================
Takes raw scanner output β generates consumer-friendly risk explanations.
Used by Telegram bot, website, and scanner API.
Cost: ~100 tokens per explanation = ~$0.0007 on Ollama Cloud
"""
import json
import logging
import os
from urllib.request import Request, urlopen
logger = logging.getLogger("rmi.risk_explainer")
OLLAMA_KEY = os.getenv("OLLAMA_API_KEY", os.getenv("DEEPSEEK_API_KEY", ""))
OLLAMA_URL = "https://ollama.com/v1/chat/completions"
BACKEND_URL = os.getenv("BACKEND_URL", "http://localhost:8000")
MODEL = "deepseek-v4-flash"
SYSTEM_PROMPT = """You are RMI Risk Analyst. Given raw token scanner data, write a consumer-friendly risk explanation in 3-4 sentences.
Rules:
- Start with the safety score and risk level (SAFE/LOW/MEDIUM/HIGH/CRITICAL)
- Mention the 1-2 most important risk flags with plain-English explanations
- If there are green flags, mention the most reassuring one
- Be direct and honest β call out scams clearly
- Use Telegram HTML formatting: <b>bold</b> for key terms
- Never give financial advice. End with "Always DYOR."
Example output:
"<b>Safety: 23/100 β HIGH RISK</b>. This token has <b>unlocked liquidity</b>, meaning the deployer can drain funds anytime. The <b>deployer wallet has 6 prior rugs</b>. No redeeming factors found. Avoid this token. Always DYOR."
"""
def explain_risks(scan: dict) -> str:
"""Generate a human-readable risk explanation from scanner data."""
if not scan or scan.get("safety_score") is None:
return "<b>Unable to analyze</b> β no scanner data available."
score = scan.get("safety_score", 50)
flags = scan.get("risk_flags", [])
green = scan.get("green_flags", [])
name = scan.get("name", scan.get("symbol", "This token"))
modules = len(scan.get("modules_run", []))
# Build a concise prompt for the AI
prompt = f"""Token safety scan results:
- Token: {name}
- Safety score: {score}/100
- Risk flags: {", ".join(flags[:5]) if flags else "none"}
- Green flags: {", ".join(green[:3]) if green else "none"}
- Modules analyzed: {modules}
Write the explanation."""
try:
body = json.dumps(
{
"model": MODEL,
"messages": [
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": prompt},
],
"max_tokens": 150,
"temperature": 0.3,
}
).encode()
req = Request(
OLLAMA_URL,
data=body,
headers={
"Authorization": f"Bearer {OLLAMA_KEY}",
"Content-Type": "application/json",
},
)
resp = urlopen(req, timeout=15)
data = json.loads(resp.read())
return data["choices"][0]["message"]["content"].strip()
except Exception as e:
logger.error(f"Risk explainer failed: {e}")
# Fallback: basic explanation without AI
return _basic_explain(scan)
def _basic_explain(scan: dict) -> str:
"""Basic explanation when AI is unavailable."""
score = scan.get("safety_score", 50)
if score >= 80:
level = "SAFE"
elif score >= 60:
level = "LOW RISK"
elif score >= 40:
level = "MEDIUM RISK"
elif score >= 20:
level = "HIGH RISK"
else:
level = "CRITICAL"
flags = scan.get("risk_flags", [])
green = scan.get("green_flags", [])
scan.get("name", scan.get("symbol", "This token"))
msg = [f"<b>Safety: {score}/100 β {level}</b>"]
if flags:
msg.append(f"Risk flags: {', '.join(flags[:3])}")
if green:
msg.append(f"Green flags: {', '.join(green[:2])}")
msg.append("Always DYOR.")
return ". ".join(msg)
# ββ News Classification ββ
NEWS_SYSTEM = """Classify crypto news headlines into categories. Reply with ONLY the category name.
Categories:
- SCAM: rug pulls, hacks, exploits, phishing, fraud
- MARKET: price action, trading, volume, market cap, BTC/ETH moves
- REGULATION: government, SEC, legal, compliance, bans
- SECURITY: vulnerability, audit, patch, wallet security
- DEFI: DeFi protocols, yield, liquidity, lending
- MEMECOIN: meme tokens, celebrity coins, pump events
- GENERAL: anything else"""
def classify_news(title: str, content: str = "") -> str:
"""Classify a news article into a category."""
text = f"{title}\n{content[:200]}" if content else title
try:
body = json.dumps(
{
"model": MODEL,
"messages": [
{"role": "system", "content": NEWS_SYSTEM},
{"role": "user", "content": text},
],
"max_tokens": 10,
"temperature": 0.1,
}
).encode()
req = Request(
OLLAMA_URL,
data=body,
headers={
"Authorization": f"Bearer {OLLAMA_KEY}",
"Content-Type": "application/json",
},
)
resp = urlopen(req, timeout=10)
data = json.loads(resp.read())
category = data["choices"][0]["message"]["content"].strip().upper()
# Normalize
for cat in ["SCAM", "MARKET", "REGULATION", "SECURITY", "DEFI", "MEMECOIN", "GENERAL"]:
if cat in category:
return cat
return "GENERAL"
except Exception as e:
logger.warning(f"News classification failed: {e}")
# Basic keyword fallback
t = (title + " " + content).lower()
if any(w in t for w in ["hack", "exploit", "rug", "scam", "phish"]):
return "SCAM"
if any(w in t for w in ["price", "btc", "eth", "bull", "bear", "market"]):
return "MARKET"
if any(w in t for w in ["sec ", "regulation", "ban", "law", "legal"]):
return "REGULATION"
return "GENERAL"
if __name__ == "__main__":
# Test
test = {
"safety_score": 23,
"risk_flags": ["LP_LOCK_LOW", "DEV_HIGH_RISK", "HONEYPOT_DETECTED"],
"green_flags": [],
"name": "SCAMCOIN",
"modules_run": ["security", "holders", "liquidity"],
}
print(explain_risks(test))
print()
print(classify_news("$4M rug pull on Solana β deployer drained LP", ""))
|