最近想在python里调用gpt模型的api来实现一个聊天机器人。但是在运行代码时会报错:
Traceback (most recent call last):
File "C:\Users\王铭瑄\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\tenacity\__init__.py", line 382, in __call__
result = fn(*args, **kwargs)
File "d:\编程资料\编程语言\python\我的程序\ChatGPT.py", line 11, in completion_with_backoff
return openai.Completion.create(**kwargs)
File "C:\Users\王铭瑄\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\openai\api_resources\completion.py", line 25, in create
return super().create(*args, **kwargs)
File "C:\Users\王铭瑄\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\openai\api_resources\abstract\engine_api_resource.py", line 153, in create
response, _, api_key = requestor.request(
File "C:\Users\王铭瑄\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\openai\api_requestor.py", line 298, in request
resp, got_stream = self._interpret_response(result, stream)
File "C:\Users\王铭瑄\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\openai\api_requestor.py", line 700, in _interpret_response
self._interpret_response_line(
File "C:\Users\王铭瑄\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\openai\api_requestor.py", line 763, in _interpret_response_line
raise self.handle_error_response(
openai.error.RateLimitError: You exceeded your current quota, please check your plan and billing details.
这个错误是因为调用gpt-3.5-torbo频率超过限制造成的,但是程序调用频率没超过openai的官方文档里说的。代码:
import openai
import os
from tenacity import (
retry,
stop_after_attempt,
wait_random_exponential,
)
@retry(wait=wait_random_exponential(min=1, max=60), stop=stop_after_attempt(6))
def completion_with_backoff(**kwargs):
return openai.Completion.create(**kwargs)
ENCRYPTED_OPENAI_API_KEY = "..."
openai.api_key = ENCRYPTED_OPENAI_API_KEY
while True:
q = input("问题:")
# res=openai.ChatCompletion.create(
# model="gpt-3.5-turbo",
# messages=[
# {"role":"user","content":q}
# ]
# )
res = completion_with_backoff(model="gpt-3.5-turbo",
messages=[
{"role": "user", "content": q}
]
)
print(res["choices"][0]["message"]["content"])
(运行此代码时开了代理)
请问大家为什么会抛出openai.error.RateLimitError以及如何解决?