<RETURN_TO_BASE

Building a Collaborative Multi-Agent AI Pipeline with CrewAI and Gemini on Google Colab

A detailed tutorial on setting up a multi-agent AI pipeline with CrewAI and Google Gemini in Colab, featuring research, analysis, and content creation agents working together.

Setting Up the Multi-Agent AI System

This tutorial guides you through building an end-to-end AI agent system using CrewAI and Google's Gemini models within Google Colab. The process begins with installing necessary packages, securing the Gemini API key, and then creating specialized agents focused on research, data analysis, content creation, and quality assurance. These agents work sequentially to efficiently complete complex tasks.

Installing Required Packages

The environment is prepared by installing CrewAI, Gemini client libraries, and related tools. The following script automates this process:

import subprocess
import sys
import os
 
def install_packages():
    """Install required packages in Colab"""
    packages = [
        "crewai",
        "crewai-tools",
        "google-generativeai",
        "python-dotenv",
        "langchain-google-genai"
    ]
   
    for package in packages:
        try:
            print(f" Installing {package}...")
            subprocess.check_call([sys.executable, "-m", "pip", "install", package, "-q"])
            print(f" {package} installed successfully!")
        except Exception as e:
            print(f" Failed to install {package}: {e}")
 
print(" Setting up Google Colab environment...")
install_packages()
print(" All packages installed!")

Configuring the Gemini API Key

The system retrieves the Gemini API key securely from Colab secrets or prompts the user to input it. This key authenticates access to Google's Gemini language model.

def setup_api_key():
    """Setup Gemini API key in Colab"""
    try:
        api_key = userdata.get('GEMINI_API_KEY')
        print(" API key loaded from Colab secrets!")
        return api_key
    except:
        print(" Gemini API key not found in Colab secrets.")
        print("Please follow these steps:")
        print("1. Go to https://makersuite.google.com/app/apikey")
        print("2. Create a free API key")
        print("3. In Colab, go to  (Secrets) in the left sidebar")
        print("4. Add a new secret named 'GEMINI_API_KEY' with your API key")
        print("5. Enable notebook access for the secret")
        print("6. Re-run this cell")
       
        from getpass import getpass
        api_key = getpass("Or enter your Gemini API key here (it will be hidden): ")
        return api_key
 
GEMINI_API_KEY = setup_api_key()

Defining the Multi-Agent System

The core of the pipeline is the ColabGeminiAgentSystem class. It initializes the Gemini API, sets up file reading tools, and creates four specialized agents:

  • Research Analyst: Conducts detailed research
  • Data Analyst: Performs data-driven analysis
  • Content Creator: Crafts engaging and accessible content
  • Quality Assurance Specialist: Ensures output quality

Each agent is configured with specific roles, goals, and memory capabilities for sequential collaboration.

Creating and Executing Tasks

The system supports multiple task types:

  • Comprehensive: Full research, analysis, content creation, and QA
  • Quick: Fast, concise analysis and summary
  • Analysis: In-depth data analysis

Tasks are created and assigned to relevant agents, then executed sequentially, enabling efficient multi-agent cooperation.

Interactive Command Interface

Users can interact with the system via commands such as:

  • research [topic] for comprehensive research
  • quick [topic] for quick analysis
  • analyze [topic] for deep analysis
  • history to view past results
  • help to display commands

This interface makes the notebook a flexible sandbox for on-demand AI research projects.

Utilities and Result Management

Helper functions allow downloading results, saving to Google Drive, and displaying formatted Markdown summaries, enhancing usability and sharing capabilities.

Demonstrations and Examples

The tutorial includes demonstrations running quick and deep analyses on topics like machine learning and sustainable energy, showcasing the system's versatility.

Example Usage

# Quick analysis
result = agent_system.execute_colab_project("Machine Learning Trends", "quick")
print(result)
 
# Show formatted result
ColabUtils.display_formatted_result(result)

Tips and Troubleshooting

  • Use "quick" tasks for faster execution
  • Results save automatically
  • Restart runtime if facing memory issues
  • Check API rate limits if errors occur

This framework empowers users to build scalable AI research and content pipelines with minimal setup, leveraging powerful language models and multi-agent collaboration within Google Colab.

🇷🇺

Сменить язык

Читать эту статью на русском

Переключить на Русский