CrewAI Official Website: https://www.crewai.comGithub: https://github.com/joaomdmoura/crewAIChinese Documentation: http://www.aidoczh.com/docs/crewai/
1. Introduction2. Detailed Explanation of Core Concepts in CrewAI2.1 Agent2.1.1 Definition and Function of Agents2.1.2 Agent Attributes2.1.3 Creating Agents2.2 Tasks2.2.1 Task Definition and Collaboration2.2.2 Task Attributes2.2.3 Creating Tasks2.2.4 Creating Tasks Using Tools2.2.5 Task Dependencies2.2.6 Asynchronous Execution2.2.7 Callback Mechanism2.2.8 Accessing Specific Task Outputs2.2.9 Tool Overriding Mechanism2.2.10 Error Handling and Validation Mechanisms2.3 Tools2.3.1 Role and Types of Tools2.3.2 Key Features of Tools2.3.3 Using CrewAI Tools2.3.3 Available CrewAI Tools2.3.4 Creating Your Own Tools2.3.5 Using LangChain Tools2.4 Processes2.4.1 Process Implementation Methods2.4.2 Role of Processes in Team Collaboration2.4.3 Assigning Processes to Managers2.4.4 Additional Task Features2.5 Teams2.5.1 Team Attributes2.5.2 Example of Creating a Team2.5.3 Team Execution Process2.6 Memory2.6.1 Components of the Memory System2.6.2 How the Memory System Empowers Agents2.6.3 Implementing Memory in Teams2.6.4 Benefits of Using CrewAI Memory System3. Practical Steps for Building Multi-Agent Systems Based on CrewAI3.1 Preparation3.1.1 Environment Configuration3.1.2 Installing CrewAI and Ollama (Optional)3.1.3 Setting Up the Model3.2 Creating Agents3.2.1 Defining Agent Roles and Goals3.2.2 Configuring Agent Attributes3.2.3 Writing Agent Background Stories (Optional but Recommended)3.3 Defining Tasks3.3.1 Clearly Describing Tasks3.3.2 Assigning Task Agents3.3.3 Configuring Task Attributes3.4 Integrating Tools3.4.1 Choosing Suitable Tools3.4.2 Integrating Tools with Agents and Tasks3.5 Building Teams3.5.1 Combining Agents3.5.2 Defining Team Processes3.5.3 Configuring Team Attributes3.6 Executing Tasks and Monitoring3.6.1 Starting Team Task Execution3.6.2 Monitoring Task Execution Process3.6.3 Handling Task Execution Results4. Case Studies: Applications of CrewAI in Real Projects4.1 Smart Customer Service System4.1.1 System Architecture and Agent Roles4.1.2 Task Flow and Collaboration4.2 Smart Content Creation Assistant4.2.1 Agent Functions and Division of Labor4.2.2 Creation Process and Collaboration Mechanism4.3 Smart Investment Decision-Making System4.3.1 System Composition and Agent Responsibilities4.3.2 Decision-Making Process and Collaboration Model
1. Introduction
In the field of artificial intelligence, Multi-Agent systems are gradually becoming key technologies for solving complex problems and achieving efficient collaboration. CrewAI, as a powerful multi-Agent collaboration tool, provides developers with a convenient way to build intelligent collaborative systems. This article will detail how to build a Multi-Agent system based on CrewAI.
2. Detailed Explanation of Core Concepts in CrewAI
2.1 Agent
2.1.1 Definition and Function of Agents
An Agent is an autonomous unit in CrewAI, capable of executing tasks, making decisions, and communicating with other agents. They are like members of a team, each taking on specific roles, such as researcher, writer, or customer support, contributing to the team’s goals.
2.1.2 Agent Attributes
-
Role: Clearly defines the function of the agent in the team, determining the types of tasks it is best suited to perform. For instance, agents with a researcher role excel at collecting and analyzing information, while writers focus on content creation.
-
Goal: Guides the decision-making process of the agent, representing the individual goal the agent strives to achieve. For example, the goal of a data analyst agent might be to extract actionable insights to support business decisions.
-
Backstory: Provides rich background information for the agent’s role and goals, enhancing the dynamics of interaction and collaboration. For example, the backstory for a data analyst might be, “You are a data analyst at a large company, responsible for analyzing data and providing insights for the business. You are currently working on a project analyzing the performance of our marketing campaigns.”
-
LLM (Optional): Represents the language model running the agent. If not specified, it defaults to the model name retrieved from the
OPENAI_MODEL_NAME
environment variable, otherwise defaults to “gpt-4”. Developers can choose the appropriate language model based on their needs to meet the processing requirements of different tasks. -
Tools (Optional): A set of capabilities or functionalities that the agent can use, typically custom class instances compatible with the execution environment, with a default value of an empty list. These tools can extend the agent’s capabilities, enabling it to perform operations such as web searches and data analysis.
-
LLM for Function Calling (Optional): Specifies the language model for processing tool calls of the agent. If passed, it overrides the team function calling LLM, with a default value of
None
. By flexibly configuring this attribute, developers can control the tool calling behavior of the agent more precisely. -
Max Iterations (Optional): The maximum number of iterations the agent can perform before being forced to provide the best answer, with a default value of 25. Properly setting this parameter helps control the depth and efficiency of task execution.
-
Max Requests (Optional): The maximum number of requests the agent can perform per minute, used to avoid rate limits, with a default value of
None
. Developers can set this based on actual conditions to ensure stable system operation. -
Max Execution Time (Optional): The maximum duration for which the agent can execute a task, with a default value of
None
, indicating no maximum execution time limit. This attribute can control the execution period of tasks to prevent long-term resource occupation. -
Verbose Mode (Optional): Setting this to
True
configures the internal logger to provide detailed execution logs for easier debugging and monitoring, with a default value ofFalse
. Enabling verbose mode during development and optimization helps quickly locate issues. -
Allow Delegation (Optional): Agents can delegate tasks or issues to each other, ensuring tasks are handled by the most suitable agent, with a default value of
True
. This feature promotes flexible collaboration within the team, enhancing task handling accuracy and efficiency. -
Step Callback (Optional): A function called after each step of the agent, which can be used to log operations or perform other actions, overriding the team
step_callback
. Developers can use this callback function to implement custom monitoring and handling logic. -
Cache (Optional): Indicates whether the agent should use caching for tool usage, with a default value of
True
. Caching mechanisms help improve task execution efficiency and reduce redundant calculations.
2.1.3 Creating Agents
Agents can interact with each other using CrewAI’s built-in delegation and communication mechanisms. This allows for dynamic task management and problem-solving within the team.
To create an agent, you typically initialize an instance of the Agent
class with the desired attributes. Here is a conceptual example including all attributes:
# Example: Creating an agent with all attributesfrom crewai import Agent
agent = Agent( role='Data Analyst', goal='Extract actionable insights', backstory="""You are a data analyst at a large company. You are responsible for analyzing data and providing insights for the business. You are currently working on a project analyzing the performance of our marketing campaigns.""", tools=[my_tool1, my_tool2], # Optional, defaults to empty list llm=my_llm, # Optional function_calling_llm=my_llm, # Optional max_iter=15, # Optional max_rpm=None, # Optional verbose=True, # Optional allow_delegation=True, # Optional step_callback=my_intermediate_step_callback, # Optional cache=True # Optional)
2.2 Tasks
2.2.1 Task Definition and Collaboration
In the CrewAI framework, a task is the specific work that agents need to complete, containing various details required for execution, such as a clear description, the agent responsible for execution, and the tools needed. Tasks can be collaborative, managing attributes and orchestrating processes to enable multiple agents to work together and enhance team cooperation efficiency.
2.2.2 Task Attributes
-
Description: A clear and concise statement of the task content, allowing agents to understand what needs to be done. For example, “Find and summarize the latest and most relevant news about artificial intelligence.”
-
Agent: The agent responsible for executing the task, which can be specified directly or assigned by Crew’s processes based on role, availability, and other factors. This ensures that the task can find a suitable executor.
-
Expected Output: A detailed description of what is expected after the task is completed, making it clear to the agent what the task’s goals and expected results are. For example, “Summarize the top 5 most important AI news items in a bulleted list.”
-
Tools (Optional): Functionalities or capabilities used by agents to execute tasks, enhancing task performance and agent interaction. By selecting appropriate tools, agents can complete tasks more efficiently.
-
Asynchronous Execution (Optional): When set, the task will execute asynchronously, allowing the continuation of other tasks without waiting for completion, suitable for long-running tasks or tasks that do not significantly impact subsequent task execution.
-
Context (Optional): Specifies the task whose output serves as context for this task, clarifying dependencies between tasks and enabling effective transmission and utilization of task outputs.
-
Configuration (Optional): Additional configuration details for the agent executing the task, providing further customization flexibility for task execution.
-
Output JSON (Optional): Output JSON object, requiring the OpenAI client, and can only set one output format. This property facilitates data interaction and integration with other systems.
-
Output Pydantic (Optional): Output Pydantic model object, also requiring the OpenAI client, and can only set one output format. Developers can choose the appropriate output format based on actual needs.
-
Output File (Optional): Saves the task output to a file, which, when used with
Output JSON
orOutput Pydantic
, can specify how to save the output. This helps with data persistence and subsequent analysis. -
Callback (Optional): A Python callable function executed after the task is completed, which can be used to trigger operations or notifications based on task results, such as sending emails.
-
Human Input (Optional): Indicates whether the task requires final human feedback, which is very useful for tasks that need human supervision to ensure the accuracy and reliability of task results.
2.2.3 Creating Tasks
Creating a task involves defining its scope, the responsible agent, and any additional attributes for flexibility:
from crewai import Task
task = Task( description='Find and summarize the latest and most relevant news about artificial intelligence', agent=sales_agent)
“Task Assignment”: Directly assign a <span>agent</span>
for assignment, or allow CrewAI’s hierarchical processes to decide based on role, availability, etc.
2.2.4 Creating Tasks Using 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 are a researcher at a large company. You are responsible for analyzing data and providing insights for the business.""", verbose=True)
search_tool = SerperDevTool()
task = Task( description='Find and summarize the latest AI news', expected_output='Summarize the top 5 most important AI news in a bulleted list', agent=research_agent, tools=[search_tool])
crew = Crew( agents=[research_agent], tasks=[task], verbose=2)
result = crew.kickoff()
print(result)
2.2.5 Task Dependencies
In CrewAI, the output of one task is automatically passed to the next task, which is very useful when one task depends on the output of another task but is not executed immediately after it. This is accomplished through the task’s context
attribute:
# ...
research_ai_task = Task( description='Find and summarize the latest AI news', expected_output='Summarize the top 5 most important AI news in a bulleted list', async_execution=True, agent=research_agent, tools=[search_tool])
research_ops_task = Task( description='Find and summarize the latest AI operational news', expected_output='Summarize the top 5 most important AI operational news in a bulleted list', async_execution=True, agent=research_agent, tools=[search_tool])
write_blog_task = Task( description="Write a complete blog post about the importance of AI and its latest news", expected_output='A complete blog post of 4 paragraphs', agent=writer_agent, context=[research_ai_task, research_ops_task])
# ...
2.2.6 Asynchronous Execution
Tasks executed asynchronously mean that Crew does not wait for completion before proceeding to the next task. This is very useful for tasks that take a long time to complete or tasks that are not critical to the execution of the next task. The context
attribute can define this in future tasks, which should wait for the asynchronous task’s output to complete.
#...
list_ideas = Task( description="Explore 5 interesting ideas for articles about AI.", expected_output="A bulleted list of 5 article ideas.", agent=researcher, async_execution=True # Will execute asynchronously)
list_important_history = Task( description="Research the history of AI and tell me 5 important events.", expected_output="A bulleted list of 5 important events.", agent=researcher, async_execution=True # Will execute 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 completion of two tasks)
#...
2.2.7 Callback Mechanism
A callback function is executed after the task is completed, allowing for operations or notifications to be triggered based on the task results.
# ...
def callback_function(output: TaskOutput): # Perform certain operations after task completion # For 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='Summarize the top 5 most important AI news in a bulleted list', agent=research_agent, tools=[search_tool], callback=callback_function)
2.2.8 Accessing Specific Task Outputs
After a set of Crew runs, specific task outputs can be accessed using the task object’s output
attribute:
# ...
task1 = Task( description='Find and summarize the latest AI news', expected_output='Summarize the top 5 most important AI news in a bulleted list', agent=research_agent, tools=[search_tool])
#...
crew = Crew( agents=[research_agent], tasks=[task1, task2, task3], verbose=2)
result = crew.kickoff()
# Returns a TaskOutput object containing the task's description and resultprint(f""" Task completed! Task: {task1.output.description} Output: {task1.output.raw_output}""")
2.2.9 Tool Overriding Mechanism
Specifying tools in a task allows for dynamic adjustments to agent capabilities, highlighting the flexibility of CrewAI.
2.2.10 Error Handling and Validation Mechanisms
During the creation and execution of tasks, certain validation mechanisms exist to ensure the robustness and reliability of task attributes. These validations include, but are not limited to:
-
Ensuring that each task sets only one output type to maintain clear output expectations.
-
Preventing manual assignment of the
id
attribute to maintain the integrity of the unique identifier system.
These validations help maintain consistency and reliability in task execution within the CrewAI framework.
2.3 Tools
2.3.1 Role and Types of Tools
CrewAI tools empower agents with various capabilities, including web searches, data analysis, content generation, and task delegation, enabling agents to perform a range of complex operations, spanning from simple searches to complex interactions and effective teamwork.
2.3.2 Key Features of Tools
-
Utility: Designed for tasks, covering functional needs across multiple domains, such as web searches, data analysis, content generation, etc., meeting the work needs of agents in different scenarios.
-
Integration: Seamlessly integrates into workflows, closely cooperating with agents and tasks, enhancing the overall capabilities of agents for efficient collaboration.
-
Customizability: Provides flexibility, allowing developers to either develop custom tools to meet specific needs or utilize existing tools for personalized configurations based on the actual needs of agents.
-
Error Handling: Equipped with robust error handling mechanisms, ensuring that tools can handle exceptions gracefully during operation, maintaining smooth system operation.
-
Caching Mechanism: Features intelligent caching capabilities to optimize performance and reduce redundant operations. Developers can also fine-tune the caching mechanism through the
cache_function
attribute to further improve efficiency.
2.3.3 Using CrewAI Tools
To enhance agent capabilities through CrewAI tools, additional tool packages need to be installed:
pip install 'crewai[tools]'
Here is a usage example:
import os
from crewai import Agent, Task, Crew
# Import CrewAI tools from crewai_tools import ( DirectoryReadTool, FileReadTool, SerperDevTool, WebsiteSearchTool)# Set API keysos.environ["SERPER_API_KEY"] = "Your Key" # serper.dev API keyos.environ["OPENAI_API_KEY"] = "Your Key"
# Instantiate toolsdocs_tool = DirectoryReadTool(directory='./blog-posts')file_tool = FileReadTool()search_tool = SerperDevTool()web_rag_tool = WebsiteSearchTool()
# Create agentsresearcher = Agent( role='Market Research Analyst', goal='Provide the latest market analysis on the AI industry', backstory='An expert analyst with keen insight into market trends.', tools=[search_tool, web_rag_tool], verbose=True)
writer = Agent( role='Content Writer', goal='Write engaging blog posts about the AI industry', backstory='A skilled writer passionate about technology.', tools=[docs_tool, file_tool], verbose=True)# Define tasksresearch = Task( description='Research the latest trends in the AI industry and provide a summary.', expected_output='Summary of the top three hot developments in the AI industry, providing unique perspectives on their importance.', agent=researcher)write = Task( description='Write an engaging blog post based on the research analyst's summary. Draw inspiration from the latest blog posts in the directory.', expected_output='A four-paragraph blog post formatted in markdown, engaging, informative, and easy to understand, avoiding complex terminology.', agent=writer, output_file='blog-posts/new_post.md' # The final blog post will be saved here)
# Assemble the crewcrew = Crew( agents=[researcher, writer], tasks=[research, write], verbose=2)
# Execute taskscrew.kickoff()
2.3.3 Available CrewAI Tools
CrewAI provides a rich set of tools, such as<span>CodeDocsSearchTool</span>
(a RAG tool for searching code documentation and related technical documents),<span>CSVSearchTool</span>
(a RAG tool for searching in CSV files, handling structured data),<span>DirectorySearchTool</span>
(for directory searches, browsing the file system), etc. Each tool has its specific uses and advantages, allowing selection based on task requirements.
2.3.4 Creating Your Own Tools
First, you need to install the tool package:
pip install 'crewai[tools]'
-
Subclassing
<span>BaseTool</span>
: By inheriting from theBaseTool
class, developers can create custom tools. The tool’s name, description, and implementation of the_run
method defining the tool’s specific functionality must be defined. For example:
from crewai_tools import BaseTool
class MyCustomTool(BaseTool): name: str = "My Tool Name" description: str = "Clearly describe what this tool is used for; your agent will need this information to use it." def _run(self, argument: str) -> str: # Implement logic here return "Result from custom tool"
-
Using the
<span>tool</span>
decorator: Thetool
decorator allows for a more concise creation of custom tools. Simply define a function and add the@tool
decorator above it, providing the tool name and description. For example:
from crewai_tools import tool
@tool("My Tool Name")def my_tool(question: str) -> str: """Clearly describe what this tool is used for; your agent will need this information to use it.""" # Function logic here return "Result from your custom tool"
-
Custom Caching Mechanism: Tools can choose to implement
cache_function
to fine-tune caching behavior. Determine when to cache results based on specific conditions, providing fine-grained control over caching logic. For example:
from crewai_tools import tool
@tooldef multiplication_tool(first_number: int, second_number: int) -> str: """Useful when you need to multiply two numbers.""" return first_number * second_number
def cache_func(args, result): # In this case, cache the result only when it is an even number cache = result % 2 == 0 return cache
multiplication_tool.cache_function = cache_func
2.3.5 Using LangChain Tools
CrewAI seamlessly integrates with LangChain’s toolset, allowing developers to utilize built-in tools provided by LangChain, such as<span>GoogleSerperAPIWrapper</span>
, by simply configuring them for integration into the CrewAI system, thus extending agent capabilities. For example:
from crewai import Agent
from langchain.agents import Tool
from langchain.utilities import GoogleSerperAPIWrapper
# Set API keysos.environ["SERPER_API_KEY"] = "Your Key"search = GoogleSerperAPIWrapper()
# Create and assign the search tool to the agentserper_tool = Tool( name="Intermediate Answer", func=search.run, description="A tool for search-based queries")
agent = Agent( role='Research Analyst', goal='Provide the latest market analysis', backstory='An expert analyst with keen insight into market trends.', tools=[serper_tool])
2.4 Processes
2.4.1 Process Implementation Methods
-
Sequential Execution: Tasks are executed in a predefined order, where the output of one task serves as the context for the next task, ensuring order and coherence in task execution.
-
Hierarchical: Organizes tasks within a management hierarchy, enabling the process by specifying a manager language model (
manager_llm
). The manager agent oversees task execution, including planning, delegating, and verifying tasks, with tasks assigned based on agent capabilities rather than pre-assignment. -
Consensus Process (Planned): Aims to achieve collaborative decision-making among agents during task execution, introducing a democratic task management approach, which has not yet been implemented in the codebase but reflects CrewAI’s pursuit of continuous development and innovation.
2.4.2 Role of Processes in Team Collaboration
Processes enable individual agents to operate as a cohesive whole, simplifying collaborative efforts to achieve common goals in an efficient and coordinated manner. Through reasonable process design, teams can better respond to complex tasks and improve overall work efficiency.
2.4.3 Assigning Processes to Managers
When creating a manager, developers can specify the process type to set the execution strategy. For hierarchical processes, a manager_llm
must be defined for the manager agent. For example:
from crewai import Crew
from crewai.process import Process
from langchain_openai import ChatOpenAI
# Sequential Processcrew = Crew( agents=my_agents, tasks=my_tasks, process=Process.sequential)
# Hierarchical Process, ensuring to provide manager_llmcrew = Crew( agents=my_agents, tasks=my_tasks, process=Process.hierarchical, manager_llm=ChatOpenAI(model="gpt-4"))
2.4.4 Additional Task Features
-
Asynchronous Execution: Allows tasks to execute asynchronously, achieving parallel processing and improving overall crew productivity. Developers can flexibly choose synchronous or asynchronous execution methods based on task characteristics.
-
Human Input Review: Provides an optional human review feature for task outputs, ensuring quality and accuracy before finalizing task results, introducing an additional layer of supervision to ensure reliable task execution.
-
Output Customization: Tasks support various output formats, such as JSON (
output_json
), Pydantic models (output_pydantic
), and file outputs (output_file
), meeting different needs and facilitating data processing and utilization.
2.5 Teams
2.5.1 Team Attributes
-
Tasks: A list of tasks assigned to the team, clearly defining the work the team needs to complete.
-
Agents: A list of agents that belong to the team, determining the composition of team members, with each agent having its unique roles and capabilities.
-
Processes (Optional): The type of process the team follows, such as sequential or hierarchical processes, determining the order and manner of task execution.
-
Level of Detail (Optional): The level of detail in logging during execution, allowing developers to obtain sufficient information during debugging and monitoring.
-
Manager LLM (Optional, Required for Hierarchical Processes): The language model used by the manager agent in hierarchical processes to manage task execution.
-
Function Calling LLM (Optional): If passed, the team will use this LLM for function calls for all agents, allowing developers to configure flexibly based on needs.
-
Configuration (Optional): Optional configuration settings for the team, provided in
Json
orDict[str, Any]
format, for further customizing team behavior. -
Max RPM (Optional): The maximum number of requests that can be executed per minute during team execution, avoiding rate limits, overriding individual agents’
max_rpm
settings. -
Language (Optional): The language used by the team, defaulting to English, which can be adjusted based on actual needs.
-
Language Files (Optional): The path to language files for the team, facilitating multilingual support.
-
Memory (Optional): Used to store execution memories (short-term, long-term, entity memories), enhancing the execution and learning capabilities of the team.
-
Cache (Optional): Specifies whether to use caching to store tool execution results, improving process efficiency.
-
Embeddings (Optional): Configuration of the embeddings used by the team, mainly for memory functions, affecting the way data is embedded and retrieved.
-
Full Output (Optional): Determines whether the team returns a complete output containing all task outputs or only the final output, meeting different result acquisition needs.
-
Step Callback (Optional): A function called after each step of each agent, used for logging operations or performing other actions, not overriding agent-specific
step_callback
. -
Task Callback (Optional): A function called after each task is completed, used for monitoring or executing other operations after the task.
-
Shared Team (Optional): Whether to share complete team information and execution status with the CrewAI team to improve the library and allow model training.
-
Output Log File (Optional): Whether to create a file containing complete team outputs and execution status, allowing specification of file paths and names.
2.5.2 Example of Creating a Team
The following is an example of assembling a team, demonstrating how to combine agents with complementary roles and tools, assign tasks, and choose processes:
from crewai import Crew, Agent, Task, Process
from langchain_community.tools import DuckDuckGoSearchRun
# Define agents with specific roles and toolsresearcher = Agent( role='Senior Research Analyst', goal='Identify innovative AI technologies', tools=[DuckDuckGoSearchRun()])
writer = Agent( role='Content Writer', goal='Write engaging articles about AI discoveries', verbose=True)
# Create tasks for agentsresearch_task = Task( description='Identify breakthrough AI technologies', agent=researcher)write_article_task = Task( description='Write an article about the latest AI technologies', agent=writer)
# Assemble the team using sequential processesmy_crew = Crew( agents=[researcher, writer], tasks=[research_task, write_article_task], process=Process.sequential, full_output=True, verbose=True,)
2.5.3 Team Execution Process
-
Sequential Process: Tasks are executed one after another, with a linear flow of work, simple and intuitive, suitable for scenarios where there is a clear order between tasks.
-
Hierarchical Process: The manager agent coordinates the team, delegating tasks and verifying results before proceeding. This process requires
manager_llm
and is suitable for managing and distributing complex tasks, ensuring efficient execution and quality control. -
Starting the Team: Use the
kickoff()
method to start the team workflow, executing tasks according to the defined process and obtaining task execution results. For example:
# Start the team's task executionresult = my_crew.kickoff()print(result)
2.6 Memory
2.6.1 Components of the Memory System
-
Short-term Memory: Temporarily stores recent interactions and results, enabling agents to quickly recall and utilize information closely related to the current context, maintaining coherence in dialogues or task sequences, and making more contextually relevant decisions and responses.
-
Long-term Memory: Like a knowledge repository, retains valuable insights and learning outcomes accumulated from past executions. Over time, agents can continuously draw from this experience, gradually building and refining their knowledge system, thereby enhancing their decision-making and problem-solving capabilities to better handle complex and dynamic task scenarios.
-
Entity Memory: Focuses on capturing and organizing various entity information encountered during task execution, covering people, places, concepts, etc. This helps agents understand the intrinsic relationships between complex information, allowing for more efficient processing and integration of related knowledge, providing a more comprehensive and in-depth perspective for problem-solving.
-
Context Memory: Aims to maintain contextual information during interactions, ensuring that agents can consistently respond relevantly in a series of tasks or continuous dialogues. Even during long interactions, they can accurately understand task backgrounds and intentions, avoiding information gaps or misunderstandings, thus providing more accurate and reasonable outputs.
2.6.2 How the Memory System Empowers Agents
-
Enhanced Context Awareness: With the collaborative effect of short-term and context memory, agents can firmly grasp the contextual threads during the evolution of dialogues or tasks. Whether in information correlations during multi-turn dialogues or logical continuities in task sequences, agents can generate more coherent, consistent, and contextually appropriate responses based on stored contextual information, greatly enhancing the fluidity and logicality of the interaction experience.
-
Experience Accumulation and Learning Acceleration: Long-term memory provides agents with a platform for continuous growth and evolution. By storing and reviewing past actions and results, agents can summarize lessons learned, discover patterns, and continuously optimize their decision-making strategies and problem-solving methods. This process of experience accumulation enables agents to make wiser and more efficient decisions when facing similar issues, significantly improving work efficiency and quality.
-
Entity Understanding and Information Processing Optimization: Entity memory equips agents with the ability to recognize and remember key entities, enabling them to quickly focus on core points when processing complex information. This not only helps agents understand task requirements more accurately but also allows them to swiftly filter valuable content when faced with vast amounts of information, improving the speed and accuracy of information processing, thus effectively addressing complex tasks and diverse problem scenarios.
2.6.3 Implementing Memory in Teams
When configuring a team, developers can flexibly enable and customize each memory component based on team goals and task nature. By default, the memory system is off, and the memory function can be activated by setting memory = True
in the team configuration, injecting powerful memory capabilities into the team. The memory defaults to using OpenAI Embeddings, but developers can also adjust the embedder
parameter to set it to other models, such as Google AI, Azure OpenAI, GPT4ALL, Vertex AI, or Cohere, to meet different scenario needs. For example:
from crewai import Crew, Agent, Task, Process
# Using a team with memory functionality, adopting default OpenAI Embeddingsmy_crew = Crew( agents=[...], tasks=[...], process=Process.sequential, memory=True, verbose=True)
# Using a team with memory functionality, switching to Google AI embeddingsmy_crew = Crew( agents=[...], tasks=[...], process=Process.sequential, memory=True, verbose=True, embedder={ "provider": "google", "config":{ "model": 'models/embedding-001', "task_type": "retrieval_document", "title": "Embeddings for Embedchain" } })
2.6.4 Benefits of Using CrewAI Memory System
-
Adaptive Learning and Continuous Optimization: Over time and through continuous task execution, teams can gradually adapt to new information and task requirements, continuously refining their methods and strategies for handling tasks. The memory system enables agents to learn from past experiences, automatically adjusting their behavior patterns, thus becoming more composed when facing new situations, and the overall efficiency of the team will improve with the accumulation of experience.
-
Enhanced Personalized Experience: The memory function allows agents to record and recognize user preferences, historical interactions, and other information, providing users with more personalized services and experiences. Whether in content recommendations, problem-solving, or interaction methods, agents can respond accurately based on users’ personalized characteristics, meeting specific user needs and enhancing the stickiness and satisfaction between users and the system.
-
Strengthened Problem-Solving Capabilities: A rich memory repository provides agents with a strong knowledge backing, enabling them to fully leverage past learning outcomes and contextual information when solving problems. By quickly retrieving and utilizing relevant experiences, agents can analyze problems more comprehensively, discover potential solutions, and make wiser and more accurate decisions, effectively enhancing their ability to solve complex problems and providing strong support for the successful operation of the team.
3. Practical Steps for Building Multi-Agent Systems Based on CrewAI
3.1 Preparation
3.1.1 Environment Configuration
Ensure that the system meets the installation requirements for CrewAI and related dependencies. Depending on the operating system, specific software packages or configuration of environment variables may need to be installed. For example, certain systems may require the installation of a Python environment, relevant libraries, and configuration of API keys, etc.
3.1.2 Installing CrewAI and Ollama (Optional)
Follow the official documentation to install CrewAI and Ollama in sequence (if you need to use Ollama). During the installation process, pay attention to version compatibility, dependencies, and other issues to ensure a smooth installation. For example, use the pip command to install CrewAI and its tool package:<span>pip install crewai[tools]</span>
, and follow Ollama’s installation guide for corresponding operations.
3.1.3 Setting Up the Model
In the configuration file of CrewAI, set the language model (LLM) to the desired model, such as Llama3, etc. This step ensures that agents can use the appropriate language model for reasoning and decision-making when executing tasks.
3.2 Creating Agents
3.2.1 Defining Agent Roles and Goals
Clearly define the roles and goals of each agent based on task requirements. For example, create an agent responsible for information gathering, whose role could be defined as “Information Gatherer” with the goal of “Collecting relevant information in a specific field”; or create an agent focused on data analysis, whose role is “Data Analyst” with the goal of “Conducting in-depth analysis of collected data to extract valuable insights.”
3.2.2 Configuring Agent Attributes
Reasonably configure agent attributes based on the agent’s role and task characteristics, such as selecting an appropriate language model, adding necessary tools, setting maximum iteration counts, caching strategies, etc. For example, the information gatherer agent can be configured with web search tools and set a higher maximum request count to improve information retrieval efficiency, while the data analyst agent may need to connect to specific database tools and adjust maximum iteration counts based on the complexity of analysis tasks.
3.2.3 Writing Agent Background Stories (Optional but Recommended)
Write detailed background stories for agents, although this is an optional step, it can provide richer context for the agent’s behavior and decisions, enhancing the anthropomorphic characteristics of the agents, which helps improve the interactivity and interpretability of the system. For example, for the information gatherer, the background story could be: “You are a professional information gatherer, skilled at quickly filtering valuable content from a vast amount of online information. You have long been engaged in data collection work, are well-versed in various information sources, and proficient in various search techniques and tools. This task aims to help the team obtain the latest information on [specific field], providing strong support for subsequent analysis and decision-making.”
3.3 Defining Tasks
3.3.1 Clearly Describing Tasks
Provide clear and accurate descriptions for each task, ensuring that agents can clearly understand the specific requirements of the task. For example, “Analyze user feedback about [product name] on social media over the past month, extracting major opinions and suggestions” or “Predict the development trends of [industry name] over the next three months based on market data.”
3.3.2 Assigning Task Agents
Reasonably assign tasks to the corresponding agents based on their roles and capabilities. You can directly specify the agent responsible for executing the task or utilize CrewAI’s process mechanism to allow the system to automatically assign tasks based on the agents’ availability, expertise, etc. For instance, assign information gathering tasks to the information gatherer agent and data analysis tasks to the data analyst agent.
3.3.3 Configuring Task Attributes
Based on task requirements, set other attributes of the task, such as expected output format (JSON, Pydantic model, or file, etc.), whether asynchronous execution is needed, whether human input review is required, and setting task context, etc. For instance, for tasks with high real-time requirements, you can set them to execute asynchronously to improve the overall response speed of the system; for important decision-making tasks, you can enable human input review to ensure the accuracy of results.
3.4 Integrating Tools
3.4.1 Choosing Suitable Tools
Choose appropriate tools from CrewAI’s rich tool library, such as<span>WebsiteSearchTool</span>
for web searches,<span>FileReadTool</span>
for file reading,<span>CSVSearchTool</span>
for data analysis, etc. Custom tools can also be created as needed to meet the unique needs of specific tasks.
3.4.2 Integrating Tools with Agents and Tasks
Assign selected tools to the corresponding agents, enabling agents to call these tools during task execution. When defining tasks, clearly specify the tools required for the task, ensuring that tools are closely integrated with tasks and work in harmony. For example, in information gathering tasks, equip the information gatherer agent with<span>WebsiteSearchTool</span>
and<span>DirectorySearchTool</span>
to allow it to retrieve information from web pages and local directories.
3.5 Building Teams
3.5.1 Combining Agents
Combine agents with different roles and capabilities to form a collaborative team. Ensure that the roles of team members complement each other, enabling them to work together to complete complex tasks. For example, a team can include agents such as information gatherers, data analysts, content creators, etc., each leveraging their expertise to achieve project goals.
3.5.2 Defining Team Processes
Choose suitable team processes based on the characteristics and requirements of the tasks, such as sequential or hierarchical processes. Sequential processes are suitable for scenarios where there is a clear order between tasks, while hierarchical processes are suitable for complex tasks requiring management and coordination, with task distribution and supervision by the manager agent.
3.5.3 Configuring Team Attributes
Set various attributes of the team according to its needs, such as logging detail level, maximum request rate, language settings, memory and caching configurations, whether to share team information, etc. Reasonable configuration of team attributes can optimize the team’s operational efficiency, meeting different needs in various scenarios.
3.6 Executing Tasks and Monitoring
3.6.1 Starting Team Task Execution
Use the kickoff()
method to start the team’s task execution process. The system will drive agents to start working according to the defined process and task assignments. During task execution, progress can be monitored in real-time through logging, console output, and other means.
3.6.2 Monitoring Task Execution Process
Utilize CrewAI’s logging features and relevant monitoring tools to closely observe the execution status of tasks, the operational steps of agents, output results, and other information. Detailed monitoring helps promptly identify potential issues during task execution, such as agent execution errors, task timeouts, high resource consumption, etc.
3.6.3 Handling Task Execution Results
After task execution is completed, obtain and process the output results of the tasks. Analyze and extract valuable information based on the expected output format of the task for subsequent decision-making or further processing. Simultaneously, based on the results of task execution and issues discovered during monitoring, make necessary adjustments and optimizations to agents, tasks, or team configurations to improve system performance and accuracy.
4. Case Studies: Applications of CrewAI in Real Projects
4.1 Smart Customer Service System
4.1.1 System Architecture and Agent Roles
In the smart customer service system, multiple agents are constructed to simulate the workflow of human customer service. This includes user question reception agents, question classification agents, information inquiry agents, answer generation agents, and answer review agents. The user question reception agent is responsible for receiving user inquiries and passing them to the question classification agent; the question classification agent classifies the questions based on their types and assigns them to the corresponding information inquiry agents; the information inquiry agents use various tools (such as knowledge base query tools, database search tools, etc.) to find relevant information; the answer generation agents generate preliminary answers based on the information queried; the answer review agents review and optimize the generated answers to ensure their accuracy and quality.
4.1.2 Task Flow and Collaboration
When a user poses a question, the user question reception agent records the question and passes it to the question classification agent. The question classification agent analyzes the keywords of the questions and uses semantic understanding techniques to classify the questions into different types, such as common questions, technical questions, after-sales questions, etc. Then, based on the classification results, it assigns the questions to the corresponding information inquiry agents. The information inquiry agents search for relevant information in knowledge bases, databases, or external resources based on the question types. For example, for common questions, answers can be directly retrieved from the knowledge base; for technical questions, it may be necessary to consult technical documentation or interact with technical expert systems. The answer generation agents use natural language generation techniques to transform the information into clear and understandable answers. Finally, the answer review agents perform grammar checks, logical reviews, and politeness assessments of the answers and optimize and modify them further if necessary. Throughout the process, various agents achieve efficient collaboration through CrewAI’s communication mechanisms and task orchestration features, providing users with quick and accurate answers.
4.2 Smart Content Creation Assistant
4.2.1 Agent Functions and Division of Labor
The smart content creation assistant consists of multiple agents working collaboratively, including theme planning agents, material collection agents, content generation agents, and content polishing agents. The theme planning agent is responsible for determining the creation theme based on user needs or market trends; the material collection agent collects relevant materials such as articles, images, and data using web search tools and database query tools; the content generation agent creates the first draft based on the theme and materials using natural language generation algorithms; the content polishing agent optimizes the first draft for grammar, style adjustments, and logical structuring, making it more aligned with user requirements and expectations.
4.2.2 Creation Process and Collaboration Mechanism
When a user presents a creation request (such as writing an article about [specific topic]), the theme planning agent first conducts theme analysis and planning, determining the rough framework and key content of the article. Then, the material collection agent searches for relevant articles, research reports, and cases online based on the theme, organizes the materials, and passes them to the content generation agent. The content generation agent generates the first draft of the article based on the theme framework and collected materials. For example, it can generate paragraph content based on the viewpoints and data in the materials and organize the structure of the article. Finally, the content polishing agent comprehensively polishes the first draft, checks for grammar errors, optimizes word choices, and adjusts sentence structures to make the article smoother, more fluent, and more engaging. Throughout the creation process, agents share information and feedback in real-time through CrewAI’s communication and collaboration mechanisms, ensuring the smooth progress of the creation work.
4.3 Smart Investment Decision-Making System
4.3.1 System Composition and Agent Responsibilities
The smart investment decision-making system consists of market data collection agents, data analysis agents, risk assessment agents, investment strategy recommendation agents, and investment portfolio optimization agents. The market data collection agents are responsible for collecting market data in real-time from various financial data sources (such as stock exchanges, financial news websites, financial databases, etc.), including stock prices, trading volumes, macroeconomic indicators, etc.; the data analysis agents clean, organize, and analyze the collected data, extracting valuable information such as market trends, industry dynamics, and company financial conditions; the risk assessment agents assess the risk levels of investment targets using risk assessment models based on the data analysis results; the investment strategy recommendation agents recommend suitable investment strategies based on market conditions and investors’ risk preferences, such as long-term investment, short-term speculation, and diversified investments; the investment portfolio optimization agents optimize the investment portfolio based on the recommended investment strategies, determining the best asset allocation ratios.
4.3.2 Decision-Making Process and Collaboration Model
The market data collection agents continuously collect the latest market data and pass it to the data analysis agents. The data analysis agents conduct in-depth analysis of the data, for example, predicting stock price trends using technical analysis methods or assessing the intrinsic value of companies through fundamental analysis. The risk assessment agents evaluate the risk levels of different investment targets based on the data analysis results, historical data, and risk models. The investment strategy recommendation agents recommend personalized investment strategies for investors based on market trends, risk assessment results, and investors’ risk preferences. For example, for risk-averse investors, a higher proportion of bonds and funds may be recommended; for aggressive investors, a higher proportion of stock investments may be recommended. Finally, the investment portfolio optimization agents use mathematical optimization algorithms to determine the optimal investment portfolio configuration to maximize returns and minimize risks. Throughout the decision-making process, various agents collaborate closely, achieving efficient data flow and scientific decision-making through CrewAI’s process orchestration and communication mechanisms.
References:https://blog.csdn.net/wjjc1017/article/details/138162739