-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhome_assistant.py
61 lines (47 loc) · 1.98 KB
/
home_assistant.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import os
import time
from audio_to_text import AudioToTextWorker
from chat_api_client import ChatApiClient
from rest_api_client import RestApiClient
class HomeAssistant:
def __init__(self) -> None:
self.read_environ()
self.read_system_prompts()
self.audio_to_text_worker = AudioToTextWorker()
self.chat_api_client = ChatApiClient(self.system_prompts)
self.rest_api_client = RestApiClient(self.home_assistant_host, self.home_assistant_token)
def read_environ(self):
self.home_assistant_host = os.environ["HOME_ASSISTANT_HOST"]
if not self.home_assistant_host:
raise KeyError("HOME_ASSISTANT_HOST not set")
self.home_assistant_token = os.environ["HOME_ASSISTANT_API_TOKEN"]
if not self.home_assistant_token:
raise KeyError("HOME_ASSISTANT_API_TOKEN not set")
if not os.environ["OPENAI_API_KEY"]:
raise KeyError("OPENAI_API_KEY not set")
def read_system_prompts(self, path: str = "./system_prompts.txt"):
with open("./system_prompts.txt", "r") as file:
system_prompts = []
for line in file:
system_prompts.append(line)
self.system_prompts = "\n".join(system_prompts)
def run(self):
while True:
self.audio_to_text_worker.record_audio()
prompt = self.audio_to_text_worker.audio_to_text()
print(f"User: {prompt}")
if "Bye-bye" in prompt or not prompt:
break
reply = self.chat_api_client.chat(prompt)
if isinstance(reply, str):
print(f"Assistant: {reply}")
continue
status = self.rest_api_client.request(reply)
if status:
print(f"Assistant: Task Done.")
else:
print(f"Assistant: Task Failed.")
time.sleep(2)
if __name__ == "__main__":
home_assistant = HomeAssistant()
home_assistant.run()