-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_api_keys.py
More file actions
110 lines (86 loc) · 3.63 KB
/
test_api_keys.py
File metadata and controls
110 lines (86 loc) · 3.63 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/env python3
"""
Test API Keys - A simplified script to check if both Google and OpenAI API keys are working
"""
import os
import sys
import json
import logging
# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger("APIKeyTest")
def test_google_api():
"""Test if Google Gemini API key is working"""
try:
import google.generativeai as genai
# Get API key from environment variable
api_key = os.environ.get("GOOGLE_API_KEY", "")
if not api_key:
logger.error("GOOGLE_API_KEY environment variable not set")
return False
# Configure the Gemini API
genai.configure(api_key=api_key)
# Get available models
try:
models = [m.name for m in genai.list_models()]
logger.info(f"Available Gemini models: {models[:5]}... (truncated)")
# Try to use a model
model_name = "models/gemini-1.5-flash" # Use a standard model
model = genai.GenerativeModel(model_name)
# Simple test prompt
response = model.generate_content("Hello, what's the weather like today?")
logger.info("Google Gemini API test successful!")
logger.info(f"Response preview: {str(response)[:100]}...")
return True
except Exception as e:
logger.error(f"Error with Google Gemini API: {e}")
return False
except ImportError:
logger.error("google-generativeai package not installed")
return False
def test_openai_api():
"""Test if OpenAI API key is working"""
try:
from openai import OpenAI
# Get API key from environment variable
api_key = os.environ.get("OPENAI_API_KEY", "")
if not api_key:
logger.error("OPENAI_API_KEY environment variable not set")
return False
# Initialize OpenAI client
client = OpenAI(api_key=api_key)
# Try a simple completion
try:
response = client.chat.completions.create(
model="gpt-3.5-turbo", # Use a standard model
messages=[{"role": "user", "content": "Hello, how are you today?"}],
max_tokens=20
)
logger.info("OpenAI API test successful!")
logger.info(f"Response: {response.choices[0].message.content}")
return True
except Exception as e:
logger.error(f"Error with OpenAI API: {e}")
return False
except ImportError:
logger.error("openai package not installed")
return False
def main():
"""Main function to test both APIs"""
logger.info("Testing API keys...")
# Test Google API
google_success = test_google_api()
# Test OpenAI API
openai_success = test_openai_api()
# Summary
logger.info("\n===== API TEST RESULTS =====")
logger.info(f"Google Gemini API: {'✓ WORKING' if google_success else '✗ NOT WORKING'}")
logger.info(f"OpenAI API: {'✓ WORKING' if openai_success else '✗ NOT WORKING'}")
if google_success and openai_success:
logger.info("All API keys are working correctly!")
elif not google_success and not openai_success:
logger.error("Both API keys are not working. Please check your credentials.")
else:
logger.warning("One API key is working, but the other is not. Review the logs above for details.")
if __name__ == "__main__":
main()