File size: 4,742 Bytes
5c25abd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/env python3
"""
Test script to verify API connections for OpenAI and DeepSeek
"""
import os
import httpx
from dotenv import load_dotenv
from openai import OpenAI

# Load environment variables
load_dotenv()

def test_openai():
    """Test OpenAI API connection"""
    print("\n" + "="*60)
    print("Testing OpenAI API Connection")
    print("="*60)
    
    api_key = os.getenv("OPENAI_API_KEY")
    if not api_key:
        print("❌ OPENAI_API_KEY not found in environment")
        return False
    
    print(f"✓ API Key found (length: {len(api_key)})")
    
    try:
        # Test with HTTP/2 enabled (default)
        print("\n[Test 1] Using default settings (HTTP/2 enabled)...")
        client = OpenAI(api_key=api_key, timeout=30.0)
        response = client.chat.completions.create(
            model="gpt-4o-mini",
            messages=[{"role": "user", "content": "Say 'connection test passed'"}],
            max_tokens=10
        )
        print(f"✅ Success with HTTP/2: {response.choices[0].message.content}")
    except Exception as e:
        print(f"❌ Failed with HTTP/2: {type(e).__name__}: {str(e)}")
    
    try:
        # Test with HTTP/2 disabled
        print("\n[Test 2] Using HTTP/1.1 (HTTP/2 disabled)...")
        client = OpenAI(
            api_key=api_key, 
            timeout=30.0,
            http_client=httpx.Client(http2=False, timeout=30.0)
        )
        response = client.chat.completions.create(
            model="gpt-4o-mini",
            messages=[{"role": "user", "content": "Say 'connection test passed'"}],
            max_tokens=10
        )
        print(f"✅ Success with HTTP/1.1: {response.choices[0].message.content}")
        return True
    except Exception as e:
        print(f"❌ Failed with HTTP/1.1: {type(e).__name__}: {str(e)}")
        return False


def test_deepseek():
    """Test DeepSeek API connection"""
    print("\n" + "="*60)
    print("Testing DeepSeek API Connection")
    print("="*60)
    
    api_key = os.getenv("DEEPSEEK_API_KEY")
    if not api_key:
        print("❌ DEEPSEEK_API_KEY not found in environment")
        return False
    
    print(f"✓ API Key found (length: {len(api_key)})")
    
    try:
        # Test with HTTP/2 enabled (default)
        print("\n[Test 1] Using default settings (HTTP/2 enabled)...")
        client = OpenAI(
            api_key=api_key, 
            base_url="https://api.deepseek.com",
            timeout=30.0
        )
        response = client.chat.completions.create(
            model="deepseek-chat",
            messages=[{"role": "user", "content": "Say 'connection test passed'"}],
            max_tokens=10
        )
        print(f"✅ Success with HTTP/2: {response.choices[0].message.content}")
    except Exception as e:
        print(f"❌ Failed with HTTP/2: {type(e).__name__}: {str(e)}")
    
    try:
        # Test with HTTP/2 disabled
        print("\n[Test 2] Using HTTP/1.1 (HTTP/2 disabled)...")
        client = OpenAI(
            api_key=api_key, 
            base_url="https://api.deepseek.com",
            timeout=30.0,
            http_client=httpx.Client(http2=False, timeout=30.0)
        )
        response = client.chat.completions.create(
            model="deepseek-chat",
            messages=[{"role": "user", "content": "Say 'connection test passed'"}],
            max_tokens=10
        )
        print(f"✅ Success with HTTP/1.1: {response.choices[0].message.content}")
        return True
    except Exception as e:
        print(f"❌ Failed with HTTP/1.1: {type(e).__name__}: {str(e)}")
        return False


def test_network_connectivity():
    """Test basic network connectivity"""
    print("\n" + "="*60)
    print("Testing Network Connectivity")
    print("="*60)
    
    urls = [
        "https://api.openai.com",
        "https://api.deepseek.com",
        "https://api.anthropic.com",
        "https://generativelanguage.googleapis.com"
    ]
    
    for url in urls:
        try:
            client = httpx.Client(timeout=10.0)
            response = client.get(url)
            print(f"✅ {url}: HTTP {response.status_code}")
        except Exception as e:
            print(f"❌ {url}: {type(e).__name__}: {str(e)}")


if __name__ == "__main__":
    print("\n🔍 API Connection Test Suite")
    print("="*60)
    
    # Test network connectivity first
    test_network_connectivity()
    
    # Test OpenAI
    openai_ok = test_openai()
    
    # Test DeepSeek
    deepseek_ok = test_deepseek()
    
    # Summary
    print("\n" + "="*60)
    print("Summary")
    print("="*60)
    print(f"OpenAI:  {'✅ OK' if openai_ok else '❌ FAILED'}")
    print(f"DeepSeek: {'✅ OK' if deepseek_ok else '❌ FAILED'}")
    print("="*60)