Crewai — Ai Agent

DhanushKumar
25 min readJun 26, 2024

--

CrewAI is a robust framework for orchestrating collaborative AI agents. It leverages the strengths of individual agents, structured task management, and seamless collaboration to handle complex tasks efficiently. By integrating tools, defining processes, and utilizing memory, CrewAI ensures that each agent performs optimally, contributing to the overall success of the project. This approach not only enhances productivity but also opens up new possibilities for AI applications across various domains.

Core Concepts

  • Agents
  • Tasks
  • Tools
  • Processes
  • Crews
  • Memory

Agents

  • Agents in CrewAI have defined roles, goals, and backstories that guide their decision-making and collaboration.
  • Agents are equipped with specialized tools that enable them to perform their assigned tasks.
  • Agents can delegate tasks or questions to one another based on their capabilities.
  • Agents in CrewAI are autonomous AI entities designed to perform specific roles or tasks.
  • Each agent is equipped with unique capabilities, knowledge, and skills tailored to its designated function.
  • These agents can be thought of as specialized workers, each with its own area of expertise.
  • They can process information, make decisions, and carry out actions within their domain of competence.

Tasks

  • Tasks in CrewAI define the specific objectives and expected outputs for the agents.
  • Tasks can be executed asynchronously, and their outputs can be customized.
  • Tasks leverage the agents’ tools and capabilities to accomplish their goals.
  • Tasks are the individual units of work that need to be accomplished within a project.
  • In CrewAI, tasks are well-defined, structured pieces of work assigned to specific agents.
  • They have clear objectives, inputs, and expected outputs.
  • Tasks can range from simple operations to complex problem-solving activities, depending on the project’s requirements.

Tools

  • Tools in CrewAI are integrated capabilities that agents can utilize to perform their tasks.
  • CrewAI supports custom tool development by subclassing BaseTool or using the tool decorator.
  • Agents can be assigned specific tools during initialization to enhance their functionality.
  • Tools in CrewAI refer to the various resources, algorithms, APIs, or functionalities that agents can utilize to complete their tasks.
  • These tools extend the capabilities of agents, allowing them to interact with external systems, process data, or perform specialized operations.
  • Tools can include language models, data analysis libraries, web scraping utilities, or any other software component that aids in task completion.

Processes

  • Processes in CrewAI define the execution logic for the crew, such as sequential or hierarchical workflows.
  • The process determines how tasks are distributed and executed among the agents.
  • Processes can be configured to leverage language model management and other advanced attributes.
  • Processes in CrewAI define the workflow and sequence of tasks within a project.
  • They outline how different tasks are interconnected, determining the order of execution and the flow of information between agents.
  • Processes ensure that complex projects are broken down into manageable steps, with clear dependencies and handoffs between different stages of work.

Crews

  • Crews in CrewAI are the assemblage of agents, tasks, and the process that governs their collaboration.
  • Crews can utilize memory systems, caching, and other advanced features to optimize performance and enhance agent capabilities.
  • Crews can be configured with various attributes, such as verbose logging, rate limiting, and internationalization support.
  • A crew is a coordinated group of agents working together to achieve a common goal.
  • In CrewAI, crews are carefully assembled teams of agents, each bringing their unique skills to the table.
  • The crew concept allows for the orchestration of multiple agents, enabling them to collaborate effectively on complex projects that require diverse expertise.

Memory

  • CrewAI’s memory system includes short-term, long-term, entity, and contextual memory components.
  • Memory enables agents to maintain context, accumulate experiences, and develop a deeper understanding of entities.
  • Memory can be customized by configuring the embedder provider and other memory-related attributes.
  • Memory in CrewAI refers to the system’s ability to store, retrieve, and utilize information across tasks and over time. This can include short-term memory for ongoing tasks, as well as long-term memory for retaining important information or lessons learned from previous projects.
  • Memory enables agents to build upon past experiences, maintain context, and make more informed decisions as they work through tasks.

These core concepts work together to create a powerful and flexible framework for AI collaboration:

Agents use their specialized skills to tackle specific tasks.

Tasks are organized and distributed among the agents based on their capabilities.

Tools enhance the agents’ abilities to complete their assigned tasks effectively.

Processes structure the flow of work, ensuring that tasks are completed in the right order and that information flows smoothly between agents.

Crews bring together multiple agents with complementary skills to tackle complex projects.

Memory allows the system to learn and improve over time, retaining valuable information and experiences.

By integrating these elements, CrewAI creates a dynamic and efficient system for handling complex AI-driven projects across various domains.

Agents :

Agents in CrewAI:

Agents are the fundamental building blocks of the CrewAI framework. They are AI entities designed to perform specific roles or tasks within a larger system. Each agent is essentially a specialized worker with its own set of skills, knowledge, and capabilities.

Key characteristics of Agents:

  1. Specialization: Each agent is typically designed for a specific role or domain of expertise.
  2. Autonomy: Agents can make decisions and take actions within their area of competence.
  3. Interaction: They can communicate with other agents and integrate into larger workflows.
  4. Adaptability: Agents can often learn and improve their performance over time.

Functionalities of Agents with LLMs:

