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.

Executing the Crew with Flexible Tasks

With the task and inputs defined, we can now execute the crew and observe how the placeholders are utilized during execution.

When the kickoff() method is called, CrewAI processes the task by replacing the placeholders with the values from the inputs dictionary. The travel agent then uses these values to generate a response. The output will be a list of popular attractions in San Diego, formatted according to the expected output defined in the task.

Summary and Next Steps

In this lesson, we explored how to create flexible tasks using placeholders in CrewAI. You learned how placeholders make tasks adaptable by allowing them to change based on dynamic inputs. We walked through a code example that demonstrated defining a task with placeholders, setting up inputs, and executing the crew to see the results. As you move forward, you'll have the opportunity to practice these concepts through hands-on exercises. In the next unit, we'll continue to build on these skills, exploring more advanced features of CrewAI. Keep practicing, and you'll soon be proficient in creating dynamic and adaptable workflows with CrewAI.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal