Crew AI is a powerful and easy-to-use multi-agent framework designed to simplify the AI development process and provide users with flexible tools and resources. In this series of articles, I will share insights and techniques I have gained while learning Crew AI, and demonstrate how to apply this knowledge to practical projects. This is not only a detailed Crew AI tutorial but also showcases its practical application through the development of a GameFi consultation system.
There are three key elements in the Crew AI framework: Agent, Task, and Crew. An Agent is an entity that performs specific tasks, a Task is a set of instructions defining the actions of an Agent, and a Crew is the system that manages and coordinates the Agents. The entire development philosophy is highly personified, akin to you playing the role of a project manager, defining various employees (Agents), assigning them different tasks (Tasks), and ultimately creating a team (Crew) to fulfill the project's requirements for your clients.
This chapter will briefly introduce these three elements and create a simple GameFi recommendation system.
- Agent
An Agent is the basic operational unit in Crew AI, similar to a virtual character with specific duties and capabilities. In Crew AI, an Agent is defined by the following four basic elements, which are essentially a single natural language sentence in the code.
- Role: The role describes the identity and responsibilities of the Agent, defining its position and function within the system. The role determines how the Agent behaves and interacts with other Agents.
- Goal: The goal is the specific task or result the Agent needs to achieve. Each Agent has a clear goal that guides its behavior and decisions.
- Backstory: The backstory provides context and circumstances for the Agent, helping to define its behavior and decision-making style. The backstory can include the Agent's hypothetical experiences, knowledge domain, and working style.
- Tools: Tools are the specific resources or methods the Agent uses to perform tasks. Tools can be external APIs, data processing libraries, search engines, etc. Note that an Agent can still function without tools; they are not mandatory.
- Task
A Task is the specific work an Agent needs to complete, defined in Crew AI by the following four elements.
- Description: The description is a detailed explanation of the task, clearly stating its purpose and content.
- Expected Output: The expected output defines the specific result or product required upon task completion. It sets clear standards for task completion, ensuring the Agent knows what kind of result to deliver.
- Tools: Tools are the specific resources or methods the Agent uses while performing the task. They are the same as the tools defined for an Agent, meaning an employee can temporarily rely on a tool to complete a specific task.
- Agent: The Agent is the specific role that executes the task.
- Crew
A Crew is a team of multiple Agents working together to complete more complex tasks. Defining a Crew primarily involves defining a team of Agents and a list of Tasks, as well as a Process, which can be understood as the project execution process.
- Agents: Define the members of the Crew, i.e., which Agents are included.
- Tasks: Define the specific Tasks that need to be completed within the Crew.
- Process: The key to defining a Crew is defining the Process. In simple terms, it is a sequential structure where each task is processed one after the other, with the final task being the Crew's ultimate output. For advanced operations such as defining parallel and sequential tasks, Crew AI has a specialized Process class that will be elaborated on in subsequent chapters.
- Practical Application
In this practical application, we will temporarily not involve Tools, and the Process will only need to be set as a sequential structure. The aim of this chapter is to understand the basic usage of Agent, Task, and Crew. So first, we import these three classes.
from crewai import Agent, Task, Crew
Next, we define two Agents: one for searching GameFi information and the other for recommending GameFi.
gamefi_search_agent = Agent(
role="GameFi Search Assistant",
goal="Find the GameFi game's detailed according to the user's query,which is {query}",
backstory="You are a GameFi Search Assistant, your task is to find the detailed page"
" of the GameFi game according to the user's query. GameFi is the blockchain game"
)
gamefi_recommend_agent = Agent(
role="GameFi Recommend Assistant",
goal="Recommend the GameFi game according to the user's query,which is {query}",
backstory="You are a GameFi Recommend Assistant, your task is to recommend the GameFi game"
" according to the user's query. GameFi is the blockchain game"
)
Define their tasks: one for searching GameFi and the other for recommendation.
task_search = Task(
description="find GameFi games according to {query}",
expected_output="games' details",
agent=gamefi_search_agent
)
task_recommend = Task(
description="recommend GameFi games according to {query}",
expected_output="games' recommendation",
agent=gamefi_recommend_agent
)
Finally, define a Crew to coordinate them.
crew = Crew(
agents=[gamefi_search_agent, gamefi_recommend_agent],
tasks=[task_search, task_recommend],
)
After defining the Crew, call the kickoff method and pass in an inputs dictionary, where the keys correspond to the contents defined in {} for Agents and Tasks.
inputs = {"query": "I need a fps gameFi game"}
result = crew.kickoff(inputs=inputs)
Finally, to run the program, you need to define the OpenAI API key in the environment variable.
export OPENAI_API_KEY="Your API Key"
Yes, Crew AI uses OpenAI's GPT model as the default LLM. Once the OpenAI API key is defined in the environment variable, the program will automatically obtain this key without additional operations in the code. Subsequent chapters will introduce how to use LLMs other than GPT.
Next, you can run the program and observe the results.