|
from crewai import Agent, Task, Crew, Process
|
|
from crewai_tools import SerperDevTool
|
|
import os
|
|
|
|
|
|
llm_models = [
|
|
"gemini/gemini-1.5-flash",
|
|
"gemini/gemini-1.5-pro",
|
|
"gemini/gemini-pro"
|
|
]
|
|
|
|
selected_model = llm_models[0]
|
|
|
|
def set_model(selected_model_name):
|
|
global selected_model
|
|
selected_model = selected_model_name
|
|
|
|
def configure_api_keys(gemini_api_key, search_choice, serper_api_key):
|
|
if not gemini_api_key:
|
|
raise ValueError("Gemini API key is required")
|
|
os.environ['GEMINI_API_KEY'] = gemini_api_key
|
|
search_tool = None
|
|
if search_choice == "Yes":
|
|
if not serper_api_key:
|
|
raise ValueError("Serper API key is required for online search")
|
|
os.environ['SERPER_API_KEY'] = serper_api_key
|
|
search_tool = SerperDevTool()
|
|
return search_tool
|
|
|
|
def run_crew_cga(gemini_api_key, search_choice, serper_api_key, topic):
|
|
try:
|
|
search_tool = configure_api_keys(gemini_api_key, search_choice, serper_api_key)
|
|
|
|
researcher = Agent(
|
|
role="Online Research Specialist",
|
|
goal=f"Aggregate comprehensive information on {topic}",
|
|
verbose=True,
|
|
backstory="Expert research analyst with data sourcing expertise",
|
|
tools=[search_tool] if search_tool else [],
|
|
llm=selected_model,
|
|
allow_delegation=True
|
|
)
|
|
|
|
content_writer = Agent(
|
|
role="Expert Content Writer",
|
|
goal=f"Create SEO-optimized content on {topic}",
|
|
verbose=True,
|
|
backstory="Professional writer with digital journalism background",
|
|
tools=[],
|
|
llm=selected_model,
|
|
allow_delegation=False
|
|
)
|
|
|
|
research_task = Task(
|
|
description=f"Conduct SEO research on '{topic}'",
|
|
expected_output="Detailed research report with SEO recommendations",
|
|
tools=[search_tool] if search_tool else [],
|
|
agent=researcher
|
|
)
|
|
|
|
writer_task = Task(
|
|
description=f"Write SEO-optimized article on '{topic}'",
|
|
expected_output="Polished article draft ready for publication",
|
|
agent=content_writer,
|
|
output_file="content.md"
|
|
)
|
|
|
|
crew = Crew(
|
|
agents=[researcher, content_writer],
|
|
tasks=[research_task, writer_task],
|
|
process=Process.sequential,
|
|
verbose=True,
|
|
max_rpm=100,
|
|
share_crew=True,
|
|
output_log_file=True
|
|
)
|
|
|
|
crew.kickoff(inputs={'topic': topic})
|
|
with open("content.md", "r") as f:
|
|
content = f.read()
|
|
with open("logs.txt", 'r') as f:
|
|
logs = f.read()
|
|
|
|
with open("logs.txt", 'w') as f:
|
|
f.truncate(0)
|
|
return content, logs
|
|
except Exception as e:
|
|
return f"Error: {str(e)}", str(e)
|
|
|