When integrated with Large Language Models, agents in CrewAI can exhibit a wide range of powerful functionalities:

Natural Language Processing:

  • Understanding and generating human-like text
  • Parsing complex instructions
  • Extracting key information from unstructured text

Task Comprehension:

  • Interpreting assigned tasks and their requirements
  • Breaking down complex tasks into manageable subtasks

Knowledge Application:

  • Leveraging the vast knowledge base of the LLM to inform decision-making
  • Applying domain-specific knowledge to solve problems

Reasoning and Problem-Solving:

  • Analyzing situations and proposing solutions
  • Conducting logical reasoning to approach complex issues

Language Generation:

  • Producing coherent and contextually appropriate responses
  • Crafting reports, summaries, or other textual outputs

Information Synthesis:

  • Combining information from multiple sources
  • Drawing insights and conclusions from diverse data points

Contextual Understanding:

  • Maintaining context throughout a conversation or task
  • Adapting responses based on the broader context of the project

Multi-turn Interactions:

  • Engaging in back-and-forth dialogues
  • Clarifying ambiguities and asking for additional information when needed

Tool Usage:

  • Understanding when and how to use external tools or APIs
  • Interpreting the results of tool usage and incorporating them into the workflow

Role-playing:

  • Adopting specific personas or roles as required by the task
  • Maintaining consistent behavior aligned with the assigned role

Ethical Consideration:

  • Applying ethical guidelines in decision-making processes
  • Flagging potential ethical concerns in tasks or outputs

Learning and Adaptation:

  • Improving performance based on feedback
  • Adapting to new information or changing requirements within a task

Implementation in CrewAI:

In CrewAI, agents powered by LLMs are typically implemented as follows:

Definition: Each agent is defined with specific attributes, including its role, capabilities, and any constraints on its behavior.

Prompt Engineering: The agent’s behavior is often guided by carefully crafted prompts that define its role, task objectives, and operational parameters.

Integration: Agents are integrated into the larger CrewAI system, allowing them to interact with other agents, access tools, and participate in defined processes.

Task Assignment: Based on their capabilities, agents are assigned specific tasks within the overall project workflow.

Execution: Agents process their assigned tasks, leveraging the LLM’s capabilities to understand, reason, and generate appropriate outputs.

Collaboration: Agents can share information, request assistance, or hand off tasks to other agents as needed.

Output Generation: The final output of an agent’s work is produced, which could be in the form of text, decisions, or actions.

By leveraging the power of LLMs, agents in CrewAI can handle a wide variety of complex tasks, from data analysis and content creation to problem-solving and decision support, making them versatile and powerful components in AI-driven workflows.

# Example: Creating an agent with all attributes
from crewai import Agent

agent = Agent(
role='Data Analyst',
goal='Extract actionable insights',
backstory="""You're a data analyst at a large company.
You're responsible for analyzing data and providing insights
to the business.
You're currently working on a project to analyze the
performance of our marketing campaigns.""",
tools=[my_tool1, my_tool2], # Optional, defaults to an empty list
llm=my_llm, # Optional
function_calling_llm=my_llm, # Optional
max_iter=15, # Optional
max_rpm=None, # Optional
max_execution_time=None, # Optional
verbose=True, # Optional
allow_delegation=True, # Optional
step_callback=my_intermediate_step_callback, # Optional
cache=True, # Optional
system_template=my_system_template, # Optional
prompt_template=my_prompt_template, # Optional
response_template=my_response_template, # Optional
config=my_config, # Optional
crew=my_crew, # Optional
tools_handler=my_tools_handler, # Optional
cache_handler=my_cache_handler, # Optional
callbacks=[callback1, callback2], # Optional
agent_executor=my_agent_executor # Optional
)



agent = Agent(
role="{topic} specialist",
goal="Figure {goal} out",
backstory="I am the master of {role}",
system_template="""<|start_header_id|>system<|end_header_id|>

{{ .System }}<|eot_id|>""",
prompt_template="""<|start_header_id|>user<|end_header_id|>

{{ .Prompt }}<|eot_id|>""",
response_template="""<|start_header_id|>assistant<|end_header_id|>

{{ .Response }}<|eot_id|>""",
)

Tasks in CrewAI:

Tasks are the fundamental units of work within the CrewAI framework. They represent specific, well-defined pieces of work that need to be accomplished as part of a larger project or goal.

Key characteristics of Tasks:

  1. Clarity: Each task has a clear objective and expected outcome.
  2. Assignability: Tasks can be assigned to specific agents based on their capabilities.
  3. Measurability: The completion and success of a task can be evaluated.
  4. Modularity: Tasks can be part of a larger process or broken down into subtasks.

Application of Tasks with LLMs:

When working with Large Language Models, tasks take on specific characteristics and applications:

Natural Language Task Definition:

  • Tasks can be defined in natural language, leveraging the LLM’s ability to understand complex instructions.
  • Example: “Summarize the key points of this research paper in bullet points.”

Context-Aware Processing:

  • Tasks can include relevant context that the LLM needs to consider.
  • Example: “Given the current market trends [context provided], predict potential challenges for our product launch.”

