Sarath0x8f commited on
Commit
3bab37f
·
verified ·
1 Parent(s): a9df80e

Upload MarketingPostGeneratorAgent.py

Browse files
Files changed (1) hide show
  1. MarketingPostGeneratorAgent.py +153 -0
MarketingPostGeneratorAgent.py ADDED
@@ -0,0 +1,153 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import List
2
+ from crewai import Agent, Crew, Process, Task
3
+ from crewai.project import CrewBase, agent, crew, task
4
+ from crewai_tools import SerperDevTool, ScrapeWebsiteTool
5
+ from pydantic import BaseModel, Field
6
+ import os
7
+
8
+ # Model options
9
+ llm_models = [
10
+ "gemini/gemini-1.5-flash",
11
+ "gemini/gemini-1.5-pro",
12
+ "gemini/gemini-pro"
13
+ ]
14
+ selected_model = llm_models[0]
15
+
16
+
17
+ def set_model(selected_model_name):
18
+ global selected_model
19
+ selected_model = selected_model_name
20
+
21
+
22
+ def configure_api_keys(gemini_api_key):
23
+ if not gemini_api_key:
24
+ raise ValueError("Gemini API key is required")
25
+ os.environ['GEMINI_API_KEY'] = gemini_api_key
26
+
27
+
28
+ # Pydantic models for output validation
29
+ class MarketStrategy(BaseModel):
30
+ """Market strategy model"""
31
+ name: str = Field(..., description="Name of the market strategy")
32
+ tatics: List[str] = Field(..., description="List of tactics to be used in the market strategy")
33
+ channels: List[str] = Field(..., description="List of channels to be used in the market strategy")
34
+ KPIs: List[str] = Field(..., description="List of KPIs to be used in the market strategy")
35
+
36
+
37
+ class CampaignIdea(BaseModel):
38
+ """Campaign idea model"""
39
+ name: str = Field(..., description="Name of the campaign idea")
40
+ description: str = Field(..., description="Description of the campaign idea")
41
+ audience: str = Field(..., description="Audience of the campaign idea")
42
+ channel: str = Field(..., description="Channel of the campaign idea")
43
+
44
+
45
+ class Copy(BaseModel):
46
+ """Copy model"""
47
+ title: str = Field(..., description="Title of the copy")
48
+ body: str = Field(..., description="Body of the copy")
49
+
50
+
51
+ @CrewBase
52
+ class MarketingPostsCrew:
53
+ """MarketingPosts crew"""
54
+ agents_config = 'config/MarketingPostGeneratorAgent/agents.yaml'
55
+ tasks_config = 'config/MarketingPostGeneratorAgent/tasks.yaml'
56
+
57
+ @agent
58
+ def lead_market_analyst(self) -> Agent:
59
+ return Agent(
60
+ config=self.agents_config['lead_market_analyst'],
61
+ tools=[SerperDevTool(), ScrapeWebsiteTool()],
62
+ verbose=True,
63
+ memory=False,
64
+ llm=selected_model
65
+ )
66
+
67
+ @agent
68
+ def chief_marketing_strategist(self) -> Agent:
69
+ return Agent(
70
+ config=self.agents_config['chief_marketing_strategist'],
71
+ tools=[SerperDevTool(), ScrapeWebsiteTool()],
72
+ verbose=True,
73
+ memory=False,
74
+ llm=selected_model
75
+ )
76
+
77
+ @agent
78
+ def creative_content_creator(self) -> Agent:
79
+ return Agent(
80
+ config=self.agents_config['creative_content_creator'],
81
+ verbose=True,
82
+ memory=False,
83
+ llm=selected_model
84
+ )
85
+
86
+ @task
87
+ def research_task(self) -> Task:
88
+ return Task(
89
+ config=self.tasks_config['research_task'],
90
+ agent=self.lead_market_analyst()
91
+ )
92
+
93
+ @task
94
+ def project_understanding_task(self) -> Task:
95
+ return Task(
96
+ config=self.tasks_config['project_understanding_task'],
97
+ agent=self.chief_marketing_strategist()
98
+ )
99
+
100
+ @task
101
+ def marketing_strategy_task(self) -> Task:
102
+ return Task(
103
+ config=self.tasks_config['marketing_strategy_task'],
104
+ agent=self.chief_marketing_strategist(),
105
+ output_json=MarketStrategy
106
+ )
107
+
108
+ @task
109
+ def campaign_idea_task(self) -> Task:
110
+ return Task(
111
+ config=self.tasks_config['campaign_idea_task'],
112
+ agent=self.creative_content_creator(),
113
+ output_json=CampaignIdea
114
+ )
115
+
116
+ @task
117
+ def copy_creation_task(self) -> Task:
118
+ return Task(
119
+ config=self.tasks_config['copy_creation_task'],
120
+ agent=self.creative_content_creator(),
121
+ context=[self.marketing_strategy_task(), self.campaign_idea_task()],
122
+ output_json=Copy
123
+ )
124
+
125
+ @crew
126
+ def crew(self) -> Crew:
127
+ """Creates the MarketingPosts crew"""
128
+ return Crew(
129
+ agents=self.agents, # Automatically created by the @agent decorator
130
+ tasks=self.tasks, # Automatically created by the @task decorator
131
+ process=Process.sequential,
132
+ verbose=True,
133
+ )
134
+
135
+
136
+ def run_crew_mpga(gemini_api_key, inputs):
137
+ """
138
+ Run the MarketingPosts crew using the provided Gemini API key and inputs.
139
+
140
+ Args:
141
+ gemini_api_key (str): The Gemini API key to configure the environment.
142
+ inputs (dict): Input parameters required by the tasks (e.g., customer_domain, project_description).
143
+
144
+ Returns:
145
+ The final output from the crew kickoff.
146
+ """
147
+ try:
148
+ configure_api_keys(gemini_api_key)
149
+ crew_instance = MarketingPostsCrew().crew()
150
+ result = crew_instance.kickoff(inputs=inputs)
151
+ return result
152
+ except Exception as e:
153
+ return f"Error: {str(e)}"