Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Creating Tasks in CrewAI

Introduction

In this tutorial, we will walk you through the process of creating tasks in CrewAI. Tasks are fundamental units of work that can be assigned to handlers for execution. Understanding how to create and manage tasks is crucial for leveraging the full power of CrewAI.

Prerequisites

Before we start, ensure that you have the following:

  • Basic understanding of CrewAI and its components.
  • Access to a CrewAI environment.
  • Basic programming skills.

Step 1: Define the Task

The first step in creating a task is defining what the task will do. This involves specifying the task's name, the actions it will perform, and any necessary parameters.

Example: Defining a Task

Let's define a simple task that sends a greeting message.

task = {
  "name": "send_greeting",
  "description": "Sends a greeting message",
  "parameters": {
    "message": "Hello, World!"
  },
  "actions": [{
    "type": "send_message",
    "content": "${parameters.message}"
  }]
}

Step 2: Create the Task in CrewAI

Once you have defined the task, the next step is to create it in CrewAI. This can be done using the CrewAI API or through the CrewAI dashboard.

Example: Creating a Task using the API

You can create the task by making a POST request to the CrewAI API with the task definition.

POST /api/tasks
Content-Type: application/json

{
  "name": "send_greeting",
  "description": "Sends a greeting message",
  "parameters": {
    "message": "Hello, World!"
  },
  "actions": [{
    "type": "send_message",
    "content": "${parameters.message}"
  }]
}

Step 3: Assign the Task to a Handler

After creating the task, you need to assign it to a handler who will execute the task. Handlers are responsible for performing the actions specified in the task.

Example: Assigning a Task

You can assign the task by specifying the handler's ID and the task ID.

POST /api/handlers/{handler_id}/tasks
Content-Type: application/json

{
  "task_id": "send_greeting"
}

Step 4: Monitor Task Execution

Once the task is assigned, you can monitor its execution through the CrewAI dashboard or by querying the API.

Example: Monitoring Task Execution

You can check the status of the task by making a GET request to the CrewAI API.

GET /api/tasks/{task_id}/status
{
  "task_id": "send_greeting",
  "status": "completed",
  "result": "Message sent successfully."
}

Conclusion

In this tutorial, we covered the process of creating tasks in CrewAI from start to finish. We defined a task, created it in CrewAI, assigned it to a handler, and monitored its execution. With these steps, you can effectively manage and execute tasks using CrewAI.