Multi-Step Operations:

  • Complex tasks can be broken down into a series of steps that the LLM can process sequentially.

Example:

1) Extract key financial metrics from this report.

2) Compare them to industry benchmarks.

3) Provide insights on the company’s performance.”

Conditional Execution:

  • Tasks can include conditional logic that the LLM must interpret and follow.
  • Example: “If the sentiment analysis is positive, generate a follow-up marketing message. Otherwise, create a customer service response.”

Information Gathering:

  • Tasks can involve collecting and synthesizing information from various sources.
  • Example: “Research the latest advancements in renewable energy and compile a report highlighting three key innovations.”

Creative Generation:

  • LLMs can be tasked with creating original content based on specific parameters.
  • Example: “Write a short story in the style of Edgar Allan Poe, set in a futuristic city.”

Analysis and Interpretation:

  • Tasks can require the LLM to analyze data or text and provide insights.
  • Example: “Analyze this customer feedback data and identify the top three areas for improvement.”

Dialogue Simulation:

  • LLMs can be tasked with simulating conversations for various purposes.
  • Example: “Simulate a customer service conversation addressing a product return request.”

Code Generation and Analysis:

  • Tasks can involve generating, reviewing, or explaining code.
  • Example: “Write a Python function to calculate the Fibonacci sequence and explain how it works.”

Task Decomposition:

  • LLMs can be used to break down complex tasks into smaller, manageable subtasks.
  • Example: “Break down the process of launching a new product into a list of essential tasks and subtasks.”

Implementation in CrewAI:

Task Definition: Tasks are defined with clear objectives, inputs, and expected outputs.

Agent Assignment: Tasks are assigned to appropriate agents based on their capabilities and the task requirements.

Execution: The assigned agent processes the task using the LLM’s capabilities, following any specified steps or conditions.

Output Generation: The agent produces the task output, which could be text, analysis, decisions, or other forms of information.

Validation: The task output may be validated or reviewed, either by another agent or through predefined criteria.

Integration: The task output is integrated into the larger workflow, potentially becoming input for subsequent tasks.

Iteration: Based on the outcome, new tasks might be generated or existing ones modified.

Challenges and Considerations:

  1. Task Clarity: Ensuring that tasks are defined clearly enough for the LLM to understand and execute accurately.
  2. Handling Ambiguity: Designing tasks that can cope with potential ambiguities or uncertainties in the input or context.
  3. Error Handling: Implementing mechanisms to detect and handle cases where the LLM misinterprets a task or produces incorrect outputs.
  4. Scalability: Managing the complexity of tasks and their interdependencies as projects grow in scale.
  5. Ethical Considerations: Ensuring that tasks are designed and executed in an ethically responsible manner, considering potential biases or misuse of the LLM’s capabilities.

By leveraging LLMs, CrewAI can handle a wide variety of complex tasks, making it a powerful framework for AI-driven project management and problem-solving across various domains.

from crewai import Task

task = Task(
description='Find and summarize the latest and most relevant news on AI',
agent=sales_agent
)

Creating a Task with Tools

import os
os.environ["OPENAI_API_KEY"] = "Your Key"
os.environ["SERPER_API_KEY"] = "Your Key" # serper.dev API key

from crewai import Agent, Task, Crew
from crewai_tools import SerperDevTool

research_agent = Agent(
role='Researcher',
goal='Find and summarize the latest AI news',
backstory="""You're a researcher at a large company.
You're responsible for analyzing data and providing insights
to the business.""",
verbose=True
)

search_tool = SerperDevTool()

task = Task(
description='Find and summarize the latest AI news',
expected_output='A bullet list summary of the top 5 most important AI news',
agent=research_agent,
tools=[search_tool]
)

crew = Crew(
agents=[research_agent],
tasks=[task],
verbose=2
)

result = crew.kickoff()
print(result)

Referring to Other Tasks

# ...

research_ai_task = Task(
description='Find and summarize the latest AI news',
expected_output='A bullet list summary of the top 5 most important AI news',
async_execution=True,
agent=research_agent,
tools=[search_tool]
)

research_ops_task = Task(
description='Find and summarize the latest AI Ops news',
expected_output='A bullet list summary of the top 5 most important AI Ops news',
async_execution=True,
agent=research_agent,
tools=[search_tool]
)

write_blog_task = Task(
description="Write a full blog post about the importance of AI and its latest news",
expected_output='Full blog post that is 4 paragraphs long',
agent=writer_agent,
context=[research_ai_task, research_ops_task]
)

#...

Asynchronous Execution

#...

list_ideas = Task(
description="List of 5 interesting ideas to explore for an article about AI.",
expected_output="Bullet point list of 5 ideas for an article.",
agent=researcher,
async_execution=True # Will be executed asynchronously
)

list_important_history = Task(
description="Research the history of AI and give me the 5 most important events.",
expected_output="Bullet point list of 5 important events.",
agent=researcher,
async_execution=True # Will be executed asynchronously
)

write_article = Task(
description="Write an article about AI, its history, and interesting ideas.",
expected_output="A 4 paragraph article about AI.",
agent=writer,
context=[list_ideas, list_important_history] # Will wait for the output of the two tasks to be completed
)

