Machine Learning

To create AI agent to write blog posts with crewai

I love to write. You may notice that if you follow me or my blog. For that reason, I generate new content and talk about science scientists and artificial intelligence.

I got this practice over the past few years when I just started starting my way in the data science, reading and emerging my skills. At that time, I heard some experienced professionals in the area saying that a good learning process made new and in place, teaching whatever you learned.

In addition, I had just moved to the US, and no one knew here. So I had to start somewhere, causing my expert picture in this competition market. I remember that I had talked to my cousin, who was in the technical sector, and she told me: Write blog posts about your experiences. Tell people what you're doing. And so I did.

And I've never stopped.

Soon in 2025, I now have two published hundreds of print, many of which look at the data science, published book, and good audience.

Writing is very helpful in the data science.

Recently, one of my favorite things are amazing natural language machines and large languages ​​language. Learn how these models of modern work were interesting.

That interest led me to analytation about Agentic Ai. So, I've learned about it CrewaiThe simple and open packet that helps to build agents ai in a fun and simple way, with a small code. I decided to test the agents to write a blog post, and see how that goes.

In this post, we will learn how to create those agents and make them work together to produce simple blog posts.

Let's do that.

What is crew?

A crew Of agents AI a combination of two or more agents, each of them doing the job of finding the final goal.

For this read, we will create a group that will work with the production of a small blog on the topic we will give.

The crew of agents moves in work. Photo by the writer

The flow is effective as follows:

  1. We choose a given title agents to write about.
  2. As soon as the crew has begun, it will enter the foundation of information, read some of my previously written topics, and try to imitate my writing style. Then, it creates a set of guidelines and transmits to the next agel.
  3. Next, planning agent takes over and searching the Internet looking for good content on the topic. Creates a content system and sends it to the next agent.
  4. The author's agent gets the writing system and generates it according to the context and information received.
  5. Finally, the content is transferred to the final agent, the editor, updating the last document as a result.

In the following section, we will see how this can be created.

Code

Crewai is a nice python pack for making our code. So, let's start by installing two required packages.

pip install crewai crewai-tools

Next, if you want, you can follow the instructions in Rush And he has a full building for a project that we have done for just a few orders on the end. Basically, it will include some reliance, generate the Suggested Folder building for Crewai projects, and generate more .yas and files.

I myself prefer to build such things, but it's up to you. The page is written in the index section.

Folder structure

So, here we go.

We will build these Folds:

And these files:

  • In suspense Folder: Create files agents.yaml including tasks.yaml
  • In knowledge Folder, where I will install files in my writing style.
  • Project root: Create crew.py including main.py.
The player structure. Photo by author.

Be sure to create folders with words mentioned, as Crewai looks for agents and activities within suspense Folder and Information Homeground InformationS A knowledge folder.

Next, let's put our agents.

Agents

Areas made up:

  • Name of Agreement: writer_style
  • Verse: The LLMS is a good participant, so here you can tell them what role should be played.
  • Goal: Tell the model what that Abeli's goal is.
  • Misfortony: Describe the story behind this word, who, what it is.
writer_style:
  role: >
    Writing Style Analyst
  goal: >
    Thoroughly read the knowledge base and learn the characteristics of the crew, 
    such as tone, style, vocabulary, mood, and grammar.
  backstory: >
    You are an experienced ghost writer who can mimic any writing style.
    You know how to identify the tone and style of the original writer and mimic 
    their writing style.
    Your work is the basis for the Content Writer to write an article on this topic.

I will not do it with all agents created in this group. I believe you get an idea. For a set of propts that describes each agent what they will do. All agent instructions are stored in the agent.Yaml file.

Think about it as if you were a lease Manager to create a group. Consider what kind of expert will require, and what skills are required.

We need 4 experts to work for the final policy of producing written content: (1) a Stylist(2) a Raid(3) a Writer, and (4) with Editor.

If you want to see the setup them, just check the perfect code in GitTub repository.

Functions

Now, go back to the comparison of people who employ people, as long as we rent “our whole group, it is time to classify the apps. We know 4 agents, but that's what each of them will do.

Well, that will be fixed in the file tasks.yaml.

For example, let me show you a writer's agent code. Again, these are the necessary components:

  • Name of work: write
  • Description: An explanation is like telling experts how much you want to do, just as we didn't say how to do their new job. Give specific instructions for getting a very good result.
  • Expected Release: This is how we want to see the result. Note that I give instructions such as Blog Post size, the number of categories, and other information that helps my agent give me the expected result.
  • Agent to do: Here, we show a agent to do this work, using the same name set up in agents.yaml File.
  • Output file: Now it works all the time, but if so, this is a conflict to use. We requested a Markown file as output.
write:
  description: >
    1. Use the content plan to craft a compelling blog post on {topic}.
    2. Incorporate SEO keywords naturally.
    3. Sections/Subtitles are properly named in an engaging manner. Make sure 
    to add Introduction, Problem Statement, Code, Before You Go, References.
    4. Add a summarizing conclusion - This is the "Before You Go" section.
    5. Proofread for grammatical errors and alignment with the writer's style.
    6. Use analogies to make the article more engaging and complex concepts easier
    to understand.
  expected_output: >
    A well-written blog post in markdown format, ready for publication.
    The article must be within a 7 to 12 minutes read.
    Each section must have at least 3 paragraphs.
    When writing code, you will write a snippet of code and explain what it does. 
    Be careful to not add a huge snippet at a time. Break it in reasonable chunks.
    In the examples, create a sample dataset for the code.
    In the Before You Go section, you will write a conclusion that is engaging
    and factually accurate.
  agent: content_writer
  output_file: blog_post.md

After agents and functions are specified, it is time to make our work flow.

To add crew

Now we will create a file crew.pyWhere we will translate the drainage presented before the Python code.

