From f368fd52bd0c9429e9d58c67311720d07a3c1882 Mon Sep 17 00:00:00 2001 From: Dobromir Popov Date: Tue, 19 Dec 2023 22:33:48 +0000 Subject: [PATCH] commands to ask the LLM --- agent-py-bot/agent.py | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/agent-py-bot/agent.py b/agent-py-bot/agent.py index fb86aa4..ee9aa47 100644 --- a/agent-py-bot/agent.py +++ b/agent-py-bot/agent.py @@ -35,12 +35,13 @@ LLM_ENDPOINT = "http://192.168.0.11:11434/api/chat" #driver = webdriver.Chrome(options=chrome_options) async def start(update: Update, context): - await context.bot.send_message(chat_id=update.effective_chat.id, text="Hi! I'm your bot.") + await context.bot.send_message(chat_id=update.effective_chat.id, text="Hi! I'm your AI bot. Ask me aything with /ask") async def echo(update: Update, context): await context.bot.send_message(chat_id=update.effective_chat.id, text=update.message.text) -async def query_llm(user_message): + +def query_llm(user_message): """Query the LLM with the user's message.""" data = { "model": "llama2", @@ -53,6 +54,11 @@ async def query_llm(user_message): else: return "Error: Unable to reach LLM" +def ask(update, context): + user_message = ' '.join(context.args) + llm_response = query_llm(user_message) + update.message.reply_text(llm_response) + async def screenshot(update, context): """Take a screenshot of a webpage.""" url = ' '.join(context.args) @@ -68,9 +74,16 @@ async def screenshot(update, context): # image.save('screenshot.png') # update.message.reply_photo(photo=open('screenshot.png', 'rb')) -async def error(update, context): - """Log Errors caused by Updates.""" - logger.warning(f'Update "{update}" caused error "{context.error}"') +async def error_handler(update: Update): #context: CallbackContext + """Handle errors occurred during the execution of commands.""" + # Log the error before we do anything else + # logger.error(f"An error occurred: {context.error}") + logger.error(f"An error occurred:") + + # Send a message to the user + # error_message = "Sorry, an unexpected error occurred. Please try again later." + # if update.effective_message: + # await context.bot.send_message(chat_id=update.effective_chat.id, text=error_message) async def main(): @@ -79,11 +92,18 @@ async def main(): application = Application.builder().token(TOKEN).build() # Add handlers to the application + # Command handlers should be registered before the generic message handler application.add_handler(CommandHandler("start", start)) - application.add_handler(MessageHandler(filters.TEXT, echo)) application.add_handler(CommandHandler("screenshot", screenshot)) # Ensure screenshot function is async - application.add_handler(CommandHandler("ai",query_llm)) - application.add_error_handler(error) # Ensure error function is async + application.add_handler(CommandHandler("ai", ask)) + application.add_handler(CommandHandler("ask", ask)) + application.add_handler(CommandHandler("llm", ask)) + + # This handler should be last as it's the most generic + application.add_handler(MessageHandler(filters.TEXT, echo)) + + # Register the error handler + application.add_error_handler(error_handler) # Run the bot await application.run_polling()