In this section, we will provide hands-on examples for building AI agents using CrewAI, Swarm AI, LangChain, Google Vertex AI, and Hugging Face Transformers. Each framework will demonstrate:


1. Developing AI Agents with CrewAI

CrewAI is a multi-agent collaboration framework that allows AI agents to work together and execute tasks autonomously. It is ideal for workflow automation, research analysis, and task delegation.

Installation

pip install crewai langchain openai

Use Case: AI Agents for News Collection & Analysis

This example creates a news fetcher agent that collects the latest AI news and an analyst agent that summarizes key insights.

Define Function for External Tool Calling

import requests

def fetch_latest_news():
    url = "<https://newsapi.org/v2/everything?q=artificial%20intelligence&apiKey=your_newsapi_key>"
    response = requests.get(url).json()
    return response["articles"][:5]  # Return top 5 AI news articles

Developing CrewAI Agents

from crewai import Crew, Agent, Task
from langchain.chat_models import ChatOpenAI

# Define AI agents
news_fetcher = Agent(
    role="News Collector",
    goal="Fetch the latest AI news from the web",
    tools=[fetch_latest_news],  # Assign function calling capability
    model=ChatOpenAI(model="gpt-4-turbo")
)

news_analyst = Agent(
    role="News Analyst",
    goal="Analyze AI news and provide key insights",
    model=ChatOpenAI(model="gpt-4-turbo")
)

# Define tasks
task1 = Task(description="Fetch latest AI news", agent=news_fetcher)
task2 = Task(description="Analyze AI news trends", agent=news_analyst)

# Create AI Crew
crew = Crew(agents=[news_fetcher, news_analyst], tasks=[task1, task2])

# Execute AI pipeline
crew.kickoff()

Expected Output

News Collector: Found 5 latest AI news articles.
News Analyst: Key insights: "Breakthroughs in AI research and emerging trends in multimodal AI."


2. Developing AI Agents with Swarm AI

Swarm AI is based on swarm intelligence, where multiple AI agents collaborate to solve problems dynamically.