[DRAFT] CrewAI
CrewAI is a framework designed to orchestrate autonomous AI agents to work collaboratively on complex tasks. It empowers you to create sophisticated workflows where multiple AI agents, each with specific roles and responsibilities, operate together to achieve a common goal.
Key Concepts
- Agents: Individual AI entities with specific profiles, goals, and access to tools. They are designed to perform specific tasks within a crew.
- Tools: Resources or functionalities that agents can utilize to achieve their objectives, such as search engines, APIs, or custom code execution.
- Tasks: Specific objectives assigned to agents, guiding their actions and contributions to the overall goal.
- Crew: A collection of agents working together, coordinated to solve a problem. Crews define the workflow and collaboration strategy.
Installation
Install CrewAI using pip:
pip install crewai
You will also need to install the ollama
package if you plan to use Ollama as your LLM:
pip install ollama
Core Features
- Agent Definition: Define agents with specific roles, goals, backstories, and access to relevant tools.
- Task Assignment: Assign tasks to agents, providing clear objectives and instructions.
- Tool Integration: Integrate various tools, such as search engines, APIs, and custom functions, to enhance agent capabilities.
- Crew Orchestration: Define the workflow and collaboration strategy for the crew, specifying how agents interact and share information.
- Autonomous Execution: Allow agents to autonomously execute their tasks, leveraging their tools and collaborating with other agents as needed.
- Flexible LLM Support: Compatible with various Large Language Models (LLMs), including OpenAI, local models.
- Memory Management: Provides mechanisms for agents to retain and utilize past interactions, improving context and decision-making.
Basic Example
Setting up the Environment
Make sure you have an LLM model available for CrewAI to call. This example will use the mistralai/Mistral-7B-Instruct-v0.2
model available on Ollama. You can install it by running the following commands.
ollama pull mistralai/Mistral-7B-Instruct-v0.2
Code Example:
import os
from crewai import Crew, Agent, Task
from textwrap import dedent
os.environ["OPENAI_API_KEY"] = "YOUR_OPENAI_API_KEY" # If using OpenAI
os.environ["OPENAI_MODEL_NAME"] = "gpt-4" # for OpenAI usage
# Define your agents with roles, goals, and backstories
researcher = Agent(
role='Investigative Journalist',
goal='Uncover groundbreaking news stories',
backstory="A seasoned journalist with a knack for finding hidden connections and insights.",
verbose=True,
allow_delegation=True
)
writer = Agent(
role='Tech Writer',
goal='Craft compelling narratives about technology',
backstory="A skilled writer with a deep understanding of tech trends and a talent for storytelling.",
verbose=True,
allow_delegation=True
)
# Define tasks for your agents
task1 = Task(
description='Research the latest developments in AI-powered healthcare solutions.',
agent=researcher
)
task2 = Task(
description='Write an engaging article about the impact of AI on the future of medicine, based on the research provided.',
agent=writer
)
# Create a crew and assign tasks
crew = Crew(
agents=[researcher, writer],
tasks=[task1, task2],
verbose=2 # Set verbosity to 2 for detailed logs
)
# Kick off the crew's work
result = crew.kickoff()
print("Final Result:", result)
Running with Ollama (Local LLM)
To use CrewAI with Ollama, you need to adapt the agent definitions to specify the Ollama model. Ensure ollama Python package has been installed. Here's how you can modify the agent definitions:
import os
from crewai import Crew, Agent, Task
from textwrap import dedent
# Define your agents with roles, goals, and backstories
researcher = Agent(
role='Investigative Journalist',
goal='Uncover groundbreaking news stories',
backstory="A seasoned journalist with a knack for finding hidden connections and insights.",
verbose=True,
allow_delegation=True,
llm={"model": "mistralai/Mistral-7B-Instruct-v0.2"} # Specify the ollama model
)
writer = Agent(
role='Tech Writer',
goal='Craft compelling narratives about technology',
backstory="A skilled writer with a deep understanding of tech trends and a talent for storytelling.",
verbose=True,
allow_delegation=True,
llm={"model": "mistralai/Mistral-7B-Instruct-v0.2"} # Specify the ollama model
)
# Define tasks for your agents
task1 = Task(
description='Research the latest developments in AI-powered healthcare solutions.',
agent=researcher
)
task2 = Task(
description='Write an engaging article about the impact of AI on the future of medicine, based on the research provided.',
agent=writer
)
# Create a crew and assign tasks
crew = Crew(
agents=[researcher, writer],
tasks=[task1, task2],
verbose=2 # Set verbosity to 2 for detailed logs
)
# Kick off the crew's work
result = crew.kickoff()
print("Final Result:", result)
Use Cases
- Content Creation: Generate articles, blog posts, and marketing materials using a crew of writers, editors, and researchers.
- Research and Analysis: Conduct in-depth research on a topic, leveraging multiple researchers to gather and analyze data.
- Customer Support: Automate customer support interactions using a crew of support agents, each specializing in different areas.
- Software Development: Build software applications by coordinating developers, testers, and project managers as agents within a crew.
- Financial Analysis: Analyze financial data and generate reports using a crew of financial analysts and researchers.