Spaces:
Running
Running
File size: 3,705 Bytes
6c4292b af01c91 6c4292b af01c91 6c4292b af01c91 6c4292b af01c91 6c4292b af01c91 6c4292b af01c91 6c4292b af01c91 6c4292b af01c91 6c4292b af01c91 6c4292b af01c91 6c4292b |
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 |
import os
import time
import requests
from typing import List, Dict, Optional
from config import Config
from openai import OpenAI
class DeepseekClient(OpenAI):
def __init__(self, api_key=None, **kwargs):
super().__init__(api_key=api_key or Config.get_deepseek_key(), **kwargs)
if not self.api_key:
raise ValueError("Deepseek API key is not set")
self.base_url = Config.DEEPSEEK_API_BASE
self.max_retries = 3
self.retry_delay = 2 # Initial delay in seconds
# Debug info
print(f"Initializing DeepseekClient with:")
print(f" api_key: {'*' * 8}{self.api_key[-4:]}" if self.api_key else " api_key: None")
print(f" base_url: {self.base_url}")
def create(self, messages: List[Dict[str, str]], model: str = None, response_format: Optional[Dict] = None, **kwargs) -> str:
if not self.api_key:
raise ValueError("Deepseek API key is not set")
# Debug info
print("\n[DeepseekClient] Starting create request")
print(f" Using model: {model or Config.DEEPSEEK_MODEL}")
print(f" Messages count: {len(messages)}")
if messages:
print(f" First message: {messages[0]['content'][:50]}...")
retries = 0
last_error = None
while retries < self.max_retries:
try:
print(f"[DeepseekClient] Attempting API request (retry {retries + 1}/{self.max_retries})")
response = self.chat.completions.create(
model=model or Config.DEEPSEEK_MODEL,
messages=messages,
temperature=0.7,
max_tokens=1000,
response_format=response_format,
**kwargs
)
response_data = response.model_dump()
if "error" in response_data:
raise ValueError(f"Deepseek API returned error: {response_data['error']}")
if not response_data.get('choices'):
raise ValueError("No choices in Deepseek API response")
if not response_data['choices'][0].get('message'):
raise ValueError("No message in Deepseek API response choice")
content = response_data['choices'][0]['message']['content']
print("[DeepseekClient] API request successful")
print(f"[DeepseekClient] Received response content (length: {len(content)})")
return content
except requests.exceptions.RequestException as e:
print(f"[DeepseekClient] Request failed: {str(e)}")
print(f"Deepseek Request Error: {str(e)}")
last_error = e
retries += 1
if retries < self.max_retries:
time.sleep(self.retry_delay * (2 ** retries))
except ValueError as e:
print(f"[DeepseekClient] Value error occurred: {str(e)}")
print(f"Deepseek Value Error: {str(e)}")
last_error = e
break
except Exception as e:
print(f"[DeepseekClient] Unexpected error occurred: {str(e)}")
print(f"Deepseek Unexpected Error: {str(e)}")
last_error = e
break
# If all retries failed or other error occurred
error_msg = str(last_error) if last_error else "Maximum retries exceeded"
if "Rate limit exceeded" in error_msg:
raise Exception(f"Deepseek rate limit exceeded. Please try again later or switch to a different model.")
raise Exception(f"Deepseek API error: {error_msg}")
|