ok so generate me a plan to create a ai agents using crewai
- User
ok so generate me a plan to create a ai agents using crewai
Assistant
ok so generate me a plan to create a ai agents using crewai
Here's a step-by-step plan to create AI agents using CrewAI, including tools, task decomposition, and execution flow:
python -m venv crewai-env
source crewai-env/bin/activate # Linux/Mac)
# OR crewai-env\Scripts\activate (Windows)
pip install crewai
pip install langchain-community crewai-tools duckduckgo-search
Create agents.py:
from crewai import Agent
from crewai_tools import ScrapeWebsiteTool, SerperDevTool
# Tools
scrape_tool = ScrapeWebsiteTool()
search_tool = SerperDevTool()
researcher = Agent(
role="Senior Research Analyst",
goal="Discover cutting-edge developments in AI/tech",
backstory="Expert in data-driven insights with 10+ years experience",
tools=[search_tool, scrape_tool],
verbose=True,
allow_delegation=False
)
writer = Agent(
role="Technical Content Strategist",
goal="Create engaging content about tech trends",
backstory="Former tech journalist turned content expert",
tools=[scrape_tool],
verbose=True
)
reviewer = Agent(
role="Quality Assurance Specialist",
goal="Ensure accuracy and clarity of reports",
backstory="Detail-oriented editor with PhD in technical communication",
verbose=True
)
Create tasks.py:
from crewai import Task
research_task = Task(
description="Investigate AI advancements in 2024. Focus on breakthroughs in NLP.",
expected_output="Bullet-point report with key facts, sources, and data points",
agent=researcher,
output_file="research.md"
)
content_task = Task(
description="Write blog post using research data. Target: technical managers.",
expected_output="1500-word article with 3 sections: Intro, Analysis, Future Outlook",
agent=writer,
context=[research_task]
)
review_task = Task(
description="Fact-check and improve readability. Ensure no plagiarism.",
expected_output="Final polished document in Markdown format",
agent=reviewer,
context=[content_task]
)
Create crew_setup.py:
from crewai import Crew
from agents import researcher, writer, reviewer
from tasks import research_task, content_task, review_task
tech_crew = Crew(
agents=[researcher, writer, reviewer],
tasks=[research_task, content_task, review_task],
process="sequential", # Options: sequential, hierarchical, consensual
memory=True, # Enable conversational memory
verbose=2
)
# In crew_setup.py
if __name__ == "__main__":
result = tech_crew.kickoff(inputs={"topic": "Generative AI in healthcare"})
with open("final_report.md", "w") as f:
f.write(result)
Custom Tools (Create custom_tools.py):
from crewai_tools import BaseTool
class PDFAnalyzerTool(BaseTool):
name = "PDF Analyst"
description = "Extracts text from PDFs"
def _run(self, file_path: str):
from PyPDF2 import PdfReader
reader = PdfReader(file_path)
text = "".join(page.extract_text() for page in reader.pages)
return text
Process Optimization:
process="hierarchical" for complex workflowsmax_iter=15 to agents to prevent loopsHuman-in-the-Loop:
content_task = Task(
# ...
human_input=True # Pauses for human feedback after execution
)
Tool Selection:
ScrapeWebsiteTool()SerperDevTool()FileReadTool()Agent Configuration:
llm parameter for custom models (e.g., llm=OpenAI(model="gpt-4-turbo"))max_rpm to manage API rate limitsEvaluation Metrics:
crewai.metrics modulegraph LR
A[User Input] --> B(Researcher)
B -->|Research Data| C(Writer)
C -->|Draft Content| D(Reviewer)
D --> E[Final Report]
max_iter or simplify tasksexpected_output examplesThis plan provides a production-ready framework. Start with simple agents (2-3), then expand to hierarchical teams as complexity grows. Remember to:
crewai.output_parser for structured outputsFor advanced use cases:
crewai.utilitiescrewai.memory modules