CrewAI
CrewAI

In the real world of artificial intelligence, language models have emerged as powerful tools for generating content, answering queries, and even assisting in creative writing. With the advent of technologies like CrewAI, harnessing the potential of these models has become more accessible to beginners and intermediate users alike. In this guide, we will explore how to leverage CrewAI, along with specific tools and techniques, to harness the power of advanced language models for content creation.

Dependencies to install

Dependencies: Before diving into the details, let’s understand the key dependencies required for working with CrewAI:

CrewAI

CrewAI is a platform that facilitates collaborative work among agents (AI entities) to accomplish tasks. It streamlines the workflow by assigning roles and tasks to agents and coordinating their actions.

To install CrewAI, you can use pip, Python’s package manager:

pip install crewai

LLMs (Language Learning Models):

LLMs (Language Learning Models): LLMs are advanced language models trained on vast amounts of text data. These models can understand and generate human-like text based on given prompts.

The specific LLMs used in the example code are part of the Langchain library. You can install Langchain using pip:

pip install langchain or pip install -U langchain-community

DuckDuckGoSearchRun: This tool provides functionalities for web scraping and searching the internet for relevant information. It enables agents to gather data from various sources to fulfill their tasks.

This tool is a custom tool or module specific to the example code. If it’s publicly available or provided by Langchain, you might need to install Langchain as mentioned above. Otherwise, you may need to install additional dependencies or use alternative web scraping libraries like BeautifulSoup or Scrapy.

Ollama is a component of the Langchain library, specifically designed for language model-related tasks. To install Ollama along with Langchain, you can use pip, Python’s package manager. You need to install Ollama software in your computer by simply downloading and following their installation guide: https://ollama.com/.

You can also install specific model from their list of models by simply typing this command in the command prompt or terminal: ollama run modelname or ollama run llama3 llama3 is the latest open source model or library released by Meta formerly known as Facebook.

Decomposing the Code

Step 1: Setting up Dependencies:

Import necessary modules from CrewAI and Langchain libraries.

Example: Setup Your CrewAI and langchain_community and Ollama


# Step 1: Setup Your CrewAI and langchain_community and Ollama
from crewai import Agent, Task, Crew, Process
from langchain.llms import Ollama
from langchain.callbacks.manager import CallbackManager
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
from langchain_community.tools import DuckDuckGoSearchRun

Step 2: set up the search tool and initialize your model:

In the above we have the necessary dependencies installed. Assuming you’ve already installed Langchain and DuckDuckGoSearchRun.

Set up the DuckDuckGoSearchRun tool for internet search capabilities.

Initialize the Ollama model, which serves as the core language model for generating text-based content.

Example: Here’s how you can set up the search tool and initialize the Ollama model:


# Set up the search tool and initialize your model
search_tool = DuckDuckGoSearchRun()

# Set up the Ollama model
ollama_llm = Ollama(
    model="llama2",
    callback_manager=CallbackManager([StreamingStdOutCallbackHandler()])
)

Step 3: Define Agents and Tasks:

Create two types of agents: Researcher and Tech Content Strategist.

Assign roles, goals, and necessary tools to each agent.

Define tasks for agents to accomplish, specifying descriptions and expected outputs.

Example: Defining your agents with roles and goals


# Define your agents with roles and goals
researcher = Agent(
    role='Researcher',
    goal='Search the internet for the information requested',
    backstory="""
    You are a researcher. Using the information in the task, you find out some of the most popular facts about the topic along with some of the trending aspects.
    You provide a lot of information thereby allowing a choice in the content selected for the final blog.
    """,
    verbose=True,
    allow_delegation=False,
    tools=[search_tool],
    llm=ollama_llm
)

writer = Agent(
    role='Tech Content Strategist',
    goal='Craft compelling content on a set of information provided by the researcher.',
    backstory="""You are a writer known for your humorous but informative way of explaining. 
    You transform complex concepts into compelling narratives.""",
    verbose=True,
    allow_delegation=True,
    llm=ollama_llm
)

Step 4: Instantiate Crew and Assign Tasks:

Create a Crew instance and add agents and tasks to it.

Configure the verbose level for logging purposes.

Initiate the Crew to kick off the collaborative work among agents.

Example: Creating tasks for your agents


# Create tasks for your agents
task1 = Task(
    description="""Research about open source LLMs vs closed source LLMs. 
    Your final answer MUST be a full analysis report""",
    expected_output="Full analysis report in bullet points",  # Provide the expected output here
    agent=researcher
)

