Skip to content

Commit

Permalink
Add llamaindex groq agent
Browse files Browse the repository at this point in the history
  • Loading branch information
kaavee315 committed Sep 25, 2024
1 parent beee3ed commit 45b82e3
Show file tree
Hide file tree
Showing 10 changed files with 152 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GROQ_API_KEY=KEY
44 changes: 44 additions & 0 deletions python/examples/quickstarters/calendar_agent/llamaindex/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Calendar Agent
> Fork and Clone this repository if needed!
## Introduction
This project is an example which uses Composio to seamlessly convert your to-do lists into Google Calendar events.
It automatically schedules tasks with specified labels and times, ensuring your calendar is always up-to-date and organized.

## How it Works
### 1. Setup and Initialization
* Import packages, Load environment variables.
* Initialize the language model, Define Composio tools. We are using Google Calendar tool, So that our agent can execute actions using this tool.
* Retrieve Data and time and format them appropriately for scheduling.
### 2. Creating and Configuring the Agent
* Define a string todo with the tasks to be scheduled.
* Create the Agent with the role of "Google Calendar Agent", Provide the agent with a goal and backstory, emphasizing its responsibility to interact with Google Calendar APIs.
* Pass the tools obtained from ComposioToolSet and the initialized language model (llm) to the agent.
### 3. Defining the Task and Executing the Agent
* Task is instantiated with a description that includes the to-do list, current date, and timezone.
* Call `task.execute()` to have the agent carry out the task using the provided tools and language model.
## Steps to Run
**Navigate to the Project Directory:**
Change to the directory where the `setup.sh`, `main.py`, `requirements.txt`, and `README.md` files are located. For example:
```shell
cd path/to/project/directory
```

### 1. Run the Setup File
Make the setup.sh Script Executable (if necessary):
On Linux or macOS, you might need to make the setup.sh script executable:
```shell
chmod +x setup.sh
```
Execute the setup.sh script to set up the environment, install dependencies, login to composio and
add necessary tools:
```shell
./setup.sh
```
Now, Fill in the .env file with your secrets.

### 2. Run the python script
```shell
python cookbook/examples/calendar_agent/main.py
```
A new event has been added to our Google calendar!
66 changes: 66 additions & 0 deletions python/examples/quickstarters/calendar_agent/llamaindex/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import os
import dotenv
#from textwrap import dedent
from composio_llamaindex import Action, App, ComposioToolSet
from composio_llamaindex import App, ComposioToolSet, Action
from llama_index.core.agent import FunctionCallingAgentWorker
from llama_index.core.llms import ChatMessage
from llama_index.llms.groq import Groq
from datetime import datetime
from llama_index.core import Settings

# Load environment variables from .env file
dotenv.load_dotenv()
Settings.llm = Groq(model="llama3-groq-70b-8192-tool-use-preview", api_key=os.environ["GROQ_API_KEY"])
llm = Groq(model="llama-3.2-3b-preview", api_key=os.environ["GROQ_API_KEY"])


# Initialize the ComposioToolSet
toolset = ComposioToolSet()

# Get the RAG tool from the Composio ToolSet
tools = toolset.get_tools(apps=[App.GOOGLECALENDAR])

# Retrieve the current date and time
date = datetime.today().strftime("%Y-%m-%d")
timezone = datetime.now().astimezone().tzinfo

# Setup Todo
todo = """
1PM - 3PM -> Code,
5PM - 7PM -> Meeting,
9AM - 12AM -> Learn something,
8PM - 10PM -> Game
"""

# Define the RAG Agent
prefix_messages = [
ChatMessage(
role="system",
content=(
"""
You are an AI agent responsible for taking actions on Google Calendar on users' behalf.
You need to take action on Calendar using Google Calendar APIs. Use correct tools to run APIs from the given tool-set.
"""
),
)
]

# Initialize a FunctionCallingAgentWorker with the tools, LLM, and system messages
agent = FunctionCallingAgentWorker(
tools=tools, # Tools available for the agent to use
llm=llm, # Language model for processing requests
prefix_messages=prefix_messages, # Initial system messages for context
max_function_calls=10, # Maximum number of function calls allowed
allow_parallel_tool_calls=False, # Disallow parallel tool calls
verbose=True, # Enable verbose output
).as_agent()

response = agent.chat(
"""
Book slots according to {todo}.
Properly Label them with the work provided to be done in that time period.
Schedule it for today. Today's date is {date} (it's in YYYY-MM-DD format)
and make the timezone be {timezone}.
"""
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
llama-index
composio-llamaindex
llama-index-llms-groq
python-dotenv

36 changes: 36 additions & 0 deletions python/examples/quickstarters/calendar_agent/llamaindex/setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

#!/bin/bash

# Create a virtual environment
echo "Creating virtual environment..."
python3 -m venv ~/.venvs/calendar_agent

# Activate the virtual environment
echo "Activating virtual environment..."
source ~/.venvs/calendar_agent/bin/activate

# Install libraries from requirements.txt
echo "Installing libraries from requirements.txt..."
pip install -r requirements.txt

# Login to your account
echo "Login to your Composio account"
composio login

# Add calendar tool
echo "Add Google calendar tool. Finish the flow"
composio add googlecalendar

# Copy env backup to .env file
if [ -f ".env.example" ]; then
echo "Copying .env.example to .env..."
cp .env.example .env
else
echo "No .env.example file found. Creating a new .env file..."
touch .env
fi

# Prompt user to fill the .env file
echo "Please fill in the .env file with the necessary environment variables."

echo "Setup completed successfully!"

0 comments on commit 45b82e3

Please sign in to comment.