Em(AI)l Agent is a Python-based AI agent that leverages Large Language Models (LLMs) to automatically draft and send emails. It is controlled via a simple Telegram bot interface, allowing users to send complex email requests using natural language.
The agent operates through a two-step process:
-
Content Generation: When a user provides a topic or instruction via the Telegram bot, the agent first uses a local LLM (via
Ollamaandlangchain) to generate an appropriate subject and body for the email. This process is guided by a prompt template that ensures the email is well-structured and signed correctly. -
Action Execution: The generated subject and body are then passed to a second LangChain agent equipped with
GmailToolkit. This agent interprets the user's initial command (e.g., "send" or "draft") and the recipient's details to perform the final action: either sending the email directly or saving it as a draft in your Gmail account.
- Natural Language Commands: Draft or send emails by describing your request in plain English.
- Telegram Bot Integration: Easy-to-use interface through a Telegram bot.
- Local LLM Powered: Uses Ollama with models like Llama 3.1 for privacy and cost-effectiveness.
- Gmail Integration: Securely interacts with your Gmail account to send emails and create drafts using the
GmailToolkit. - Structured Output: Employs Pydantic models to ensure reliable generation of email subjects and bodies from the LLM.
- Python 3.8+
- Ollama installed and running.
- The Llama 3.1 model pulled in Ollama:
ollama pull llama3.1 - Telegram API credentials (API ID, API Hash) and a Bot Token.
- Google Cloud Project with the Gmail API enabled. You will need to create OAuth 2.0 credentials and download the
credentials.jsonfile.
-
Clone the repository:
git clone https://github.com/Soham-KT/Em-AI-l-Agent.git cd Em-AI-l-Agent -
Set up Gmail API Credentials:
- Follow the instructions from the Google API Python Quickstart to enable the Gmail API and download your
credentials.jsonfile. - Place the
credentials.jsonfile in the root directory of this project. - The first time you run the application, you will be prompted to authorize access to your Gmail account through a browser window. A
token.jsonfile will be created to store your credentials for future runs.
- Follow the instructions from the Google API Python Quickstart to enable the Gmail API and download your
-
Install Python dependencies:
pip install -r requirements.txt
-
Configure environment variables: Create a
.envfile in the root directory and add your Telegram credentials:TELEGRAM_API=<YOUR_TELEGRAM_API_ID> TELEGRAM_HASH=<YOUR_TELEGRAM_API_HASH> TELEGRAM_BOT_TOKEN=<YOUR_TELEGRAM_BOT_TOKEN>
-
Start Ollama: Make sure the Ollama service is running on your machine.
-
Run the application:
python main.py
The Telegram bot will start and connect to your account.
-
Interact with the bot: Open Telegram and start a conversation with your bot. You can use the following commands:
/start: Initializes the conversation with the bot./help: Provides instructions and the prompt format./info: Gives a brief description of the bot.
-
Send or Draft an Email: To instruct the agent, send a message following the format provided by the
/helpcommand. The agent will parse your request, generate the content, and perform the requested action.Example Prompts:
Send an email to John Doe, their email: johndoe@fake.com. The topic is a discussion about explaining black holes.Draft an email to contact@company.com requesting a project update.
The bot will confirm once the task is completed.
main.py: The entry point of the application. It runs the Telethon client for the Telegram bot, handles commands, and passes user requests to the email agent.llm_mail.py: Contains the core logic for the email agent. It defines functions to generate email content using an LLM and to create and invoke the Gmail agent for sending/drafting.requirements.txt: Lists all the necessary Python packages for the project.test.py: A simple script for testing the Gmail agent functionality independently of the Telegram bot.LICENSE: The Apache 2.0 license for the project.
This project is licensed under the Apache License 2.0. See the LICENSE file for details.