#...

Callback Mechanism

# ...

def callback_function(output: TaskOutput):
# Do something after the task is completed
# Example: Send an email to the manager
print(f"""
Task completed!
Task: {output.description}
Output: {output.raw_output}
""")

research_task = Task(
description='Find and summarize the latest AI news',
expected_output='A bullet list summary of the top 5 most important AI news',
agent=research_agent,
tools=[search_tool],
callback=callback_function
)

#...

Accessing a Specific Task Output

# ...
task1 = Task(
description='Find and summarize the latest AI news',
expected_output='A bullet list summary of the top 5 most important AI news',
agent=research_agent,
tools=[search_tool]
)

#...

crew = Crew(
agents=[research_agent],
tasks=[task1, task2, task3],
verbose=2
)

result = crew.kickoff()

# Returns a TaskOutput object with the description and results of the task
print(f"""
Task completed!
Task: {task1.output.description}
Output: {task1.output.raw_output}
""")

Tools in CrewAI:

Tools in CrewAI are external resources, functionalities, or APIs that agents can utilize to enhance their capabilities and complete tasks more effectively. They serve as extensions to the core abilities of the agents, allowing them to interact with external systems, process specialized data, or perform specific operations that may be beyond the native capabilities of the LLM.

Application of Tools with LLMs:

When integrated with Large Language Models, tools significantly expand the range of tasks that can be accomplished. Here’s how tools are typically applied in conjunction with LLMs:

Augmenting LLM Capabilities:

  • Tools provide specialized functions that complement the general knowledge and language processing abilities of LLMs.
  • Example: A calculator tool can perform precise mathematical operations that an LLM might struggle with.

Accessing External Data:

  • Tools can fetch real-time or specialized data that isn’t part of the LLM’s training.
  • Example: A weather API tool can provide current weather conditions for a specific location.

Performing Specific Actions:

  • Tools allow LLMs to interact with external systems or perform actions in the real world.
  • Example: An email tool can send emails based on the LLM’s generated content.

Enhancing Decision Making:

  • Tools can provide additional data or analysis to inform the LLM’s reasoning process.
  • Example: A sentiment analysis tool can provide insights that the LLM can use to generate more nuanced responses.

Key Features and Functionalities of Tools:

API Integration:

  • Tools often interface with external APIs to access specific services or data sources.
  • Functionality: Allows seamless integration with a wide range of web services and databases.

Data Processing:

  • Tools can handle various data formats and perform specialized data manipulations.
  • Functionality: Enables tasks like data cleaning, transformation, and analysis.

Visualization Generation:

  • Some tools can create charts, graphs, or other visual representations of data.
  • Functionality: Enhances the LLM’s ability to present information in a visually appealing manner.

Code Execution:

  • Tools may include the ability to execute code in various programming languages.
  • Functionality: Allows the LLM to test, debug, or run code snippets as part of its tasks.

Natural Language Understanding:

  • Specialized NLP tools can perform tasks like named entity recognition or sentiment analysis.
  • Functionality: Provides deeper linguistic insights to inform the LLM’s processing.

Search and Retrieval:

  • Tools can perform targeted searches across databases or the internet.
  • Functionality: Enables the LLM to access up-to-date or specialized information.

File Manipulation:

  • Tools for reading, writing, or modifying various file types.
  • Functionality: Allows the LLM to work with documents, spreadsheets, or other file formats.

Scheduling and Time Management:

  • Tools for managing calendars, setting reminders, or scheduling events.
  • Functionality: Enhances the LLM’s ability to handle time-based tasks and planning.

Translation and Localization:

  • Tools for translating text between languages or adapting content for different locales.
  • Functionality: Extends the LLM’s language capabilities beyond its training data.

Image and Audio Processing:

  • Tools for analyzing or generating images and audio.
  • Functionality: Allows the LLM to work with multimedia content more effectively.

Implementation in CrewAI:

Tool Definition: Each tool is defined with its specific functionalities, input requirements, and output formats.

Integration: Tools are integrated into the CrewAI framework, making them accessible to agents.

Invocation: Agents can call upon tools as needed during task execution.

Input Preparation: The LLM prepares the necessary inputs for the tool based on the task requirements.

Output Interpretation: The LLM interprets and incorporates the tool’s output into its overall task processing.

Error Handling: The system includes mechanisms to handle cases where tools fail or return unexpected results.

Key Considerations:

  1. Tool Selection: Choosing the right set of tools that complement the LLM’s capabilities and meet project requirements.
  2. Seamless Integration: Ensuring smooth interaction between the LLM and various tools.
  3. Security and Privacy: Managing access controls and data handling when using external tools.
  4. Performance Optimization: Balancing the use of tools with the LLM’s processing to maintain efficient operation.
  5. Versioning and Updates: Keeping tools up-to-date and managing compatibility with the LLM and overall system.

By leveraging a diverse set of tools, CrewAI significantly expands the capabilities of LLM-based agents, allowing them to handle a wider range of complex, real-world tasks across various domains.

pip install 'crewai[tools]'

