CrewAI
CrewAI is a powerful framework for creating and orchestrating AI agents that can collaborate to perform complex tasks.
CrewAI Agents
In the CrewAI framework, an Agent is an autonomous unit that can:
-
Perform specific tasks
-
Make decisions based on its role and goal
-
Use tools to accomplish objectives
-
Communicate and collaborate with other agents
-
Maintain memory of interactions
-
Delegate tasks when allowed
CrewAI Framework is supported only with GPT-4.1, GPT-4o, GPT-4o mini, o3-mini, and o1 models.
Set Up the Environment
Install CrewAI and its dependencies:
pip install crewai crewai-tools
pip install 'crewai[tools]'
Agents in CrewAI can be defined using YAML configuration or directly in code. The recommended approach is to use YAML for better maintainability.
Define Agent Using YAML Configuration
To define agent using YAML configuration:
-
Navigate to config/agents.yaml file.
-
Define your agent in the agents.yaml file:
researcher:
role: "{topic} Senior Data Researcher"
goal: "Uncover cutting-edge developments in {topic}"
backstory: "You're a seasoned researcher with a knack for uncovering the latest developments in {topic}. Known for your ability to find the most relevant information and present it in a clear and concise manner."
reporting_analyst:
role: "{topic} Reporting Analyst"
goal: "Create detailed reports based on {topic} data analysis and research findings"
backstory: "You're a meticulous analyst with a keen eye for detail. You're known for your ability to turn complex data into clear and concise reports, making it easy for others to understand and act on the information you provide." -
Define task specifications for agents in the tasks.yaml file:
research_task:
description: >
Conduct a thorough research about {topic}
Make sure you find any interesting and relevant information.
expected_output: >
A list with 10 bullet points of the most relevant information about {topic}
agent: researcher
reporting_task:
description: >
Review the context you got and expand each topic into a full section for a report.
Make sure the report is detailed and contains any and all relevant information.
expected_output: >
A fully fledged report with the main topics, each with a full section of information.
Formatted as markdown without '```
agent: reporting_analyst
output_file: report.md
Implement Agents
Integrate any of the Compass APIs. In the below example, GPT-4o has been used.
Define Compass LLM (GPT-4o) using the CrewAI framework:
from crewai import LLM
import os
MODEL_NAME = "gpt-4o"
MODEL_URL = "https://api.core42.ai/v1"
API_KEY = <api_key>
llm = LLM(
model = MODEL_NAME,
base_url = MODEL_URL,
api_key = API_KEY
)
You can test if the model is working using the code below:
sample_prompt = "Explain the concept of artificial intelligence in simple terms."
response = llm.call(sample_prompt)
print("Custom LLM Response:", response)
Two agents are created for reference: researcher and reporting_analyst.
Working sample code of building agents, defining tasks, and agentic functionality using CrewAI framework is given below:
import yaml
from crewai import Agent, Task, Crew
def load_config(file_path):
"""Load configuration from a YAML file."""
with open(file_path, 'r') as file:
return yaml.safe_load(file)
def create_agent(config, name, topic, llm):
"""Create an agent using the custom LLM."""
agent_config = config[name]
return Agent(
role=agent_config['role'].format(topic=topic),
goal=agent_config['goal'].format(topic=topic),
backstory=agent_config['backstory'].format(topic=topic),
verbose=True,
llm=llm,
)
def create_task(config, name, agent, topic):
"""Create a task and assign it to an agent."""
task_config = config[name]
return Task(
description=task_config['description'].format(topic=topic),
expected_output=task_config['expected_output'].format(topic=topic),
agent=agent
)
def main(topic):
"""Main function to set up and run the crew."""
# Load configurations
agents_config = load_config('config/agents.yaml')
tasks_config = load_config('config/tasks.yaml')
# Create agents using the custom LLM
researcher = create_agent(agents_config, 'researcher', topic, llm)
reporting_analyst = create_agent(agents_config, 'reporting_analyst', topic, llm)
# Create tasks for the agents
research_task = create_task(tasks_config, 'research_task', researcher, topic)
reporting_task = create_task(tasks_config, 'reporting_task', reporting_analyst, topic)
# Create and run the crew
crew = Crew(
agents=[researcher, reporting_analyst],
tasks=[research_task, reporting_task],
verbose=True # Set verbosity to True for detailed logs
)
result = crew.kickoff()
print(result)
if __name__ == "__main__":
topic = "Quantum Computing" # You can change this to any topic you want to research
main(topic)
Response:
Configurable Agent Parameters
Key parameters for agent configuration include:
-
Critical Parameters:
role
,goal
, andbackstory
-
Memory and Context:
memory
,respect_context_window
,knowledge_sources
-
Execution Control:
max_iter
,max_execution_time
,max_rpm
,max_retry_limit
-
Code Execution:
allow_code_execution
,code_execution_mode
-
Templates:
system_template
,prompt_template
,response_template
Best Practices
-
Use YAML configuration for cleaner, more maintainable agent definitions.
-
Assign specialized roles to different agents for complex tasks.
-
Utilize performance metrics analysis to fine-tune agents for specific contexts.