Construct an AI Analysis Assistant utilizing CrewAI and Composio

Date:

Share post:

Introduction

With each iteration of the LLM growth, we’re nearing the age of AI brokers. An age the place AI brokers are deeply built-in into software program workflows, dealing with nearly all of duties, from automating private productiveness duties like scheduling conferences and managing emails to offering customized reminders and organizing every day to-do lists. On an enterprise stage, AI brokers can streamline operations by automating buyer help, optimizing provide chain logistics, enhancing information evaluation, and enhancing decision-making processes. This enables companies to function extra effectively, cut back prices, and focus human efforts on extra inventive duties. However to construct brokers that work just isn’t a straightforward job, particularly in manufacturing. A lot effort is being spent to construct the tooling ecosystem to make brokers helpful and dependable. This text will discover two such instruments, CrewAI and Composio, for constructing helpful AI brokers utilizing Claude Sonet.

Studying Goal

  • Perceive AI brokers.
  • Study CrewAI – a software for orchestrating AI brokers.
  • Discover Compoiso – a platform for integrating instruments with Brokers.
  • Construct an AI analysis assistant with Slack and Notion integration.

This text was printed as part of the Knowledge Science Blogathon.

What are AI Brokers?

The time period AI brokers is being utilized in each trendy AI discourse currently. So, what are AI brokers? The brokers are items of software program that may dynamically work together with their atmosphere by way of a set of instruments, and the “AI” in “AI agents” seek advice from the Giant Language Fashions or Giant Multi-modal Fashions.

As we all know, LLMs possess a condensed type of human data of their weights,  which permits them to investigate and purpose a fancy job step-by-step. When the LLMs have entry to the proper instruments, they’ll break down an issue assertion and use the proper instruments to execute duties as and when wanted. The most effective instance of this might be Chatgpt app itself. It has entry to the code interpreter, the Web, and Dalle. Primarily based on the given immediate, it decides what to make use of and when. So, AI brokers are LLMs augmented with instruments and targets.

Nevertheless, straightforwardly utilizing instruments with LLMs will not be sufficient for performing advanced duties. We have to orchestrate a number of brokers with particular instruments and targets.

What’s CrewAI?

CrewAI is an open-source software for orchestrating a number of AI brokers to perform advanced duties. It gives a collaborative method the place brokers can assume roles, delegate duties, and share targets, akin to a real-world crew. These are the core options of CrewAI.

  • Brokers: Brokers are autonomous items accountable for reasoning, job delegation, and speaking with different brokers akin to the staff members in a real-world staff.
  • Duties: Duties are particular assignments given to brokers. It particulars all of the steps and actions an agent must take to carry out a required goal.
  • Instruments: Instruments are essential to carry out duties which might be past the LLMs, comparable to net scraping, responding to emails, job scheduling, and many others.
  • Course of: The processes in CrewAI orchestrate the execution of duties by brokers. This ensures duties are distributed and executed effectively by brokers in a predefined method. The method is both sequential, the place duties are accomplished sequentially, or hierarchical, the place duties are executed primarily based on a managerial hierarchy.
  • Crews: The Crews in CrewAI are collaborative brokers with duties and instruments working in direction of carrying out advanced duties.

Here’s a thoughts map for CrewAI.

CrewAI and Composio

What’s Composio?

Composio is a platform that gives 100+ instruments, comparable to Slack, GitHub, Discord, and many others, with actions and triggers to combine with the AI workflows. It may be built-in with LangChain, Autogen, and CrewAI to make brokers helpful and dependable. This makes it a lot simpler for brokers to work with exterior apps. On this article, we’ll use the Slack and Notion instruments from Composio to construct an AI assistant that listens to a Slack channel and writes a full report on the subject to a Notion file. So, let’s hop on to the coding half.

Constructing an AI Analysis Assistant

Let’s outline the aim of our AI analysis assistant. So, our AI analysis assistant consists of a set of brokers working in collaboration to perform the duty. The assistant listens to a particular Slack channel by way of a Slack bot, and when a message is distributed to the channel, the AI crew springs into motion. It takes the Slack message as the primary job, sequentially distributes the duties to the analysis agent to supply urgent factors, an analyst to explain and increase on these factors, a Notion agent to create and write the contents to a textual content file, and at last, the Slack agent to reply in a Slack channel with the textual content doc.

Right here’s the visible illustration of the workflow.

CrewAI and Composio

