This commit is contained in:
2025-07-02 06:02:55 +00:00
parent 1bef25c3ac
commit 788362f966

View File

@@ -126,7 +126,7 @@ def retry_on_failure(func):
decorator_logger.warning(f"Wiederhole Versuch {attempt + 1}/{max_retries_config} fuer '{effective_func_name}'...")
return func(*args, **kwargs)
except (gspread.exceptions.SpreadsheetNotFound, AuthenticationError, ValueError) as e:
except (gspread.exceptions.SpreadsheetNotFound, AuthenticationError, ValueError, InvalidRequestError) as e:
decorator_logger.critical(f"❌ ENDGUELTIGER FEHLER bei '{effective_func_name}': Permanentes Problem erkannt. {type(e).__name__} - {str(e)[:150]}...")
decorator_logger.exception("Details:")
raise e
@@ -769,21 +769,24 @@ def call_openai_chat(prompt, temperature=0.3, model=None):
current_model = model if model else getattr(Config, 'TOKEN_MODEL', 'gpt-3.5-turbo')
try:
# Optional: Token-Zählung für Debugging
# prompt_tokens = token_count(prompt, model=current_model)
# logger.debug(f"Sende Prompt an OpenAI ({current_model}, geschaetzt {prompt_tokens} Tokens)...")
# NEU: OpenAI v1.x Client-Instanziierung
# Es ist Best Practice, einen Client zu erstellen, anstatt die globalen Methoden zu verwenden.
# Der API-Schlüssel wird automatisch aus der Umgebungsvariable oder der Konfiguration gelesen.
client = openai.OpenAI(api_key=Config.API_KEYS.get('openai'))
response = openai.ChatCompletion.create(
# logger.debug(f"Sende Prompt an OpenAI ({current_model})...")
response = client.chat.completions.create(
model=current_model,
messages=[{"role": "user", "content": prompt}],
temperature=temperature
)
if not response or not hasattr(response, 'choices') or not response.choices:
if not response or not response.choices:
logger.error(f"OpenAI Call erfolgreich, aber keine Choices in der Antwort erhalten. Response: {str(response)[:200]}...")
raise openai.error.APIError("Keine Choices in OpenAI Antwort erhalten.")
raise APIError("Keine Choices in OpenAI Antwort erhalten.", request=None, body=None)
result = response.choices[0].message.content.strip() if hasattr(response.choices[0], 'message') and hasattr(response.choices[0].message, 'content') else ""
result = response.choices[0].message.content.strip() if response.choices[0].message and response.choices[0].message.content else ""
if not result:
logger.warning(f"OpenAI Call erfolgreich, erhielt aber leeren Inhalt in der Antwort. Prompt Anfang: {prompt[:100]}...")