-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_campaign_flow.py
475 lines (401 loc) · 17.8 KB
/
run_campaign_flow.py
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
import os
import sys
import asyncio
import argparse
import json
import re
from typing import List, Dict, Optional, Any
from dataclasses import dataclass
from datetime import datetime
# Add the project root to Python path
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
# Custom exceptions
class CampaignFlowError(Exception):
"""Base exception for campaign flow errors"""
def __init__(self, message: str, step: str, details: Optional[Dict] = None):
self.message = message
self.step = step
self.details = details or {}
super().__init__(self.message)
class ValidationError(CampaignFlowError):
"""Raised when data validation fails"""
pass
class ParsingError(CampaignFlowError):
"""Raised when parsing fails"""
pass
@dataclass
class FlowProgress:
"""Track progress of the campaign flow"""
step: str
progress: float
start_time: datetime
details: Dict[str, Any]
@property
def duration(self) -> float:
return (datetime.utcnow() - self.start_time).total_seconds()
class ProgressTracker:
"""Track progress across multiple steps"""
def __init__(self):
self.steps = {}
self.current_step = None
def start_step(self, step: str, details: Dict[str, Any] = None):
self.current_step = step
self.steps[step] = FlowProgress(
step=step,
progress=0.0,
start_time=datetime.utcnow(),
details=details or {}
)
def update_progress(self, progress: float, details: Dict[str, Any] = None):
if self.current_step and self.current_step in self.steps:
self.steps[self.current_step].progress = progress
if details:
self.steps[self.current_step].details.update(details)
def get_current_progress(self) -> Optional[FlowProgress]:
return self.steps.get(self.current_step)
def validate_research_data(data: Dict) -> bool:
"""Validate research data structure and content"""
required_fields = ['company_summary', 'market_analysis', 'competitor_analysis']
if not all(field in data for field in required_fields):
missing = [f for f in required_fields if f not in data]
raise ValidationError(
f"Missing required research fields: {', '.join(missing)}",
'research',
{'missing_fields': missing}
)
# Validate content quality
for field in required_fields:
if not data[field] or len(data[field].strip()) < 50:
raise ValidationError(
f"Insufficient content in {field}",
'research',
{'field': field, 'content_length': len(data[field].strip())}
)
return True
def validate_campaign_data(campaign: Dict) -> bool:
"""Validate campaign data structure and content"""
required_fields = [
'campaign_name',
'core_message',
'visual_theme_description',
'key_emotional_appeal'
]
if not all(field in campaign for field in required_fields):
missing = [f for f in required_fields if f not in campaign]
raise ValidationError(
f"Missing required campaign fields: {', '.join(missing)}",
'campaign',
{'missing_fields': missing}
)
# Validate content quality
for field in required_fields:
if not campaign[field] or len(campaign[field].strip()) < 10:
raise ValidationError(
f"Insufficient content in {field}",
'campaign',
{'field': field, 'content_length': len(campaign[field].strip())}
)
return True
from src.core.claude_llm import create_claude_llm
from src.core.openai_llm import create_openai_llm
from src.config.settings import load_settings
from src.core.tools import create_tavily_tool
from src.agents.research.agent import ResearchAgent
from src.agents.marketing.agent import MarketingAgent
from src.agents.AdGen.orchestrator import AdCampaignOrchestrator
from src.agents.AdGen.ad_content_generator import CreativeAgent
from src.agents.AdGen.image_gen import SDXLTurboGenerator
from src.agents.marketing.agent import parse_research_results
def parse_research_results(research_results: str) -> Dict:
"""
Enhanced parser for research results with validation
Args:
research_results: Raw research results string
Returns:
Dictionary containing parsed and validated research data
Raises:
ParsingError: If parsing fails
ValidationError: If validation fails
"""
try:
# First try to extract sections from the final answer format
sections = {
'company_summary': '',
'market_analysis': '',
'competitor_analysis': ''
}
# Look for numbered sections in the research results
basic_info_match = re.search(r'1\.\s+\*\*Basic Company Information\*\*:(.+?)(?=2\.|$)', research_results, re.DOTALL)
market_match = re.search(r'3\.\s+\*\*Market Position\*\*:(.+?)(?=4\.|$)', research_results, re.DOTALL)
audience_match = re.search(r'4\.\s+\*\*Target Audience\*\*:(.+?)(?=$)', research_results, re.DOTALL)
if basic_info_match:
sections['company_summary'] = basic_info_match.group(1).strip()
if market_match:
sections['market_analysis'] = market_match.group(1).strip()
if audience_match:
sections['competitor_analysis'] = audience_match.group(1).strip()
# If sections are empty, try alternative format
if not any(sections.values()):
# Try to extract from bullet points or dashes
company_info = re.findall(r'[-•]\s*(.*?)(?=[-•]|$)', research_results, re.DOTALL)
if company_info:
sections['company_summary'] = '\n'.join(company_info).strip()
sections['market_analysis'] = sections['company_summary'] # Use same content as fallback
sections['competitor_analysis'] = sections['company_summary'] # Use same content as fallback
# If still empty, use the entire research results
if not any(sections.values()):
content = research_results.strip()
if len(content) > 50: # Minimum content length check
sections['company_summary'] = content
sections['market_analysis'] = content
sections['competitor_analysis'] = content
else:
raise ParsingError(
"Research results too short or empty",
'research',
{'content_length': len(content)}
)
# Validate parsed data
validate_research_data(sections)
return sections
except (AttributeError, IndexError) as e:
raise ParsingError(
"Failed to parse research results",
'research',
{'error': str(e), 'research_results': research_results[:100] + '...'}
)
def parse_campaign_ideas(marketing_results: str) -> List[Dict]:
"""
Enhanced parser for campaign ideas with validation
Args:
marketing_results: Raw marketing results string
Returns:
List of campaign dictionaries with validated fields
Raises:
ParsingError: If parsing fails
ValidationError: If validation fails
"""
if not marketing_results or not marketing_results.strip():
raise ParsingError(
"Marketing results are empty",
'marketing',
{'marketing_results': marketing_results}
)
campaigns = []
# Extract success metrics first
success_metrics = []
metrics_section = re.search(r'Success Metrics:(.+?)(?=Campaign |$)', marketing_results, re.DOTALL)
if metrics_section:
metrics_text = metrics_section.group(1).strip()
success_metrics = [m.strip() for m in metrics_text.split('\n') if m.strip()]
# Split and parse campaigns
campaign_sections = re.split(r'### Campaign |## Campaign ', marketing_results)
campaign_sections = [s for s in campaign_sections if s.strip()] # Remove empty sections
if not campaign_sections:
raise ParsingError(
"No campaign sections found",
'marketing',
{'marketing_results': marketing_results[:100] + '...'}
)
for i, section in enumerate(campaign_sections, 1):
try:
# Enhanced regex patterns with better field matching
patterns = {
'campaign_name': r'(?:Campaign Name:|Name:)(.+?)(?=Core Message:|$)',
'core_message': r'Core Message:(.+?)(?=Visual Theme Description:|Visual Theme:|$)',
'visual_theme': r'Visual Theme(?:\s*Description)?:(.+?)(?=Key Emotional Appeal:|Emotional Appeal:|$)',
'emotional_appeal': r'(?:Key )?Emotional Appeal:(.+?)(?=Social Media Focus:|Social Media:|$)',
'social_media': r'Social Media(?:\s*Focus)?:(.+?)(?=Campaign Timeline:|Timeline:|$)',
'timeline': r'(?:Campaign )?Timeline:(.+?)(?=Budget Allocation:|Budget:|$)',
'budget': r'Budget(?:\s*Allocation)?:(.+?)(?=Success Metrics:|$)',
}
# Extract fields with enhanced error handling
extracted_fields = {}
for field, pattern in patterns.items():
match = re.search(pattern, section, re.DOTALL)
if match:
extracted_fields[field] = match.group(1).strip()
# Build campaign dictionary with validation
campaign = {
"campaign_name": extracted_fields.get('campaign_name', f'Campaign {i}'),
"core_message": extracted_fields.get('core_message', ''),
"visual_theme_description": extracted_fields.get('visual_theme', ''),
"key_emotional_appeal": extracted_fields.get('emotional_appeal', ''),
"campaign_timeline": extracted_fields.get('timeline', ''),
"budget_allocation": extracted_fields.get('budget', ''),
"success_metrics": success_metrics,
"prompt_suggestions": {
"brand_focused": extracted_fields.get('core_message', ''),
"visual_focused": extracted_fields.get('visual_theme', ''),
"social_media": extracted_fields.get('social_media', '')
}
}
# Validate campaign data
validate_campaign_data(campaign)
campaigns.append(campaign)
except ValidationError as ve:
print(f"Validation error in campaign {i}: {ve.message}")
continue
except Exception as e:
print(f"Error parsing campaign {i}: {str(e)}")
continue
if not campaigns:
# Return fallback campaign with validation
fallback = {
"campaign_name": "Brand Awareness Campaign",
"core_message": "Highlighting unique value proposition",
"visual_theme_description": "Clean, professional design that reflects brand identity",
"key_emotional_appeal": "Trust and reliability",
"campaign_timeline": "Q1 2024",
"budget_allocation": "Standard allocation across channels",
"success_metrics": ["Increase brand awareness", "Drive engagement"],
"prompt_suggestions": {
"brand_focused": "Showcasing brand values and mission",
"visual_focused": "Professional and trustworthy imagery",
"social_media": "Engaging content across key platforms"
}
}
validate_campaign_data(fallback)
campaigns.append(fallback)
return campaigns
async def run_campaign_flow(company_name: str, target_audience: str, progress_tracker: Optional[ProgressTracker] = None):
"""
Enhanced end-to-end campaign generation flow with progress tracking
Args:
company_name: Name of the company to generate campaign for
target_audience: Description of the target audience
progress_tracker: Optional progress tracker instance
Returns:
Dictionary containing results from each phase
Raises:
CampaignFlowError: If any step fails
"""
# Initialize progress tracking if not provided
if not progress_tracker:
progress_tracker = ProgressTracker()
try:
# Load settings and initialize tools
settings = load_settings()
llm = create_openai_llm(api_key=settings.openai_api_key)
tavily_tool = create_tavily_tool(api_key=settings.tavily_api_key)
tools = [tavily_tool]
# Step 1: Research Phase
progress_tracker.start_step('research', {
'company_name': company_name,
'target_audience': target_audience
})
print("\n=== Starting Research Phase ===")
research_agent = ResearchAgent(llm=llm, tools=tools, verbose=True)
await research_agent.initialize()
research_results = await research_agent.run(
company_name=company_name,
target_audience=target_audience
)
# Validate research results
parsed_research = parse_research_results(research_results)
progress_tracker.update_progress(1.0, {'status': 'completed'})
# Step 2: Marketing Strategy Phase
progress_tracker.start_step('marketing', {
'research_summary': parsed_research['company_summary'][:100] + '...'
})
print("\n=== Starting Marketing Strategy Phase ===")
marketing_agent = MarketingAgent(llm=llm, tools=tools, verbose=True)
await marketing_agent.initialize()
marketing_results = await marketing_agent.run(
company_summary=parsed_research["company_summary"],
target_audience=target_audience,
brand_values=parsed_research["market_analysis"]
)
# Parse and validate campaign ideas
campaign_ideas = parse_campaign_ideas(marketing_results)
progress_tracker.update_progress(1.0, {
'campaigns_generated': len(campaign_ideas)
})
# Step 3: Ad Generation Phase
progress_tracker.start_step('ad_generation', {
'num_campaigns': len(campaign_ideas)
})
print("\n=== Starting Ad Generation Phase ===")
creative_agent = CreativeAgent(llm=llm, tools=tools, verbose=True)
await creative_agent.initialize()
image_generator = SDXLTurboGenerator()
orchestrator = AdCampaignOrchestrator(
creative_agent=creative_agent,
image_generator=image_generator,
llm=llm
)
# Update campaigns with research insights
for campaign in campaign_ideas:
campaign.update({
"brand_info": parsed_research["company_summary"],
"target_audience": target_audience,
"market_context": parsed_research["market_analysis"],
"competitor_insights": parsed_research["competitor_analysis"]
})
# Generate campaign assets
campaign_results = await orchestrator.generate_campaign(
brand_info=parsed_research["company_summary"],
target_audience=target_audience,
campaign_goals="; ".join(campaign_ideas[0].get("success_metrics", [])),
campaign_ideas=campaign_ideas
)
progress_tracker.update_progress(1.0, {
'assets_generated': len(campaign_results.get('assets', []))
})
# Compile final results
results = {
"research_results": {
"raw": research_results,
"parsed": parsed_research
},
"marketing_results": {
"raw": marketing_results,
"campaigns": campaign_ideas
},
"campaign_results": campaign_results,
"flow_metrics": {
step: {
"duration": prog.duration,
"details": prog.details
} for step, prog in progress_tracker.steps.items()
}
}
return results
except CampaignFlowError as e:
print(f"\nCampaign flow error in {e.step}: {e.message}")
if e.details:
print("Error details:", json.dumps(e.details, indent=2))
raise
except Exception as e:
print(f"\nUnexpected error: {str(e)}")
raise CampaignFlowError(
f"Unexpected error: {str(e)}",
progress_tracker.current_step or 'unknown',
{'error_type': type(e).__name__}
)
async def main_async(company_name: str, target_audience: str, output_file: str = None):
"""Run the campaign flow with progress tracking and save results."""
progress_tracker = ProgressTracker()
results = await run_campaign_flow(
company_name=company_name,
target_audience=target_audience,
progress_tracker=progress_tracker
)
if output_file:
with open(output_file, 'w') as f:
json.dump(results, f, indent=2)
print(f"\nResults saved to: {output_file}")
return results
def main():
parser = argparse.ArgumentParser(description='Run end-to-end campaign generation flow')
parser.add_argument('company_name', help='Name of the company')
parser.add_argument('target_audience', help='Description of target audience')
parser.add_argument('--output', '-o', help='Output file to save results (optional)')
args = parser.parse_args()
asyncio.run(main_async(
company_name=args.company_name,
target_audience=args.target_audience,
output_file=args.output
))
if __name__ == "__main__":
main()