我正在尝试使用 OpenAI API 密钥创建一个聊天机器人。这是代码:
import openai
openai.api_key = "[key]"
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Tell me a joke."}
]
)
print(response.choices[0].message.content)
我收到一个 RateLimitError 消息,表示我已经超出了当前的使用限制,我需要升级我的计划,即使我以前从未使用过它。我什至在有机会发送消息之前就收到了错误。这是错误:
/usr/local/bin/python3 /Users/[]/Coding/chatgpt_api_test.py
[] ~ % /usr/local/bin/python3 /Users/[]/Coding/chatgpt_api_test.py
Traceback (most recent call last):
File "/Users/[]/Coding/chatgpt_api_test.py", line 4, in <module>
response = openai.ChatCompletion.create(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/openai/api_resources/chat_completion.py", line 25, in create
return super().create(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/openai/api_resources/abstract/engine_api_resource.py", line 153, in create
response, _, api_key = requestor.request(
^^^^^^^^^^^^^^^^^^
File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/openai/api_requestor.py", line 298, in request
resp, got_stream = self._interpret_response(result, stream)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/openai/api_requestor.py", line 700, in _interpret_response
self._interpret_response_line(
File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/openai/api_requestor.py", line 765, in _interpret_response_line
raise self.handle_error_response(
openai.error.RateLimitError: You exceeded your current quota, please check your plan and billing details. For more information on this error, read the docs: https://platform.openai.com/docs/guides/error-codes/api-errors.
我检查了我的 OpenAI 计费凭证,仍然有默认的、未使用的可用令牌数量 - 我也尝试过获取新的 API 密钥,但仍然遇到相同的错误。这是我的代码/使用问题吗,还是我应该修复我的 OpenAI 帐户设置中的某些内容(或者是其他内容)
我看到您使用的是旧版本的 openai-python,所以我认为这可能仅在您的特定版本中出现。
该代码适用于我的 openai-python==0.28.0
运行
pip install openai-python=0.28.0
并重试。