diff --git a/telegram_inference_bot.py b/telegram_inference_bot.py index ed05e99..ac84025 100644 --- a/telegram_inference_bot.py +++ b/telegram_inference_bot.py @@ -1,15 +1,3 @@ -""" -telegram_inference_bot.py - -This module implements a Telegram bot that processes user messages and -images, interacts with the OpenAI API to provide intelligent responses, -and manages conversation history and temporary file storage for images. - -It supports the following commands: -- /start: Initializes the bot and welcomes the user. -- /clear: Clears the user's conversation history and any uploaded images. -""" - import json import os import importlib @@ -74,25 +62,10 @@ for tool in tools: functions.extend(tool.get_functions()) async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: - """ - Handles the /start command. Sends a welcome message to the user. - - Args: - update (Update): Incoming update containing the message. - context (ContextTypes.DEFAULT_TYPE): Context for the callback. - """ 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.") async def clear(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: - """ - Handles the /clear command. Clears the user's conversation history - and any uploaded images. - - Args: - update (Update): Incoming update containing the message. - context (ContextTypes.DEFAULT_TYPE): Context for the callback. - """ user_id = update.effective_user.id if user_id in conversation_history: del conversation_history[user_id] @@ -103,14 +76,6 @@ async def clear(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: await update.message.reply_text("Conversation history and image cleared. Let's start fresh!") async def handle_image(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: - """ - Handles image uploads from users. Downloads the largest photo to - a temporary file for further processing. - - Args: - update (Update): Incoming update containing the image. - context (ContextTypes.DEFAULT_TYPE): Context for the callback. - """ user_id = update.effective_user.id # Get the largest available photo @@ -128,14 +93,6 @@ async def handle_image(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No await update.message.reply_text("I've received your image. What would you like to know about it?") async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: - """ - Processes incoming text messages from users, calls the OpenAI API - for inference, and sends the assistant's reply back to the user. - - Args: - update (Update): Incoming update containing the text message. - context (ContextTypes.DEFAULT_TYPE): Context for the callback. - """ try: user_id = update.effective_user.id user_message = update.message.text diff --git a/tools/persona_tool.py b/tools/persona_tool.py index 6596d1a..30e109f 100644 --- a/tools/persona_tool.py +++ b/tools/persona_tool.py @@ -1,11 +1,13 @@ +import os import openai import json from tools.base_tool import BaseTool class PersonaTool(BaseTool): - def __init__(self, api_key: str): + def __init__(self): super().__init__() - openai.api_key = api_key + + self.api_key = os.environ.get("OPENAI_API_KEY") def generate_response(self, persona_description: str, query: str) -> str: """