File size: 2,098 Bytes
3ee7b82
 
 
 
 
 
 
 
 
 
 
f5c3d9c
3ee7b82
 
 
 
 
 
 
 
 
f5c3d9c
 
 
 
 
 
3ee7b82
 
 
 
 
 
 
 
 
 
 
 
f5c3d9c
3ee7b82
f5c3d9c
3ee7b82
 
 
f5c3d9c
3ee7b82
 
 
 
 
 
 
 
 
 
 
f5c3d9c
 
 
3ee7b82
 
 
f5c3d9c
3ee7b82
 
 
 
 
 
 
f5c3d9c
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
#!/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!")