import os
from crewai import Agent, Task, Crew
# Importing crewAI tools
from crewai_tools import (
DirectoryReadTool,
FileReadTool,
SerperDevTool,
WebsiteSearchTool
)

# Set up API keys
os.environ["SERPER_API_KEY"] = "Your Key" # serper.dev API key
os.environ["OPENAI_API_KEY"] = "Your Key"

# Instantiate tools
docs_tool = DirectoryReadTool(directory='./blog-posts')
file_tool = FileReadTool()
search_tool = SerperDevTool()
web_rag_tool = WebsiteSearchTool()

# Create agents
researcher = Agent(
role='Market Research Analyst',
goal='Provide up-to-date market analysis of the AI industry',
backstory='An expert analyst with a keen eye for market trends.',
tools=[search_tool, web_rag_tool],
verbose=True
)

writer = Agent(
role='Content Writer',
goal='Craft engaging blog posts about the AI industry',
backstory='A skilled writer with a passion for technology.',
tools=[docs_tool, file_tool],
verbose=True
)

# Define tasks
research = Task(
description='Research the latest trends in the AI industry and provide a summary.',
expected_output='A summary of the top 3 trending developments in the AI industry with a unique perspective on their significance.',
agent=researcher
)

write = Task(
description='Write an engaging blog post about the AI industry, based on the research analyst’s summary. Draw inspiration from the latest blog posts in the directory.',
expected_output='A 4-paragraph blog post formatted in markdown with engaging, informative, and accessible content, avoiding complex jargon.',
agent=writer,
output_file='blog-posts/new_post.md' # The final blog post will be saved here
)

# Assemble a crew
crew = Crew(
agents=[researcher, writer],
tasks=[research, write],
verbose=2
)

# Execute tasks
crew.kickoff()

Processes in CrewAI:

Processes in CrewAI define the workflow and sequence of tasks within a project. They provide a structured approach to organizing and executing complex work that involves multiple agents and tasks.

Key Functionalities of Processes:

Workflow Definition:

  • Processes outline the sequence of tasks and their dependencies.
  • They define how work flows from one agent or task to another.

Task Orchestration:

  • Processes manage the assignment and execution of tasks among different agents.
  • They ensure that tasks are performed in the correct order and by the appropriate agents.

Information Flow Management:

  • Processes control how information is passed between tasks and agents.
  • They ensure that outputs from one task are properly used as inputs for subsequent tasks.

Decision Points:

  • Processes can include conditional logic and decision points that determine the flow of work based on certain criteria or outcomes.

Parallel and Sequential Execution:

  • Processes can define which tasks can be executed in parallel and which must be done sequentially.

Error Handling and Recovery:

  • Processes include mechanisms for dealing with errors or unexpected outcomes at various stages of the workflow.

Iteration and Looping:

  • Processes can include iterative steps or loops for repetitive tasks or refinement of outputs.

Monitoring and Reporting:

  • Processes often include checkpoints for monitoring progress and generating reports on the status of the workflow.

Application of Processes with LLMs:

When implemented with Large Language Models, processes in CrewAI take on specific characteristics and applications:

Dynamic Workflow Generation:

  • LLMs can be used to generate or modify process workflows based on high-level project descriptions.
  • Example: “Create a process for developing a marketing strategy for a new product launch.”

Natural Language Process Definition:

  • Processes can be defined using natural language, which the LLM interprets and translates into executable workflows.
  • Example: “First, analyze market trends. Then, identify target demographics. Finally, develop messaging strategies.”

Adaptive Task Assignment:

  • LLMs can dynamically assign tasks to agents based on the current context and agent capabilities.
  • Example: Analyzing the complexity of a writing task and assigning it to the most suitable agent.

Intelligent Decision Making:

  • LLMs can make informed decisions at process branch points based on complex criteria.
  • Example: Deciding whether to proceed with a product development phase based on market research results.

Context-Aware Task Execution:

  • LLMs ensure that each task is executed with full awareness of the broader process context.
  • Example: Writing a conclusion for a report while considering all previously generated sections.

Dynamic Sub-Process Generation:

  • LLMs can create detailed sub-processes for complex tasks as needed.
  • Example: Breaking down a “Create Marketing Campaign” task into multiple sub-tasks with their own workflows.

Process Optimization:

  • LLMs can analyze process performance and suggest optimizations.
  • Example: Identifying bottlenecks in the workflow and proposing alternative task sequences.

Natural Language Interfacing:

  • Users can interact with and modify processes using natural language commands, which the LLM interprets.
  • Example: “Add a review step after the content creation task.”

Cross-Process Coordination:

  • LLMs can manage dependencies and information flow between multiple parallel processes.
  • Example: Coordinating product development, marketing, and sales processes for a new product launch.

Exception Handling:

  • LLMs can generate appropriate responses to unexpected situations or errors in the process.
  • Example: Formulating a recovery plan if a critical task fails.

Implementation in CrewAI:

Process Definition:

Processes are defined either through structured formats (like YAML or JSON) or through natural language instructions interpreted by the LLM.

Task Decomposition:

The LLM breaks down high-level processes into specific, actionable tasks.

Agent Assignment:

The system, guided by the LLM, assigns tasks to appropriate agents based on their capabilities and the task requirements.

