|
| 1 | +import asyncio |
| 2 | +from typing import TypedDict |
| 3 | + |
| 4 | +from pydantic import BaseModel, Field # pyright: ignore [reportUnknownVariableType] |
| 5 | + |
| 6 | +import workflowai |
| 7 | +from workflowai import Model |
| 8 | + |
| 9 | + |
| 10 | +class MarketingCopyInput(BaseModel): |
| 11 | + """The product or concept for which to generate marketing copy.""" |
| 12 | + |
| 13 | + idea: str = Field(description="A short description or name of the product.") |
| 14 | + |
| 15 | + |
| 16 | +class MarketingCopyOutput(BaseModel): |
| 17 | + """Contains the AI generated marketing copy text for the provided product or concept.""" |
| 18 | + |
| 19 | + marketing_text: str = Field(description="Persuasive marketing copy text.") |
| 20 | + |
| 21 | + |
| 22 | +@workflowai.agent(id="marketing-copy-generator", model=Model.GPT_4O_MINI_LATEST) |
| 23 | +async def generate_marketing_copy_agent(_: MarketingCopyInput) -> MarketingCopyOutput: |
| 24 | + """ |
| 25 | + Write persuasive marketing copy for the provided idea. |
| 26 | + Focus on benefits and emotional appeal. |
| 27 | + """ |
| 28 | + ... |
| 29 | + |
| 30 | + |
| 31 | +class EvaluateCopyInput(BaseModel): |
| 32 | + """Input type for evaluating the quality of marketing copy.""" |
| 33 | + |
| 34 | + marketing_text: str = Field(description="The marketing copy text to evaluate.") |
| 35 | + |
| 36 | + |
| 37 | +class EvaluateCopyOutput(BaseModel): |
| 38 | + """Evaluation results for the marketing copy.""" |
| 39 | + |
| 40 | + has_call_to_action: bool = Field(description="Whether a call to action is present.") |
| 41 | + emotional_appeal: int = Field(description="Emotional appeal rating (1-10).") |
| 42 | + clarity: int = Field(description="Clarity rating (1-10).") |
| 43 | + |
| 44 | + |
| 45 | +# We use a smarter model (O1) to review the copy since evaluation requires more nuanced understanding |
| 46 | +@workflowai.agent(id="marketing-copy-evaluator", model=Model.O1_MINI_LATEST) |
| 47 | +async def evaluate_marketing_copy_agent(_: EvaluateCopyInput) -> EvaluateCopyOutput: |
| 48 | + """ |
| 49 | + Evaluate the marketing copy for: |
| 50 | + 1) Presence of a call to action (true/false) |
| 51 | + 2) Emotional appeal (1-10) |
| 52 | + 3) Clarity (1-10) |
| 53 | + Return the results as a structured output. |
| 54 | + """ |
| 55 | + ... |
| 56 | + |
| 57 | + |
| 58 | +class RewriteCopyInput(BaseModel): |
| 59 | + """Input for rewriting the marketing copy with targeted improvements.""" |
| 60 | + |
| 61 | + original_copy: str = Field(default="", description="Original marketing copy.") |
| 62 | + add_call_to_action: bool = Field(default=False, description="Whether we need a clear call to action.") |
| 63 | + strengthen_emotional_appeal: bool = Field(default=False, description="Whether emotional appeal needs a boost.") |
| 64 | + improve_clarity: bool = Field(default=False, description="Whether clarity needs improvement.") |
| 65 | + |
| 66 | + |
| 67 | +class RewriteCopyOutput(BaseModel): |
| 68 | + """Contains the improved marketing copy.""" |
| 69 | + |
| 70 | + marketing_text: str = Field(description="The improved marketing copy text.") |
| 71 | + |
| 72 | + |
| 73 | +# Claude 3.5 Sonnet is a more powerful model for copywriting |
| 74 | +@workflowai.agent(model=Model.CLAUDE_3_5_SONNET_LATEST) |
| 75 | +async def rewrite_marketing_copy_agent(_: RewriteCopyInput) -> RewriteCopyOutput: |
| 76 | + """ |
| 77 | + Rewrite the marketing copy with the specified improvements: |
| 78 | + - A clear CTA if requested |
| 79 | + - Stronger emotional appeal if requested |
| 80 | + - Improved clarity if requested |
| 81 | + """ |
| 82 | + ... |
| 83 | + |
| 84 | + |
| 85 | +class MarketingResult(TypedDict): |
| 86 | + original_copy: str |
| 87 | + final_copy: str |
| 88 | + was_improved: bool |
| 89 | + quality_metrics: EvaluateCopyOutput |
| 90 | + |
| 91 | + |
| 92 | +async def generate_marketing_copy(idea: str) -> MarketingResult: |
| 93 | + """ |
| 94 | + Demonstrates a chain flow: |
| 95 | + 1) Generate an initial marketing copy. |
| 96 | + 2) Evaluate its quality. |
| 97 | + 3) If the quality is lacking, request a rewrite with clearer CTA, stronger emotional appeal, and/or clarity. |
| 98 | + 4) Return the final copy and metrics. |
| 99 | + """ |
| 100 | + # Step 1: Generate initial copy |
| 101 | + generation = await generate_marketing_copy_agent(MarketingCopyInput(idea=idea)) |
| 102 | + original_copy = generation.marketing_text |
| 103 | + final_copy = original_copy |
| 104 | + |
| 105 | + # Step 2: Evaluate the copy |
| 106 | + evaluation = await evaluate_marketing_copy_agent(EvaluateCopyInput(marketing_text=original_copy)) |
| 107 | + |
| 108 | + # Step 3: Check evaluation results. If below thresholds, rewrite |
| 109 | + needs_improvement = not evaluation.has_call_to_action or evaluation.emotional_appeal < 7 or evaluation.clarity < 7 |
| 110 | + |
| 111 | + if needs_improvement: |
| 112 | + rewrite = await rewrite_marketing_copy_agent( |
| 113 | + RewriteCopyInput( |
| 114 | + original_copy=original_copy, |
| 115 | + add_call_to_action=not evaluation.has_call_to_action, |
| 116 | + strengthen_emotional_appeal=evaluation.emotional_appeal < 7, |
| 117 | + improve_clarity=evaluation.clarity < 7, |
| 118 | + ), |
| 119 | + ) |
| 120 | + final_copy = rewrite.marketing_text |
| 121 | + |
| 122 | + return { |
| 123 | + "original_copy": original_copy, |
| 124 | + "final_copy": final_copy, |
| 125 | + "was_improved": needs_improvement, |
| 126 | + "quality_metrics": evaluation, |
| 127 | + } |
| 128 | + |
| 129 | + |
| 130 | +if __name__ == "__main__": |
| 131 | + idea = "A open-source platform for AI agents" |
| 132 | + result = asyncio.run(generate_marketing_copy(idea)) |
| 133 | + |
| 134 | + print("\n=== Input Idea ===") |
| 135 | + print(idea) |
| 136 | + |
| 137 | + print("\n=== Marketing Copy ===") |
| 138 | + print(result["original_copy"]) |
| 139 | + |
| 140 | + print("\n=== Quality Assessment ===") |
| 141 | + metrics = result["quality_metrics"] |
| 142 | + print(f"✓ Call to Action: {'Present' if metrics.has_call_to_action else 'Missing'}") |
| 143 | + print(f"✓ Emotional Appeal: {metrics.emotional_appeal}/10") |
| 144 | + print(f"✓ Clarity: {metrics.clarity}/10") |
| 145 | + |
| 146 | + if result["was_improved"]: |
| 147 | + print("\n=== Improved Marketing Copy ===") |
| 148 | + print(result["final_copy"]) |
| 149 | + print() |
0 commit comments