gpt4 book ai didi

python - OpenAI GPT-3 API 错误 : "AttributeError: module ' openai' has no attribute 'GPT' "

转载 作者:行者123 更新时间:2023-12-02 22:45:36 32 4
gpt4 key购买 nike

我有最新版本的 OpenAi,但缺少某些属性。我试过重新安装它,没有解决它。 GPT 和 Chat 是我发现还不能用的。

切记,我是 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'

最佳答案

您将变量 response_str 初始化了两次,这就是您得到 AttributeError: module 'openai' has no attribute 'GPT' 的原因。

从此更改您的代码...

# 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()

...为此。

# 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_split = response_str.split(user_name + ": ", 1)[0].split(bot_name + ": ", 1)[0]

conversation += response_str_split + "\n"
print(response_str_split)

engine.say(response_str_split)
engine.runAndWait()

关于python - OpenAI GPT-3 API 错误 : "AttributeError: module ' openai' has no attribute 'GPT' ",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74988365/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com