Spaces:
Sleeping
Sleeping
Commit ·
853482f
1
Parent(s): 43c61c9
Upgrade Trend Decoder prompt for better quality
Browse files- core/thunderbird_engine.py +39 -36
core/thunderbird_engine.py
CHANGED
|
@@ -81,56 +81,59 @@ def predict_niche_trends() -> dict:
|
|
| 81 |
|
| 82 |
return {"trend_predictions": predictions}
|
| 83 |
|
|
|
|
| 84 |
def decode_market_trend(topic: str, llm_instance) -> Dict[str, str]:
|
| 85 |
"""
|
| 86 |
-
|
| 87 |
-
Used for the Trend Decoder feature.
|
| 88 |
"""
|
| 89 |
-
print(f"🧠 [Thunderbird] Decoding Trend: {topic}")
|
| 90 |
|
| 91 |
-
#
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
|
| 98 |
if not llm_instance:
|
| 99 |
-
return
|
| 100 |
|
| 101 |
-
#
|
| 102 |
-
|
| 103 |
-
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
|
|
|
|
|
|
| 109 |
|
| 110 |
JSON Response:
|
| 111 |
[/INST]"""
|
| 112 |
|
| 113 |
try:
|
| 114 |
-
response = llm_instance(prompt, max_tokens=256, temperature=0.
|
| 115 |
text = response['choices'][0]['text'].strip()
|
| 116 |
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
# Fallback if JSON parsing fails but we have text - Return a standard structure
|
| 132 |
-
return default_response
|
| 133 |
|
| 134 |
except Exception as e:
|
| 135 |
-
print(f"
|
| 136 |
-
return
|
|
|
|
| 81 |
|
| 82 |
return {"trend_predictions": predictions}
|
| 83 |
|
| 84 |
+
|
| 85 |
def decode_market_trend(topic: str, llm_instance) -> Dict[str, str]:
|
| 86 |
"""
|
| 87 |
+
Decodes a keyword into a strategy. USES HYBRID AI + TEMPLATE LOGIC.
|
|
|
|
| 88 |
"""
|
| 89 |
+
print(f"🧠 [Thunderbird] Decoding Trend with Upgraded Prompt: {topic}")
|
| 90 |
|
| 91 |
+
# --- 1. Smart Fallback (Same as before) ---
|
| 92 |
+
base_template = FALLBACK_STRATEGIES["Default"]
|
| 93 |
+
topic_lower = topic.lower()
|
| 94 |
+
if "ai" in topic_lower: base_template = FALLBACK_STRATEGIES["AI"]
|
| 95 |
+
elif "tiktok" in topic_lower: base_template = FALLBACK_STRATEGIES["TikTok"]
|
| 96 |
+
elif "micro" in topic_lower: base_template = FALLBACK_STRATEGIES["Micro"]
|
| 97 |
|
| 98 |
if not llm_instance:
|
| 99 |
+
return base_template
|
| 100 |
|
| 101 |
+
# --- 2. THE UPGRADED PROMPT (This is the fix) ---
|
| 102 |
+
# We give it today's date and a better persona.
|
| 103 |
+
today_date = datetime.now().strftime("%Y-%m-%d")
|
| 104 |
+
prompt = f"""[INST]
|
| 105 |
+
You are "PulseAI", a Senior Digital Strategy Director providing a competitive edge.
|
| 106 |
+
Today's Date is {today_date}. All your analysis must be modern and relevant to today.
|
| 107 |
|
| 108 |
+
Analyze this trend: "{topic}".
|
| 109 |
+
|
| 110 |
+
Provide a concise, expert briefing in a valid JSON format with 3 keys:
|
| 111 |
+
1. "summary": A single, sharp sentence explaining what is happening now.
|
| 112 |
+
2. "impact": A single sentence explaining why this matters for a brand's revenue or reach.
|
| 113 |
+
3. "strategy": One creative, specific content idea an agency can execute this week. Example: "A 30s reel comparing old vs. new AI tools."
|
| 114 |
|
| 115 |
JSON Response:
|
| 116 |
[/INST]"""
|
| 117 |
|
| 118 |
try:
|
| 119 |
+
response = llm_instance(prompt, max_tokens=256, temperature=0.6, stop=["[INST]"], echo=False)
|
| 120 |
text = response['choices'][0]['text'].strip()
|
| 121 |
|
| 122 |
+
# Parse the JSON from the response text
|
| 123 |
+
start = text.find('{')
|
| 124 |
+
end = text.rfind('}') + 1
|
| 125 |
+
if start != -1 and end != 0:
|
| 126 |
+
data = json.loads(text[start:end])
|
| 127 |
+
# Ensure no key is empty, fall back to template if needed
|
| 128 |
+
return {
|
| 129 |
+
"summary": data.get("summary") or base_template["summary"],
|
| 130 |
+
"impact": data.get("impact") or base_template["impact"],
|
| 131 |
+
"strategy": data.get("strategy") or base_template["strategy"]
|
| 132 |
+
}
|
| 133 |
+
else:
|
| 134 |
+
# If JSON is broken, use the fallback
|
| 135 |
+
raise ValueError("LLM did not return valid JSON.")
|
|
|
|
|
|
|
| 136 |
|
| 137 |
except Exception as e:
|
| 138 |
+
print(f" - ⚠️ LLM Error or Bad JSON (Using Fallback): {e}")
|
| 139 |
+
return base_template
|