Spaces:
Sleeping
Sleeping

refactor: improve code readability and structure in OpenAI integration tests and services, update requirements for consistency
f5c3d9c
#!/usr/bin/env python3 | |
import os | |
import sys | |
from dotenv import load_dotenv | |
print("π OpenAI Connection Debug Script") | |
print("=" * 40) | |
# Load environment | |
load_dotenv() | |
api_key = os.getenv("OPENAI_API_KEY") | |
print(f"1. API Key configured: {'β Yes' if api_key else 'β No'}") | |
if api_key: | |
print(f" Preview: {api_key[:10]}...") | |
# Test basic HTTP connectivity | |
print("\n2. Testing basic HTTP connectivity...") | |
try: | |
import requests | |
response = requests.get( | |
"https://api.openai.com/v1/models", | |
headers={"Authorization": f"Bearer {api_key}"}, | |
timeout=30, | |
) | |
print(f" Status: {response.status_code}") | |
if response.status_code == 200: | |
print(" β Direct HTTP request works!") | |
else: | |
print(f" β HTTP error: {response.text[:200]}") | |
except Exception as e: | |
print(f" β Network error: {str(e)}") | |
# Test OpenAI library - simple client | |
print("\n3. Testing OpenAI library (simple config)...") | |
try: | |
from openai import OpenAI | |
client = OpenAI(api_key=api_key) | |
response = client.chat.completions.create( | |
model="gpt-3.5-turbo", | |
messages=[{"role": "user", "content": "Say 'test'"}], | |
max_tokens=5, | |
) | |
print(" β OpenAI library works!") | |
print(f" Response: {response.choices[0].message.content}") | |
except Exception as e: | |
print(f" β OpenAI library error: {str(e)}") | |
print(f" Error type: {type(e).__name__}") | |
# Test OpenAI library - with timeout config | |
print("\n4. Testing OpenAI library (with timeout)...") | |
try: | |
from openai import OpenAI | |
client = OpenAI(api_key=api_key, timeout=60.0, max_retries=2) | |
response = client.chat.completions.create( | |
model="gpt-3.5-turbo", | |
messages=[{"role": "user", "content": "Say 'test'"}], | |
max_tokens=5, | |
) | |
print(" β OpenAI library with timeout works!") | |
print(f" Response: {response.choices[0].message.content}") | |
except Exception as e: | |
print(f" β OpenAI library with timeout error: {str(e)}") | |
print(f" Error type: {type(e).__name__}") | |
print("\nπ Debug complete!") | |