Execution Management:

The LLM oversees the execution of the process, ensuring tasks are completed in the correct order and managing the flow of information between tasks.

Adaptive Control:

The LLM continuously monitors the process, making real-time decisions on task execution, resource allocation, and workflow adjustments.

Output Integration:

The LLM ensures that outputs from each task are properly integrated into the overall process and used effectively in subsequent tasks.

Reporting and Communication:

The LLM generates status updates, progress reports, and communicates important information to relevant stakeholders throughout the process.

Key Considerations:

  1. Flexibility vs. Structure: Balancing the need for structured workflows with the flexibility to adapt to changing circumstances.
  2. Scalability: Ensuring processes can scale effectively for both simple and highly complex projects.
  3. Interpretability: Making sure processes remain understandable and manageable, even as they grow in complexity.
  4. Error Resilience: Designing processes that can gracefully handle unexpected inputs, errors, or changing conditions.
  5. Ethical Considerations: Ensuring that processes adhere to ethical guidelines and don’t inadvertently introduce biases or undesirable outcomes.

By leveraging LLMs, processes in CrewAI become more dynamic, adaptive, and capable of handling complex, multi-stage projects across various domains. The integration of LLMs allows for more natural interaction with processes, more intelligent decision-making within workflows, and greater flexibility in handling diverse and evolving project requirements.

from crewai import Crew
from crewai.process import Process
from langchain_openai import ChatOpenAI

# Example: Creating a crew with a sequential process
crew = Crew(
agents=my_agents,
tasks=my_tasks,
process=Process.sequential
)

# Example: Creating a crew with a hierarchical process
# Ensure to provide a manager_llm or manager_agent
crew = Crew(
agents=my_agents,
tasks=my_tasks,
process=Process.hierarchical,
manager_llm=ChatOpenAI(model="gpt-4")
# or
# manager_agent=my_manager_agent
)

Note: Ensure my_agents and my_tasks are defined prior to creating a Crew object, and for the hierarchical process, either manager_llm or manager_agent is also required.

Sequential Process

This approach follows a methodical and deliberate path through tasks, emulating dynamic team workflows. Task execution follows the predefined order in the task list, with the output of one task serving as context for the next.

To customize task context, utilize the context parameter in the Task class to specify outputs that should be used as context for subsequent tasks.

Hierarchical Process

Emulates a corporate hierarchy, CrewAI allows specifying a custom manager agent or automatically creates one, requiring the specification of a manager language model (manager_llm). This agent oversees task execution, including planning, delegation, and validation. Tasks are not pre-assigned; the manager allocates tasks to agents based on their capabilities, reviews outputs, and assesses task completion.

Process Class: Detailed Overview

The Process class is implemented as an enumeration (Enum), ensuring type safety and restricting process values to the defined types (sequential, hierarchical). The consensual process is planned for future inclusion, emphasizing our commitment to continuous development and innovation.

Additional Task Features

  • Asynchronous Execution: Tasks can now be executed asynchronously, allowing for parallel processing and efficiency improvements. This feature is designed to enable tasks to be carried out concurrently, enhancing the overall productivity of the crew.
  • Human Input Review: An optional feature that enables the review of task outputs by humans to ensure quality and accuracy before finalization. This additional step introduces a layer of oversight, providing an opportunity for human intervention and validation.
  • Output Customization: Tasks support various output formats, including JSON (output_json), Pydantic models (output_pydantic), and file outputs (output_file), providing flexibility in how task results are captured and utilized. This allows for a wide range of output possibilities, catering to different needs and requirements.

Crews in CrewAI:

A Crew in CrewAI is a coordinated group of AI agents working together to achieve a common goal or complete a complex project. Think of a Crew as a team of specialized AI workers, each bringing unique skills and capabilities to the table.

Key Functionalities of Crews:

Collaborative Problem Solving:

  • Crews enable multiple agents to work together on complex tasks that require diverse skills.
  • They can tackle multifaceted problems by leveraging the combined expertise of various agents.

Role-based Task Distribution:

  • Crews assign tasks to agents based on their specific roles and capabilities.
  • This allows for efficient division of labor and specialization within the team.

Information Sharing:

  • Crews facilitate the exchange of information and insights between agents.
  • This enables more comprehensive analysis and decision-making.

Hierarchical Organization:

  • Crews can be structured with different levels of authority or specialization.
  • This allows for management-like agents to oversee and coordinate the work of other agents.

Adaptive Team Composition:

  • Crews can be dynamically formed or adjusted based on the specific requirements of a project.
  • This allows for flexibility in addressing varying task complexities.

Parallel Processing:

  • Crews enable multiple agents to work on different aspects of a project simultaneously.
  • This can significantly speed up the completion of complex tasks.

Quality Control and Peer Review:

  • Crews can implement internal review processes where agents check and validate each other’s work.
  • This helps in maintaining high standards of output quality.

Scalability:

  • Crews can be scaled up or down depending on the project size and complexity.
  • This allows for efficient resource allocation across different projects.

Application of Crews with LLMs:

When implemented with Large Language Models, Crews in CrewAI take on specific characteristics and applications:

