mirror of
https://github.com/hanzoai/python-sdk.git
synced 2026-10-23 20:23:00 +00:00
drop a script that existed to hold a key
openai_api_check.py was not a test — pytest never collected it, nothing imported it, and its whole body was a hand-run connectivity probe against a vendor with the credential written in as a literal. There is nothing to keep. The value it carried must still be rotated: it is in this repo's history and the history is public.
This commit is contained in:
@@ -1,65 +0,0 @@
|
||||
"""Simple script to test OpenAI API connectivity."""
|
||||
|
||||
import sys
|
||||
|
||||
from openai import OpenAI
|
||||
|
||||
import os
|
||||
|
||||
# The credential is read from the environment. A key in a source file is a key
|
||||
# in every clone of it.
|
||||
api_key = os.environ.get("OPENROUTER_API_KEY")
|
||||
if not api_key:
|
||||
sys.exit("set OPENROUTER_API_KEY")
|
||||
|
||||
|
||||
def check_api_connection():
|
||||
"""Test a simple OpenAI API connection."""
|
||||
print("Testing OpenAI API connection...")
|
||||
|
||||
try:
|
||||
# Initialize the client with the API key
|
||||
client = OpenAI(api_key=api_key)
|
||||
|
||||
# Make a simple request
|
||||
response = client.chat.completions.create(
|
||||
model="gpt-3.5-turbo", # Use a simpler model
|
||||
messages=[
|
||||
{"role": "system", "content": "You are a helpful assistant."},
|
||||
{"role": "user", "content": "Say hello!"},
|
||||
],
|
||||
max_tokens=10, # Request minimal tokens for a quick test
|
||||
)
|
||||
|
||||
# If we get here, the connection worked
|
||||
print("SUCCESS: API connection established!")
|
||||
print(f"Response: {response.choices[0].message.content}")
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
# Print detailed error information
|
||||
print(f"ERROR: {type(e).__name__}: {str(e)}")
|
||||
|
||||
# Check for common error types and provide more helpful messages
|
||||
if "invalid_api_key" in str(e).lower() or "authentication" in str(e).lower():
|
||||
print("\nPossible cause: The API key may be invalid or expired.")
|
||||
print("Solution: Obtain a new API key from the OpenAI dashboard.")
|
||||
|
||||
elif "insufficient_quota" in str(e).lower():
|
||||
print("\nPossible cause: Your OpenAI account may be out of credits.")
|
||||
print(
|
||||
"Solution: Check your usage and billing information in the OpenAI dashboard."
|
||||
)
|
||||
|
||||
elif "connection" in str(e).lower():
|
||||
print("\nPossible cause: Network connectivity issue.")
|
||||
print("Solution: Check your internet connection and firewall settings.")
|
||||
print(" Some networks block OpenAI API calls.")
|
||||
print(" Try using a different network or VPN.")
|
||||
|
||||
return False
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
success = check_api_connection()
|
||||
sys.exit(0 if success else 1)
|
||||
Reference in New Issue
Block a user