Spaces:
Paused
Paused
Fix Connection error for OpenAI and DeepSeek: add retry logic and increase timeout to 120s
Browse files250 unmodified lines25125225325425425525625725825825926026150 unmodified lines31231331431531631731831932032131532232332432532632732832933033133233333426 unmodified lines361362363364365366367368369370348371372373374375376377378379380381382383268 unmodified lines65265365465562365665765865966066166266366466527 unmodified lines69369469569669769869965770070170270370470570670770870971071171271371471513 unmodified lines72973073173267773373473573673773873974074174218 unmodified lines761762763764765766767702768769770771772773774775776777778779780781782783250 unmodified lines if provider == ModelProvider.OPENAI: if not OPENAI_API_KEY: raise ValueError(f"OpenAI API key not configured. Please set OPENAI_API_KEY environment variable to use {provider.value} provider.") self.client = openai.OpenAI(api_key=OPENAI_API_KEY) self.client = openai.OpenAI(api_key=OPENAI_API_KEY, timeout=120.0) elif provider == ModelProvider.DEEPSEEK: if not DEEPSEEK_API_KEY: raise ValueError(f"DeepSeek API key not configured. Please set DEEPSEEK_API_KEY environment variable to use {provider.value} provider.") self.client = openai.OpenAI(api_key=DEEPSEEK_API_KEY, base_url="https://api.deepseek.com") self.client = openai.OpenAI(api_key=DEEPSEEK_API_KEY, base_url="https://api.deepseek.com", timeout=120.0) elif provider == ModelProvider.ANTHROPIC: if not ANTHROPIC_API_KEY: raise ValueError(f"Anthropic API key not configured. Please set ANTHROPIC_API_KEY environment variable to use {provider.value} provider.")50 unmodified lines if not is_reasoning_model: completion_params["temperature"] = 0
# Retry logic for OpenAI analysis max_retries = 3 last_error = None response = None for attempt in range(max_retries): try: print(f"[DEBUG] OpenAI Analysis call attempt {attempt + 1}/{max_retries}") response = self.client.chat.completions.create(**completion_params) response = self.client.chat.completions.create(**completion_params) break except Exception as api_err: last_error = api_err print(f"[ERROR] OpenAI Analysis attempt {attempt + 1} failed: {type(api_err).__name__}: {str(api_err)}") if attempt < max_retries - 1: time.sleep(2 ** attempt) else: raise last_error
response_text = response.choices[0].message.content # Verify it's valid JSON26 unmodified lines completion_params["response_format"] = {'type': 'json_object'} completion_params["temperature"] = 0
# Retry logic for DeepSeek analysis max_retries = 3 last_error = None response = None for attempt in range(max_retries): try: print(f"[DEBUG] DeepSeek Analysis call attempt {attempt + 1}/{max_retries}") response = self.client.chat.completions.create(**completion_params) response = self.client.chat.completions.create(**completion_params) break except Exception as api_err: last_error = api_err print(f"[ERROR] DeepSeek Analysis attempt {attempt + 1} failed: {type(api_err).__name__}: {str(api_err)}") if attempt < max_retries - 1: time.sleep(2 ** attempt) else: raise last_error
response_text = response.choices[0].message.content # Verify and clean JSON268 unmodified lines raise Exception(f"Текст судового рішення занадто короткий або відсутній (довжина: {len(court_decision_text) if court_decision_text else 0} символів). Будь ласка, перевірте вхідні дані.")
if provider == ModelProvider.OPENAI.value: # Increase timeout for complex models and HF Spaces environment client = OpenAI(api_key=OPENAI_API_KEY) client = OpenAI(api_key=OPENAI_API_KEY, timeout=120.0) # Retry logic for connection errors max_retries = 3 last_error = None response = None try: print(f"[DEBUG] OpenAI Generation - Model: {model_name}") 27 unmodified lines else: completion_params["temperature"] = GENERATION_TEMPERATURE
# Execute with retries for attempt in range(max_retries): try: print(f"[DEBUG] OpenAI API call attempt {attempt + 1}/{max_retries}") response = client.chat.completions.create(**completion_params) response = client.chat.completions.create(**completion_params) break except Exception as api_err: last_error = api_err error_type = type(api_err).__name__ print(f"[ERROR] OpenAI API attempt {attempt + 1} failed: {error_type}: {str(api_err)}") if attempt < max_retries - 1: wait_time = 2 ** attempt # 1, 2, 4 seconds print(f"[DEBUG] Retrying in {wait_time}s...") time.sleep(wait_time) else: raise last_error
response_text = response.choices[0].message.content print(f"[DEBUG] OpenAI response length: {len(response_text) if response_text else 0}") 13 unmodified lines }
if provider == ModelProvider.DEEPSEEK.value: # Increase timeout for DeepSeek API client = OpenAI(api_key=DEEPSEEK_API_KEY, base_url="https://api.deepseek.com") client = OpenAI(api_key=DEEPSEEK_API_KEY, base_url="https://api.deepseek.com", timeout=120.0) # Retry logic for DeepSeek max_retries = 3 last_error = None response = None try: print(f"[DEBUG] DeepSeek Generation - Model: {model_name}") 18 unmodified lines if not is_reasoning: completion_params["temperature"] = GENERATION_TEMPERATURE
# Execute with retries for attempt in range(max_retries): try: print(f"[DEBUG] DeepSeek API call attempt {attempt + 1}/{max_retries}") response = client.chat.completions.create(**completion_params) response = client.chat.completions.create(**completion_params) break except Exception as api_err: last_error = api_err error_type = type(api_err).__name__ print(f"[ERROR] DeepSeek API attempt {attempt + 1} failed: {error_type}: {str(api_err)}") if attempt < max_retries - 1: wait_time = 2 ** attempt print(f"[DEBUG] Retrying in {wait_time}s...") time.sleep(wait_time) else: raise last_error
response_text = response.choices[0].message.content print(f"[DEBUG] DeepSeek response length: {len(response_text) if response_text else 0}") main.py+86-8