DEV Community

Vivek Alhat
Vivek Alhat

Posted on

Building a Multi-Agent Blog Writer Using Crew AI

#ai

Creating high-quality, engaging blog content often involves multiple stages, from planning and writing to editing. With Crew AI, you can build a multi-agent system that automates this process using AI, streamlining content creation while maintaining quality. In this blog, we’ll explore what Crew AI is, its key components, and how to build a blog writing multi-agent system step by step.

What is Crew AI?

Crew AI is a Python-based framework for orchestrating intelligent agents to collaborate on complex tasks. Each agent represents a specialized entity with a distinct role, expertise, and goals. By defining tasks and assigning them to agents, Crew AI enables you to build workflows where agents seamlessly interact to achieve a shared goal.

Key Concepts

  1. Agent
    An Agent represents an individual entity responsible for executing specific tasks. Each agent has a:

    • Role: Defines its primary responsibility.
    • Goal: Specifies the desired outcome for its tasks.
    • Backstory: Provides contextual information to enhance its performance.
  2. Task
    A Task represents a unit of work assigned to an agent. It includes:

    • Description: A detailed explanation of what the agent must do.
    • Expected Output: Criteria for evaluating task completion.
    • Assigned Agent: The agent responsible for executing the task.
  3. Crew
    A Crew is a collection of agents working together to complete tasks. The tasks can be executed sequentially or hierarchically, depending on the Process configuration.

  4. Process
    The Process defines how tasks are executed. It can be:

    • Sequential: Tasks are completed in a predefined order.
    • Hierarchical: Tasks are executed in a managerial hierarchy.

In this example, we use the groq/llama-3.1-8b-instant language model as the LLM. This model provides the natural language capabilities for agents to perform their roles effectively. You'll need an API key to interact with this LLM. You can create your Groq account here.

Let’s dive into building a blog writing system where agents collaboratively plan, write, and edit a blog post.

1. Setting Up the Environment

Before starting, ensure you have the required dependencies installed.

pip install crewai groq
Enter fullscreen mode Exit fullscreen mode

2. Initialize the LLM

The language model is the backbone of our system. Define it as follows:

from crewai import LLM  

llm = LLM(  
    model="groq/llama-3.1-8b-instant",  
    api_key="",  # Replace with your API key  
)
Enter fullscreen mode Exit fullscreen mode

This sets up the LLM that all agents will use for processing text-based tasks.

3. Defining Agents

a) Content Planner
The Content Planner is responsible for creating a structured outline for the blog post. Define it with a clear goal and backstory to guide its outputs:

from crewai import Agent  

planner = Agent(  
    role="Content Planner",  
    goal="Develop a comprehensive and structured content outline for {topic}, including key sections, subsections, and supporting points.",  
    backstory="An expert content strategist skilled at breaking down complex topics into manageable parts.",  
    llm=llm,  
)
Enter fullscreen mode Exit fullscreen mode

b) Content Writer
The Content Writer expands the outline into a detailed blog post:

writer = Agent(  
    role="Content Writer",  
    goal="Produce captivating and informative blog posts about {topic}.",  
    backstory="A versatile writer passionate about simplifying complex ideas.",  
    llm=llm,  
)
Enter fullscreen mode Exit fullscreen mode

c) Content Editor
The Content Editor refines and polishes the blog post to ensure quality:

editor = Agent(  
    role="Content Editor",  
    goal="Refine the blog post, ensuring clarity, coherence, and grammatical accuracy.",  
    backstory="A meticulous editor with a strong eye for detail.",  
    llm=llm,  
)
Enter fullscreen mode Exit fullscreen mode

4. Defining Tasks

a) Content Planning Task
The first task involves creating an outline:

from crewai import Task  

plan = Task(  
    description="Create a detailed content outline for {topic}, including main sections and key points.",  
    expected_output="A structured outline for {topic}, with headings and bullet points.",  
    agent=planner,  
)
Enter fullscreen mode Exit fullscreen mode

b) Content Writing Task
The second task transforms the outline into a complete blog post:

write = Task(  
    description="Transform the outline into a 1000-word blog post in markdown format.",  
    expected_output="A comprehensive blog post with clear language and examples.",  
    agent=writer,  
)
Enter fullscreen mode Exit fullscreen mode

c) Content Editing Task
The final task involves refining the blog post:

edit = Task(  
    description="Review and polish the blog post, ensuring quality and alignment with the outline.",  
    expected_output="A polished, error-free blog post with enhanced structure and tone.",  
    agent=editor,  
)
Enter fullscreen mode Exit fullscreen mode

5. Assembling the Crew

Combine the agents and tasks into a crew:

from crewai import Crew, Process  

crew = Crew(  
    agents=[planner, writer, editor],  
    tasks=[plan, write, edit],  
    verbose=True,  
    process=Process.sequential,  # Execute tasks in sequence  
)
Enter fullscreen mode Exit fullscreen mode

6. Kicking Off the Process

Prompt the user for the blog topic and start the process:

topic = input("Topic?\n")  
edit.output_file = topic + ".md"  # Save the final output as a markdown file  
crew.kickoff(inputs={"topic": topic})
Enter fullscreen mode Exit fullscreen mode

How It Works

  • The Content Planner creates an outline for the given topic.
  • The Content Writer expands the outline into a detailed blog post.
  • The Content Editor reviews and refines the content for quality.

The entire process runs sequentially, ensuring each task builds upon the output of the previous one.

Top comments (0)