I have the latest version of OpenAi, but some of the attributes are missing. I have tried to reinstall it, didn't solve it. GPT and Chat are the ones i've discovered not working yet.
Bare in mind, i'm new to python and have basic knowledge of the language. The code is taken from GitHub
My code if it tells you something:
import openai
import pyttsx3
import speech_recognition as sr
from api_key import API_KEY
openai.api_key = API_KEY
engine = pyttsx3.init()
r = sr.Recognizer()
mic = sr.Microphone(device_index=1)
conversation = ""
user_name = "You"
bot_name = "DAI"
while True:
with mic as source:
print("\nlistening...")
r.adjust_for_ambient_noise(source, duration=0.2)
audio = r.listen(source)
print("no longer listening.\n")
try:
user_input = r.recognize_google(audio)
except:
continue
prompt = user_name + ": " + user_input + "\n" + bot_name+ ": "
conversation += prompt # allows for context
# fetch response from open AI api
response = openai.Completion.create(engine='text-davinci-003', prompt=conversation, max_tokens=100)
response_str = response["choices"][0]["text"].replace("\n", "")
response_str = response_str.split(user_name + ": ", 1)[0].split(bot_name + ": ", 1)[0]
conversation += response_str + "\n"
print(response_str)
engine.say(response_str)
engine.runAndWait()
All help will be appreciated
Edit:
The last answer made the error go away, but it put out a new one:
Thank you, it worked. But it still didn't work, I still get an error. I will try to resolve it if I can. Would appreciate any help and tips I get. This is probably not the last error I will encounter.
This is the error: Traceback (most recent call last):
File "/Users/danieforsell22b/Desktop/GPT3VoiceBot/gpt3Bot.py", line 22, in <module>
r.adjust_for_ambient_noise(source, duration=0.2)
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/speech_recognition/__init__.py", line 569, in adjust_for_ambient_noise
assert source.stream is not None, "Audio source must be entered before adjusting, see documentation for ``AudioSource``; are you using ``source`` outside of a ``with`` statement?"
^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: Audio source must be entered before adjusting, see documentation for ``AudioSource``; are you using ``source`` outside of a ``with`` statement?
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/danieforsell22b/Desktop/GPT3VoiceBot/gpt3Bot.py", line 20, in <module>
with mic as source:
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/speech_recognition/__init__.py", line 201, in __exit__
self.stream.close()
^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'close'