-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart_conversation.py
122 lines (97 loc) · 3.77 KB
/
start_conversation.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env python3
'''
Speak with ChatGPT!
Dependencies:
- openai
- SpeechRecognition
- pyaudio
- pyttsx3
'''
import openai
import speech_recognition as sr
import pyttsx3
openai.api_key = "YOUR_OPENAI_API_KEY"
MODEL_ENGINE = "text-davinci-003"
USER = "USER"
AI = "ChatGPT"
class SpeakAI(object):
def __init__(self):
self.r = sr.Recognizer()
# given text, read it out loud using Google Assistant.
# Return if the user still wants to continue the conversation
def speak_text(self,
text):
try:
engine = pyttsx3.init()
engine.say(text)
engine.runAndWait()
except:
return False
return True
def get_response(self, prompt):
response = openai.Completion.create(
model=MODEL_ENGINE,
prompt=prompt,
temperature=0.5,
max_tokens=1024,
top_p=1.0,
frequency_penalty=0.0,
presence_penalty=0.0
)
# return converted text
return response.choices[0].text
def get_conversation(self,
conversation_history,
AI,
n=10):
# if conversation is too long, take the last n prompts to be provided as history to the AI agent
conv_len = len(conversation_history.split("::"))
if conv_len > n:
conversation_history = "::".join(
conversation_history.split("::")[-(n):])
response = self.get_response(conversation_history)
# label response as the AI response. To be appended to conversation history
response = response.split("::", 1)[1] if "::" in response else response
conversation_history += "{0}:: {1} ".format(
AI, response)
return conversation_history, response
def run(self):
conversation_history = "" # start a new conversation
while True:
try:
with sr.Microphone() as source:
print("{0}: (Ask anything from ChatGPT ...)".format(USER))
self.r.adjust_for_ambient_noise(source)
# listen for USER input
audio = self.r.listen(source)
# Using Google Assistant to recognize audio
inputText = self.r.recognize_google(audio)
inputText = inputText.lower()
conversation_history += "{0}:: {1}. ".format(
USER, inputText)
conversation_history, chatGPT_response = self.get_conversation(
conversation_history, AI, n=10)
print("{0}: {1} ".format(AI, chatGPT_response))
try:
continue_conversation = self.speak_text(chatGPT_response.split(
"::", 1)[1])
except:
continue_conversation = self.speak_text(
chatGPT_response)
if not continue_conversation:
print(
"\n\n #### Program terminated. Please find the transcript below. ####\n")
print(conversation_history)
break
except sr.RequestError as e:
print("Could not request results; {0}".format(e))
except sr.UnknownValueError:
print("unknown error occurred!")
except KeyboardInterrupt:
print(
"\n\n #### Program terminated. Please find the transcript below. ####\n")
print(conversation_history)
break
if __name__ == "__main__":
AI_agent = SpeakAI()
AI_agent.run()