Multi-AI Agent Systems with CrewAI | Research and Write an Article

Multi-AI Agent Systems with CrewAI | Research and Write an Article

Build a multi-agent AI system with CrewAI for efficient content creation. Learn setup, planning, writing, and editing with top AI models.

Multi-AI Agent Systems with CrewAI | Research and Write an Article

Introduction

Artificial intelligence (AI) has revolutionized content creation, making it faster and more efficient. However, a single AI model often struggles to handle complex workflows that require planning, writing, and editing. This is where multi-agent systems come into play.

In this tutorial, we’ll explore how to use CrewAI, a powerful framework that enables multiple AI agents to collaborate in content creation. You’ll learn how to set up agents for planning, writing, and editing an article, creating a streamlined workflow powered by AI.

What is CrewAI?

CrewAI is a framework that allows multiple AI-powered agents to work together on specific tasks. Each agent has a role, goal, and backstory, ensuring that they function effectively as part of a team.

Prerequisites

Before we begin, ensure you have Python installed. If you’re running this on your machine, install the required libraries using:

pip install crewai==0.28.8 crewai_tools==0.1.6 langchain_community==0.0.29

This tutorial uses GPT-3.5 Turbo as the primary language model, but you can also integrate other AI models like Mistral, Cohere, and Hugging Face models.

Step 1: Setting Up Environment

First, set up the OpenAI API key and define the AI model to be used:

import os
from utils import get_openai_api_key

openai_api_key = get_openai_api_key()
os.environ["OPENAI_MODEL_NAME"] = 'gpt-3.5-turbo'

Step 2: Defining AI Agents

Each AI agent has a specific role:

Content Planner Agent

The Content Planner is responsible for researching the topic, structuring an outline, and ensuring SEO optimization.

from crewai import Agent

planner = Agent(
    role="Content Planner",
    goal="Develop a structured and engaging content plan for {topic}",
    backstory="You're responsible for collecting relevant information, trends, and key points to create an outline.",
    allow_delegation=False,
    verbose=True
)

Content Writer Agent

The Content Writer creates the article based on the planner’s research and ensures it is engaging and informative.

writer = Agent(
    role="Content Writer",
    goal="Write an insightful and engaging article based on the content plan.",
    backstory="You write a well-structured blog post following the provided outline and supporting facts.",
    allow_delegation=False,
    verbose=True
)

Editor Agent

The Editor reviews the article for grammar, tone, and consistency with journalistic best practices.

editor = Agent(
    role="Editor",
    goal="Ensure the final article is polished, professional, and error-free.",
    backstory="You proofread the article, making necessary corrections and ensuring it aligns with the brand's voice.",
    allow_delegation=False,
    verbose=True
)

Step 3: Defining Tasks for Each Agent

Each agent is assigned a task that details its responsibilities.

Task: Content Planning

from crewai import Task

plan = Task(
    description=(
        "1. Research the latest trends and key players in {topic}.\n"
        "2. Identify the target audience and their interests.\n"
        "3. Create a structured content outline with SEO keywords."
    ),
    expected_output="A detailed content plan with an outline and SEO strategy.",
    agent=planner
)

Task: Writing

write = Task(
    description=(
        "1. Write an engaging blog post based on the content plan.\n"
        "2. Ensure SEO-friendly formatting and structure.\n"
        "3. Use clear and concise language with proper headings."
    ),
    expected_output="A well-structured blog post in markdown format.",
    agent=writer
)

Task: Editing

edit = Task(
    description="Proofread and edit the article for clarity, grammar, and consistency.",
    expected_output="A final version of the article, ready for publication.",
    agent=editor
)

Step 4: Creating and Running the Crew

Now, we create a Crew that consists of our three agents, each handling its respective task.

from crewai import Crew

crew = Crew(
    agents=[planner, writer, editor],
    tasks=[plan, write, edit],
    verbose=2
)

To run the multi-agent workflow, execute:

result = crew.kickoff(inputs={"topic": "Artificial Intelligence"})

Step 5: Displaying the Final Article

To visualize the generated article in markdown format:

from IPython.display import Markdown
Markdown(result)

You can also try this workflow with a custom topic by changing the input:

topic = "YOUR TOPIC HERE"
result = crew.kickoff(inputs={"topic": topic})
Markdown(result)

Alternative AI Models

Besides GPT-3.5 Turbo, other language models can be integrated with CrewAI:

Using Hugging Face Model

from langchain_community.llms import HuggingFaceHub

llm = HuggingFaceHub(
    repo_id="HuggingFaceH4/zephyr-7b-beta",
    huggingfacehub_api_token="<HF_TOKEN_HERE>",
    task="text-generation"
)

Using Mistral API

export OPENAI_API_KEY=your-mistral-api-key
export OPENAI_API_BASE=https://api.mistral.ai/v1
export OPENAI_MODEL_NAME="mistral-small"

Using Cohere API

from langchain_community.chat_models import ChatCohere
os.environ["COHERE_API_KEY"] = "your-cohere-api-key"
llm = ChatCohere()

For more AI integrations, refer to CrewAI’s documentation.

Conclusion

This tutorial demonstrated how to create a multi-agent AI system for content generation using CrewAI. By leveraging specialized agents (Planner, Writer, and Editor), you can automate complex workflows efficiently. This approach improves content accuracy, readability, and SEO while minimizing human intervention.

Leave a Comment

Your email address will not be published. Required fields are marked *