So, right here’s how we will probably be constructing.

  • Arrange a growth atmosphere utilizing Venv or Poetry.
  • Arrange the Composio toolset for Notion and Slack.
  • Construct brokers utilizing CrewAI, Composio toolsets, and Anthropic’s Claude Sonnet (can use different fashions).
  • Construct a Flask server with Ngrok tunneling that listens to the Slack bot.

Step 1: Set-Up Setting

To begin, create a digital atmosphere and set up the dependencies listed beneath.

crewai[tools]==0.*
composio-crewai==0.2.*
composio-langchain==0.2.*
composio-core==0.2.*
flask==3.0.3
python-dotenv==1.0.1
langchain-anthropic

When you use Poetry, then clone the repository and run 

poetry set up
poetry shell

We additionally want a safe Ngrok tunnel for the native Flask server to obtain triggers from the Slack bot. Ngrok is a safe tunneling service that exposes the native server to the web for growth and testing actions. So, set up Ngrok, relying in your system OS. You could must create an account with them. As soon as downloaded, join port 2000 to Ngrok, the place we’ll host our Flask server.

ngrok http 2000

This could begin a ngrok tunnel to port 2000.

CrewAI and Composio

Additionally, we have to outline a number of atmosphere variables, an Anthropic API key, a Slack Channel ID, and a set off ID. So, create these variables in a .env file.

ANTHROPIC_API_KEY=

CHANNEL_ID=

TRIGGER_ID=

Add your API key and Slack channel ID. You’ll find the Slack channel ID from its URL, which often begins with “C*”; for instance, C074RUF0UQ5 is the channel ID of the channel https://app.slack.com/shopper/T074SRB4FGS/C073RUF0UQ5. We are going to set the TRIGGER_ID within the subsequent part.

Now, we have to arrange our Notion and Slack toolset from Composio. To take action, use the Composio CLI so as to add instruments.

poetry run composio-cli add notion
poetry run composio-cli add slack

Now, set the set off callback to the Ngrok URL. This can join the Slack bot to the Flask server by way of the Ngrok tunnel. 

poetry run composio-cli set global-trigger-callback "<ngrok-url>"

Substitute <ngrok-url> with the .app URL from Ngrok proven within the terminal.

Now, allow the Slack obtain set off. This can allow the Slack bot to obtain messages from the Slack channel and ship them to the tunneled server.

poetry run composio-cli enable-trigger slack_receive_message

It can output a TRIGGER_ID. Now, replace the identical in .env.

Construct Brokers with CrewAI

Now, let’s construct the brokers. On this setup, we outline 4 completely different brokers: a researcher to interrupt down the duties, an analyst to investigate the duties, a notion agent to write down content material to a textual content file, and a Slack agent to ship affirmation of the duty together with the textual content file to the Slack channel.

Outline Agent roles and Duties

First, we have to outline Agent roles and duties. We are able to outline these in two completely different Yaml recordsdata: the Yaml config recordsdata for Brokers.

researcher:
  position: >
    {matter} Senior Knowledge Researcher
  aim: >
    Uncover cutting-edge developments in {matter}
  backstory: >
    You are a seasoned researcher with a knack for uncovering the newest
    developments in {matter}. Identified on your skill to search out essentially the most related
    data and current it clearly and concisely.

reporting_analyst:
  position: >
    {matter} Reporting Analyst
  aim: >
    Create detailed experiences primarily based on {matter} information evaluation and analysis findings
  backstory: >
    You are a meticulous analyst with a eager eye for element. You are recognized for
    your skill to show advanced information into clear and concise experiences, making
    it simple for others to grasp and act on the knowledge you present.

notion_agent:
  position: >
    Notion Updater
  aim: >
    You are taking motion on Notion utilizing the Notion API
  backstory: >
    You might be AI agent that's accountable for taking actions on Notion on 
    customers behalf. You might want to take motion on Notion utilizing Notion APIs

slack_agent:
  position: >
    Slack Updater
  aim: >
    You are taking motion on Slack utilizing the Notion API
  backstory: >
    You might be an AI agent that's accountable for taking actions on Slack on 
    customers behalf. You might want to take motion on Slack utilizing Slack APIs

Outline the same file for duties.

research_task:
  description: >
    Conduct an intensive analysis about {matter}
    Be sure you discover any attention-grabbing and related data given
    the present yr is 2024.
  expected_output: >
    A listing with 10 bullet factors of essentially the most related details about {matter}.

