hachat.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. from dotenv import dotenv_values, load_dotenv
  2. import os
  3. import speech_recognition as sr
  4. from utils.prompt import Prompt
  5. from langchain import LLMMathChain, SerpAPIWrapper
  6. from langchain.chains import RetrievalQA
  7. from langchain.llms import OpenAI
  8. from langchain.indexes import VectorstoreIndexCreator
  9. from langchain.document_loaders import TextLoader
  10. from langchain.utilities import OpenWeatherMapAPIWrapper
  11. from prompt_toolkit.key_binding import KeyBindings
  12. from models.ha import ChatHA
  13. from tools.ha import (
  14. HALightControl,
  15. HATempHumReading,
  16. HAGeolocation,
  17. HASensorReading
  18. )
  19. from langchain.agents import Tool
  20. load_dotenv()
  21. prompt = Prompt('hachat')
  22. search = SerpAPIWrapper()
  23. weather = OpenWeatherMapAPIWrapper()
  24. chat = ChatHA(tools=[
  25. Tool(
  26. name = "Search",
  27. func=search.run,
  28. description="useful for when you need to answer questions about current events"
  29. ),
  30. Tool(
  31. name = "Weather",
  32. func=weather.run,
  33. description="useful for when you need to search for weather information"
  34. ),
  35. HALightControl(),
  36. HATempHumReading(),
  37. HAGeolocation(),
  38. HASensorReading(),
  39. ]
  40. )
  41. # index = VectorstoreIndexCreator().from_loaders(
  42. # [
  43. # TextLoader('vectore_documents/administrative.txt', encoding='utf8'),
  44. # TextLoader('vectore_documents/rotld.txt', encoding='utf8')
  45. # ]
  46. # )
  47. if __name__ == '__main__':
  48. import argparse
  49. parser = argparse.ArgumentParser(description='Chat with the AI')
  50. parser.add_argument('--novoice', type=bool, default=False, help='novoice: disable voice input')
  51. args = parser.parse_args()
  52. input_mode = 'text'
  53. if not args.novoice:
  54. input_mode = prompt.get_input_mode()
  55. if not input_mode:
  56. prompt.print("You're an imbecile...I shall decide for you", tag='warning')
  57. input_mode = 'text'
  58. prompt.print(f"Input mode is {input_mode}")
  59. if input_mode == 'voice':
  60. r = sr.Recognizer()
  61. while True:
  62. input = None
  63. if input_mode == 'voice':
  64. prompt.print("Listening...")
  65. with sr.Microphone() as source:
  66. audio = r.listen(source)
  67. try:
  68. input = r.recognize_google_cloud(audio, language='ro-RO')
  69. print(f"{input}")
  70. except sr.UnknownValueError:
  71. prompt.print("Chat could not understand audio", tag='error')
  72. except sr.RequestError as e:
  73. prompt.print("Chat error; {0}".format(e), tag='error')
  74. else:
  75. input = prompt.get().strip()
  76. if not input:
  77. continue
  78. if input == 'exit':
  79. break
  80. try:
  81. result = chat.run(input)
  82. print("\n\nAI:>{}\n".format(result))
  83. except Exception as e:
  84. print("\n\nSystem:>{}\n".format(e))