Merge pull request #51 from bucolucas/feature/daemon-apprentice-system
Implement daemon and apprentice bot system
This commit is contained in:
@@ -0,0 +1,18 @@
|
|||||||
|
# Telegram Bot Tokens
|
||||||
|
TELEGRAM_BOT_TOKEN=your_daemon_bot_token_here
|
||||||
|
TELEGRAM_APPRENTICE_BOT_TOKEN=your_apprentice_bot_token_here
|
||||||
|
|
||||||
|
# OpenAI API Key
|
||||||
|
OPENAI_API_KEY=your_openai_api_key_here
|
||||||
|
|
||||||
|
# Anthropic API Key
|
||||||
|
ANTHROPIC_API_KEY=your_anthropic_api_key_here
|
||||||
|
|
||||||
|
# GitHub Repository Information
|
||||||
|
GITHUB_REPO_OWNER=your_github_username_or_organization
|
||||||
|
GITHUB_REPO_NAME=your_repo_name
|
||||||
|
GITHUB_ACCESS_TOKEN=your_github_personal_access_token
|
||||||
|
|
||||||
|
# Chat IDs
|
||||||
|
DAEMON_CHAT_ID=your_daemon_chat_id
|
||||||
|
APPRENTICE_CHAT_ID=your_apprentice_chat_id
|
||||||
@@ -0,0 +1,76 @@
|
|||||||
|
# Telegram Inference Bot with Daemon and Apprentice System
|
||||||
|
|
||||||
|
This project implements a Telegram bot system with two instances: a daemon bot and an apprentice bot. The daemon bot handles the main workload, while the apprentice bot can be updated and take over when needed.
|
||||||
|
|
||||||
|
## Features
|
||||||
|
|
||||||
|
- Daemon bot for handling main workload
|
||||||
|
- Apprentice bot for updates and handovers
|
||||||
|
- Automatic updates for the apprentice bot when new pull requests are merged
|
||||||
|
- Inter-bot communication
|
||||||
|
- Support for both OpenAI and Anthropic AI models
|
||||||
|
- Tool integration for extended functionality
|
||||||
|
|
||||||
|
## Setup
|
||||||
|
|
||||||
|
1. Clone the repository:
|
||||||
|
```
|
||||||
|
git clone https://github.com/your_username/your_repo_name.git
|
||||||
|
cd your_repo_name
|
||||||
|
```
|
||||||
|
|
||||||
|
2. Install the required dependencies:
|
||||||
|
```
|
||||||
|
pip install -r requirements.txt
|
||||||
|
```
|
||||||
|
|
||||||
|
3. Copy the `.env.example` file to `.env` and fill in your actual values:
|
||||||
|
```
|
||||||
|
cp .env.example .env
|
||||||
|
```
|
||||||
|
|
||||||
|
4. Edit the `.env` file with your specific tokens and settings.
|
||||||
|
|
||||||
|
5. Run the bot:
|
||||||
|
```
|
||||||
|
python telegram_inference_bot.py
|
||||||
|
```
|
||||||
|
|
||||||
|
## Environment Variables
|
||||||
|
|
||||||
|
Make sure to set the following environment variables in your `.env` file:
|
||||||
|
|
||||||
|
- `TELEGRAM_BOT_TOKEN`: Token for the daemon bot
|
||||||
|
- `TELEGRAM_APPRENTICE_BOT_TOKEN`: Token for the apprentice bot
|
||||||
|
- `OPENAI_API_KEY`: Your OpenAI API key
|
||||||
|
- `ANTHROPIC_API_KEY`: Your Anthropic API key
|
||||||
|
- `GITHUB_REPO_OWNER`: Your GitHub username or organization
|
||||||
|
- `GITHUB_REPO_NAME`: Your repository name
|
||||||
|
- `GITHUB_ACCESS_TOKEN`: Your GitHub personal access token
|
||||||
|
- `DAEMON_CHAT_ID`: Chat ID for the daemon bot
|
||||||
|
- `APPRENTICE_CHAT_ID`: Chat ID for the apprentice bot
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
- Start the bot by running `python telegram_inference_bot.py`
|
||||||
|
- Use `/start` to begin interacting with either bot
|
||||||
|
- Use `/handover` in the daemon bot to initiate a handover to the apprentice bot
|
||||||
|
- The apprentice bot will automatically check for updates every 15 minutes
|
||||||
|
|
||||||
|
## Commands
|
||||||
|
|
||||||
|
- `/start`: Start interacting with the bot
|
||||||
|
- `/clear`: Clear conversation history
|
||||||
|
- `/switch`: Switch between GPT models (GPT-4 and GPT-4 Mini)
|
||||||
|
- `/toggle`: Toggle between OpenAI and Anthropic providers
|
||||||
|
- `/status`: Check the current model and provider
|
||||||
|
- `/handover`: (Daemon bot only) Initiate handover to apprentice bot
|
||||||
|
- `/update`: (Apprentice bot only) Manually trigger an update
|
||||||
|
|
||||||
|
## Contributing
|
||||||
|
|
||||||
|
Please read [CONTRIBUTING.md](CONTRIBUTING.md) for details on our code of conduct and the process for submitting pull requests.
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details.
|
||||||
+73
-14
@@ -4,6 +4,8 @@ import importlib
|
|||||||
import inspect
|
import inspect
|
||||||
import logging
|
import logging
|
||||||
import asyncio
|
import asyncio
|
||||||
|
import subprocess
|
||||||
|
import requests
|
||||||
from telegram import Update, __version__ as telegram_version, InlineKeyboardButton, InlineKeyboardMarkup
|
from telegram import Update, __version__ as telegram_version, InlineKeyboardButton, InlineKeyboardMarkup
|
||||||
from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes, CallbackQueryHandler
|
from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes, CallbackQueryHandler
|
||||||
from openai import OpenAI
|
from openai import OpenAI
|
||||||
@@ -38,8 +40,12 @@ logging.basicConfig(level=logging.WARNING, handlers=[
|
|||||||
logging.FileHandler('logs/output.log', mode='a')
|
logging.FileHandler('logs/output.log', mode='a')
|
||||||
])
|
])
|
||||||
|
|
||||||
# Set up Telegram bot
|
# Set up Telegram bots
|
||||||
TELEGRAM_BOT_TOKEN = os.getenv('TELEGRAM_BOT_TOKEN')
|
DAEMON_BOT_TOKEN = os.getenv('TELEGRAM_BOT_TOKEN')
|
||||||
|
APPRENTICE_BOT_TOKEN = os.getenv('TELEGRAM_APPRENTICE_BOT_TOKEN')
|
||||||
|
GITHUB_REPO_OWNER = os.getenv('GITHUB_REPO_OWNER')
|
||||||
|
GITHUB_REPO_NAME = os.getenv('GITHUB_REPO_NAME')
|
||||||
|
GITHUB_ACCESS_TOKEN = os.getenv('GITHUB_ACCESS_TOKEN')
|
||||||
|
|
||||||
# Load system prompt
|
# Load system prompt
|
||||||
with open("prompts/developer_prompt.txt", "r") as file:
|
with open("prompts/developer_prompt.txt", "r") as file:
|
||||||
@@ -272,22 +278,75 @@ async def abort_processing(update: Update, context: ContextTypes.DEFAULT_TYPE) -
|
|||||||
else:
|
else:
|
||||||
await query.edit_message_text(text="No active processing to abort.")
|
await query.edit_message_text(text="No active processing to abort.")
|
||||||
|
|
||||||
|
async def handover(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
||||||
|
if context.bot.token == DAEMON_BOT_TOKEN:
|
||||||
|
# Daemon bot initiating handover
|
||||||
|
apprentice_chat_id = os.getenv('APPRENTICE_CHAT_ID')
|
||||||
|
await context.bot.send_message(chat_id=apprentice_chat_id, text="Handover initiated. Taking control.")
|
||||||
|
await update.message.reply_text("Handover initiated. Apprentice bot is now in control.")
|
||||||
|
else:
|
||||||
|
await update.message.reply_text("Handover can only be initiated by the daemon bot.")
|
||||||
|
|
||||||
|
async def update_apprentice(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
||||||
|
if context.bot.token == APPRENTICE_BOT_TOKEN:
|
||||||
|
try:
|
||||||
|
# Pull latest changes
|
||||||
|
subprocess.run(["git", "pull", "origin", "main"], check=True)
|
||||||
|
|
||||||
|
# Restart the bot
|
||||||
|
os.execv(sys.executable, ['python'] + sys.argv)
|
||||||
|
except subprocess.CalledProcessError as e:
|
||||||
|
logging.error(f"Failed to pull latest changes: {e}")
|
||||||
|
await update.message.reply_text("Failed to update. Please check the logs.")
|
||||||
|
except Exception as e:
|
||||||
|
logging.error(f"Failed to restart the bot: {e}")
|
||||||
|
await update.message.reply_text("Failed to restart. Please check the logs.")
|
||||||
|
else:
|
||||||
|
await update.message.reply_text("Update can only be performed by the apprentice bot.")
|
||||||
|
|
||||||
|
async def check_for_updates(context: ContextTypes.DEFAULT_TYPE) -> None:
|
||||||
|
url = f"https://api.github.com/repos/{GITHUB_REPO_OWNER}/{GITHUB_REPO_NAME}/pulls"
|
||||||
|
headers = {"Authorization": f"token {GITHUB_ACCESS_TOKEN}"}
|
||||||
|
response = requests.get(url, headers=headers)
|
||||||
|
|
||||||
|
if response.status_code == 200:
|
||||||
|
pull_requests = response.json()
|
||||||
|
for pr in pull_requests:
|
||||||
|
if pr['state'] == 'closed' and pr['merged']:
|
||||||
|
# A pull request was merged, update the apprentice bot
|
||||||
|
await context.bot.send_message(chat_id=os.getenv('APPRENTICE_CHAT_ID'), text="A new update is available. Updating now...")
|
||||||
|
await update_apprentice(None, context)
|
||||||
|
break
|
||||||
|
|
||||||
def main() -> None:
|
def main() -> None:
|
||||||
# Create the Application and pass it your bot's token
|
# Create the Application and pass it your bot's token
|
||||||
application = Application.builder().token(TELEGRAM_BOT_TOKEN).build()
|
daemon_app = Application.builder().token(DAEMON_BOT_TOKEN).build()
|
||||||
|
apprentice_app = Application.builder().token(APPRENTICE_BOT_TOKEN).build()
|
||||||
|
|
||||||
# Add handlers
|
# Add handlers for both bots
|
||||||
application.add_handler(CommandHandler("start", start))
|
for app in [daemon_app, apprentice_app]:
|
||||||
application.add_handler(CommandHandler("clear", clear))
|
app.add_handler(CommandHandler("start", start))
|
||||||
application.add_handler(CommandHandler("switch", switch))
|
app.add_handler(CommandHandler("clear", clear))
|
||||||
application.add_handler(CommandHandler("toggle", switch_providers))
|
app.add_handler(CommandHandler("switch", switch))
|
||||||
application.add_handler(CommandHandler("status", status))
|
app.add_handler(CommandHandler("toggle", switch_providers))
|
||||||
application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message))
|
app.add_handler(CommandHandler("status", status))
|
||||||
application.add_handler(CallbackQueryHandler(abort_processing, pattern='^abort$'))
|
app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message))
|
||||||
|
app.add_handler(CallbackQueryHandler(abort_processing, pattern='^abort$'))
|
||||||
|
|
||||||
# Start the Bot
|
# Add handover command only to daemon bot
|
||||||
logging.info("Bot is running...")
|
daemon_app.add_handler(CommandHandler("handover", handover))
|
||||||
application.run_polling()
|
|
||||||
|
# Add update command only to apprentice bot
|
||||||
|
apprentice_app.add_handler(CommandHandler("update", update_apprentice))
|
||||||
|
|
||||||
|
# Set up job queue to check for updates every 15 minutes
|
||||||
|
job_queue = apprentice_app.job_queue
|
||||||
|
job_queue.run_repeating(check_for_updates, interval=900, first=10)
|
||||||
|
|
||||||
|
# Start both bots
|
||||||
|
logging.info("Bots are running...")
|
||||||
|
daemon_app.run_polling()
|
||||||
|
apprentice_app.run_polling()
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
Reference in New Issue
Block a user