Lets build a team of AI assistants working for us. CrewAI lets us do just that pretty easily. By building “crews” of specialized agents, we can automate tasks, generate creative text formats, and access information in a whole new way. This tutorial will guide us through creating a simple CrewAI project, complete with code samples.
Prerequisites:
- A code editor (like Visual Studio Code)
- Basic understanding of Python
Assembling the Crew:
Our CrewAI project will focus on trip planning. We’ll build three agents:
- Expert Travel Agent: This agent will find destinations based on user preferences.
- City Selection Expert: Given a country, this agent will suggest interesting cities to visit.
- Local Tour Guide: This agent will find tours and attractions within a chosen city.
Define the crew
import sys
import os
import streamlit as st
from crewai import Agent, Task, Crew, Process
from langchain_community.tools import DuckDuckGoSearchRun
from langchain.agents import Tool
# os.environ["OPENAI_API_KEY"] = "YOUR_API_KEY"
#openai.api_key = st.secrets["OPEN_API_KEY"]
duckduckgo_search = DuckDuckGoSearchRun()
def create_travel_crew(destination):
# Define Agents
travel_advisor = Agent(
role="Travel Advisor", # Changed from Expert Travel Agent
goal=f"Craft a personalized itinerary for a trip based on your preferences.",
backstory="A seasoned globetrotter, passionate about creating unforgettable travel experiences!",
verbose=True,
allow_delegation=True,
tools=[duckduckgo_search],
LLM="gpt-3.5-turbo"
)
city_explorer = Agent( # New agent - City Explorer
role="City Explorer", # New role
goal=f"Explore potential destinations and suggest exciting cities based on your interests.",
backstory="An expert in uncovering hidden travel gems, ready to find the perfect city for your trip!",
verbose=True,
allow_delegation=True,
tools=[duckduckgo_search],
LLM="gpt-3.5-turbo"
)
activity_scout = Agent(
role="Activity Scout",
goal=f"Find exciting activities and attractions in {destination} that match your interests.",
backstory="An expert curator of unique experiences, ready to unveil the hidden gems of {destination}.",
verbose=True,
allow_delegation=True,
tools=[duckduckgo_search],
LLM="gpt-3.5-turbo"
)
logistics_coordinator = Agent(
role="Logistics Coordinator",
goal=f"Help you navigate the logistics of your trip to {destination}, including flights, accommodation, and transportation.",
backstory="A logistical whiz, ensuring your trip runs smoothly from start to finish.",
verbose=True,
allow_delegation=True,
tools=[duckduckgo_search],
LLM="gpt-3.5-turbo"
)
Each agent now has a role, backstory (to give it personality), and a goal that defines its function.
Step 2: Plan Your Crew’s Tasks
Next, we’ll create “tasks” that our agents will complete. Here, the tasks will build upon each other:
- Find Destination: User provides preferences, travel agent finds matching destinations.
- Choose City: User selects a country, city selection expert suggests a city to visit.
- Plan Activities: User picks a city, local tour guide searches for tours and attractions.
Step 3: Form Your Crew
Now, it’s time to bring these agents together! We’ll use CrewAI’s built-in functions to define the tasks and how the agents will collaborate.
# Define Tasks (modified based on new agent)
# Task 1 can use either travel_advisor or city_explorer depending on user input
if destination:
task1 = Task(
description=f"Plan a personalized itinerary for a trip to {destination}. Consider preferences like travel style (adventure, relaxation, etc.), budget, and desired activities.",
expected_output="Personalized itinerary for a trip to {destination}.",
agent=travel_advisor,
LLM="gpt-3.5-turbo"
)
else:
task1 = Task(
description=f"Help you explore potential destinations and suggest exciting cities to visit based on your interests (e.g., beaches, culture, nightlife).",
expected_output="List of recommended destinations and interesting cities to visit.",
agent=city_explorer,
LLM="gpt-3.5-turbo"
)
task2 = Task(
description=f"Find exciting activities and attractions in {destination} that align with your preferences (e.g., museums, hiking, nightlife).",
expected_output="List of recommended activities and attractions in {destination}.",
agent=activity_scout,
LLM="gpt-3.5-turbo"
)
task3 = Task(
description=f"Help you navigate the logistics of your trip to {destination}. Search for flights, accommodation options, and local transportation based on your preferences and itinerary.",
expected_output="Recommendations for flights, accommodation, and transportation for your trip to {destination}.",
agent=logistics_coordinator,
LLM="gpt-3.5-turbo"
)
# Create and Run the Crew
travel_crew = Crew(
agents=[travel_advisor, city_explorer, activity_scout, logistics_coordinator],
tasks=[task1, task2, task3],
verbose=2,
process=Process.sequential,
LLM="gpt-3.5-turbo"
)
crew_result = travel_crew.kickoff()
return crew_result
Define Tasks (modified based on new agent)
# Task 1 can use either travel_advisor or city_explorer depending on user input
if destination:
task1 = Task(
description=f"Plan a personalized itinerary for a trip to {destination}. Consider preferences like travel style (adventure, relaxation, etc.), budget, and desired activities.",
expected_output="Personalized itinerary for a trip to {destination}.",
agent=travel_advisor,
LLM="gpt-3.5-turbo"
)
else:
task1 = Task(
description=f"Help you explore potential destinations and suggest exciting cities to visit based on your interests (e.g., beaches, culture, nightlife).",
expected_output="List of recommended destinations and interesting cities to visit.",
agent=city_explorer,
LLM="gpt-3.5-turbo"
)
task2 = Task(
description=f"Find exciting activities and attractions in {destination} that align with your preferences (e.g., museums, hiking, nightlife).",
expected_output="List of recommended activities and attractions in {destination}.",
agent=activity_scout,
LLM="gpt-3.5-turbo"
)
task3 = Task(
description=f"Help you navigate the logistics of your trip to {destination}. Search for flights, accommodation options, and local transportation based on your preferences and itinerary.",
expected_output="Recommendations for flights, accommodation, and transportation for your trip to {destination}.",
agent=logistics_coordinator,
LLM="gpt-3.5-turbo"
)
Step 4: Kickoff the Crew
Running the crewai project . My python file was app.py so I used python app.py as my command. This will prompt the user for input at each stage, with each agent completing its task and passing information to the next.
# Create and Run the Crew
travel_crew = Crew(
agents=[travel_advisor, city_explorer, activity_scout, logistics_coordinator],
tasks=[task1, task2, task3],
verbose=2,
process=Process.sequential,
LLM="gpt-3.5-turbo"
)
crew_result = travel_crew.kickoff()
return crew_result
Here is the full code
import sys
import os
import streamlit as st
from crewai import Agent, Task, Crew, Process
from langchain_community.tools import DuckDuckGoSearchRun
from langchain.agents import Tool
# os.environ["OPENAI_API_KEY"] = "YOUR_API_KEY"
#openai.api_key = st.secrets["OPEN_API_KEY"]
duckduckgo_search = DuckDuckGoSearchRun()
def create_travel_crew(destination):
# Define Agents
travel_advisor = Agent(
role="Travel Advisor", # Changed from Expert Travel Agent
goal=f"Craft a personalized itinerary for a trip based on your preferences.",
backstory="A seasoned globetrotter, passionate about creating unforgettable travel experiences!",
verbose=True,
allow_delegation=True,
tools=[duckduckgo_search],
LLM="gpt-3.5-turbo"
)
city_explorer = Agent( # New agent - City Explorer
role="City Explorer", # New role
goal=f"Explore potential destinations and suggest exciting cities based on your interests.",
backstory="An expert in uncovering hidden travel gems, ready to find the perfect city for your trip!",
verbose=True,
allow_delegation=True,
tools=[duckduckgo_search],
LLM="gpt-3.5-turbo"
)
activity_scout = Agent(
role="Activity Scout",
goal=f"Find exciting activities and attractions in {destination} that match your interests.",
backstory="An expert curator of unique experiences, ready to unveil the hidden gems of {destination}.",
verbose=True,
allow_delegation=True,
tools=[duckduckgo_search],
LLM="gpt-3.5-turbo"
)
logistics_coordinator = Agent(
role="Logistics Coordinator",
goal=f"Help you navigate the logistics of your trip to {destination}, including flights, accommodation, and transportation.",
backstory="A logistical whiz, ensuring your trip runs smoothly from start to finish.",
verbose=True,
allow_delegation=True,
tools=[duckduckgo_search],
LLM="gpt-3.5-turbo"
)
# Define Tasks (modified based on new agent)
# Task 1 can use either travel_advisor or city_explorer depending on user input
if destination:
task1 = Task(
description=f"Plan a personalized itinerary for a trip to {destination}. Consider preferences like travel style (adventure, relaxation, etc.), budget, and desired activities.",
expected_output="Personalized itinerary for a trip to {destination}.",
agent=travel_advisor,
LLM="gpt-3.5-turbo"
)
else:
task1 = Task(
description=f"Help you explore potential destinations and suggest exciting cities to visit based on your interests (e.g., beaches, culture, nightlife).",
expected_output="List of recommended destinations and interesting cities to visit.",
agent=city_explorer,
LLM="gpt-3.5-turbo"
)
task2 = Task(
description=f"Find exciting activities and attractions in {destination} that align with your preferences (e.g., museums, hiking, nightlife).",
expected_output="List of recommended activities and attractions in {destination}.",
agent=activity_scout,
LLM="gpt-3.5-turbo"
)
task3 = Task(
description=f"Help you navigate the logistics of your trip to {destination}. Search for flights, accommodation options, and local transportation based on your preferences and itinerary.",
expected_output="Recommendations for flights, accommodation, and transportation for your trip to {destination}.",
agent=logistics_coordinator,
LLM="gpt-3.5-turbo"
)
# Create and Run the Crew
travel_crew = Crew(
agents=[travel_advisor, city_explorer, activity_scout, logistics_coordinator],
tasks=[task1, task2, task3],
verbose=2,
process=Process.sequential,
LLM="gpt-3.5-turbo"
)
crew_result = travel_crew.kickoff()
return crew_result
CrewAI offers a wide range of features, including:
- Hierarchical workflows: Let your agents work on tasks simultaneously.
- Customizable tools: Integrate external APIs and services.
- Verbose mode: Get more detailed explanations from your agents.
Explore the CrewAI documentation https://docs.crewai.com/
I further used streamlit to pipe all the output to a web page . I will post in a future article.