我有最新版本的OpenAi,但是有些属性丢失了。我试着重新安装,没有解决。GPT和聊天是我发现还不能用的。
坦率地说,我是python的新手,对这门语言有基本的了解。代码取自GitHub
我的代码如果它告诉你什么:
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()
所有的帮助将不胜感激
编辑:
最后一个答案消除了这个错误,但又产生了一个新的错误:
谢谢,它成功了。但是它仍然不起作用,我仍然得到一个错误。我将尝试解决它,如果我可以。将感谢任何帮助和提示我得到。这可能不是我会遇到的最后一个错误。
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'
1条答案
按热度按时间hl0ma9xz1#
变量
response_str
初始化了两次,这就是得到AttributeError: module 'openai' has no attribute 'GPT'
的原因。更改您的代码...
......到这个。