Creating Flexible Tasks with Placeholders
Introduction and Context
Welcome to another lesson! This time, we will focus on creating flexible tasks using placeholders. By the end of this lesson, you will understand how to define tasks that can adapt to different inputs, making your CrewAI agents more versatile and efficient. This lesson builds on the foundational concepts introduced in the previous lesson, where we explored the basics of agents, tasks, and crews. Let's dive in and learn how to make tasks more adaptable with placeholders.
Creating Flexible Tasks with Placeholders
Placeholders are a powerful feature in CrewAI that allows tasks to be more flexible and adaptable. They act as variables within task descriptions and expected outputs, enabling you to define tasks that can change based on different inputs. This means you can create a single task template that can be reused in various scenarios by simply altering the input values. For example, instead of hardcoding a city name in a task, you can use a placeholder like {city}. This makes your tasks dynamic and reusable, allowing agents to handle a wider range of requests without needing to redefine tasks for each specific case.
Let's break down a code example to see how placeholders are used in practice. We start by defining a travel agent and a task with placeholders in the description and expected output.
In this code, the description and expected_output fields of the Task class use placeholders {num_attractions} and {city}. These placeholders will be replaced with actual values when the task is executed, allowing the task to adapt to different input scenarios.
Setting Up Dynamic Inputs
To utilize the placeholders effectively, we need to set up dynamic inputs that will be used during task execution. This involves specifying the values for the placeholders when the crew is run.
Here, we define a dictionary inputs with keys corresponding to the placeholders in the task. The values assigned to these keys will replace the placeholders during execution, allowing the task to be customized for different cities and numbers of attractions.
