amitbhatt6075 commited on
Commit
853482f
·
1 Parent(s): 43c61c9

Upgrade Trend Decoder prompt for better quality

Browse files
Files changed (1) hide show
  1. 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
- Takes a news headline or keyword and generates a strategic 'Agency Brief'.
87
- Used for the Trend Decoder feature.
88
  """
89
- print(f"🧠 [Thunderbird] Decoding Trend: {topic}")
90
 
91
- # Fallback default agar LLM nahi hai
92
- default_response = {
93
- "summary": f"'{topic}' is currently gaining significant traction online.",
94
- "impact": "Brands tapping into this can see increased organic reach.",
95
- "strategy": "Create content relating to this topic immediately using your brand voice."
96
- }
97
 
98
  if not llm_instance:
99
- return default_response
100
 
101
- # Prompt Strategy: Agency Expert Persona
102
- prompt = f"""[INST] You are a senior Digital Strategy Director.
103
- Analyze this trending topic: "{topic}".
 
 
 
104
 
105
- Provide a briefing for your marketing team in strictly JSON format with these 3 keys:
106
- 1. "summary": (What is happening? 1 sentence)
107
- 2. "impact": (Why should a Brand/Agency care? 1 sentence)
108
- 3. "strategy": (One specific actionable content idea or advice)
 
 
109
 
110
  JSON Response:
111
  [/INST]"""
112
 
113
  try:
114
- response = llm_instance(prompt, max_tokens=256, temperature=0.7, stop=["[INST]"], echo=False)
115
  text = response['choices'][0]['text'].strip()
116
 
117
- try:
118
- # Try finding the first { and last } to parse JSON
119
- start = text.find('{')
120
- end = text.rfind('}') + 1
121
- if start != -1 and end != 0:
122
- data = json.loads(text[start:end])
123
- return {
124
- "summary": data.get("summary", default_response["summary"]),
125
- "impact": data.get("impact", default_response["impact"]),
126
- "strategy": data.get("strategy", default_response["strategy"])
127
- }
128
- except:
129
- pass
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" AI Trend Decoding Failed: {e}")
136
- return default_response
 
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