Revert httpx.Client customization: use default OpenAI SDK settings for async compatibility in HF Spaces
Browse files- main.py +8 -27
- test_api_connection.py +151 -0
main.py
CHANGED
|
@@ -3,7 +3,6 @@ import sys
|
|
| 3 |
import json
|
| 4 |
import time
|
| 5 |
import boto3
|
| 6 |
-
import httpx
|
| 7 |
from pathlib import Path
|
| 8 |
from typing import Dict, List, Optional, Tuple, Any
|
| 9 |
from anthropic import Anthropic
|
|
@@ -252,20 +251,13 @@ class LLMAnalyzer:
|
|
| 252 |
if provider == ModelProvider.OPENAI:
|
| 253 |
if not OPENAI_API_KEY:
|
| 254 |
raise ValueError(f"OpenAI API key not configured. Please set OPENAI_API_KEY environment variable to use {provider.value} provider.")
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
timeout=120.0,
|
| 258 |
-
http_client=httpx.Client(http2=False, timeout=120.0)
|
| 259 |
-
)
|
| 260 |
elif provider == ModelProvider.DEEPSEEK:
|
| 261 |
if not DEEPSEEK_API_KEY:
|
| 262 |
raise ValueError(f"DeepSeek API key not configured. Please set DEEPSEEK_API_KEY environment variable to use {provider.value} provider.")
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
base_url="https://api.deepseek.com",
|
| 266 |
-
timeout=120.0,
|
| 267 |
-
http_client=httpx.Client(http2=False, timeout=120.0)
|
| 268 |
-
)
|
| 269 |
elif provider == ModelProvider.ANTHROPIC:
|
| 270 |
if not ANTHROPIC_API_KEY:
|
| 271 |
raise ValueError(f"Anthropic API key not configured. Please set ANTHROPIC_API_KEY environment variable to use {provider.value} provider.")
|
|
@@ -662,13 +654,8 @@ def generate_legal_position(
|
|
| 662 |
raise Exception(f"Текст судового рішення занадто короткий або відсутній (довжина: {len(court_decision_text) if court_decision_text else 0} символів). Будь ласка, перевірте вхідні дані.")
|
| 663 |
|
| 664 |
if provider == ModelProvider.OPENAI.value:
|
| 665 |
-
#
|
| 666 |
-
|
| 667 |
-
client = OpenAI(
|
| 668 |
-
api_key=OPENAI_API_KEY,
|
| 669 |
-
timeout=120.0,
|
| 670 |
-
http_client=httpx.Client(http2=False, timeout=120.0)
|
| 671 |
-
)
|
| 672 |
|
| 673 |
# Retry logic for connection errors
|
| 674 |
max_retries = 3
|
|
@@ -744,14 +731,8 @@ def generate_legal_position(
|
|
| 744 |
}
|
| 745 |
|
| 746 |
if provider == ModelProvider.DEEPSEEK.value:
|
| 747 |
-
#
|
| 748 |
-
|
| 749 |
-
client = OpenAI(
|
| 750 |
-
api_key=DEEPSEEK_API_KEY,
|
| 751 |
-
base_url="https://api.deepseek.com",
|
| 752 |
-
timeout=120.0,
|
| 753 |
-
http_client=httpx.Client(http2=False, timeout=120.0)
|
| 754 |
-
)
|
| 755 |
|
| 756 |
# Retry logic for DeepSeek
|
| 757 |
max_retries = 3
|
|
|
|
| 3 |
import json
|
| 4 |
import time
|
| 5 |
import boto3
|
|
|
|
| 6 |
from pathlib import Path
|
| 7 |
from typing import Dict, List, Optional, Tuple, Any
|
| 8 |
from anthropic import Anthropic
|
|
|
|
| 251 |
if provider == ModelProvider.OPENAI:
|
| 252 |
if not OPENAI_API_KEY:
|
| 253 |
raise ValueError(f"OpenAI API key not configured. Please set OPENAI_API_KEY environment variable to use {provider.value} provider.")
|
| 254 |
+
# Use default httpx settings for better compatibility with async environments
|
| 255 |
+
self.client = openai.OpenAI(api_key=OPENAI_API_KEY, timeout=120.0)
|
|
|
|
|
|
|
|
|
|
| 256 |
elif provider == ModelProvider.DEEPSEEK:
|
| 257 |
if not DEEPSEEK_API_KEY:
|
| 258 |
raise ValueError(f"DeepSeek API key not configured. Please set DEEPSEEK_API_KEY environment variable to use {provider.value} provider.")
|
| 259 |
+
# Use default httpx settings for better compatibility with async environments
|
| 260 |
+
self.client = openai.OpenAI(api_key=DEEPSEEK_API_KEY, base_url="https://api.deepseek.com", timeout=120.0)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 261 |
elif provider == ModelProvider.ANTHROPIC:
|
| 262 |
if not ANTHROPIC_API_KEY:
|
| 263 |
raise ValueError(f"Anthropic API key not configured. Please set ANTHROPIC_API_KEY environment variable to use {provider.value} provider.")
|
|
|
|
| 654 |
raise Exception(f"Текст судового рішення занадто короткий або відсутній (довжина: {len(court_decision_text) if court_decision_text else 0} символів). Будь ласка, перевірте вхідні дані.")
|
| 655 |
|
| 656 |
if provider == ModelProvider.OPENAI.value:
|
| 657 |
+
# Use default OpenAI client settings for async compatibility in HF Spaces
|
| 658 |
+
client = OpenAI(api_key=OPENAI_API_KEY, timeout=120.0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 659 |
|
| 660 |
# Retry logic for connection errors
|
| 661 |
max_retries = 3
|
|
|
|
| 731 |
}
|
| 732 |
|
| 733 |
if provider == ModelProvider.DEEPSEEK.value:
|
| 734 |
+
# Use default client settings for async compatibility in HF Spaces
|
| 735 |
+
client = OpenAI(api_key=DEEPSEEK_API_KEY, base_url="https://api.deepseek.com", timeout=120.0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 736 |
|
| 737 |
# Retry logic for DeepSeek
|
| 738 |
max_retries = 3
|
test_api_connection.py
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Test script to verify API connections for OpenAI and DeepSeek
|
| 4 |
+
"""
|
| 5 |
+
import os
|
| 6 |
+
import httpx
|
| 7 |
+
from dotenv import load_dotenv
|
| 8 |
+
from openai import OpenAI
|
| 9 |
+
|
| 10 |
+
# Load environment variables
|
| 11 |
+
load_dotenv()
|
| 12 |
+
|
| 13 |
+
def test_openai():
|
| 14 |
+
"""Test OpenAI API connection"""
|
| 15 |
+
print("\n" + "="*60)
|
| 16 |
+
print("Testing OpenAI API Connection")
|
| 17 |
+
print("="*60)
|
| 18 |
+
|
| 19 |
+
api_key = os.getenv("OPENAI_API_KEY")
|
| 20 |
+
if not api_key:
|
| 21 |
+
print("❌ OPENAI_API_KEY not found in environment")
|
| 22 |
+
return False
|
| 23 |
+
|
| 24 |
+
print(f"✓ API Key found (length: {len(api_key)})")
|
| 25 |
+
|
| 26 |
+
try:
|
| 27 |
+
# Test with HTTP/2 enabled (default)
|
| 28 |
+
print("\n[Test 1] Using default settings (HTTP/2 enabled)...")
|
| 29 |
+
client = OpenAI(api_key=api_key, timeout=30.0)
|
| 30 |
+
response = client.chat.completions.create(
|
| 31 |
+
model="gpt-4o-mini",
|
| 32 |
+
messages=[{"role": "user", "content": "Say 'connection test passed'"}],
|
| 33 |
+
max_tokens=10
|
| 34 |
+
)
|
| 35 |
+
print(f"✅ Success with HTTP/2: {response.choices[0].message.content}")
|
| 36 |
+
except Exception as e:
|
| 37 |
+
print(f"❌ Failed with HTTP/2: {type(e).__name__}: {str(e)}")
|
| 38 |
+
|
| 39 |
+
try:
|
| 40 |
+
# Test with HTTP/2 disabled
|
| 41 |
+
print("\n[Test 2] Using HTTP/1.1 (HTTP/2 disabled)...")
|
| 42 |
+
client = OpenAI(
|
| 43 |
+
api_key=api_key,
|
| 44 |
+
timeout=30.0,
|
| 45 |
+
http_client=httpx.Client(http2=False, timeout=30.0)
|
| 46 |
+
)
|
| 47 |
+
response = client.chat.completions.create(
|
| 48 |
+
model="gpt-4o-mini",
|
| 49 |
+
messages=[{"role": "user", "content": "Say 'connection test passed'"}],
|
| 50 |
+
max_tokens=10
|
| 51 |
+
)
|
| 52 |
+
print(f"✅ Success with HTTP/1.1: {response.choices[0].message.content}")
|
| 53 |
+
return True
|
| 54 |
+
except Exception as e:
|
| 55 |
+
print(f"❌ Failed with HTTP/1.1: {type(e).__name__}: {str(e)}")
|
| 56 |
+
return False
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
def test_deepseek():
|
| 60 |
+
"""Test DeepSeek API connection"""
|
| 61 |
+
print("\n" + "="*60)
|
| 62 |
+
print("Testing DeepSeek API Connection")
|
| 63 |
+
print("="*60)
|
| 64 |
+
|
| 65 |
+
api_key = os.getenv("DEEPSEEK_API_KEY")
|
| 66 |
+
if not api_key:
|
| 67 |
+
print("❌ DEEPSEEK_API_KEY not found in environment")
|
| 68 |
+
return False
|
| 69 |
+
|
| 70 |
+
print(f"✓ API Key found (length: {len(api_key)})")
|
| 71 |
+
|
| 72 |
+
try:
|
| 73 |
+
# Test with HTTP/2 enabled (default)
|
| 74 |
+
print("\n[Test 1] Using default settings (HTTP/2 enabled)...")
|
| 75 |
+
client = OpenAI(
|
| 76 |
+
api_key=api_key,
|
| 77 |
+
base_url="https://api.deepseek.com",
|
| 78 |
+
timeout=30.0
|
| 79 |
+
)
|
| 80 |
+
response = client.chat.completions.create(
|
| 81 |
+
model="deepseek-chat",
|
| 82 |
+
messages=[{"role": "user", "content": "Say 'connection test passed'"}],
|
| 83 |
+
max_tokens=10
|
| 84 |
+
)
|
| 85 |
+
print(f"✅ Success with HTTP/2: {response.choices[0].message.content}")
|
| 86 |
+
except Exception as e:
|
| 87 |
+
print(f"❌ Failed with HTTP/2: {type(e).__name__}: {str(e)}")
|
| 88 |
+
|
| 89 |
+
try:
|
| 90 |
+
# Test with HTTP/2 disabled
|
| 91 |
+
print("\n[Test 2] Using HTTP/1.1 (HTTP/2 disabled)...")
|
| 92 |
+
client = OpenAI(
|
| 93 |
+
api_key=api_key,
|
| 94 |
+
base_url="https://api.deepseek.com",
|
| 95 |
+
timeout=30.0,
|
| 96 |
+
http_client=httpx.Client(http2=False, timeout=30.0)
|
| 97 |
+
)
|
| 98 |
+
response = client.chat.completions.create(
|
| 99 |
+
model="deepseek-chat",
|
| 100 |
+
messages=[{"role": "user", "content": "Say 'connection test passed'"}],
|
| 101 |
+
max_tokens=10
|
| 102 |
+
)
|
| 103 |
+
print(f"✅ Success with HTTP/1.1: {response.choices[0].message.content}")
|
| 104 |
+
return True
|
| 105 |
+
except Exception as e:
|
| 106 |
+
print(f"❌ Failed with HTTP/1.1: {type(e).__name__}: {str(e)}")
|
| 107 |
+
return False
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
def test_network_connectivity():
|
| 111 |
+
"""Test basic network connectivity"""
|
| 112 |
+
print("\n" + "="*60)
|
| 113 |
+
print("Testing Network Connectivity")
|
| 114 |
+
print("="*60)
|
| 115 |
+
|
| 116 |
+
urls = [
|
| 117 |
+
"https://api.openai.com",
|
| 118 |
+
"https://api.deepseek.com",
|
| 119 |
+
"https://api.anthropic.com",
|
| 120 |
+
"https://generativelanguage.googleapis.com"
|
| 121 |
+
]
|
| 122 |
+
|
| 123 |
+
for url in urls:
|
| 124 |
+
try:
|
| 125 |
+
client = httpx.Client(timeout=10.0)
|
| 126 |
+
response = client.get(url)
|
| 127 |
+
print(f"✅ {url}: HTTP {response.status_code}")
|
| 128 |
+
except Exception as e:
|
| 129 |
+
print(f"❌ {url}: {type(e).__name__}: {str(e)}")
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
if __name__ == "__main__":
|
| 133 |
+
print("\n🔍 API Connection Test Suite")
|
| 134 |
+
print("="*60)
|
| 135 |
+
|
| 136 |
+
# Test network connectivity first
|
| 137 |
+
test_network_connectivity()
|
| 138 |
+
|
| 139 |
+
# Test OpenAI
|
| 140 |
+
openai_ok = test_openai()
|
| 141 |
+
|
| 142 |
+
# Test DeepSeek
|
| 143 |
+
deepseek_ok = test_deepseek()
|
| 144 |
+
|
| 145 |
+
# Summary
|
| 146 |
+
print("\n" + "="*60)
|
| 147 |
+
print("Summary")
|
| 148 |
+
print("="*60)
|
| 149 |
+
print(f"OpenAI: {'✅ OK' if openai_ok else '❌ FAILED'}")
|
| 150 |
+
print(f"DeepSeek: {'✅ OK' if deepseek_ok else '❌ FAILED'}")
|
| 151 |
+
print("="*60)
|