Test the Agent here
A sophisticated Telegram agent powered by a ReAct-based AI model. It leverages a vector database for efficient semantic search and is designed to be deployed on AWS. The agent can understand and respond to user queries, providing information about restaurants in Barcelona.
The Restaurants Agent is a conversational AI designed to provide information about restaurants. It uses a combination of a powerful language model (via Groq), a PostgreSQL vector database (PGVector), and a set of custom tools to understand and respond to user queries. The agent is exposed via a FastAPI application and includes a reference implementation for a Telegram Bot.
This section will guide you through setting up the project for both local development and a containerized Docker environment.
Before you begin, you need to gather several API keys and credentials.
-
Clone the repository:
git clone https://github.com/your-username/whatsapp-agent.git cd whatsapp-agent -
Create the Environment File: Navigate to the agent service directory, copy the example environment file, and prepare to populate it.
cd services/agent cp .env.example .env -
Populate
.envFile: Open the.envfile with your favorite editor. You will need the following keys:GROQ_API_KEY: Get your API key from the Groq Console.API_KEY_BOT_TELEGRAM: Create a new bot and get the token from @BotFather on Telegram. Instructions here.API_KEY_GOOGLE_MAPS: Get a free API key from the Google Maps Platform.API_KEY_GOOGLE_GENAI: The agent uses Google'stext-embedding-004model. Create your key from the Google AI Studio.DATABASE_SERVICE_URL: We recommend Supabase for a free, hosted PGVector database. Create a project and find your connection string underOrganization > Project > Connect.SECRET_TOKEN: Generate a secure random string using a tool like the IT Tools Token Generator. This is used to secure the webhook between Telegram and the agent.- Observability: To track the agent's performance, you can use Comet's Opik. Get your
API_KEY_OPIK,OPIK_WORKSPACE, andOPIK_PROJECT_NAMEfrom the Comet Opik Docs.
Note: You can leave
TABLE_NAME,EMBEDDING_DIMENSIONS,TIME_PARTITION_INTERVAL, andOPENAI_API_KEYwith their default values for now.
This setup is ideal for contributing to the project's code.
-
Install
uv: We useuvfor Python package management. Follow the official installation instructions. -
Install Dependencies: From the
services/agentdirectory, create a virtual environment and install the required packages.# In services/agent/ uv venv uv sync -
Run the Agent: Once the dependencies are installed and your
.envfile is configured, you can start the agent.# Make sure your virtual environment is activated source .venv/bin/activate # Run the FastAPI server uvicorn api:app --host 0.0.0.0 --port 8000 --reload
This is the recommended way to run the entire application stack, including the database.
-
Ensure Prerequisites are Met: Make sure you have cloned the repository and fully configured the
.envfile as described in the Prerequisites section. -
Start the Database: This command starts a PostgreSQL container with the PGVector extension.
docker-compose -f docker-compose/docker-compose-pgvector.yaml up -d
-
Build and Run the Agent: This builds the agent's Docker image and runs it, connecting it to the environment variables.
cd services/agent docker build -t restaurants-agent . docker run -p 80:80 --env-file .env restaurants-agent
The core of the project is the ReActAgent, a custom implementation of the ReAct (Reasoning and Acting) framework. It processes user input, decides which tools to use, and generates a response.
api.py: A FastAPI application that exposes the agent's functionality via API endpoints.react_agent.py: TheReActAgentimplementation. It uses a language model for reasoning and its memory is connected to the PGVector database.tools.py: Defines the tools the agent can use, such as searching for restaurants or getting coordinates.
This service populates the vector database with restaurant information using the Google Maps API.
upload_place.py: Contains the main logic for fetching and processing data from the Google Maps API.googlemaps_api.py: A wrapper for the Google Maps API.place.py: Defines thePlacedata structure for restaurants.
This service provides semantic and keyword search capabilities over the vector database.
vector_store.py: TheVectorStoreclass, which provides a high-level interface for the PGVector database.distance_coordinates.py: A utility for location-based searches.
A reference implementation of a Telegram bot that interacts with the agent.
bot.py: The main logic for the Telegram bot, built withaiogram.config.py: Handles configuration for the bot.
To connect your bot to the running agent, you need to set a webhook.
-
Set Webhook:
curl -X POST "https://api.telegram.org/bot<YOUR_TELEGRAM_TOKEN>/setWebhook" \ -H "Content-Type: application/json" \ -d '{"url": "YOUR_AGENT_ENDPOINT_URL", "secret_token": "YOUR_SECRET_TOKEN"}'
-
Delete Webhook:
curl -X POST "https://api.telegram.org/bot<YOUR_TELEGRAM_TOKEN>/deleteWebhook"
- A user sends a message to the Telegram bot.
- The bot forwards the message to the agent's API endpoint via the configured webhook.
- The
ReActAgentreceives the message, understands the user's intent, and decides which tool to use. - If a restaurant search is needed, the agent queries the
search_engineservice. - The
search_engineperforms a semantic search in the PGVector database and returns the most relevant results. - The agent uses the results to formulate a helpful response and sends it back to the user via the Telegram API.
The project is designed for deployment on an AWS EC2 instance, optionally behind a Load Balancer. The included GitHub Actions workflow (.github/workflows/deployment.yaml) automates this process.
The CI/CD pipeline performs the following steps on a push to the main branch:
- Builds the agent's Docker image.
- Pushes the image to a private Amazon ECR repository.
- Connects to the target EC2 instance via SSH.
- Pulls the latest Docker image from ECR.
- Stops the old container and runs a new one with the updated image.
This agent is deployed! You can test it in https://t.me/AgenticAgent_bot
