From ae5e295f0d80530c183dd22b7ac398903d0f77a6 Mon Sep 17 00:00:00 2001 From: bucolucas Date: Sun, 18 Aug 2024 17:18:24 -0500 Subject: [PATCH 1/4] Remove OpenAI from telegram_inference_bot.py --- telegram_inference_bot.py | 109 +++++++------------------------------- 1 file changed, 19 insertions(+), 90 deletions(-) diff --git a/telegram_inference_bot.py b/telegram_inference_bot.py index 909ad34..798eb12 100644 --- a/telegram_inference_bot.py +++ b/telegram_inference_bot.py @@ -8,7 +8,6 @@ import subprocess import requests from telegram import Update, __version__ as telegram_version, InlineKeyboardButton, InlineKeyboardMarkup from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes, CallbackQueryHandler -from openai import OpenAI from dotenv import load_dotenv from tools.base_tool import BaseTool from anthropic import Anthropic @@ -16,24 +15,11 @@ from anthropic import Anthropic # Load environment variables load_dotenv() -openai_client = OpenAI() - anthropic_client = Anthropic( api_key=os.environ.get("ANTHROPIC_API_KEY"), default_headers={"anthropic-beta": "max-tokens-3-5-sonnet-2024-07-15"} ) -GPT_4O = "gpt-4o" -GPT_4O_MINI = "gpt-4o-mini" - -model_max_tokens = { - GPT_4O: 4096, - GPT_4O_MINI: 16384 -} - -use_smart_model = False -use_anthropic = True - # Set up logging to console and file logging.basicConfig(level=logging.WARNING, handlers=[ logging.StreamHandler(), @@ -119,17 +105,12 @@ async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE) -> response = get_chat_response(messages) tool_calls = [] - if use_anthropic: - fullMessage = [] - for message_part in response.content: - fullMessage.append(message_part) - if message_part.type == "tool_use": - tool_calls.append(message_part) - messages.append({"role": "assistant", "content": fullMessage}) - else: - assistant_message = response.choices[0].message - if hasattr(assistant_message, 'function_call') and assistant_message.function_call is not None: - tool_calls.append(assistant_message.function_call) + fullMessage = [] + for message_part in response.content: + fullMessage.append(message_part) + if message_part.type == "tool_use": + tool_calls.append(message_part) + messages.append({"role": "assistant", "content": fullMessage}) toolUseCount = 0 @@ -145,40 +126,22 @@ async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE) -> tool_response = call_tool(tool_call) tool_use_results.append({"type": "tool_result", "tool_use_id": tool_call.id, "content": json.dumps(tool_response)}) - formatted_result = {} - - if use_anthropic: - formatted_result = {"role": "user", "content":tool_use_results} - else: - formatted_result = {"role": "function", "name": function_name, "content": json.dumps(tool_use_results[0])} + formatted_result = {"role": "user", "content": tool_use_results} messages.append(formatted_result) response = get_chat_response(messages) - assistant_message = "" - if use_anthropic: - fullMessage = [] - for message_part in response.content: - fullMessage.append(message_part) - if message_part.type == "tool_use": - tool_calls.append(message_part) - messages.append({"role": "assistant", "content": fullMessage}) - else: - assistant_message = response.choices[0].message - conversation_history[user_id].append({"role": "assistant", "content": assistant_message}) - if hasattr(assistant_message, 'function_call') and assistant_message.function_call is not None: - tool_calls.append(assistant_message.function_call) - else: - conversation_history[user_id].append({"role": "assistant", "content": assistant_message}) - assistant_reply = assistant_message + fullMessage = [] + for message_part in response.content: + fullMessage.append(message_part) + if message_part.type == "tool_use": + tool_calls.append(message_part) + messages.append({"role": "assistant", "content": fullMessage}) toolUseCount += 1 if (toolUseCount == 0): - if use_anthropic: - assistant_reply = response.content - else: - assistant_reply = assistant_message + assistant_reply = response.content conversation_history[user_id].append({"role": "assistant", "content": assistant_reply}) if len(conversation_history[user_id]) > 20: @@ -188,35 +151,21 @@ async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE) -> await context.bot.delete_message(chat_id=update.effective_chat.id, message_id=status_message.message_id) del processing_status[user_id] - if use_anthropic: - await update.message.reply_text(messages[-1]["content"][0].text) - else: - await update.message.reply_text(assistant_reply.content) + await update.message.reply_text(messages[-1]["content"][0].text) except Exception as e: logging.error(f"An error occurred: {str(e)}") await update.message.reply_text("Sorry, an error occurred while processing your request.") def call_tool(function_call): - function_name = function_call.name if use_anthropic else function_call.name - function_args = json.dumps(function_call.input) if use_anthropic else function_call.arguments + function_name = function_call.name + function_args = json.dumps(function_call.input) for tool in tools: if function_name in [f["name"] for f in tool.get_functions()]: return tool.execute(function_name, **json.loads(function_args)) def get_chat_response(messages): - return get_claude_response(messages) if use_anthropic else get_openai_response(messages) - -def get_openai_response(messages): - model = GPT_4O if use_smart_model else GPT_4O_MINI - response = openai_client.chat.completions.create( - model=model, - messages = [{"role": "system", "content": system_prompt}] + messages, - functions=functions, - function_call="auto", - max_tokens=model_max_tokens[model] - ) - return response + return get_claude_response(messages) def get_claude_response(messages): anthropic_tools = [ @@ -241,26 +190,8 @@ def get_claude_response(messages): return response -async def switch(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: - global use_smart_model - use_smart_model = not use_smart_model - model = GPT_4O if use_smart_model else GPT_4O_MINI - logging.info(f"Switched to model: {model}") - await update.message.reply_text(f"Switched to model: {model}") - -async def switch_providers(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: - await clear(update, context) - global use_anthropic - use_anthropic = not use_anthropic - logging.info("Using Anthropic" if use_anthropic else "Using OpenAI") - await update.message.reply_text("Using Anthropic" if use_anthropic else "Using OpenAI") - async def status(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: - if use_anthropic: - await update.message.reply_text("Currently using claude-3-5-sonnet-20240620") - else: - model = GPT_4O if use_smart_model else GPT_4O_MINI - await update.message.reply_text(f"Currently using: {model}") + await update.message.reply_text("Currently using claude-3-5-sonnet-20240620") async def abort_processing(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: query = update.callback_query @@ -327,8 +258,6 @@ def main() -> None: for app in [daemon_app, apprentice_app]: app.add_handler(CommandHandler("start", start)) app.add_handler(CommandHandler("clear", clear)) - app.add_handler(CommandHandler("switch", switch)) - app.add_handler(CommandHandler("toggle", switch_providers)) app.add_handler(CommandHandler("status", status)) app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message)) app.add_handler(CallbackQueryHandler(abort_processing, pattern='^abort$')) From 4829ec038e1f43aa72dc27a9b152ee9519cc167d Mon Sep 17 00:00:00 2001 From: bucolucas Date: Sun, 18 Aug 2024 17:25:30 -0500 Subject: [PATCH 2/4] Add always-on keyboard for common commands --- telegram_inference_bot.py | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/telegram_inference_bot.py b/telegram_inference_bot.py index 798eb12..639ea60 100644 --- a/telegram_inference_bot.py +++ b/telegram_inference_bot.py @@ -6,7 +6,7 @@ import logging import sys import subprocess import requests -from telegram import Update, __version__ as telegram_version, InlineKeyboardButton, InlineKeyboardMarkup +from telegram import Update, __version__ as telegram_version, InlineKeyboardButton, InlineKeyboardMarkup, ReplyKeyboardMarkup from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes, CallbackQueryHandler from dotenv import load_dotenv from tools.base_tool import BaseTool @@ -59,9 +59,19 @@ functions = [] for tool in tools: functions.extend(tool.get_functions()) +def get_keyboard(): + keyboard = [ + ['/switch', '/toggle'], + ['/status', '/reset'] + ] + return ReplyKeyboardMarkup(keyboard, resize_keyboard=True) + async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: logging.info("Bot started") - await update.message.reply_text("Hello! I'm your AI assistant. How can I help you today? You can send me images and then ask questions about them.") + await update.message.reply_text( + "Hello! I'm your AI assistant. How can I help you today? You can send me images and then ask questions about them.", + reply_markup=get_keyboard() + ) async def clear(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: user_id = update.effective_user.id @@ -71,7 +81,7 @@ async def clear(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: tool.clear() logging.info(f"Cleared conversation history and image for user {user_id}") - await update.message.reply_text("Conversation history and image cleared. Let's start fresh!") + await update.message.reply_text("Conversation history and image cleared. Let's start fresh!", reply_markup=get_keyboard()) async def update_status_message(context: ContextTypes.DEFAULT_TYPE, chat_id: int, message_id: int, status: str): keyboard = [ @@ -151,11 +161,11 @@ async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE) -> await context.bot.delete_message(chat_id=update.effective_chat.id, message_id=status_message.message_id) del processing_status[user_id] - await update.message.reply_text(messages[-1]["content"][0].text) + await update.message.reply_text(messages[-1]["content"][0].text, reply_markup=get_keyboard()) except Exception as e: logging.error(f"An error occurred: {str(e)}") - await update.message.reply_text("Sorry, an error occurred while processing your request.") + await update.message.reply_text("Sorry, an error occurred while processing your request.", reply_markup=get_keyboard()) def call_tool(function_call): function_name = function_call.name @@ -191,7 +201,7 @@ def get_claude_response(messages): return response async def status(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: - await update.message.reply_text("Currently using claude-3-5-sonnet-20240620") + await update.message.reply_text("Currently using claude-3-5-sonnet-20240620", reply_markup=get_keyboard()) async def abort_processing(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: query = update.callback_query @@ -214,9 +224,9 @@ async def handover(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: # 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.") + await update.message.reply_text("Handover initiated. Apprentice bot is now in control.", reply_markup=get_keyboard()) else: - await update.message.reply_text("Handover can only be initiated by the daemon bot.") + await update.message.reply_text("Handover can only be initiated by the daemon bot.", reply_markup=get_keyboard()) async def update_apprentice(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: if context.bot.token == APPRENTICE_BOT_TOKEN: @@ -228,12 +238,12 @@ async def update_apprentice(update: Update, context: ContextTypes.DEFAULT_TYPE) 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.") + await update.message.reply_text("Failed to update. Please check the logs.", reply_markup=get_keyboard()) 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.") + await update.message.reply_text("Failed to restart. Please check the logs.", reply_markup=get_keyboard()) else: - await update.message.reply_text("Update can only be performed by the apprentice bot.") + await update.message.reply_text("Update can only be performed by the apprentice bot.", reply_markup=get_keyboard()) async def check_for_updates(context: ContextTypes.DEFAULT_TYPE) -> None: url = f"https://api.github.com/repos/{GITHUB_REPO_OWNER}/{GITHUB_REPO_NAME}/pulls" @@ -259,6 +269,9 @@ def main() -> None: app.add_handler(CommandHandler("start", start)) app.add_handler(CommandHandler("clear", clear)) app.add_handler(CommandHandler("status", status)) + app.add_handler(CommandHandler("switch", start)) # Placeholder for /switch command + app.add_handler(CommandHandler("toggle", start)) # Placeholder for /toggle command + app.add_handler(CommandHandler("reset", clear)) # Use clear function for /reset command app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message)) app.add_handler(CallbackQueryHandler(abort_processing, pattern='^abort$')) From 03bf9eecdd9a085a40ec593c2ad7aa6f6b6aa3cd Mon Sep 17 00:00:00 2001 From: bucolucas Date: Sun, 18 Aug 2024 17:30:07 -0500 Subject: [PATCH 3/4] Remove unused keyboard commands --- telegram_inference_bot.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/telegram_inference_bot.py b/telegram_inference_bot.py index 639ea60..87276c1 100644 --- a/telegram_inference_bot.py +++ b/telegram_inference_bot.py @@ -61,7 +61,6 @@ for tool in tools: def get_keyboard(): keyboard = [ - ['/switch', '/toggle'], ['/status', '/reset'] ] return ReplyKeyboardMarkup(keyboard, resize_keyboard=True) @@ -269,8 +268,6 @@ def main() -> None: app.add_handler(CommandHandler("start", start)) app.add_handler(CommandHandler("clear", clear)) app.add_handler(CommandHandler("status", status)) - app.add_handler(CommandHandler("switch", start)) # Placeholder for /switch command - app.add_handler(CommandHandler("toggle", start)) # Placeholder for /toggle command app.add_handler(CommandHandler("reset", clear)) # Use clear function for /reset command app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message)) app.add_handler(CallbackQueryHandler(abort_processing, pattern='^abort$')) From 3c072451654a36345479657e9a51b355f986d3b0 Mon Sep 17 00:00:00 2001 From: bucolucas Date: Sun, 18 Aug 2024 17:44:09 -0500 Subject: [PATCH 4/4] Add logging to debug job queue initialization --- telegram_inference_bot.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/telegram_inference_bot.py b/telegram_inference_bot.py index 87276c1..2433c3e 100644 --- a/telegram_inference_bot.py +++ b/telegram_inference_bot.py @@ -280,7 +280,12 @@ def main() -> None: # 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) + logging.info(f"Job queue initialized: {job_queue}") + if job_queue is None: + logging.error("Job queue is None. This should not happen.") + else: + job = job_queue.run_repeating(check_for_updates, interval=900, first=10) + logging.info(f"Job scheduled: {job}") # Start both bots logging.info("Bots are running...")