2024-08-17 13:00:37 -05:00
|
|
|
import json
|
2024-08-16 12:43:59 -05:00
|
|
|
import os
|
|
|
|
|
import importlib
|
|
|
|
|
import inspect
|
2024-08-17 09:28:17 -05:00
|
|
|
import tempfile
|
|
|
|
|
import base64
|
2024-08-17 18:31:53 -05:00
|
|
|
import logging
|
2024-08-18 07:57:18 -05:00
|
|
|
import anthropic
|
2024-08-16 12:43:59 -05:00
|
|
|
from telegram import Update
|
|
|
|
|
from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes
|
|
|
|
|
from openai import OpenAI
|
|
|
|
|
from dotenv import load_dotenv
|
|
|
|
|
from tools.base_tool import BaseTool
|
|
|
|
|
|
|
|
|
|
# Load environment variables
|
|
|
|
|
load_dotenv()
|
|
|
|
|
|
2024-08-18 07:57:18 -05:00
|
|
|
openai_client = OpenAI()
|
|
|
|
|
|
|
|
|
|
anthropic_client = anthropic.Anthropic(
|
|
|
|
|
api_key=os.environ.get("ANTHROPIC_API_KEY")
|
|
|
|
|
)
|
2024-08-16 12:43:59 -05:00
|
|
|
|
2024-08-17 09:28:17 -05:00
|
|
|
GPT_4O = "gpt-4o"
|
|
|
|
|
GPT_4O_MINI = "gpt-4o-mini"
|
2024-08-17 13:00:37 -05:00
|
|
|
|
2024-08-18 07:35:52 -05:00
|
|
|
model_max_tokens = {
|
|
|
|
|
GPT_4O: 4096,
|
|
|
|
|
GPT_4O_MINI: 16384
|
|
|
|
|
}
|
2024-08-17 19:30:38 -05:00
|
|
|
|
2024-08-18 07:35:52 -05:00
|
|
|
use_smart_model = True
|
2024-08-18 07:57:18 -05:00
|
|
|
use_anthropic = False
|
2024-08-17 19:30:18 -05:00
|
|
|
|
2024-08-17 19:30:38 -05:00
|
|
|
# Set up logging to console and file
|
2024-08-18 07:35:52 -05:00
|
|
|
logging.basicConfig(level=logging.WARNING, handlers=[
|
2024-08-17 18:32:08 -05:00
|
|
|
logging.StreamHandler(),
|
|
|
|
|
logging.FileHandler('logs/output.log', mode='a')
|
|
|
|
|
])
|
2024-08-17 19:30:18 -05:00
|
|
|
|
2024-08-16 12:43:59 -05:00
|
|
|
# Set up Telegram bot
|
|
|
|
|
TELEGRAM_BOT_TOKEN = os.getenv('TELEGRAM_BOT_TOKEN')
|
|
|
|
|
|
2024-08-17 17:53:25 -05:00
|
|
|
# Load system prompt
|
|
|
|
|
with open("prompts/developer_prompt.txt", "r") as file:
|
|
|
|
|
system_prompt = file.read().strip()
|
|
|
|
|
|
2024-08-16 12:43:59 -05:00
|
|
|
# Dictionary to store conversation history for each user
|
|
|
|
|
conversation_history = {}
|
|
|
|
|
|
2024-08-17 09:28:17 -05:00
|
|
|
# Dictionary to store the last image file for each user
|
|
|
|
|
user_images = {}
|
|
|
|
|
|
2024-08-16 12:43:59 -05:00
|
|
|
# Load tools
|
|
|
|
|
tools = []
|
|
|
|
|
tools_dir = os.path.join(os.path.dirname(__file__), 'tools')
|
|
|
|
|
for filename in os.listdir(tools_dir):
|
|
|
|
|
if filename.endswith('.py') and filename != '__init__.py' and filename != 'base_tool.py':
|
|
|
|
|
module_name = f'tools.{filename[:-3]}'
|
|
|
|
|
module = importlib.import_module(module_name)
|
|
|
|
|
for name, obj in inspect.getmembers(module):
|
|
|
|
|
if inspect.isclass(obj) and issubclass(obj, BaseTool) and obj != BaseTool:
|
|
|
|
|
tools.append(obj())
|
|
|
|
|
|
|
|
|
|
# Collect all function definitions
|
|
|
|
|
functions = []
|
|
|
|
|
for tool in tools:
|
|
|
|
|
functions.extend(tool.get_functions())
|
|
|
|
|
|
|
|
|
|
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
2024-08-17 18:32:08 -05:00
|
|
|
logging.info("Bot started")
|
2024-08-17 09:28:17 -05:00
|
|
|
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.")
|
2024-08-16 12:43:59 -05:00
|
|
|
|
|
|
|
|
async def clear(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
|
|
|
|
user_id = update.effective_user.id
|
|
|
|
|
if user_id in conversation_history:
|
|
|
|
|
del conversation_history[user_id]
|
2024-08-17 09:28:17 -05:00
|
|
|
if user_id in user_images:
|
|
|
|
|
os.remove(user_images[user_id])
|
|
|
|
|
del user_images[user_id]
|
2024-08-17 18:32:08 -05:00
|
|
|
logging.info(f"Cleared conversation history and image for user {user_id}")
|
2024-08-17 09:28:17 -05:00
|
|
|
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:
|
|
|
|
|
user_id = update.effective_user.id
|
|
|
|
|
|
|
|
|
|
# Get the largest available photo
|
|
|
|
|
photo = max(update.message.photo, key=lambda x: x.file_size)
|
|
|
|
|
|
|
|
|
|
# Download the photo
|
|
|
|
|
photo_file = await context.bot.get_file(photo.file_id)
|
|
|
|
|
|
|
|
|
|
# Create a temporary file to store the image
|
|
|
|
|
with tempfile.NamedTemporaryFile(delete=False, suffix='.jpg') as temp_file:
|
|
|
|
|
await photo_file.download_to_drive(custom_path=temp_file.name)
|
|
|
|
|
user_images[user_id] = temp_file.name
|
|
|
|
|
|
2024-08-17 18:32:08 -05:00
|
|
|
logging.info(f"Received image from user {user_id}")
|
2024-08-17 09:28:17 -05:00
|
|
|
await update.message.reply_text("I've received your image. What would you like to know about it?")
|
2024-08-16 12:43:59 -05:00
|
|
|
|
|
|
|
|
async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
|
|
|
|
try:
|
|
|
|
|
user_id = update.effective_user.id
|
|
|
|
|
user_message = update.message.text
|
|
|
|
|
|
2024-08-17 18:32:08 -05:00
|
|
|
logging.info(f"Message from user {user_id}: {user_message}")
|
|
|
|
|
|
2024-08-16 12:43:59 -05:00
|
|
|
# Initialize conversation history for new users
|
|
|
|
|
if user_id not in conversation_history:
|
|
|
|
|
conversation_history[user_id] = []
|
|
|
|
|
|
|
|
|
|
# Add user message to conversation history
|
|
|
|
|
conversation_history[user_id].append({"role": "user", "content": user_message})
|
|
|
|
|
|
|
|
|
|
# Prepare messages for OpenAI API
|
2024-08-17 13:00:37 -05:00
|
|
|
messages = [{"role": "system", "content": system_prompt}] + conversation_history[user_id]
|
2024-08-16 12:43:59 -05:00
|
|
|
|
2024-08-18 07:57:18 -05:00
|
|
|
response = get_chat_response(messages)
|
2024-08-16 12:43:59 -05:00
|
|
|
|
|
|
|
|
# Extract the assistant's reply
|
|
|
|
|
assistant_message = response.choices[0].message
|
2024-08-17 13:00:37 -05:00
|
|
|
toolUseCount = 0
|
2024-08-17 09:28:17 -05:00
|
|
|
if hasattr(assistant_message, 'function_call') and assistant_message.function_call:
|
2024-08-17 18:50:30 -05:00
|
|
|
while hasattr(assistant_message, 'function_call') and assistant_message.function_call and toolUseCount < 50: # Todo: put amount in env
|
2024-08-18 07:35:52 -05:00
|
|
|
tool_response = call_tool(assistant_message.function_call)
|
2024-08-17 13:00:37 -05:00
|
|
|
|
|
|
|
|
conversation_history[user_id].append({"role": "function", "name": assistant_message.function_call.name, "content": json.dumps(tool_response)})
|
|
|
|
|
messages.append({
|
|
|
|
|
"role": "function",
|
|
|
|
|
"name": assistant_message.function_call.name,
|
|
|
|
|
"content": json.dumps(tool_response)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
# Call API again to get the final response
|
2024-08-18 07:57:18 -05:00
|
|
|
assistant_message = get_chat_response(messages).choices[0].message
|
2024-08-17 13:00:37 -05:00
|
|
|
if not hasattr(assistant_message, 'function_call') or not assistant_message.function_call:
|
|
|
|
|
assistant_reply = assistant_message.content
|
|
|
|
|
conversation_history[user_id].append({"role": "assistant", "content": assistant_reply})
|
2024-08-16 12:43:59 -05:00
|
|
|
else:
|
|
|
|
|
assistant_reply = assistant_message.content
|
2024-08-17 13:00:37 -05:00
|
|
|
# Add assistant's reply to conversation history
|
|
|
|
|
conversation_history[user_id].append({"role": "assistant", "content": assistant_reply})
|
|
|
|
|
|
2024-08-16 12:43:59 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
# Trim conversation history if it gets too long (e.g., keep last 10 messages)
|
|
|
|
|
if len(conversation_history[user_id]) > 10:
|
|
|
|
|
conversation_history[user_id] = conversation_history[user_id][-10:]
|
|
|
|
|
|
|
|
|
|
# Send the reply back to the user
|
|
|
|
|
await update.message.reply_text(assistant_reply)
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
2024-08-17 18:31:53 -05:00
|
|
|
logging.error(f"An error occurred: {str(e)}")
|
2024-08-16 12:43:59 -05:00
|
|
|
await update.message.reply_text("Sorry, an error occurred while processing your request.")
|
|
|
|
|
|
2024-08-18 07:35:52 -05:00
|
|
|
def call_tool(function_call):
|
2024-08-17 13:00:37 -05:00
|
|
|
# Execute the function
|
|
|
|
|
function_name = function_call.name
|
|
|
|
|
function_args = function_call.arguments
|
|
|
|
|
for tool in tools:
|
|
|
|
|
if function_name in [f["name"] for f in tool.get_functions()]:
|
|
|
|
|
return tool.execute(function_name, **eval(function_args))
|
|
|
|
|
|
2024-08-18 07:57:18 -05:00
|
|
|
def get_chat_response(messages):
|
|
|
|
|
if use_anthropic:
|
|
|
|
|
response = get_openai_response(messages)
|
|
|
|
|
else:
|
|
|
|
|
response = get_claude_response(messages)
|
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
def get_openai_response(messages):
|
|
|
|
|
model = GPT_4O if use_smart_model else GPT_4O_MINI
|
|
|
|
|
response = openai_client.chat.completions.create(
|
2024-08-17 13:00:37 -05:00
|
|
|
model=model,
|
|
|
|
|
messages=messages,
|
|
|
|
|
functions=functions,
|
|
|
|
|
function_call="auto",
|
2024-08-18 07:35:52 -05:00
|
|
|
max_tokens=model_max_tokens[model]
|
2024-08-17 13:00:37 -05:00
|
|
|
)
|
|
|
|
|
return response
|
|
|
|
|
|
2024-08-18 07:57:18 -05:00
|
|
|
def get_claude_response(messages):
|
|
|
|
|
response = anthropic_client.messages.create(
|
|
|
|
|
messages=messages,
|
|
|
|
|
max_tokens=4096,
|
|
|
|
|
model="claude-3-5-sonnet-20240620"
|
|
|
|
|
)
|
|
|
|
|
return response
|
|
|
|
|
|
2024-08-18 07:35:52 -05:00
|
|
|
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}")
|
|
|
|
|
update.message.reply_text(f"Switched to model: {model}")
|
|
|
|
|
|
2024-08-18 07:57:18 -05:00
|
|
|
def switch_anthropic(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
|
|
|
|
global use_anthropic
|
|
|
|
|
use_anthropic = not use_anthropic
|
|
|
|
|
logging.info("Using Anthropic" if use_anthropic else "Using OpenAI")
|
|
|
|
|
update.message.reply_text("Using Anthropic" if use_anthropic else "Using OpenAI")
|
|
|
|
|
|
2024-08-18 07:35:52 -05:00
|
|
|
async def status(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
2024-08-18 07:58:19 -05:00
|
|
|
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}")
|
2024-08-18 07:35:52 -05:00
|
|
|
|
2024-08-16 12:43:59 -05:00
|
|
|
def main() -> None:
|
|
|
|
|
# Create the Application and pass it your bot's token
|
|
|
|
|
application = Application.builder().token(TELEGRAM_BOT_TOKEN).build()
|
|
|
|
|
|
|
|
|
|
# Add handlers
|
|
|
|
|
application.add_handler(CommandHandler("start", start))
|
|
|
|
|
application.add_handler(CommandHandler("clear", clear))
|
2024-08-18 07:35:52 -05:00
|
|
|
application.add_handler(CommandHandler("switch", switch))
|
2024-08-18 07:57:18 -05:00
|
|
|
application.add_handler(CommandHandler("toggle", switch_anthropic))
|
2024-08-18 07:35:52 -05:00
|
|
|
application.add_handler(CommandHandler("status", status))
|
2024-08-17 09:28:17 -05:00
|
|
|
application.add_handler(MessageHandler(filters.PHOTO, handle_image))
|
2024-08-16 12:43:59 -05:00
|
|
|
application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message))
|
|
|
|
|
|
|
|
|
|
# Start the Bot
|
2024-08-17 18:31:53 -05:00
|
|
|
logging.info("Bot is running...")
|
2024-08-16 12:43:59 -05:00
|
|
|
application.run_polling()
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2024-08-17 13:00:37 -05:00
|
|
|
main()
|