Home AI Tools Build an AI Research Assistant Using CrewAI and Composio

Build an AI Research Assistant Using CrewAI and Composio

0
Build an AI Research Assistant Using CrewAI and Composio

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 degree, AI brokers can streamline operations by automating buyer help, optimizing provide chain logistics, enhancing knowledge evaluation, and enhancing decision-making processes. This permits companies to function extra effectively, cut back prices, and focus human efforts on extra inventive duties. However to construct brokers that work isn’t 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.

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 trendy AI discourse recently. So, what are AI brokers? The brokers are items of software program that may dynamically work together with their setting through a set of instruments, and the “AI” in “AI brokers” check with the Giant Language Fashions or Giant Multi-modal Fashions.

As we all know, LLMs possess a condensed type of human information of their weights,  which permits them to investigate and motive a posh activity step-by-step. When the LLMs have entry to the best instruments, they will break down an issue assertion and use the best instruments to execute duties as and when wanted. The very best instance of this may be Chatgpt app itself. It has entry to the code interpreter, the Web, and Dalle. Based mostly on the given immediate, it decides what to make use of and when. So, AI brokers are LLMs augmented with instruments and targets.

Nonetheless, straightforwardly utilizing instruments with LLMs might 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 supplies 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 chargeable for reasoning, activity delegation, and speaking with different brokers akin to the workforce members in a real-world workforce.
  • 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, corresponding 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 based mostly on a managerial hierarchy.
  • Crews: The Crews in CrewAI are collaborative brokers with duties and instruments working in direction of undertaking advanced duties.

Here’s a thoughts map for CrewAI.

CrewAI and Composio

What’s Composio?

Composio is a platform that gives 100+ instruments, corresponding 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.

Constructing an AI Analysis Assistant

Let’s outline the objective 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 distributed to the channel, the AI crew springs into motion. It takes the Slack message as the primary activity, sequentially distributes the duties to the analysis agent to offer 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 shall be constructing.

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

In case 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. It’s possible you’ll 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.

ngrok http 2000

This could begin a ngrok tunnel to port 2000.

CrewAI and Composio

Also, we have to outline a couple of setting 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 may join the Slack bot to the Flask server through 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 may 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 would 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 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 totally different Yaml information: the Yaml config information for Brokers.

researcher:
  function: >
    {matter} Senior Knowledge Researcher
  objective: >
    Uncover cutting-edge developments in {matter}
  backstory: >
    You are a seasoned researcher with a knack for uncovering the most recent
    developments in {matter}. Recognized to your means to search out essentially the most related
    info and current it clearly and concisely.

reporting_analyst:
  function: >
    {matter} Reporting Analyst
  objective: >
    Create detailed experiences based mostly on {matter} knowledge evaluation and analysis findings
  backstory: >
    You are a meticulous analyst with a eager eye for element. You are identified for
    your means to show advanced knowledge into clear and concise experiences, making
    it straightforward for others to know and act on the knowledge you present.

notion_agent:
  function: >
    Notion Updater
  objective: >
    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. You want to take motion on Notion utilizing Notion APIs

slack_agent:
  function: >
    Slack Updater
  objective: >
    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. You want to take motion on Slack utilizing Slack APIs

Outline an identical file for duties.

research_task:
  description: >
    Conduct an intensive analysis about {matter}
    Be sure to discover any fascinating and related info given
    the present yr is 2024.
  expected_output: >
    An inventory with 10 bullet factors of essentially the most related details about {matter}.

reporting_task:
  description: >
    Assessment the context you bought and broaden every matter right into a full part for a report.
    Ensure that the report is detailed and incorporates any and all related info.
  expected_output: >
    A totally fledge report with the primary 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 achieved and ultimate 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, Activity
from crewai.mission 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 setting 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 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) -> Activity:
        """Create a analysis activity"""
        return Activity(config=self.tasks_config["research_task"], agent=self.researcher())

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

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

    @activity
    def slack_task(self) -> Activity:
        """Create a slack activity"""
        return Activity(
            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 achieved. We’ve 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.

# most important.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 setting variables within the .env file")
    exit(1)

def run_crew(matter: str):
    inputs = {"matter": 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 obtained", payload)

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

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

python most important.py

Go to the Slack channel that you simply chosen and ship a message. The server will immediately choose 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 likely to carry out higher. Sonnet does 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 may 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 providers. You need to 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 popular matter within the AI area. And because the tooling and LLM infrastructure improves, it may be anticipated the subsequent era of software program methods could have AI brokers constructed into them. Many mundane workflows shall 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 Giant Multi-modal Fashions enhanced with instruments and targets, enabling them to work together dynamically with their setting 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, corresponding 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 isn’t owned by Analytics Vidhya and is used on the Writer’s discretion.

Incessantly 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 setting 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 extra programming, which might develop into advanced and cumbersome as the dimensions of duties grows.

Sunil Kumar Dash