We begin to import the required modules.

#Imports
import os
from crewai import Agent, Task, Process, Crew, LLM
from crewai.project import CrewBase, agent, crew, task
from crewai.knowledge.source.pdf_knowledge_source import PDFKnowledgeSource
from crewai_tools import SerperDevTool

We will use basic Agent, Task, Crew, Process including LLM to create our flow. PDFKnowledgeSource The first agent will help my style of writing, and SerperDevtool is an Internet search tool. To that thing, be sure to get your API key at

The best practice in the development of software is to keep your API buttons and separate configuration settings in your code. We will use a .env The file is, to provide a safe place to save these values. Here is the command to suspend our area.

from dotenv import load_dotenv
load_dotenv()

Then, we will use PDFKnowledgeSource To show employees where to search the author style. By default, such a tool looks at the information folder of your project, thus the value of the word is the same.

# Knowledge sources

pdfs = PDFKnowledgeSource(
    file_paths=['article1.pdf',
                'article2.pdf',
                'article3.pdf'
                ]
)

Now we can set the llm we want to use for the team. There may be them. I tested off their bites, and those I liked qwen-qwq-32b including gpt-4o. If you select Opelai's, you will need API key. On qwen-qwq, just because of the code and put the comment of Opelai lines .. you need an API key from GRIQ.

# LLMs

llm = LLM(
    # model="groq/qwen-qwq-32b",
    # api_key= os.environ.get("GROQ_API_KEY"),
    model= "gpt-4o",
    api_key= os.environ.get("OPENAI_API_KEY"),
    temperature=0.4
)

Now we have to create a Crew BaseDisplays where crewai can find agent files and job configuration files.

# Creating the crew: base shows where the agents and tasks are defined

@CrewBase
class BlogWriter():
    """Crew to write a blog post"""
    agents_config = "config/agents.yaml"
    tasks_config = "config/tasks.yaml"

Agents are working

And we're ready to create a code for each agent. They are made up of decoration @agent to show that the next job is an agent. We then use the class agent and show the agent name in the Config File, Versig level, for 1 low, 2. You can also use a boolean value, as true or false.

Finally, we specify whether an agent uses any tool, and where will it use the model.

# Configuring the agents
    @agent
    def writer_style(self) -> Agent:
        return Agent(
                config=self.agents_config['writer_style'],
                verbose=1,
                knowledge_sources=[pdfs]
                )

    @agent
    def planner(self) -> Agent:
        return Agent(
        config=self.agents_config['planner'],
        verbose=True,
        tools=[SerperDevTool()],
        llm=llm
        )

    @agent
    def content_writer(self) -> Agent:
        return Agent(
        config=self.agents_config['content_writer'],
        verbose=1
        )

    @agent
    def editor(self) -> Agent:
        return Agent(
        config=self.agents_config['editor'],
        verbose=1
        )

Functions Activities

The next step is to create jobs. Similarly with agents, we will create a job and decorate with @task. We use the class work to earn a file that works with dense eaters and points to our use of our tasks.yaml File to be used for each work done. If any output file is expected, use output_file the argument.

# Configuring the tasks    

    @task
    def style(self) -> Task:
        return Task(
        config=self.tasks_config['mystyle'],
        )

    @task
    def plan(self) -> Task:
        return Task(
        config=self.tasks_config['plan'],
        )

    @task
    def write(self) -> Task:
        return Task(
        config=self.tasks_config['write'],
        output_file='output/blog_post.md' # This is the file that will be contain the final blog post.
        )

    @task
    def edit(self) -> Task:
        return Task(
        config=self.tasks_config['edit']
        )

Crew

To paste everything together, now we make work and decorate with @crew the decoration. That work will have an agent and the functions in the right order is made, because the selected process here is simple: respectively. In other words, everything runs the sequence, from the beginning to the end.

@crew

    def crew(self) -> Crew:
        """Creates the Blog Post crew"""

        return Crew(
            agents= [self.writer_style(), self.planner(), self.content_writer(), self.editor(), self.illustrator()],
            tasks= [self.style(), self.plan(), self.write(), self.edit(), self.illustrate()],
            process=Process.sequential,
            verbose=True
        )

Running Staff

Running the crew very easy. We build main.py File and submit a CREW foundation BlogWriter created. Then we just use the activities crew().kickoff(inputs) Made it, passing the dictionary by installing it to produce blog posts.

# Script to run the blog writer project

# Warning control
import warnings
warnings.filterwarnings('ignore')
from crew import BlogWriter


def write_blog_post(topic: str):
    # Instantiate the crew
    my_writer = BlogWriter()
    # Run
    result = (my_writer
              .crew()
              .kickoff(inputs = {
                  'topic': topic
                  })
    )

    return result

if __name__ == "__main__":

    write_blog_post("Price Optimization with Python")

There is there. The result is a good blog post by the llm. See below.

Leading to the blog post. GIF is a writer.

That is good!

Before you go

Before you leave, you know this blog post was created 100% me. The boat I created was a test I wanted to do to learn more about how to build AI's agents and to work together. Also, as I said, I love writing, so this is something I can read and test quality.

My view is that this group is still doing the best job. They are able to finish jobs successfully, but they give me shallow posts and code. I will not publish this, but at least it can be started, maybe.

From here here, I encourage you to learn more about Crewai. I have taken their free course when João de Moura, the Creator's Creator, show us how to build different groups of groups. It is very interesting.

GitHub Repository

about me

If you want to learn more about my work, or follow my blog (actually me!), Here are your contacts and portfolio.

Progress

[Quickstart CrewAI](

[CrewAI Documentation](

[GROQ](

[OpenAI](

[CrewAI Free Course](

Source link

Related Articles

Leave a Reply

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

Back to top button