Hey everyone! Welcome to this crash course on creating multiple AI agents for real-world use cases using Crew AI. If you haven't heard about this platform yet, let me introduce you to Crew AI, an agent framework that helps you create multiple AI agents for various innovative use cases.
In this post, we'll explore the Crew AI platform, demonstrate an exciting use case, and guide you through creating a complete end-to-end solution. We will develop multiple AI agents capable of interacting efficiently to accomplish tasks more effectively. Crew AI stands out because the agents can communicate with each other seamlessly, enhancing the overall efficiency of task execution.
Understanding the Use Case
Let's imagine you have a YouTube channel with over 1,000 videos. You want to create a blog platform where each video has its corresponding blog page. Manually doing this task is incredibly time-consuming. However, with Crew AI, we can automate this entire process, from extracting content from videos to generating and publishing blog posts.
Components of Crew AI
Crew AI consists of three main components:
Agents: These are specialized individuals (or AI models) with specific roles and expertise, such as a data scientist, content writer, or researcher.
Tasks: Each agent is assigned specific tasks related to their role.
Tools: Tools are third-party APIs or internal tools that agents use to complete their tasks efficiently.
Implementing the Use Case
Step 1: Setting Up the Virtual Environment
Before diving into the code, it's crucial to set up a virtual environment. This ensures that all dependencies are managed correctly and do not interfere with other projects.
1.1 Installing Anaconda
If you haven't installed Anaconda yet, download and install it from Anaconda's official website.
1.2 Creating and Activating the Virtual Environment
Activate Conda: Open your terminal or command prompt and ensure Conda is activated.Create a Virtual Environment:
conda create -p venv python=3.10
Activate the Virtual Environment:
conda activate venv
Step 2: Setting Up the Project
2.1 Create a requirements.txt File
Create a requirements.txt file and add the following dependencies:
# Required libraries for the project
crewai
crewai_tools
load_dotenv
langchain-huggingface
This file lists the required libraries for the project. These libraries will be installed using pip.
2.2 Create agents.py
Create a file named agents.py and add the following code:
from crewai import Agent
from tools import youtube_search_tool
from dotenv import load_dotenv
# Load environment variables from a .env file
load_dotenv()
import os
# Set environment variables for API keys and model configuration
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
os.environ["OPENAI_MODEL_NAME"] = "gpt-4-0125-preview"
# Create a senior blog content researcher agent
blog_researcher_agent = Agent(
role='Blog Researcher from Youtube Videos',
goal='Get the relevant video transcription for the topic {topic} from the provided YT channel',
verbose=True,
memory=True,
backstory=(
"Expert in understanding videos in AI, Data Science, Machine Learning, and General AI, "
"and providing suggestions."
),
tools=[youtube_search_tool],
allow_delegation=True
)
# Create a blog writer agent
blog_writer_agent = Agent(
role='Blog Writer',
goal='Narrate compelling tech stories about the video {topic} from YT video',
verbose=True,
memory=True,
backstory=(
"With a flair for simplifying complex topics, you craft engaging narratives that captivate "
"and educate, bringing new discoveries to light in an accessible manner."
),
tools=[youtube_search_tool],
allow_delegation=False
)
Explanation:
Environment Variables: The dotenv library is used to load environment variables from a .env file. These variables include the OPENAI_API_KEY and OPENAI_MODEL_NAME.
Agents: Two AI agents are defined:glog_researcher_agent: This agent is responsible for researching YouTube videos based on a given topic.
blog_writer_agent: This agent is responsible for writing blog posts based on the research.
2.3 Create tools.py
Create a file named tools.py and add the following code:
# from crewai_tools import YoutubeChannelSearchTool
# Initialize the tool with a specific YouTube channel handle to target your search
youtube_search_tool = YoutubeChannelSearchTool(youtube_channel_handle='@RevanthTechTrends')
Explanation:
YouTube Search Tool: This tool is initialized with a specific YouTube channel handle (@RevanthTechTrends) to target searches within that channel.
2.4 Create tasks.py
Create a file named tasks.py and add the following code:
from crewai import Task
from tools import youtube_search_tool
from agents import blog_researcher_agent, blog_writer_agent
# Define the research task
research_task = Task(
description=(
"Identify the video {topic}. "
"Get detailed information about the video from the channel video."
),
expected_output='A comprehensive 3 paragraphs long report based on the {topic} of video content.',
tools=[youtube_search_tool],
agent=blog_researcher_agent,
)
# Define the writing task with language model configuration
write_task = Task(
description=(
"Get the info from the YouTube channel on the topic {topic}."
),
expected_output='Summarize the info from the YouTube channel video on the topic {topic} and create the content for the blog.',
tools=[youtube_search_tool],
agent=blog_writer_agent,
async_execution=False,
output_file='new-blog-post.md' # Example of output customization
)
Explanation:
Tasks: Two tasks are defined:gesearch_task: This task uses the blog_researcher_agent to gather detailed information from YouTube videos.
write_task: This task uses the blog_writer_agent to summarize the gathered information and create blog content. The output is saved to new-blog-post.md.
2.5 Create crew.py
Create a file named crew.py and add the following code:
from crewai import Crew, Process
from agents import blog_researcher_agent, blog_writer_agent
from tasks import research_task, write_task
# Forming the tech-focused crew with some enhanced configurations
tech_crew = Crew(
agents=[blog_researcher_agent, blog_writer_agent],
tasks=[research_task, write_task],
process=Process.sequential, # Optional: Sequential task execution is default
memory=True,
cache=True,
max_rpm=100,
share_crew=True
)
# Start the task execution process with enhanced feedback
result = tech_crew.kickoff(inputs={'topic': 'ChatGPT and Generative AI'})
print(result)
Explanation:
Crew: A Crew object is initialized with the defined agents and tasks. The Crew object is configured to execute tasks sequentially, with memory and caching enabled.
Topic: The kickoff method starts the task execution process with the topic "ChatGPT and Generative AI", and the result is printed.
2.6 Updating the Topic
Open the crew.py file and update the topic in the kickoff method to your desired topic. For example, to set the topic to "ChatGPT and Generative AI", the code should look like this:
# Start the task execution process with enhanced feedback
result = tech_crew.kickoff(inputs={'topic': 'ChatGPT and Generative AI'})
print(result)
Step 3: Running the Project
Finally, execute the code by running the following command in your terminal:
python crew.py
Explanation:
This command will initiate the content creation process using the AI agents defined in your project. The output will be printed in the terminal.
By following these steps, you can efficiently automate content creation using multiple AI agents with Crew AI. This setup helps you manage dependencies, define AI agents and tasks, and execute the entire process seamlessly. Happy content creating!
Conclusion
With Crew AI, we can automate the process of creating blog content from YouTube videos efficiently. This example demonstrates how multiple AI agents can work together to achieve a common goal, significantly reducing manual effort and time.
Stay tuned for more exciting tutorials and projects using Crew AI. We'll explore integrating different LLM models and other advanced use cases in future posts. Happy automating!
Commentaires