Spaces:
Paused
Paused
Update LLM.py
Browse files
LLM.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
# LLM.py (
|
| 2 |
import os, traceback, json, time, re
|
| 3 |
import httpx
|
| 4 |
from datetime import datetime
|
|
@@ -21,13 +21,11 @@ try:
|
|
| 21 |
except ImportError:
|
| 22 |
NewsFetcher = None
|
| 23 |
|
| 24 |
-
# 🔴 --- START OF CHANGE (V19.3) --- 🔴
|
| 25 |
# (استيراد VADER هنا أيضاً للـ type hinting)
|
| 26 |
try:
|
| 27 |
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
|
| 28 |
except ImportError:
|
| 29 |
SentimentIntensityAnalyzer = None
|
| 30 |
-
# 🔴 --- END OF CHANGE --- 🔴
|
| 31 |
|
| 32 |
|
| 33 |
# (تحديث الإعدادات الافتراضية لتطابق NVIDIA)
|
|
@@ -56,13 +54,13 @@ class LLMService:
|
|
| 56 |
api_key=LLM_API_KEY,
|
| 57 |
timeout=CLIENT_TIMEOUT
|
| 58 |
)
|
| 59 |
-
# 🔴 --- START OF CHANGE (V19.
|
| 60 |
-
print(f"✅ [LLMService V19.
|
| 61 |
# 🔴 --- END OF CHANGE --- 🔴
|
| 62 |
print(f" -> Endpoint: {LLM_API_URL}")
|
| 63 |
except Exception as e:
|
| 64 |
-
# 🔴 --- START OF CHANGE (V19.
|
| 65 |
-
print(f"❌ [LLMService V19.
|
| 66 |
# 🔴 --- END OF CHANGE --- 🔴
|
| 67 |
traceback.print_exc()
|
| 68 |
raise
|
|
@@ -71,11 +69,7 @@ class LLMService:
|
|
| 71 |
self.r2_service: Optional[R2Service] = None
|
| 72 |
self.learning_hub: Optional[LearningHubManager] = None
|
| 73 |
self.news_fetcher: Optional[NewsFetcher] = None
|
| 74 |
-
|
| 75 |
-
# 🔴 --- START OF CHANGE (V19.3) --- 🔴
|
| 76 |
-
# (إضافة متغير VADER)
|
| 77 |
self.vader_analyzer: Optional[SentimentIntensityAnalyzer] = None
|
| 78 |
-
# 🔴 --- END OF CHANGE --- 🔴
|
| 79 |
|
| 80 |
async def _call_llm(self, prompt: str) -> Optional[str]:
|
| 81 |
"""
|
|
@@ -247,14 +241,12 @@ class LLMService:
|
|
| 247 |
latest_news_text = "News data unavailable for re-analysis."
|
| 248 |
latest_news_score = 0.0
|
| 249 |
|
| 250 |
-
# 🔴 --- START OF CHANGE (V19.3) --- 🔴
|
| 251 |
# (استخدام self.vader_analyzer الذي تم حقنه)
|
| 252 |
if self.news_fetcher:
|
| 253 |
latest_news_text = await self.news_fetcher.get_news_for_symbol(symbol)
|
| 254 |
if self.vader_analyzer and latest_news_text: # (التحقق من المحلل المُمرر)
|
| 255 |
vader_scores = self.vader_analyzer.polarity_scores(latest_news_text)
|
| 256 |
latest_news_score = vader_scores.get('compound', 0.0)
|
| 257 |
-
# 🔴 --- END OF CHANGE --- 🔴
|
| 258 |
|
| 259 |
current_data['latest_news_text'] = latest_news_text
|
| 260 |
current_data['latest_news_score'] = latest_news_score
|
|
@@ -289,16 +281,15 @@ class LLMService:
|
|
| 289 |
candidate_data: Dict[str, Any],
|
| 290 |
learning_context: str) -> str:
|
| 291 |
"""
|
| 292 |
-
(معدل V19.
|
| 293 |
إنشاء الـ Prompt (باللغة الإنجليزية) لاتخاذ قرار التداول الأولي (Explorer).
|
| 294 |
-
(
|
| 295 |
"""
|
| 296 |
|
| 297 |
symbol = candidate_data.get('symbol', 'N/A')
|
| 298 |
|
| 299 |
# --- 1. استخراج بيانات ML (الطبقة 1) ---
|
| 300 |
-
l1_score
|
| 301 |
-
l1_reasons = candidate_data.get('reasons_for_candidacy', [])
|
| 302 |
pattern_data = candidate_data.get('pattern_analysis', {})
|
| 303 |
mc_data = candidate_data.get('monte_carlo_distribution', {})
|
| 304 |
|
|
@@ -336,12 +327,16 @@ class LLMService:
|
|
| 336 |
{learning_context}
|
| 337 |
--- END OF PLAYBOOK ---
|
| 338 |
"""
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
|
|
|
|
|
|
| 342 |
* Chart Pattern: {pattern_data.get('pattern_detected', 'None')} (Conf: {pattern_data.get('pattern_confidence', 0):.2f})
|
| 343 |
* Monte Carlo (1h): {mc_data.get('probability_of_gain', 0) * 100:.1f}% chance of profit (Expected: {mc_data.get('expected_return_pct', 0):.2f}%)
|
| 344 |
"""
|
|
|
|
|
|
|
| 345 |
news_prompt = f"""
|
| 346 |
2. **News & Sentiment Analysis:**
|
| 347 |
* Market Trend: {market_trend} (BTC: {btc_sentiment})
|
|
@@ -362,24 +357,26 @@ class LLMService:
|
|
| 362 |
* Relative 24h Net Flow (vs Daily Volume): {relative_net_flow_24h_percent:+.2f}%
|
| 363 |
"""
|
| 364 |
|
| 365 |
-
#
|
|
|
|
| 366 |
task_prompt = f"""
|
| 367 |
CONTEXT:
|
| 368 |
You are an expert AI trading analyst (Explorer Brain).
|
| 369 |
-
Analyze the
|
|
|
|
| 370 |
{playbook_prompt}
|
| 371 |
|
| 372 |
--- START OF CANDIDATE DATA ---
|
| 373 |
-
{
|
| 374 |
{news_prompt}
|
| 375 |
{whale_activity_prompt}
|
| 376 |
--- END OF CANDIDATE DATA ---
|
| 377 |
|
| 378 |
TASK:
|
| 379 |
1. **Internal Thinking (Private):** Perform a step-by-step analysis (as triggered by the system prompt).
|
| 380 |
-
* Synthesize all data points (
|
| 381 |
-
* Are the signals aligned? (e.g.,
|
| 382 |
-
* Are there conflicts? (e.g.,
|
| 383 |
* Consult the "Playbook" for learned rules.
|
| 384 |
2. **Final Decision:** Based on your internal thinking, decide the final action.
|
| 385 |
3. **Output Constraint:** Provide your final answer ONLY in the requested JSON object format, with no introductory text, markdown formatting, or explanations.
|
|
@@ -393,6 +390,7 @@ OUTPUT (JSON Object ONLY):
|
|
| 393 |
"exit_profile": "Aggressive" or "Standard" or "Patient"
|
| 394 |
}}
|
| 395 |
"""
|
|
|
|
| 396 |
|
| 397 |
# (نرسل فقط task_prompt لأنه يحتوي الآن على كل شيء)
|
| 398 |
return task_prompt
|
|
|
|
| 1 |
+
# LLM.py (V19.5 - Remove Bias Scores from Prompt)
|
| 2 |
import os, traceback, json, time, re
|
| 3 |
import httpx
|
| 4 |
from datetime import datetime
|
|
|
|
| 21 |
except ImportError:
|
| 22 |
NewsFetcher = None
|
| 23 |
|
|
|
|
| 24 |
# (استيراد VADER هنا أيضاً للـ type hinting)
|
| 25 |
try:
|
| 26 |
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
|
| 27 |
except ImportError:
|
| 28 |
SentimentIntensityAnalyzer = None
|
|
|
|
| 29 |
|
| 30 |
|
| 31 |
# (تحديث الإعدادات الافتراضية لتطابق NVIDIA)
|
|
|
|
| 54 |
api_key=LLM_API_KEY,
|
| 55 |
timeout=CLIENT_TIMEOUT
|
| 56 |
)
|
| 57 |
+
# 🔴 --- START OF CHANGE (V19.5) --- 🔴
|
| 58 |
+
print(f"✅ [LLMService V19.5] مهيأ. النموذج: {LLM_MODEL}")
|
| 59 |
# 🔴 --- END OF CHANGE --- 🔴
|
| 60 |
print(f" -> Endpoint: {LLM_API_URL}")
|
| 61 |
except Exception as e:
|
| 62 |
+
# 🔴 --- START OF CHANGE (V19.5) --- 🔴
|
| 63 |
+
print(f"❌ [LLMService V19.5] فشل تهيئة AsyncOpenAI: {e}")
|
| 64 |
# 🔴 --- END OF CHANGE --- 🔴
|
| 65 |
traceback.print_exc()
|
| 66 |
raise
|
|
|
|
| 69 |
self.r2_service: Optional[R2Service] = None
|
| 70 |
self.learning_hub: Optional[LearningHubManager] = None
|
| 71 |
self.news_fetcher: Optional[NewsFetcher] = None
|
|
|
|
|
|
|
|
|
|
| 72 |
self.vader_analyzer: Optional[SentimentIntensityAnalyzer] = None
|
|
|
|
| 73 |
|
| 74 |
async def _call_llm(self, prompt: str) -> Optional[str]:
|
| 75 |
"""
|
|
|
|
| 241 |
latest_news_text = "News data unavailable for re-analysis."
|
| 242 |
latest_news_score = 0.0
|
| 243 |
|
|
|
|
| 244 |
# (استخدام self.vader_analyzer الذي تم حقنه)
|
| 245 |
if self.news_fetcher:
|
| 246 |
latest_news_text = await self.news_fetcher.get_news_for_symbol(symbol)
|
| 247 |
if self.vader_analyzer and latest_news_text: # (التحقق من المحلل المُمرر)
|
| 248 |
vader_scores = self.vader_analyzer.polarity_scores(latest_news_text)
|
| 249 |
latest_news_score = vader_scores.get('compound', 0.0)
|
|
|
|
| 250 |
|
| 251 |
current_data['latest_news_text'] = latest_news_text
|
| 252 |
current_data['latest_news_score'] = latest_news_score
|
|
|
|
| 281 |
candidate_data: Dict[str, Any],
|
| 282 |
learning_context: str) -> str:
|
| 283 |
"""
|
| 284 |
+
(معدل V19.5)
|
| 285 |
إنشاء الـ Prompt (باللغة الإنجليزية) لاتخاذ قرار التداول الأولي (Explorer).
|
| 286 |
+
(تمت إزالة الدرجات المسبقة لتقليل الانحياز)
|
| 287 |
"""
|
| 288 |
|
| 289 |
symbol = candidate_data.get('symbol', 'N/A')
|
| 290 |
|
| 291 |
# --- 1. استخراج بيانات ML (الطبقة 1) ---
|
| 292 |
+
# (تمت إزالة l1_score و l1_reasons عمداً)
|
|
|
|
| 293 |
pattern_data = candidate_data.get('pattern_analysis', {})
|
| 294 |
mc_data = candidate_data.get('monte_carlo_distribution', {})
|
| 295 |
|
|
|
|
| 327 |
{learning_context}
|
| 328 |
--- END OF PLAYBOOK ---
|
| 329 |
"""
|
| 330 |
+
|
| 331 |
+
# 🔴 --- START OF CHANGE (V19.5) --- 🔴
|
| 332 |
+
# (تمت إزالة درجة l1_score و l1_reasons من هنا)
|
| 333 |
+
technical_summary_prompt = f"""
|
| 334 |
+
1. **Technical Analysis:**
|
| 335 |
* Chart Pattern: {pattern_data.get('pattern_detected', 'None')} (Conf: {pattern_data.get('pattern_confidence', 0):.2f})
|
| 336 |
* Monte Carlo (1h): {mc_data.get('probability_of_gain', 0) * 100:.1f}% chance of profit (Expected: {mc_data.get('expected_return_pct', 0):.2f}%)
|
| 337 |
"""
|
| 338 |
+
# 🔴 --- END OF CHANGE --- 🔴
|
| 339 |
+
|
| 340 |
news_prompt = f"""
|
| 341 |
2. **News & Sentiment Analysis:**
|
| 342 |
* Market Trend: {market_trend} (BTC: {btc_sentiment})
|
|
|
|
| 357 |
* Relative 24h Net Flow (vs Daily Volume): {relative_net_flow_24h_percent:+.2f}%
|
| 358 |
"""
|
| 359 |
|
| 360 |
+
# 🔴 --- START OF CHANGE (V19.5) --- 🔴
|
| 361 |
+
# (تم تحديث التعليمات ليعكس تحليل البيانات "الخام" بدلاً من الدرجات)
|
| 362 |
task_prompt = f"""
|
| 363 |
CONTEXT:
|
| 364 |
You are an expert AI trading analyst (Explorer Brain).
|
| 365 |
+
Analyze the following raw technical, news, and whale data for {symbol}. You must make a decision based *only* on the data provided, without any pre-calculated scores.
|
| 366 |
+
Decide if this combination of signals presents a high-potential opportunity to 'WATCH'.
|
| 367 |
{playbook_prompt}
|
| 368 |
|
| 369 |
--- START OF CANDIDATE DATA ---
|
| 370 |
+
{technical_summary_prompt}
|
| 371 |
{news_prompt}
|
| 372 |
{whale_activity_prompt}
|
| 373 |
--- END OF CANDIDATE DATA ---
|
| 374 |
|
| 375 |
TASK:
|
| 376 |
1. **Internal Thinking (Private):** Perform a step-by-step analysis (as triggered by the system prompt).
|
| 377 |
+
* Synthesize all data points (Chart Pattern, Monte Carlo, News, Whale Flow, 24h Accumulation).
|
| 378 |
+
* Are the signals aligned? (e.g., Bullish Pattern + High MC Probability + Whale Accumulation = Strong).
|
| 379 |
+
* Are there conflicts? (e.g., Bullish Pattern but high 24h Deposits = Risky).
|
| 380 |
* Consult the "Playbook" for learned rules.
|
| 381 |
2. **Final Decision:** Based on your internal thinking, decide the final action.
|
| 382 |
3. **Output Constraint:** Provide your final answer ONLY in the requested JSON object format, with no introductory text, markdown formatting, or explanations.
|
|
|
|
| 390 |
"exit_profile": "Aggressive" or "Standard" or "Patient"
|
| 391 |
}}
|
| 392 |
"""
|
| 393 |
+
# 🔴 --- END OF CHANGE --- 🔴
|
| 394 |
|
| 395 |
# (نرسل فقط task_prompt لأنه يحتوي الآن على كل شيء)
|
| 396 |
return task_prompt
|