赞
踩
题意:如何在 Python 中检查 OpenAI 密钥的有效性?
https://pypi.org/project/openai/
"The library needs to be configured with your account's secret key which is available on the website. [...] Set it as the OPENAI_API_KEY environment variable"
When I ask Chat GPT to complete a message
当我请求 Chat GPT 完成一条消息时
- import openai
- response = openai.ChatCompletion.create(
- model="gpt-3.5-turbo",
- messages=[{"role": "user", "content": "What are the trade-offs around deadwood in forests?"}]
- )
- print(response)
I get a RateLimitError: You exceeded your current quota, please check your plan and billing details.
我收到一个 RateLimitError
错误:您已超出当前配额,请检查您的计划和账单详情
Is there a python method to check that the key is valid?
是否有一种 Python 方法可以检查密钥是否有效?
- In [35]: openai.api_key
- Out[35]: 'sk-...'
The Python codes shown, accesses openai.Model
, but this is no longer supported in openai>=1.0.0, see the v1.0.0 Migration Guide or README at https://github.com/openai/openai-python for the API.
显示的 Python 代码访问了 openai.Model
,但在 openai>=1.0.0
中不再支持此功能,请参阅 v1.0.0 迁移指南 或 README 以获取 API 信息。
Here is the adapted python code:
这是调整后的 Python 代码:
- import openai
-
- def check_openai_api_key(api_key):
- client = openai.OpenAI(api_key=api_key)
- try:
- client.models.list()
- except openai.AuthenticationError:
- return False
- else:
- return True
-
-
- OPENAI_API_KEY = "sk-7....."
-
- if check_openai_api_key(OPENAI_API_KEY):
- print("Valid OpenAI API key.")
- else:
- print("Invalid OpenAI API key.")
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。