Spaces:
Paused
Paused
Improve DeepSeek compatibility (R1/reasoner support) and fix lint errors in main.py
Browse files3 unmodified lines45678910233 unmodified lines24424524624724824925072 unmodified lines32332432532632732832933033133233333433533633733833934034134234334434534634734834935035199 unmodified lines451452453454455456457458211 unmodified lines6706716726736746756766776786796806816826836846856866876886896906916926936946956966976983 unmodified linesimport timeimport boto3from pathlib import Pathfrom typing import Dict, List, Optional, Tuplefrom anthropic import Anthropicimport openaifrom openai import OpenAI233 unmodified linesclass LLMAnalyzer:"""Class for handling different LLM providers."""def __init__(self, provider: "ModelProvider", model_name: "AnalysisModelName"):self.provider = providerself.model_name = model_name72 unmodified linesasync def _analyze_with_deepseek(self, prompt: str) -> str:"""Analyze text using DeepSeek."""messages = [ChatMessage(role="system", content=SYSTEM_PROMPT),ChatMessage(role="user", content=prompt)]response_format = {'type': 'json_object'}try:response = self.client.chat.completions.create(model=self.model_name,messages=[{"role": m.role, "content": m.content} for m in messages],response_format=response_format,temperature=0)response_text = response.choices[0].message.content# Verify and clean JSONjson_data = extract_json_from_text(response_text)return json.dumps(json_data, ensure_ascii=False) if json_data else response_textexcept Exception as e:raise RuntimeError(f"Error in DeepSeek analysis: {str(e)}")async def _analyze_with_anthropic(self, prompt: str, response_schema: dict) -> str:"""Analyze text using Anthropic."""99 unmodified linesclass PrecedentAnalysisWorkflow(Workflow):"""Workflow for analyzing legal precedents."""def __init__(self, provider: ModelProvider = ModelProvider.OPENAI,model_name: AnalysisModelName = AnalysisModelName.GPT4o_MINI):super().__init__()self.analyzer = LLMAnalyzer(provider, model_name)211 unmodified linesclient = OpenAI(api_key=DEEPSEEK_API_KEY, base_url="https://api.deepseek.com")try:print(f"[DEBUG] DeepSeek Generation - Model: {model_name}")response = client.chat.completions.create(model=model_name,messages=[{"role": "system", "content": system_prompt},{"role": "user", "content": content},],temperature=GENERATION_TEMPERATURE,max_tokens=MAX_TOKENS_CONFIG["deepseek"],)response_text = response.choices[0].message.contentprint(f"[DEBUG] DeepSeek response length: {len(response_text)}")json_response = extract_json_from_text(response_text)if json_response and all(key in json_response for key in ["title", "text", "proceeding", "category"]):return json_responseelse:print(f"[WARNING] Invalid JSON structure from DeepSeek. Text: {response_text[:300]}...")raise ValueError("Invalid JSON structure")except Exception as e:print(f"[ERROR] DeepSeek generation/parsing failed: {e}")return {"title": "Автоматично сформований заголовок (DeepSeek)","text": response_text.strip() if 'response_text' in locals() else "Помилка при отриманні відповіді від DeepSeek","proceeding": "Не визначено","category": "Помилка API/Парсингу"}3 unmodified lines45678910233 unmodified lines24424524624724824925072 unmodified lines32332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735899 unmodified lines458459460461462463464465211 unmodified lines6776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187193 unmodified linesimport timeimport boto3from pathlib import Pathfrom typing import Dict, List, Optional, Tuple, Anyfrom anthropic import Anthropicimport openaifrom openai import OpenAI233 unmodified linesclass LLMAnalyzer:"""Class for handling different LLM providers."""def __init__(self, provider: Any, model_name: Any):self.provider = providerself.model_name = model_name72 unmodified linesasync def _analyze_with_deepseek(self, prompt: str) -> str:"""Analyze text using DeepSeek."""model_val = self.model_name.value if hasattr(self.model_name, "value") else str(self.model_name)is_reasoning = "reasoner" in model_val.lower()messages = []if is_reasoning:# DeepSeek R1 does not support system role, combine with usermessages.append(ChatMessage(role="user", content=f"{SYSTEM_PROMPT}\n\n{prompt}"))else:messages.append(ChatMessage(role="system", content=SYSTEM_PROMPT))messages.append(ChatMessage(role="user", content=prompt))try:completion_params = {"model": model_val,"messages": [{"role": m.role, "content": m.content} for m in messages],}# Use JSON mode and temperature only for non-reasoning modelsif not is_reasoning:completion_params["response_format"] = {'type': 'json_object'}completion_params["temperature"] = 0response = self.client.chat.completions.create(**completion_params)response_text = response.choices[0].message.content# Verify and clean JSONjson_data = extract_json_from_text(response_text)return json.dumps(json_data, ensure_ascii=False) if json_data else response_textexcept Exception as e:raise RuntimeError(f"Error in DeepSeek analysis ({model_val}): {str(e)}")async def _analyze_with_anthropic(self, prompt: str, response_schema: dict) -> str:"""Analyze text using Anthropic."""99 unmodified linesclass PrecedentAnalysisWorkflow(Workflow):"""Workflow for analyzing legal precedents."""def __init__(self, provider: Any = ModelProvider.OPENAI,model_name: Any = AnalysisModelName.GPT4o_MINI):super().__init__()self.analyzer = LLMAnalyzer(provider, model_name)211 unmodified linesclient = OpenAI(api_key=DEEPSEEK_API_KEY, base_url="https://api.deepseek.com")try:print(f"[DEBUG] DeepSeek Generation - Model: {model_name}")# Check for reasoning model (DeepSeek R1)is_reasoning = "reasoner" in model_name.lower()messages = []if is_reasoning:# R1 does not support system role, combine with usercombined_content = f"{system_prompt}\n\n{content}"messages.append({"role": "user", "content": combined_content})else:messages.append({"role": "system", "content": system_prompt})messages.append({"role": "user", "content": content})completion_params = {"model": model_name,"messages": messages,"max_tokens": MAX_TOKENS_CONFIG["deepseek"],}if not is_reasoning:completion_params["temperature"] = GENERATION_TEMPERATUREresponse = client.chat.completions.create(**completion_params)response_text = response.choices[0].message.contentprint(f"[DEBUG] DeepSeek response length: {len(response_text) if response_text else 0}")json_response = extract_json_from_text(response_text)if json_response and all(key in json_response for key in ["title", "text", "proceeding", "category"]):return json_responseelse:print(f"[WARNING] Invalid JSON structure from DeepSeek. Text: {response_text[:300] if response_text else 'None'}...")raise ValueError("Invalid JSON structure")except Exception as e:print(f"[ERROR] DeepSeek generation/parsing failed: {e}")return {"title": "Автоматично сформований заголовок (DeepSeek)","text": response_text.strip() if 'response_text' in locals() and response_text else f"Помилка при отриманні відповіді від DeepSeek: {str(e)}","proceeding": "Не визначено","category": "Помилка API/Парсингу"}
main.py+52-31