Specialized Agent Roles:

  • Different LLM instances can be fine-tuned or prompted to take on specialized roles within the Crew.
  • Example: A Crew for content creation might include agents specialized in research, writing, editing, and fact-checking.

Multi-Perspective Analysis:

  • Multiple LLM agents can analyze the same information from different angles or expertise areas.
  • Example: A financial report being analyzed by agents with expertise in economics, market trends, and risk assessment.

Iterative Refinement:

  • Crews can implement iterative processes where outputs are passed between agents for progressive refinement.
  • Example: A draft article being passed between a writer agent, an editor agent, and a fact-checker agent.

Complex Task Decomposition:

  • A manager-like LLM agent can break down complex tasks and distribute subtasks to specialized agents.
  • Example: Decomposing a product launch strategy into market research, product development, marketing, and sales tasks.

Dynamic Dialogue and Debate:

  • LLM agents within a Crew can engage in dialogue, debate, or brainstorming sessions.
  • Example: Agents with different perspectives discussing the pros and cons of a business strategy.

Cross-Domain Integration:

  • Crews can bring together LLM agents with expertise in different domains to work on interdisciplinary projects.
  • Example: Combining agents specialized in technology, design, and user experience for a software development project.

Adaptive Learning and Improvement:

  • Crews can implement feedback loops where agents learn from each other’s outputs and improve their performance over time.
  • Example: An agent specialized in writing learning from the edits made by an editor agent.

Ethical and Bias Checking:

  • Dedicated LLM agents within a Crew can be responsible for checking outputs for ethical concerns or biases.
  • Example: An ethics agent reviewing the decisions made by other agents in a policy-making task.

Natural Language Coordination:

  • A coordinator LLM agent can manage the Crew using natural language commands and updates.
  • Example: “Team, we need to pivot our strategy based on the latest market analysis. Marketing team, please update the messaging accordingly.”

Multi-Modal Task Handling:

  • Different agents within a Crew can handle different types of inputs or outputs (text, code, data analysis).
  • Example: A Crew working on a website where one agent handles content writing, another does SEO optimization, and a third generates HTML code.

Implementation in CrewAI:

Crew Assembly:

Define the roles needed for a project and assemble a Crew of LLM agents with appropriate specializations.

Task Distribution:

Assign tasks to agents based on their roles and capabilities.

Communication Protocols:

Establish how agents will share information and outputs within the Crew.

Workflow Management:

Define the sequence of operations and how work will flow between agents.

Output Integration:

Implement mechanisms to combine or synthesize outputs from multiple agents into cohesive final products.

Performance Monitoring:

Track the performance of individual agents and the Crew as a whole, making adjustments as needed.

Conflict Resolution:

Implement protocols for resolving conflicting outputs or recommendations from different agents.

Key Considerations:

  1. Coherence: Ensuring that the outputs from different agents within a Crew are consistent and coherent when combined.
  2. Efficiency: Balancing the benefits of multiple specialized agents against the computational cost and complexity.
  3. Robustness: Designing Crews that can handle unexpected situations or failures of individual agents.
  4. Scalability: Ensuring that Crew structures can scale effectively for projects of varying sizes and complexities.
  5. Ethical Alignment: Maintaining consistent ethical standards across all agents within a Crew.

By leveraging LLMs in Crew structures, CrewAI can tackle highly complex, multi-faceted projects that require diverse expertise and collaborative problem-solving. This approach allows for more sophisticated AI systems that can handle real-world tasks with greater nuance, adaptability, and effectiveness.

from crewai import Crew, Agent, Task, Process
from langchain_community.tools import DuckDuckGoSearchRun

# Define agents with specific roles and tools
researcher = Agent(
role='Senior Research Analyst',
goal='Discover innovative AI technologies',
backstory="""You're a senior research analyst at a large company.
You're responsible for analyzing data and providing insights
to the business.
You're currently working on a project to analyze the
trends and innovations in the space of artificial intelligence.""",
tools=[DuckDuckGoSearchRun()]
)

writer = Agent(
role='Content Writer',
goal='Write engaging articles on AI discoveries',
backstory="""You're a senior writer at a large company.
You're responsible for creating content to the business.
You're currently working on a project to write about trends
and innovations in the space of AI for your next meeting.""",
verbose=True
)

# Create tasks for the agents
research_task = Task(
description='Identify breakthrough AI technologies',
agent=researcher,
expected_output='A bullet list summary of the top 5 most important AI news'
)
write_article_task = Task(
description='Draft an article on the latest AI technologies',
agent=writer,
expected_output='3 paragraph blog post on the latest AI technologies'
)

# Assemble the crew with a sequential process
my_crew = Crew(
agents=[researcher, writer],
tasks=[research_task, write_article_task],
process=Process.sequential,
full_output=True,
verbose=True,
)

Memory in CrewAI:

Memory in CrewAI refers to the system’s ability to store, retrieve, and utilize information across tasks, agents, and time. It’s a crucial component that allows for continuity, learning, and context-awareness in AI operations.

Key Functionalities of Memory:

Information Retention:

  • Stores important data, insights, and outcomes from previous tasks and interactions.
  • Allows for the accumulation of knowledge over time.

