Type something to search...
Multi-Agent AI Mastery: Building Content Writing AI System with CrewAI | by Kshitij Kutumbe | Medium

Multi-Agent AI Mastery: Building Content Writing AI System with CrewAI | by Kshitij Kutumbe | Medium

In the realm of AI and language models, a multi-agent system involves several independent actors, each powered by language models, collaborating in a structured manner. In this blog, we will delve into Crew.AI, a cutting-edge framework for building multi-agent applications. Crew.AI enables AI agents to assume roles, share goals, and operate cohesively, mimicking the dynamics of a well-coordinated team.

Architecture

The architecture of Crew.AI is modular, consisting of several key components that work together to create a seamless multi-agent system. The following sections describe these components, starting from the bottom up to illustrate how they integrate.

Tool: A tool is a utility or equipment that agents use to perform specific tasks efficiently. Examples include searching the web, loading documents, and reading them. Built with LangChain, Crew.AI allows for the use of existing tools from LangChain or the creation of custom tools.

Task: A task is a specific activity that needs to be executed by an agent. Various tools are provided to execute these tasks efficiently.

Agents: Agents are the core performers in the Crew.AI framework, each with a specific role, background, goal, and memory. Each Crew.AI agent is a LangChain agent enhanced with a ReActSingleInputOutputParser. This parser is specially modified to support role-playing, incorporate a binding stop word for contextual focus, and integrate a memory mechanism using ConversationSummaryMemory for maintaining context.

Crew: The crew represents a team of agents working together to achieve a particular goal. These agents collaborate in a well-defined manner to accomplish the set tasks.

Process: The process object represents the workflow or strategy the crew follows to complete tasks. The framework defines three strategies (as of the time of writing, with plans for more):

  • Sequential: Executes tasks in a defined order, ideal for pipeline-type work where each agent performs a specific task and passes it to the next. This strategy is used in the example to write a blog on a given topic.
  • Hierarchical: Organizes tasks in a hierarchy, delegating them in a chain of command. This strategy resembles an orchestrator pattern, similar to a manager assigning work to various agents and validating results before completion. The next blog will explore a solution using this strategy.
  • Consensual Process (Planned): A popular strategy yet to be released, where agents collaborate to make decisions democratically. Although not yet implemented in Crew.AI, other multi-agent frameworks have adopted this approach and it will be explored in future blogs.

For more detailed information on these components and their APIs, refer to the Crew.AI documentation.

Let’s now see how CrewAI could be used to build an almost autonomous content/blog writing engine for yourself or your company:

Installations:

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

Imports:

from crewai import Agent, Task, Crew
import os

LLM credentials and name:

os.environ["OPENAI_MODEL_NAME"] = 'gpt-3.5-turbo'
os.environ["OPENAI_API_KEY"]=""

Defining Agents, Tasks and crew:

planner = Agent(
    role="Content Planner",
    goal="Plan engaging and factually accurate content on {topic}",
    backstory="You're working on planning a blog article "
              "about the topic: {topic}."
              "You collect information that helps the "
              "audience learn something "
              "and make informed decisions. "
              "Your work is the basis for "
              "the Content Writer to write an article on this topic.",
    allow_delegation=False,
 verbose=True
)

writer = Agent(
    role="Content Writer",
    goal="Write insightful and factually accurate "
         "opinion piece about the topic: {topic}",
    backstory="You're working on a writing "
              "a new opinion piece about the topic: {topic}. "
              "You base your writing on the work of "
              "the Content Planner, who provides an outline "
              "and relevant context about the topic. "
              "You follow the main objectives and "
              "direction of the outline, "
              "as provide by the Content Planner. "
              "You also provide objective and impartial insights "
              "and back them up with information "
              "provide by the Content Planner. "
              "You acknowledge in your opinion piece "
              "when your statements are opinions "
              "as opposed to objective statements.",
    allow_delegation=False,
    verbose=True
)
editor = Agent(
    role="Editor",
    goal="Edit a given blog post to align with "
         "the writing style of the organization. ",
    backstory="You are an editor who receives a blog post "
              "from the Content Writer. "
              "Your goal is to review the blog post "
              "to ensure that it follows journalistic best practices,"
              "provides balanced viewpoints "
              "when providing opinions or assertions, "
              "and also avoids major controversial topics "
              "or opinions when possible.",
    allow_delegation=False,
    verbose=True
)

