Spaces:
Paused
Paused
Fix OpenAI gpt-4.1 API parameters: role 'developer', max_completion_tokens, and reasoning effort support; update max_tokens in default config
Browse filesFiles changed (2)
- config/environments/default.yaml+4-4
- main.py+56-21
22 unmodified lines232425262728292627282930313222 unmodified lines# Generation Settingsgeneration: max_tokens: openai: 4096 anthropic: 4096 gemini: 4096 deepseek: 4096 openai: 2048 anthropic: 2048 gemini: 2048 deepseek: 2048 max_tokens_analysis: 4000 temperature: 0.5
243 unmodified lines24424524624724724824925030 unmodified lines2812822832842852862872882892902912852922932942956 unmodified lines302303304298299300301302303305306307308309310311312313314315316317318319320321310322323324325290 unmodified lines616617618607608609610611612613614615619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651617652653654655656657623658659660661662663629664665666667243 unmodified linesclass LLMAnalyzer: """Class for handling different LLM providers."""
def __init__(self, provider: ModelProvider, model_name: AnalysisModelName): def __init__(self, provider: "ModelProvider", model_name: "AnalysisModelName"): self.provider = provider self.model_name = model_name
30 unmodified lines
async def _analyze_with_openai(self, prompt: str, response_schema: dict) -> str: """Analyze text using OpenAI.""" # Determine model name and if it's a reasoning model model_val = self.model_name.value if hasattr(self.model_name, "value") else str(self.model_name) is_reasoning_model = any(m in model_val.lower() for m in ["gpt-4.1", "gpt-4.5", "o1", "o3"]) # Use developer role for newer models role = "developer" if is_reasoning_model else "system" messages = [ ChatMessage(role="system", content=SYSTEM_PROMPT), ChatMessage(role=role, content=SYSTEM_PROMPT), ChatMessage(role="user", content=prompt) ]
6 unmodified lines }
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 ) completion_params = { "model": model_val, "messages": [{"role": m.role, "content": m.content} for m in messages], "response_format": response_format, } # Reasoning models usually require temperature=1.0 or none if not is_reasoning_model: completion_params["temperature"] = 0
response = self.client.chat.completions.create(**completion_params) response_text = response.choices[0].message.content # Verify it's valid JSON json_data = extract_json_from_text(response_text) return json.dumps(json_data, ensure_ascii=False) if json_data else response_text except Exception as e: raise RuntimeError(f"Error in OpenAI analysis: {str(e)}") raise RuntimeError(f"Error in OpenAI analysis ({model_val}): {str(e)}")
async def _analyze_with_deepseek(self, prompt: str) -> str: """Analyze text using DeepSeek."""290 unmodified lines client = OpenAI(api_key=OPENAI_API_KEY) try: print(f"[DEBUG] OpenAI 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["openai"], ) # Check for reasoning models (gpt-4.1, o1, etc.) is_reasoning_model = any(m in model_name.lower() for m in ["gpt-4.1", "gpt-4.5", "o1", "o3"]) # Use developer role for newer models, system for others role = "developer" if is_reasoning_model else "system" messages = [ {"role": role, "content": system_prompt}, {"role": "user", "content": content}, ] # Parameters for chat completion completion_params = { "model": model_name, "messages": messages, } # Set tokens based on model capabilities if is_reasoning_model: completion_params["max_completion_tokens"] = MAX_TOKENS_CONFIG["openai"] else: completion_params["max_tokens"] = MAX_TOKENS_CONFIG["openai"] # Handle thinking/reasoning if thinking_enabled and is_reasoning_model: completion_params["reasoning_effort"] = thinking_level.lower() # Reasoning models usually don't support temperature or it must be 1.0 else: completion_params["temperature"] = GENERATION_TEMPERATURE
response = client.chat.completions.create(**completion_params) response_text = response.choices[0].message.content print(f"[DEBUG] OpenAI response length: {len(response_text)}") print(f"[DEBUG] OpenAI 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_response else: print(f"[WARNING] Invalid JSON structure from OpenAI. Text: {response_text[:300]}...") print(f"[WARNING] Invalid JSON structure from OpenAI. Text: {response_text[:300] if response_text else 'None'}...") raise ValueError("Invalid JSON structure") except Exception as e: print(f"[ERROR] OpenAI generation/parsing failed: {e}") return { "title": "Автоматично сформований заголовок (OpenAI)", "text": response_text.strip() if 'response_text' in locals() else "Помилка при отриманні відповіді", "text": response_text.strip() if 'response_text' in locals() and response_text else f"Помилка при отриманні відповіді: {str(e)}", "proceeding": "Не визначено", "category": "Помилка парсингу" }config/environments/default.yaml+4-4
main.py+56-21