python-archieve-projects/Utils/test_openai_key.py

25 lines
671 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import openai
def test_api_key(api_key):
openai.api_key = api_key
try:
# 尝试使用API密钥获取模型列表
models = openai.models.list()
print("您的OpenAI API密钥是有效的。")
print(models)
return True
except Exception as e:
print(f"出现错误可能是因为API密钥无效{e}")
return False
# 用您的实际API密钥替换这里的'YOUR_API_KEY'
your_api_key='sk-chatyi-5k90P6blmq5MA30LN2jNT3BlbkFJ98xEMl4pe1lH4P7uW0n3'
is_valid = test_api_key(your_api_key)
if is_valid:
print("密钥测试通过。")
else:
print("密钥测试失败请检查您的API密钥。")