calculate.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. from nio.events.room_events import RoomMessageText
  2. from nio.rooms import MatrixRoom
  3. async def command_calculate(room: MatrixRoom, event: RoomMessageText, bot):
  4. prompt = event.body.split()[2:]
  5. text = False
  6. results_only = True
  7. if "--text" in prompt:
  8. text = True
  9. delete = prompt.index("--text")
  10. del prompt[delete]
  11. if "--details" in prompt:
  12. results_only = False
  13. delete = prompt.index("--details")
  14. del prompt[delete]
  15. prompt = " ".join(prompt)
  16. if prompt:
  17. bot.logger.log("Querying calculation API...")
  18. for subpod in bot.calculation_api.generate_calculation_response(prompt, text, results_only, user=room.room_id):
  19. bot.logger.log(f"Sending subpod...")
  20. if isinstance(subpod, bytes):
  21. await bot.send_image(room, subpod)
  22. else:
  23. await bot.send_message(room, subpod, True)
  24. bot.log_api_usage(event, room, f"{self.calculation_api.api_code}-{self.calculation_api.calculation_api}", tokens_used)
  25. return
  26. await bot.send_message(room, "You need to provide a prompt.", True)