Build an AI Research Assistant Using CrewAI and Composio

Published on:

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 the vast majority of duties, from automating private productiveness duties like scheduling conferences and managing emails to offering personalised reminders and organizing day by 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 permits companies to function extra effectively, scale back prices, and focus human efforts on extra inventive duties. However to construct brokers that work will not be a straightforward activity, 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.
  • 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 printed as part of the Information Science Blogathon.

What are AI Brokers?

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

- Advertisement -

As we all know, LLMs possess a condensed type of human information of their weights,  which permits them to investigate and motive a fancy activity step-by-step. When the LLMs have entry to the correct instruments, they will break down an issue assertion and use the correct instruments to execute duties as and when wanted. The perfect instance of this could 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 models chargeable for reasoning, activity delegation, and speaking with different brokers akin to the crew members in a real-world crew.
  • 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 can be past the LLMs, similar to internet scraping, responding to emails, activity scheduling, and so on.
  • 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 the direction of undertaking advanced duties.

Here’s a thoughts map for CrewAI.

- Advertisement -
CrewAI and Composio

What’s Composio?

Composio is a platform that gives 100+ instruments, similar to Slack, GitHub, Discord, and so on, 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 are going to 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.

See also  Duality AI releases FalconEditor digital twin simulation platform based on Unreal

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 selected Slack channel by way of 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 activity, sequentially distributes the duties to the analysis agent to offer 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 likely be constructing.

  • Arrange a growth surroundings 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 Atmosphere

To start out, create a digital surroundings 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

In the event 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 are going to host our Flask server.

- Advertisement -
ngrok http 2000

This could begin a ngrok tunnel to port 2000.

CrewAI and Composio

Additionally, we have to outline a couple of surroundings 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 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. It will join the Slack bot to the Flask server by way of the Ngrok tunnel. 

poetry run composio-cli set global-trigger-callback ""

Change with the .app URL from Ngrok proven within the terminal.

Now, allow the Slack obtain set off. It will 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’ll 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 will outline these in two completely different Yaml information: the Yaml config information for Brokers.

researcher:
  position: >
    {subject} Senior Information Researcher
  aim: >
    Uncover cutting-edge developments in {subject}
  backstory: >
    You are a seasoned researcher with a knack for uncovering the most recent
    developments in {subject}. Recognized to your capability to search out essentially the most related
    info and current it clearly and concisely.

reporting_analyst:
  position: >
    {subject} Reporting Analyst
  aim: >
    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 advanced information into clear and concise experiences, making
    it simple for others to know and act on the knowledge you present.

notion_agent:
  position: >
    Notion Updater
  aim: >
    You're taking motion on Notion utilizing the Notion API
  backstory: >
    You're AI agent that's chargeable for taking actions on Notion on 
    customers behalf. It is advisable to take motion on Notion utilizing Notion APIs

slack_agent:
  position: >
    Slack Updater
  aim: >
    You're taking motion on Slack utilizing the Notion API
  backstory: >
    You're an AI agent that's chargeable for taking actions on Slack on 
    customers behalf. It is advisable 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 to discover any attention-grabbing and related info given
    the present yr is 2024.
  expected_output: >
    An inventory with 10 bullet factors of essentially the most related details about {subject}.

reporting_task:
  description: >
    Assessment the context you bought and increase every subject right into a full part for a report.
    Be sure that the report is detailed and accommodates any and all related info.
  expected_output: >
    A totally fledge report with the principle matters, 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 executed and closing 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.challenge import CrewBase, agent, crew, activity
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 surroundings 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 gives decorators for brokers, duties, and crew to outline them conveniently.

@CrewBase
class ClientCrew:
    """Class representing the Consumer 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,
        )

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

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

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

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

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

That is executed. We’ve outlined the required Brokers, Duties, the Course of, and Crew.

See also  We need a Red Hat for AI

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.

# foremost.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 surroundings 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 fireside up the Flask server on the localhost 2000 port the place the Ngrok has been configured.

python foremost.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 depends upon 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

Approach 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 providers. You should use the instruments to make the brokers higher and extra helpful.

See also  EU AI legislation sparks controversy over data transparency

Conclusion

The event of AI brokers is occurring in full swing and is now the most popular subject within the AI area. And because the tooling and LLM infrastructure improves, it may be anticipated the following era of software program methods can have AI brokers constructed into them. Many mundane workflows will likely be dealt with by AI brokers outfitted 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 targets, enabling them to work together dynamically with their surroundings 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, similar 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 will not be owned by Analytics Vidhya and is used on the Writer’s discretion.

Steadily Requested Questions

Q1. What are AI brokers?

A. AI brokers are LLMs or Massive Multi-modal Fashions enhanced with instruments and targets, enabling them to work together dynamically with their surroundings 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 providers like Discord, Slack, GitHub, and so on for undertaking advanced duties.

This fall. What can CrewAI do?

A. CrewAI can create collaborative brokers that may plan, motive, 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 further programming, which might turn into advanced and cumbersome as the dimensions of duties grows.

Sunil Kumar Dash

- Advertisment -

Related

- Advertisment -

Leave a Reply

Please enter your comment!
Please enter your name here