-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchat.py
More file actions
47 lines (31 loc) · 1.14 KB
/
chat.py
File metadata and controls
47 lines (31 loc) · 1.14 KB
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
import os
import openai
import subprocess
import re
from record_audio import record_audio
from get_transcription import get_transcription
from get_chat_response import get_chat_response_gpt, get_chat_response_davinci
def main():
# Set up OpenAI API credentials
openai.api_key = os.environ["OPENAI_API_KEY"]
messages = []
while True:
record_audio()
transcript = get_transcription()
print(f"User: {transcript.text}")
if re.search('^[^a-zA-Z]*$', transcript.text):
continue
# Save user input to history
messages.append({"role": "user", "content": f"Answer in one sentence. {transcript.text}"})
# Get chat response from OpenAI Chat API
chat_response = get_chat_response_gpt(messages)
# Save response to history
messages.append({"role": "assistant", "content": chat_response})
# Debug print chat response
print(f"Response: {chat_response}")
# Speak chat response
subprocess.run(["say", chat_response])
# Save only the last x interactions
messages = messages[-4:]
if __name__ == "__main__":
main()