from dotenv import dotenv_values, load_dotenv import os import speech_recognition as sr from utils.prompt import Prompt from langchain import LLMMathChain, SerpAPIWrapper from langchain.chains import RetrievalQA from langchain.llms import OpenAI from langchain.indexes import VectorstoreIndexCreator from langchain.document_loaders import TextLoader from langchain.utilities import OpenWeatherMapAPIWrapper from prompt_toolkit.key_binding import KeyBindings from models.ha import ChatHA from tools.ha import ( HALightControl, HATempHumReading, HAGeolocation, HASensorReading ) from langchain.agents import Tool load_dotenv() prompt = Prompt('hachat') search = SerpAPIWrapper() weather = OpenWeatherMapAPIWrapper() chat = ChatHA(tools=[ Tool( name = "Search", func=search.run, description="useful for when you need to answer questions about current events" ), Tool( name = "Weather", func=weather.run, description="useful for when you need to search for weather information" ), HALightControl(), HATempHumReading(), HAGeolocation(), HASensorReading(), ] ) # index = VectorstoreIndexCreator().from_loaders( # [ # TextLoader('vectore_documents/administrative.txt', encoding='utf8'), # TextLoader('vectore_documents/rotld.txt', encoding='utf8') # ] # ) if __name__ == '__main__': import argparse parser = argparse.ArgumentParser(description='Chat with the AI') parser.add_argument('--novoice', type=bool, default=False, help='novoice: disable voice input') args = parser.parse_args() input_mode = 'text' if not args.novoice: input_mode = prompt.get_input_mode() if not input_mode: prompt.print("You're an imbecile...I shall decide for you", tag='warning') input_mode = 'text' prompt.print(f"Input mode is {input_mode}") if input_mode == 'voice': r = sr.Recognizer() while True: input = None if input_mode == 'voice': prompt.print("Listening...") with sr.Microphone() as source: audio = r.listen(source) try: input = r.recognize_google_cloud(audio, language='ro-RO') print(f"{input}") except sr.UnknownValueError: prompt.print("Chat could not understand audio", tag='error') except sr.RequestError as e: prompt.print("Chat error; {0}".format(e), tag='error') else: input = prompt.get().strip() if not input: continue if input == 'exit': break try: result = chat.run(input) print("\n\nAI:>{}\n".format(result)) except Exception as e: print("\n\nSystem:>{}\n".format(e))