reporting_task:
  description: >
    Evaluate the context you bought and increase every matter right into a full part for a report.
    Ensure the report is detailed and accommodates any and all related data.
  expected_output: >
    A totally fledge report with the primary matters, every with a full part of knowledge.
    Formated as markdown with out lacking something.

notion_task:
  description: >
    Write a doc on the Notion of the abstract of the given content material.
  expected_output: >
    A Notion doc with a title and contents.

slack_task:
  description: >
    Write a message on slack channel 'random' that summarizes the whole Crewai 
    analysis exercise. 
    Write a abstract 
    of your findings and connect the report.
  expected_output: >
    A Slack message with a abstract of complete actions completed and last output.  

Construct Brokers

Create a brand new file and import libraries and env variables.

import os

from composio_crewai import App, ComposioToolset
from crewai import Agent, Crew, Course of, Job
from crewai.undertaking import CrewBase, agent, crew, job
from langchain_anthropic import ChatAnthropic


ANTHROPIC_API_KEY = os.environ.get("ANTHROPIC_API_KEY")

if ANTHROPIC_API_KEY is None:
    print("Please set ANTHROPIC_API_KEY environment variable in the .env file")
    exit(1)

Outline LLM and Composio toolset.

llm = ChatAnthropic(
    model_name="claude-3-sonnet-20240229",
    api_key=ANTHROPIC_API_KEY
)

notion_composio_toolset = ComposioToolset(apps=[App.NOTION])
slack_composio_toolset = ComposioToolset(apps=[App.SLACK])

CrewAI gives decorators for brokers, duties, and crew to outline them conveniently.

@CrewBase
class ClientCrew:
    """Class representing the Client crew"""

    agents_config = "config/agents.yaml"
    tasks_config = "config/tasks.yaml"

    @agent
    def researcher(self) -> Agent:
        """Create a researcher agent"""
        return Agent(
            config=self.agents_config["researcher"],
            verbose=True,
            llm=llm,
        )

    @agent
    def reporting_analyst(self) -> Agent:
        """Create a reporting analyst agent"""
        return Agent(
            config=self.agents_config["reporting_analyst"], verbose=True, llm=llm
        )

    @agent
    def notion_agent(self) -> Agent:
        """Create a notion agent"""
        return Agent(
            config=self.agents_config["notion_agent"],
            verbose=True,
            instruments=notion_composio_toolset,
            llm=llm,
        )

    @agent
    def slack_agent(self) -> Agent:
        """Create a slack agent"""
        return Agent(
            config=self.agents_config["slack_agent"],
            verbose=True,
            instruments=slack_composio_toolset,
            llm=llm,
        )

    @job
    def research_task(self) -> Job:
        """Create a research task"""
        return Job(config=self.tasks_config["research_task"], agent=self.researcher())

    @job
    def reporting_task(self) -> Job:
        """Create a reporting task"""
        return Job(
            config=self.tasks_config["reporting_task"],
            agent=self.reporting_analyst(),
            output_file="report.md",
        )

    @job
    def notion_task(self) -> Job:
        """Create a notion task"""
        return Job(
            config=self.tasks_config["notion_task"],
            agent=self.notion_agent(),
            instruments=notion_composio_toolset,
        )

    @job
    def slack_task(self) -> Job:
        """Create a slack task"""
        return Job(
            config=self.tasks_config["slack_task"],
            agent=self.slack_agent(),
            instruments=slack_composio_toolset,
        )

    @crew
    def crew(self) -> Crew:
        """Create the Client crew"""
        return Crew(
            brokers=self.brokers, duties=self.duties, course of=Course of.sequential, verbose=2
        )

That is completed. We have now outlined the required Brokers, Duties, the Course of, and Crew.

Create Flask Server

 Now, we have to outline a Flask server. The server API has solely a single endpoint that receives the message from the bot and kicks off the Crew into motion.

# principal.py

import os

from dotenv import load_dotenv
from flask import Flask, request

load_dotenv()

from shopper import ClientCrew

app = Flask(__name__)

TRIGGER_ID = os.environ.get("TRIGGER_ID", None)
CHANNEL_ID = os.environ.get("CHANNEL_ID", None)

if TRIGGER_ID is None or CHANNEL_ID is None:
    print("Please set TRIGGER_ID and CHANNEL_ID environment variables in the .env file")
    exit(1)

def run_crew(matter: str):
    inputs = {"topic": matter}
    ClientCrew().crew().kickoff(inputs=inputs)