task2 = Task(
    description="""Using the insights provided, develop an engaging blog
    post that highlights the most significant facts and differences between open-source LLMs and closed-source LLMs.
    Your post should be informative yet accessible, catering to a tech-savvy audience.
    Make it sound cool, and avoid complex words so it doesn't sound like AI.
    Your final answer MUST be the full blog post of at least 4 paragraphs.
    The target word count for the blog post should be between 1,500 and 2,500 words, with a sweet spot at around 2,450 words.""",
    expected_output="Full blog post of at least 4 paragraphs",  # Provide the expected output here
    agent=writer
)

Step 5: Execution and Result:

Execute the Crew’s kickoff method to start the assigned tasks.

Monitor the progress and completion of tasks.

Retrieve the result, which includes the generated content or reports.

Example: Instantiating your crew with a sequential process


# Instantiate your crew with a sequential process
crew = Crew(
    agents=[researcher, writer],
    tasks=[task1, task2],
    verbose=2, # You can set it to 1 or 2 for different logging levels
)

# Get your crew to work!
result = crew.kickoff()

print("###########")
print(result)

Full Source code

This Full script sets up a CrewAI workflow for performing research and generating content on the topic of open-source and closed-source Large Language Models (LLMs). Let’s break it down step by step:

FEEL FREE TO USE AND SHOW THE MAGIC TO THE WORLD!!!.

Example:



# Step 1: Setup Your CrewAI and langchain_community and Ollama
from crewai import Agent, Task, Crew, Process
from langchain.llms import Ollama
from langchain.callbacks.manager import CallbackManager
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
from langchain_community.tools import DuckDuckGoSearchRun

# Set up the search tool and initialize your model
search_tool = DuckDuckGoSearchRun()

# Set up the Ollama model
ollama_llm = Ollama(
    model="llama2",
    callback_manager=CallbackManager([StreamingStdOutCallbackHandler()])
)

# Define your agents with roles and goals
researcher = Agent(
    role='Researcher',
    goal='Search the internet for the information requested',
    backstory="""
    You are a researcher. Using the information in the task, you find out some of the most popular facts about the topic along with some of the trending aspects.
    You provide a lot of information thereby allowing a choice in the content selected for the final blog.
    """,
    verbose=True,
    allow_delegation=False,
    tools=[search_tool],
    llm=ollama_llm
)

writer = Agent(
    role='Tech Content Strategist',
    goal='Craft compelling content on a set of information provided by the researcher.',
    backstory="""You are a writer known for your humorous but informative way of explaining. 
    You transform complex concepts into compelling narratives.""",
    verbose=True,
    allow_delegation=True,
    llm=ollama_llm
)

# Create tasks for your agents
# Create tasks for your agents
task1 = Task(
    description="""Research about open source LLMs vs closed source LLMs. 
    Your final answer MUST be a full analysis report""",
    expected_output="Full analysis report in bullet points",  # Provide the expected output here
    agent=researcher
)

task2 = Task(
    description="""Using the insights provided, develop an engaging blog
    post that highlights the most significant facts and differences between open-source LLMs and closed-source LLMs.
    Your post should be informative yet accessible, catering to a tech-savvy audience.
    Make it sound cool, and avoid complex words so it doesn't sound like AI.
    Your final answer MUST be the full blog post of at least 4 paragraphs.
    The target word count for the blog post should be between 1,500 and 2,500 words, with a sweet spot at around 2,450 words.""",
    expected_output="Full blog post of at least 4 paragraphs",  # Provide the expected output here
    agent=writer
)


# Instantiate your crew with a sequential process
crew = Crew(
    agents=[researcher, writer],
    tasks=[task1, task2],
    verbose=2, # You can set it to 1 or 2 for different logging levels
)

# Get your crew to work!
result = crew.kickoff()

print("###########")
print(result)

Sample Output how the agents working:

CrewAI Sample Output for Content Creation

Conclusion

In this guide, we’ve explored the process of leveraging CrewAI, along with advanced language models like Ollama, for content creation tasks. By understanding the dependencies, decomposing the code, and following the step-by-step approach, beginners and intermediate users can harness the power of AI-driven content generation effectively. With the right tools and techniques, the possibilities for creative expression and information dissemination are virtually limitless in the realm of AI-powered content creation.