Skip to content

add crewai notebook #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
308 changes: 308 additions & 0 deletions Agents/CrewAI/CrewAI_ResearchWrite_Agents.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,308 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Agents with CrewAI and Together\n",
"\n",
"[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/togethercomputer/together-cookbook/blob/main/Agents/CrewAI/CrewAI_ResearchWrite_Agents.ipynb)\n",
"\n",
"<img src=\"../../images/crewai.png\" width=\"700\">\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"## Introduction\n",
"This notebook demonstrates how to use CrewAI to create a research and content generation pipeline. CrewAI is a framework for orchestrating role-playing autonomous AI agents that can work together to complete complex tasks. In this example, we'll see how a researcher, writer, and manager collaborate to generate article ideas and content.\n",
"\n",
"Adapted from [source](https://docs.crewai.com/how-to/custom-manager-agent)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Setup and Installation\n",
"\n",
"First, let's install the necessary packages:"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[31mERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.\n",
"langchain-together 0.3.0 requires langchain-openai<0.4,>=0.3, but you have langchain-openai 0.2.14 which is incompatible.\u001b[0m\u001b[31m\n",
"\u001b[0m"
]
}
],
"source": [
"# CrewAI is the framework for creating agent-based workflows\n",
"# crewai-tools provides additional functionality for the agents\n",
"!pip install -qU crewai crewai-tools"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Setting up the Language Model\n",
"\n",
"We'll use Llama-3.3-70B-Instruct-Turbo from Together.ai as our base language model. This powerful model will drive all the agents in our crew."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"from crewai import LLM\n",
"\n",
"\n",
"llm = LLM(model=\"together_ai/meta-llama/Llama-3.3-70B-Instruct-Turbo\",\n",
" api_key=os.environ.get(\"TOGETHER_API_KEY\"),\n",
" base_url=\"https://api.together.xyz/v1\"\n",
" )"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Defining Our Agents\n",
"\n",
"In this workflow, we'll create three specialized agents:\n",
"\n",
"1. **Researcher** - Responsible for gathering information and generating article ideas\n",
"2. **Writer** - Takes research and transforms it into compelling content\n",
"3. **Manager** - Oversees the process and ensures quality completion of tasks\n",
"\n",
"Each agent has a specific role, goal, and backstory that influences how they approach their tasks."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"from crewai import Agent, Task, Crew, Process\n",
"\n",
"# Define your agents\n",
"researcher = Agent(\n",
" llm = llm,\n",
" role=\"Researcher\",\n",
" goal=\"Conduct thorough research and analysis on AI and AI agents\",\n",
" backstory=\"You're an expert researcher, specialized in technology, software engineering, AI, and startups. You work as a freelancer and are currently researching for a new client.\",\n",
" allow_delegation=False,\n",
")\n",
"\n",
"writer = Agent(\n",
" llm = llm,\n",
" role=\"Senior Writer\",\n",
" goal=\"Create compelling content about AI and AI agents\",\n",
" backstory=\"You're a senior writer, specialized in technology, software engineering, AI, and startups. You work as a freelancer and are currently writing content for a new client.\",\n",
" allow_delegation=False,\n",
")\n",
"\n",
"# Define the manager agent\n",
"manager = Agent(\n",
" llm = llm,\n",
" role=\"Project Manager\",\n",
" goal=\"Efficiently manage the crew and ensure high-quality task completion\",\n",
" backstory=\"You're an experienced project manager, skilled in overseeing complex projects and guiding teams to success. Your role is to coordinate the efforts of the crew members, ensuring that each task is completed on time and to the highest standard.\",\n",
" allow_delegation=True,\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Defining the Task\n",
"\n",
"Now we need to define the specific task our crew will work on. This serves as the \"mission\" that the agents will collaborate to complete."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Define your task\n",
"task = Task(\n",
" description=\"Generate a list of 5 interesting ideas for an article, then write one captivating paragraph for each idea that showcases the potential of a full article on this topic. Return the list of ideas with their paragraphs and your notes.\",\n",
" expected_output=\"5 bullet points, each with a paragraph and accompanying notes.\",\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Creating and Running the Crew\n",
"\n",
"Now we'll assemble our agents into a crew and set them to work on our task. We're using a hierarchical process, which means the manager agent will coordinate the work of the other agents."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[1m\u001b[94m \n",
"[2025-03-09 16:27:16][🚀 CREW 'CREW' STARTED, E573DE83-F7E0-4C4B-86CF-94E0536678BC]: 2025-03-09 16:27:16.767151\u001b[00m\n",
"\u001b[1m\u001b[94m \n",
"[2025-03-09 16:27:16][📋 TASK STARTED: GENERATE A LIST OF 5 INTERESTING IDEAS FOR AN ARTICLE, THEN WRITE ONE CAPTIVATING PARAGRAPH FOR EACH IDEA THAT SHOWCASES THE POTENTIAL OF A FULL ARTICLE ON THIS TOPIC. RETURN THE LIST OF IDEAS WITH THEIR PARAGRAPHS AND YOUR NOTES.]: 2025-03-09 16:27:16.790736\u001b[00m\n",
"\u001b[1m\u001b[94m \n",
"[2025-03-09 16:27:16][🤖 AGENT 'PROJECT MANAGER' STARTED TASK]: 2025-03-09 16:27:16.800310\u001b[00m\n",
"\u001b[1m\u001b[94m \n",
"[2025-03-09 16:27:16][🤖 LLM CALL STARTED]: 2025-03-09 16:27:16.800451\u001b[00m\n",
"\u001b[1m\u001b[94m \n",
"[2025-03-09 16:27:19][✅ LLM CALL COMPLETED]: 2025-03-09 16:27:19.333287\u001b[00m\n",
"\u001b[1m\u001b[94m \n",
"[2025-03-09 16:27:19][🤖 TOOL USAGE STARTED: 'DELEGATE WORK TO COWORKER']: 2025-03-09 16:27:19.336672\u001b[00m\n",
"\u001b[1m\u001b[94m \n",
"[2025-03-09 16:27:19][🤖 AGENT 'RESEARCHER' STARTED TASK]: 2025-03-09 16:27:19.386853\u001b[00m\n",
"\u001b[1m\u001b[94m \n",
"[2025-03-09 16:27:19][🤖 LLM CALL STARTED]: 2025-03-09 16:27:19.386930\u001b[00m\n",
"\u001b[1m\u001b[94m \n",
"[2025-03-09 16:27:23][✅ LLM CALL COMPLETED]: 2025-03-09 16:27:23.577965\u001b[00m\n",
"\u001b[1m\u001b[94m \n",
"[2025-03-09 16:27:23][✅ AGENT 'RESEARCHER' COMPLETED TASK]: 2025-03-09 16:27:23.583530\u001b[00m\n",
"\u001b[1m\u001b[94m \n",
"[2025-03-09 16:27:23][✅ TOOL USAGE FINISHED: 'DELEGATE WORK TO COWORKER']: 2025-03-09 16:27:23.615377\u001b[00m\n",
"\u001b[1m\u001b[94m \n",
"[2025-03-09 16:27:23][🤖 LLM CALL STARTED]: 2025-03-09 16:27:23.615440\u001b[00m\n",
"\u001b[1m\u001b[94m \n",
"[2025-03-09 16:27:25][✅ LLM CALL COMPLETED]: 2025-03-09 16:27:25.723077\u001b[00m\n",
"\u001b[1m\u001b[94m \n",
"[2025-03-09 16:27:25][🤖 TOOL USAGE STARTED: 'DELEGATE WORK TO COWORKER']: 2025-03-09 16:27:25.729508\u001b[00m\n",
"\u001b[1m\u001b[94m \n",
"[2025-03-09 16:27:25][🤖 AGENT 'SENIOR WRITER' STARTED TASK]: 2025-03-09 16:27:25.757925\u001b[00m\n",
"\u001b[1m\u001b[94m \n",
"[2025-03-09 16:27:25][🤖 LLM CALL STARTED]: 2025-03-09 16:27:25.757977\u001b[00m\n",
"\u001b[1m\u001b[94m \n",
"[2025-03-09 16:27:35][✅ LLM CALL COMPLETED]: 2025-03-09 16:27:35.059673\u001b[00m\n",
"\u001b[1m\u001b[94m \n",
"[2025-03-09 16:27:35][✅ AGENT 'SENIOR WRITER' COMPLETED TASK]: 2025-03-09 16:27:35.077983\u001b[00m\n",
"\u001b[1m\u001b[94m \n",
"[2025-03-09 16:27:35][✅ TOOL USAGE FINISHED: 'DELEGATE WORK TO COWORKER']: 2025-03-09 16:27:35.092882\u001b[00m\n",
"\u001b[1m\u001b[94m \n",
"[2025-03-09 16:27:35][🤖 LLM CALL STARTED]: 2025-03-09 16:27:35.092933\u001b[00m\n",
"\u001b[1m\u001b[94m \n",
"[2025-03-09 16:27:40][✅ LLM CALL COMPLETED]: 2025-03-09 16:27:40.441844\u001b[00m\n",
"\u001b[1m\u001b[94m \n",
"[2025-03-09 16:27:40][✅ AGENT 'PROJECT MANAGER' COMPLETED TASK]: 2025-03-09 16:27:40.448676\u001b[00m\n",
"\u001b[1m\u001b[94m \n",
"[2025-03-09 16:27:40][✅ TASK COMPLETED: GENERATE A LIST OF 5 INTERESTING IDEAS FOR AN ARTICLE, THEN WRITE ONE CAPTIVATING PARAGRAPH FOR EACH IDEA THAT SHOWCASES THE POTENTIAL OF A FULL ARTICLE ON THIS TOPIC. RETURN THE LIST OF IDEAS WITH THEIR PARAGRAPHS AND YOUR NOTES.]: 2025-03-09 16:27:40.448859\u001b[00m\n",
"\u001b[1m\u001b[94m \n",
"[2025-03-09 16:27:40][✅ CREW 'CREW' COMPLETED, E573DE83-F7E0-4C4B-86CF-94E0536678BC]: 2025-03-09 16:27:40.476162\u001b[00m\n"
]
}
],
"source": [
"# Instantiate your crew with a custom manager\n",
"crew = Crew(\n",
" agents=[researcher, writer],\n",
" tasks=[task],\n",
" manager_agent=manager,\n",
" process=Process.hierarchical,\n",
")\n",
"\n",
"# Start the crew's work\n",
"result = crew.kickoff()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"* **\"The Future of Work: How AI Agents Are Revolutionizing Industries\"**: The first article idea, \"The Future of Work: How AI Agents Are Revolutionizing Industries,\" has the potential to explore the significant impact of artificial intelligence on various sectors, from healthcare to finance. For instance, AI-powered chatbots are being used in customer service to provide 24/7 support, freeing human employees to focus on more complex tasks. As AI agents continue to advance, we can expect to see even more innovative applications, such as automated data analysis and predictive maintenance, which will undoubtedly change the way we work and interact with technology. A full article on this topic could delve into the benefits and challenges of implementing AI agents in different industries, featuring case studies and expert insights to provide a comprehensive understanding of this emerging trend. \n",
"Notes: This article could include interviews with industry experts, case studies of successful AI implementations, and discussions of the potential challenges and limitations of AI in the workplace.\n",
"\n",
"* **\"Demystifying AI: Understanding the Basics of Artificial Intelligence\"**: The second article idea, \"Demystifying AI: Understanding the Basics of Artificial Intelligence,\" aims to introduce readers to the fundamental concepts of AI, making it an excellent opportunity to explain complex terms like machine learning, natural language processing, and deep learning in an accessible way. By using relatable examples, such as virtual assistants like Siri or Alexa, the article can illustrate how AI is already an integral part of our daily lives, from simple tasks like setting reminders to more sophisticated applications like image recognition. A deeper exploration of this topic could include the history of AI, its current state, and future directions, providing readers with a solid foundation to appreciate the potential and limitations of AI agents.\n",
"Notes: This article could include interactive elements, such as quizzes or infographics, to help readers understand complex AI concepts and provide resources for further learning.\n",
"\n",
"* **\"AI for Social Good: How Artificial Intelligence Is Being Used to Drive Positive Change\"**: The third article idea, \"AI for Social Good: How Artificial Intelligence Is Being Used to Drive Positive Change,\" highlights the potential of AI to address some of the world's most pressing challenges, such as climate change, inequality, and access to education. For example, AI-powered systems are being used to analyze satellite images to monitor deforestation, while others are helping to personalize learning experiences for students with disabilities. A full article on this topic could showcase a range of initiatives and projects that demonstrate the positive impact of AI, discussing the opportunities and challenges of using AI for social good, and featuring interviews with experts and innovators in the field to provide a nuanced understanding of this critical area.\n",
"Notes: This article could include examples of AI-powered projects and initiatives, discussions of the challenges and limitations of using AI for social good, and interviews with experts and innovators in the field.\n",
"\n",
"* **\"The Ethics of AI: Navigating the Complexities of Artificial Intelligence Development\"**: The fourth article idea, \"The Ethics of AI: Navigating the Complexities of Artificial Intelligence Development,\" tackles the crucial issue of ensuring that AI systems are designed and deployed in ways that are fair, transparent, and accountable. As AI agents become increasingly autonomous, questions arise about their decision-making processes, potential biases, and consequences for society. By exploring real-world examples, such as the use of facial recognition technology in law enforcement or the development of autonomous vehicles, the article can illustrate the ethical dilemmas that arise from AI development and use. A comprehensive examination of this topic could discuss the need for diverse and inclusive teams in AI development, the importance of explainability and transparency in AI decision-making, and the role of regulation and governance in ensuring that AI benefits society as a whole.\n",
"Notes: This article could include discussions of the ethical implications of AI development and use, examples of AI systems that have raised ethical concerns, and interviews with experts in AI ethics.\n",
"\n",
"* **\"The Future of Human-AI Collaboration: How Artificial Intelligence Agents Will Change the Way We Work and Live\"**: The fifth article idea, \"The Future of Human-AI Collaboration: How Artificial Intelligence Agents Will Change the Way We Work and Live,\" envisions a future where humans and AI systems collaborate seamlessly to achieve common goals, enhancing productivity, creativity, and innovation. By examining current examples of human-AI collaboration, such as in fields like scientific research, healthcare, or the arts, the article can demonstrate how AI agents can augment human capabilities, freeing us to focus on high-level thinking, strategy, and empathy. A deeper exploration of this topic could speculate on the long-term implications of human-AI collaboration, discussing the potential for AI to enhance human cognition, the need for new forms of education and training, and the possibilities for AI to facilitate global cooperation and problem-solving, ultimately leading to a more harmonious and productive relationship between humans and technology.\n",
"Notes: This article could include examples of current human-AI collaboration, discussions of the potential benefits and challenges of human-AI collaboration, and speculations on the long-term implications of this emerging trend.\n"
]
}
],
"source": [
"print(result.raw)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Applications and Extensions\n",
"\n",
"This pattern can be extended to many content creation workflows:\n",
"- Marketing content creation\n",
"- Research paper drafting\n",
"- Technical documentation\n",
"- Product descriptions\n",
"- Market analysis reports\n",
"\n",
"By adjusting the agents' roles, goals, and tasks, you can create specialized workflows for various content needs."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.11"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Binary file added images/crewai.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.