async def async_run_crew(channel, textual content, consumer):
    if channel == CHANNEL_ID:
        run_crew(textual content)
    return "Crew run initiated", 200


@app.route("https://www.analyticsvidhya.com/", strategies=["POST"])
async def webhook():
    payload = request.json

    message_payload = payload.get("payload", {})
    channel = message_payload.get("channel", "")

    if channel == CHANNEL_ID:
        print("Payload received", payload)

    textual content = message_payload.get("text", "")
    consumer = message_payload.get("user", "")

    return await async_run_crew(channel, textual content=textual content, consumer=consumer)


if __name__ == "__main__":
    app.run(port=2000, debug=True)

Now run the primary.py to fireplace up the Flask server on the localhost 2000 port the place the Ngrok has been configured.

python principal.py

Go to the Slack channel that you just chosen and ship a message. The server will immediately decide this up, and the Crew workflow will begin. The success of the duty will depend on the standard of the mannequin.  Larger and higher fashions like GPT-4/GPT-4o and Claude Opus are inclined to carry out higher. Sonnet does a great job.

The AI agent wrote this when requested to write down SRS for a React, NodeJs, and SQLite Chat App.

CrewAI and Composio

Right here is the repository of the codes: sunilkumardash9/CrewAIxComposio-Analysis-Assistant

Manner Ahead

We created an AI analysis assistant that may create a pleasant report of duties and replace it to a Notion file.  You’ll be able to add brokers with web entry by way of SERP instruments to make the assistant extra versatile. The Composio helps 100+ instruments with actions and triggers to let the brokers freely work together with third-party companies. You should utilize the instruments to make the brokers higher and extra helpful.

Conclusion

The event of AI brokers is occurring in full swing and is now the most well liked matter within the AI house. And because the tooling and LLM infrastructure improves, it may be anticipated the following era of software program programs may have AI brokers constructed into them. Many mundane workflows will probably be dealt with by AI brokers geared up with dependable and helpful instruments. We noticed a small glimpse whereas constructing our AI analysis assistant with Slack and Notion instruments integration.

Key Takeaways

  • AI brokers are LLMs or Giant Multi-modal Fashions enhanced with instruments and targets, enabling them to work together dynamically with their atmosphere and carry out duties.
  • CrewAI is an open-source software that orchestrates a number of AI brokers to collaboratively accomplish advanced duties by assigning roles, delegating duties, and sharing targets.
  • Composio is a platform providing over 100 instruments, comparable to Slack and GitHub, with actions and triggers for AI workflows. It seamlessly integrates with LangChain, Autogen, and CrewAI.
  • Combine Composio toolsets with AI agent frameworks like CrewAI to automate workflows that require planning and decision-making.

The media proven on this article just isn’t owned by Analytics Vidhya and is used on the Creator’s discretion.

Steadily Requested Questions

Q1. What are AI brokers?

A. AI brokers are LLMs or Giant Multi-modal Fashions enhanced with instruments and targets, enabling them to work together dynamically with their atmosphere and carry out duties.

Q2. What’s CrewAI?

A. CrewAI is an open-source agent orchestration framework for constructing role-playing and collaborative brokers. 

Q3. What’s Composio?

A. Composio is a platform that integrates environment friendly instruments with an agent framework for interacting with third-party companies like Discord, Slack, GitHub, and many others for carrying out advanced duties.

This autumn. What can CrewAI do?

A. CrewAI can create collaborative brokers that may plan, purpose, and delegate duties to different brokers akin to a real-world crew for carrying out duties.

Q5. What’s the distinction between CrewAI and Autogen?

A.  In Autogen, orchestrating brokers’ interactions requires further programming, which may turn out to be advanced and cumbersome as the size of duties grows.

Related articles

Sumer Johal, CEO of Almanac – Interview Collection

Sumer Johal is a world chief with over 25 years {of professional} expertise in constructing and managing digital-first...

Past Giant Language Fashions: How Giant Conduct Fashions Are Shaping the Way forward for AI

Synthetic intelligence (AI) has come a great distance, with giant language fashions (LLMs) demonstrating spectacular capabilities in pure...

Bridging the Hole: The Function of AI in Reworking Training

We discuss to Nhon Ma, CEO of Numerade.  Because the world adapts to the fast evolution of synthetic intelligence, the...

AI’s Life-Altering, Measurable Influence on Most cancers

Leveraging Huge Knowledge to Improve AI in Most cancers Detection and RemedyIntegrating AI into the healthcare resolution making...