plan = Task(
    description=(
        "1. Prioritize the latest trends, key players, "
            "and noteworthy news on {topic}.\n"
        "2. Identify the target audience, considering "
            "their interests and pain points.\n"
        "3. Develop a detailed content outline including "
            "an introduction, key points, and a call to action.\n"
        "4. Include SEO keywords and relevant data or sources."
    ),
    expected_output="A comprehensive content plan document "
        "with an outline, audience analysis, "
        "SEO keywords, and resources.",
    agent=planner,
)

write = Task(
    description=(
        "1. Use the content plan to craft a compelling "
            "blog post on {topic}.\n"
        "2. Incorporate SEO keywords naturally.\n"
  "3. Sections/Subtitles are properly named "
            "in an engaging manner.\n"
        "4. Ensure the post is structured with an "
            "engaging introduction, insightful body, "
            "and a summarizing conclusion.\n"
        "5. Proofread for grammatical errors and "
            "alignment with the brand's voice.\n"
    ),
    expected_output="A well-written blog post "
        "in markdown format, ready for publication, "
        "each section should have 2 or 3 paragraphs.",
    agent=writer,
)

edit = Task(
    description=("Proofread the given blog post for "
                 "grammatical errors and "
                 "alignment with the brand's voice."),
    expected_output="A well-written blog post in markdown format, "
                    "ready for publication, "
                    "each section should have 2 or 3 paragraphs.",
    agent=editor
)

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

Extracting the output:

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

And this is how the output blog will look like:

This is just one of the many applications that could be built using CrewAI or Autogen or other such frameworks. You can also make it perform more complex tasks like searching over internet, collecting data, performing data analysis, providing conclusions and so on.

Also checkout my other blog:

Related Posts

10 Creative Ways to Use ChatGPT Search The Web Feature

10 Creative Ways to Use ChatGPT Search The Web Feature

For example, prompts and outputs Did you know you can use the “search the web” feature of ChatGPT for many tasks other than your basic web search? For those who don't know, ChatGPT’s new

Read More
📚 10 Must-Learn Skills to Stay Ahead in AI and Tech 🚀

📚 10 Must-Learn Skills to Stay Ahead in AI and Tech 🚀

In an industry as dynamic as AI and tech, staying ahead means constantly upgrading your skills. Whether you’re aiming to dive deep into AI model performance, master data analysis, or transform trad

Read More
10 Powerful Perplexity AI Prompts to Automate Your Marketing Tasks

10 Powerful Perplexity AI Prompts to Automate Your Marketing Tasks

In today’s fast-paced digital world, marketers are always looking for smarter ways to streamline their efforts. Imagine having a personal assistant who can create audience profiles, suggest mar

Read More
10+ Top ChatGPT Prompts for UI/UX Designers

10+ Top ChatGPT Prompts for UI/UX Designers

AI technologies, such as machine learning, natural language processing, and data analytics, are redefining traditional design methodologies. From automating repetitive tasks to enabling personal

Read More
100 AI Tools to Finish Months of Work in Minutes

100 AI Tools to Finish Months of Work in Minutes

The rapid advancements in artificial intelligence (AI) have transformed how businesses operate, allowing people to complete tasks that once took weeks or months in mere minutes. From content creat

Read More
17 Mindblowing GitHub Repositories You Never Knew Existed

17 Mindblowing GitHub Repositories You Never Knew Existed

Github Hidden Gems!! Repositories To Bookmark Right Away Learning to code is relatively easy, but mastering the art of writing better code is much tougher. GitHub serves as a treasur

Read More