Context Preservation:

  • Maintains the context of ongoing projects or conversations.
  • Enables agents to pick up where they left off in multi-step processes.

Learning and Adaptation:

  • Facilitates improvement in agent performance based on past experiences.
  • Allows the system to adapt to user preferences and project-specific nuances.

Cross-Task Information Sharing:

  • Enables information transfer between different tasks and agents.
  • Supports holistic problem-solving by leveraging insights from various sources.

Historical Record Keeping:

  • Maintains a log of actions, decisions, and outcomes.
  • Supports auditing, explanation, and improvement of AI processes.

Pattern Recognition:

  • Identifies recurring patterns or trends across multiple tasks or projects.
  • Informs future decision-making and strategy formulation.

Types of Memory in CrewAI:

Short-Term Memory:

  • Holds recent information relevant to the current task or conversation.
  • Typically volatile and refreshed frequently.

Long-Term Memory:

  • Stores important information for extended periods.
  • Used for retaining knowledge, rules, and significant insights.

Episodic Memory:

  • Records specific events, experiences, or task executions.
  • Useful for learning from past successes or failures.

Semantic Memory:

  • Stores general knowledge and facts.
  • Supports broader understanding and contextual awareness.

Procedural Memory:

  • Retains information about how to perform specific tasks or processes.
  • Enhances efficiency in repetitive or structured operations.

Application of Memory with LLMs:

Contextual Conversation:

  • LLMs use short-term memory to maintain context in multi-turn conversations.
  • Example: Remembering details from earlier in a conversation to provide coherent responses.

Knowledge Accumulation:

  • Long-term memory allows LLMs to accumulate project-specific knowledge over time.
  • Example: Retaining important facts about a company’s products across multiple marketing tasks.

Personalization:

  • Memory enables LLMs to adapt to user preferences and communication styles.
  • Example: Remembering a user’s preferred level of detail in explanations.

Task Continuity:

  • Episodic memory allows LLMs to resume tasks effectively after interruptions.
  • Example: Picking up a complex analysis task where it left off, even after system restarts.

Cross-Project Learning:

  • Semantic memory allows insights from one project to inform work on another.
  • Example: Applying successful strategies from a past marketing campaign to a new one.

Procedural Optimization:

  • Procedural memory helps LLMs refine and optimize their approach to recurring task types.
  • Example: Improving the efficiency of code generation based on past experiences.

Error Prevention:

  • Memory of past mistakes helps LLMs avoid repeating errors.
  • Example: Remembering and avoiding a particular logical fallacy in argument construction.

Consistency Maintenance:

  • Long-term memory ensures consistency in information provided across multiple interactions.
  • Example: Maintaining consistent details about a fictional world in a storytelling task.

Dynamic Prompting:

  • Memory informs the dynamic generation of prompts for LLMs based on accumulated context.
  • Example: Crafting increasingly specific prompts as a project progresses.

Collaborative Synergy:

  • Shared memory among LLM agents in a crew enhances collaborative problem-solving.
  • Example: One agent building upon the insights generated by another in a research task.

Implementation in CrewAI:

Memory Architecture:

Design a structured system for storing different types of information (e.g., key-value stores, vector databases).

Information Extraction:

Implement mechanisms for LLMs to identify and extract important information for storage.

Retrieval Mechanisms:

Develop efficient methods for LLMs to query and retrieve relevant information from memory.

Memory Update Protocols:

Establish rules for when and how to update stored information.

Context Integration:

Implement systems to seamlessly integrate memory-retrieved information into ongoing tasks.

Memory Pruning:

Develop strategies to manage memory capacity, removing outdated or less relevant information.

Cross-Agent Memory Sharing:

Implement protocols for sharing relevant memories between different agents in a crew.

Key Considerations:

  1. Privacy and Security: Ensuring sensitive information is handled appropriately and securely.
  2. Scalability: Designing memory systems that can handle growing amounts of information efficiently.
  3. Relevance: Implementing mechanisms to retrieve the most relevant information for each task.
  4. Bias Management: Being aware of and mitigating potential biases that might be reinforced through memory.
  5. Forgetting Mechanisms: Implementing controlled “forgetting” to prevent outdated information from influencing current tasks.
  6. Ethical Use: Ensuring that the use of stored information adheres to ethical guidelines and user expectations.

By effectively implementing memory systems, CrewAI can create more intelligent, adaptive, and context-aware AI systems. This allows for more sophisticated problem-solving, improved user experiences, and the ability to tackle complex, long-term projects with greater effectiveness and continuity.

from crewai import Crew, Agent, Task, Process

# Assemble your crew with memory capabilities
my_crew = Crew(
agents=[...],
tasks=[...],
process=Process.sequential,
memory=True,
verbose=True
)

For implementation with Google Gemma API refer the following article

https://medium.com/@danushidk507/automating-ai-research-and-content-creation-building-a-smart-pipeline-with-crewai-and-googles-d9935222eafe

--

--

DhanushKumar

Data Science || Machine Learning ||Deep Learning|| Language Models || GenAI contact: danushidk507@gmail.com