-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_api_keys.py
More file actions
76 lines (63 loc) · 2.53 KB
/
test_api_keys.py
File metadata and controls
76 lines (63 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import os
import openai
import google.generativeai as genai
from dotenv import load_dotenv
def test_openai_api():
"""
Tests the OpenAI API key.
"""
print("--- 正在测试 OpenAI API Key ---")
try:
# 使用从 .env 文件加载的密钥
client = openai.OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
# 尝试列出模型作为基本测试
models = client.models.list()
# 打印一个模型以确认成功
if models.data:
print("✅ OpenAI API Key 验证成功!")
print(f" 成功获取到模型: {models.data[0].id}")
else:
print("⚠️ OpenAI API Key 有效,但未能获取到任何模型。")
except openai.AuthenticationError:
print("❌ OpenAI API Key 验证失败:无效的API Key或权限问题。")
except Exception as e:
print(f"❌ 调用 OpenAI API 时发生未知错误: {e}")
def test_gemini_api():
"""
Tests the Gemini API key.
"""
print("\n--- 正在测试 Gemini API Key ---")
try:
# 配置Gemini API
genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
# 尝试列出模型作为基本测试
models = genai.list_models()
# 打印一个模型以确认成功
model_found = False
for m in models:
if 'generateContent' in m.supported_generation_methods:
print("✅ Gemini API Key 验证成功!")
print(f" 成功获取到模型: {m.name}")
model_found = True
break
if not model_found:
print("⚠️ Gemini API Key 有效,但未能找到支持'generateContent'的模型。")
except Exception as e:
# Gemini的Python SDK可能会以多种方式抛出异常,捕获通用异常
print(f"❌ Gemini API Key 验证失败或调用时发生错误: {e}")
if __name__ == "__main__":
# 加载 .env 文件中的环境变量
if not load_dotenv():
print("警告:未能找到 .env 文件。请确保该文件存在于项目根目录。")
# 即使没有.env文件,也尝试从环境中直接读取
# 检查密钥是否存在
openai_key = os.getenv("OPENAI_API_KEY")
gemini_key = os.getenv("GEMINI_API_KEY")
if not openai_key:
print("未找到 OPENAI_API_KEY 环境变量。")
else:
test_openai_api()
if not gemini_key:
print("\n未找到 GEMINI_API_KEY 环境变量。")
else:
test_gemini_api()