20.2 C
New York
Wednesday, May 22, 2024

Construct an AI Analysis Assistant utilizing CrewAI and Composio


Introduction

With each iteration of the LLM improvement, we’re nearing the age of AI brokers. An age the place AI brokers are deeply built-in into software program workflows, dealing with the vast majority of duties, from automating private productiveness duties like scheduling conferences and managing emails to offering personalised reminders and organizing each day to-do lists. On an enterprise degree, AI brokers can streamline operations by automating buyer assist, optimizing provide chain logistics, enhancing information evaluation, and enhancing decision-making processes. This permits companies to function extra effectively, scale back prices, and focus human efforts on extra artistic duties. However to construct brokers that work is just not a simple process, 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.

CrewAI and Composio

Studying Goal

  • Perceive AI brokers.
  • Find out about 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 revealed as part of the Knowledge Science Blogathon.

What are AI Brokers?

The time period AI brokers is being utilized in each fashionable AI discourse currently. So, what are AI brokers? The brokers are items of software program that may dynamically work together with their atmosphere through a set of instruments, and the “AI” in “AI brokers” check with the Massive Language Fashions or Massive Multi-modal Fashions.

As we all know, LLMs possess a condensed type of human data of their weights,  which allows them to investigate and cause a posh process 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. One of the best 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 objectives.

Nevertheless, straightforwardly utilizing instruments with LLMs is probably not sufficient for performing complicated duties. We have to orchestrate a number of brokers with particular instruments and objectives.

What’s CrewAI?

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

  • Brokers: Brokers are autonomous models chargeable for reasoning, process 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, reminiscent of net scraping, responding to emails, process 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 undertaking complicated duties.

Here’s a thoughts map for CrewAI.

CrewAI and Composio

What’s Composio?

Composio is a platform that gives 100+ instruments, reminiscent of 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 purpose 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 through a Slack bot, and when a message is shipped to the channel, the AI crew springs into motion. It takes the Slack message as the principle process, sequentially distributes the duties to the analysis agent to supply urgent factors, an analyst to explain and broaden 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 can be constructing.

  • Arrange a improvement 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 Surroundings

To start out, create a digital atmosphere and set up the dependencies listed under.

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

If you happen to 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 improvement 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 could find the Slack channel ID from its URL, which normally begins with “C*”; for instance, C074RUF0UQ5 is the channel ID of the channel https://app.slack.com/consumer/T074SRB4FGS/C073RUF0UQ5. We’ll 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 through the Ngrok tunnel. 

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

Change <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’s going to 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 totally different brokers: a researcher to interrupt down the duties, an analyst to investigate the duties, a notion agent to jot 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 totally different Yaml recordsdata: the Yaml config recordsdata for Brokers.

researcher:
  function: >
    {subject} Senior Knowledge Researcher
  purpose: >
    Uncover cutting-edge developments in {subject}
  backstory: >
    You are a seasoned researcher with a knack for uncovering the newest
    developments in {subject}. Identified on your capability to seek out probably the most related
    data and current it clearly and concisely.

reporting_analyst:
  function: >
    {subject} Reporting Analyst
  purpose: >
    Create detailed experiences primarily based on {subject} information evaluation and analysis findings
  backstory: >
    You are a meticulous analyst with a eager eye for element. You are identified for
    your capability to show complicated information into clear and concise experiences, making
    it straightforward for others to know and act on the data you present.

notion_agent:
  function: >
    Notion Updater
  purpose: >
    You're taking motion on Notion utilizing the Notion API
  backstory: >
    You might be AI agent that's chargeable for taking actions on Notion on 
    customers behalf. It's essential to take motion on Notion utilizing Notion APIs

slack_agent:
  function: >
    Slack Updater
  purpose: >
    You're taking motion on Slack utilizing the Notion API
  backstory: >
    You might be an AI agent that's chargeable for taking actions on Slack on 
    customers behalf. It's essential to take motion on Slack utilizing Slack APIs

Outline an analogous file for duties.

research_task:
  description: >
    Conduct a radical analysis about {subject}
    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 probably the most related details about {subject}.

reporting_task:
  description: >
    Overview the context you bought and broaden every subject right into a full part for a report.
    Be certain that the report is detailed and accommodates any and all related data.
  expected_output: >
    A completely fledge report with the principle subjects, every with a full part of data.
    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 entire Crewai 
    analysis exercise. 
    Write a abstract 
    of your findings and fasten the report.
  expected_output: >
    A Slack message with a abstract of total actions accomplished 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, Process
from crewai.mission import CrewBase, agent, crew, process
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 atmosphere variable within 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 supplies decorators for brokers, duties, and crew to outline them conveniently.

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

    agents_config = "config/brokers.yaml"
    tasks_config = "config/duties.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,
        )

    @process
    def research_task(self) -> Process:
        """Create a analysis process"""
        return Process(config=self.tasks_config["research_task"], agent=self.researcher())

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

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

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

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

That is accomplished. We’ve got 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.

# major.py

import os

from dotenv import load_dotenv
from flask import Flask, request

load_dotenv()

from consumer 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 atmosphere variables within the .env file")
    exit(1)

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

async def async_run_crew(channel, textual content, person):
    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 acquired", payload)

    textual content = message_payload.get("textual content", "")
    person = message_payload.get("person", "")

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


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

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

python major.py

Go to the Slack channel that you just chosen and ship a message. The server will immediately choose 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 likely to carry out higher. Sonnet does an excellent job.

The AI agent wrote this when requested to jot 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 through 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 use the instruments to make the brokers higher and extra helpful.

Conclusion

The event of AI brokers is going on in full swing and is now the most well liked subject within the AI area. And because the tooling and LLM infrastructure improves, it may be anticipated the following technology of software program methods can have AI brokers constructed into them. Many mundane workflows can 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 Massive Multi-modal Fashions enhanced with instruments and objectives, 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 complicated duties by assigning roles, delegating duties, and sharing objectives.
  • Composio is a platform providing over 100 instruments, reminiscent of 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 is just not owned by Analytics Vidhya and is used on the Creator’s discretion.

Regularly Requested Questions

Q1. What are AI brokers?

A. AI brokers are LLMs or Massive Multi-modal Fashions enhanced with instruments and objectives, 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 undertaking complicated duties.

This autumn. What can CrewAI do?

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

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

A.  In Autogen, orchestrating brokers’ interactions requires extra programming, which might turn into complicated and cumbersome as the dimensions of duties grows.



Supply hyperlink

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles