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.

from crewai import Agent, Task, Crew, Process

# Define a basic agent
travel_agent = Agent(
    role='Travel Agent',
    goal='Help users plan their trips',
    backstory='You are an experienced travel agent who loves helping people discover new places.'
)

# Define a task with placeholders
planning_task = Task(
    description='Suggest {num_attractions} popular attractions to visit in {city}.',
    expected_output='A list of {num_attractions} popular attractions in {city} with brief descriptions.',
    agent=travel_agent
)

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.

# Set up inputs for the task
inputs = {
    "city": "San Diego",
    "num_attractions": 3
}

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.

# Assemble the crew
crew = Crew(
    agents=[travel_agent],
    tasks=[planning_task],
    process=Process.sequential
)

# Execute the crew with the specified inputs
result = crew.kickoff(inputs=inputs)

# Display the result
print(result)

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.

1. **Balboa Park**: A cultural oasis in San Diego, Balboa Park is home to numerous museums, gardens, and the famous San Diego Zoo. Visitors can explore the park's beautiful architecture and enjoy various cultural events.

2. **La Jolla Cove**: Known for its stunning coastline and marine life, La Jolla Cove is a popular spot for snorkeling, kayaking, and enjoying breathtaking ocean views. The area is also home to a variety of shops and restaurants.

3. **USS Midway Museum**: Located on the San Diego waterfront, the USS Midway Museum offers a unique experience aboard a historic aircraft carrier. Visitors can explore the ship's decks, aircraft, and exhibits to learn about naval history.
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