commands to ask the LLM
This commit is contained in:
parent
663280ced4
commit
f368fd52bd
@ -35,12 +35,13 @@ LLM_ENDPOINT = "http://192.168.0.11:11434/api/chat"
|
|||||||
#driver = webdriver.Chrome(options=chrome_options)
|
#driver = webdriver.Chrome(options=chrome_options)
|
||||||
|
|
||||||
async def start(update: Update, context):
|
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):
|
async def echo(update: Update, context):
|
||||||
await context.bot.send_message(chat_id=update.effective_chat.id, text=update.message.text)
|
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."""
|
"""Query the LLM with the user's message."""
|
||||||
data = {
|
data = {
|
||||||
"model": "llama2",
|
"model": "llama2",
|
||||||
@ -53,6 +54,11 @@ async def query_llm(user_message):
|
|||||||
else:
|
else:
|
||||||
return "Error: Unable to reach LLM"
|
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):
|
async def screenshot(update, context):
|
||||||
"""Take a screenshot of a webpage."""
|
"""Take a screenshot of a webpage."""
|
||||||
url = ' '.join(context.args)
|
url = ' '.join(context.args)
|
||||||
@ -68,9 +74,16 @@ async def screenshot(update, context):
|
|||||||
# image.save('screenshot.png')
|
# image.save('screenshot.png')
|
||||||
# update.message.reply_photo(photo=open('screenshot.png', 'rb'))
|
# update.message.reply_photo(photo=open('screenshot.png', 'rb'))
|
||||||
|
|
||||||
async def error(update, context):
|
async def error_handler(update: Update): #context: CallbackContext
|
||||||
"""Log Errors caused by Updates."""
|
"""Handle errors occurred during the execution of commands."""
|
||||||
logger.warning(f'Update "{update}" caused error "{context.error}"')
|
# 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():
|
async def main():
|
||||||
@ -79,11 +92,18 @@ async def main():
|
|||||||
application = Application.builder().token(TOKEN).build()
|
application = Application.builder().token(TOKEN).build()
|
||||||
|
|
||||||
# Add handlers to the application
|
# Add handlers to the application
|
||||||
|
# Command handlers should be registered before the generic message handler
|
||||||
application.add_handler(CommandHandler("start", start))
|
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("screenshot", screenshot)) # Ensure screenshot function is async
|
||||||
application.add_handler(CommandHandler("ai",query_llm))
|
application.add_handler(CommandHandler("ai", ask))
|
||||||
application.add_error_handler(error) # Ensure error function is async
|
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
|
# Run the bot
|
||||||
await application.run_polling()
|
await application.run_polling()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user