Fix persistent Connection Errors for OpenAI/DeepSeek: disable HTTP/2 and use robust httpx client
Browse files
main.py
CHANGED
|
@@ -3,6 +3,7 @@ import sys
|
|
| 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,11 +252,20 @@ class LLMAnalyzer:
|
|
| 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 |
-
self.client = openai.OpenAI(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 255 |
elif provider == ModelProvider.DEEPSEEK:
|
| 256 |
if not DEEPSEEK_API_KEY:
|
| 257 |
raise ValueError(f"DeepSeek API key not configured. Please set DEEPSEEK_API_KEY environment variable to use {provider.value} provider.")
|
| 258 |
-
self.client = openai.OpenAI(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 259 |
elif provider == ModelProvider.ANTHROPIC:
|
| 260 |
if not ANTHROPIC_API_KEY:
|
| 261 |
raise ValueError(f"Anthropic API key not configured. Please set ANTHROPIC_API_KEY environment variable to use {provider.value} provider.")
|
|
@@ -653,7 +663,12 @@ def generate_legal_position(
|
|
| 653 |
|
| 654 |
if provider == ModelProvider.OPENAI.value:
|
| 655 |
# Increase timeout for complex models and HF Spaces environment
|
| 656 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 657 |
|
| 658 |
# Retry logic for connection errors
|
| 659 |
max_retries = 3
|
|
@@ -729,8 +744,14 @@ def generate_legal_position(
|
|
| 729 |
}
|
| 730 |
|
| 731 |
if provider == ModelProvider.DEEPSEEK.value:
|
| 732 |
-
# Increase timeout for DeepSeek API
|
| 733 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 734 |
|
| 735 |
# Retry logic for DeepSeek
|
| 736 |
max_retries = 3
|
|
|
|
| 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 |
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 |
+
self.client = openai.OpenAI(
|
| 256 |
+
api_key=OPENAI_API_KEY,
|
| 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 |
+
self.client = openai.OpenAI(
|
| 264 |
+
api_key=DEEPSEEK_API_KEY,
|
| 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.")
|
|
|
|
| 663 |
|
| 664 |
if provider == ModelProvider.OPENAI.value:
|
| 665 |
# Increase timeout for complex models and HF Spaces environment
|
| 666 |
+
# Use specific http_client with HTTP/2 disabled to resolve Connection Errors in some environments
|
| 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 |
}
|
| 745 |
|
| 746 |
if provider == ModelProvider.DEEPSEEK.value:
|
| 747 |
+
# Increase timeout and improve reliability for DeepSeek API
|
| 748 |
+
# Use specific http_client with HTTP/2 disabled to resolve Connection Errors
|
| 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
|