recall the failed call

This commit is contained in:
2026-01-18 23:35:34 +01:00
parent e453cf379c
commit c11a81548a

View File

@@ -29,7 +29,7 @@ with open(EDSS_INSTRUCTIONS_PATH, 'r') as f:
EDSS_INSTRUCTIONS = f.read().strip() EDSS_INSTRUCTIONS = f.read().strip()
# === RUN INFERENCE 2 === # === RUN INFERENCE 2 ===
def run_inference(patient_text): def run_inference(patient_text, max_retries=3):
prompt = f'''Du bist ein medizinischer Assistent, der spezialisiert darauf ist, EDSS-Scores (Expanded Disability Status Scale) sowie alle Unterkategorien aus klinischen Berichten zu extrahieren. prompt = f'''Du bist ein medizinischer Assistent, der spezialisiert darauf ist, EDSS-Scores (Expanded Disability Status Scale) sowie alle Unterkategorien aus klinischen Berichten zu extrahieren.
### Regeln für die Ausgabe: ### Regeln für die Ausgabe:
1. **Reason**: Erstelle eine prägnante Zusammenfassung (max. 400 Zeichen) der Befunde auf **DEUTSCH**, die zur Einstufung führen. 1. **Reason**: Erstelle eine prägnante Zusammenfassung (max. 400 Zeichen) der Befunde auf **DEUTSCH**, die zur Einstufung führen.
@@ -65,8 +65,8 @@ Patientenbericht:
''' '''
start_time = time.time() start_time = time.time()
for attempt in range(max_retries + 1):
try: try:
# Make API call using OpenAI client
response = client.chat.completions.create( response = client.chat.completions.create(
messages=[ messages=[
{ {
@@ -83,12 +83,11 @@ Patientenbericht:
temperature=0.0, temperature=0.0,
response_format={"type": "json_object"} response_format={"type": "json_object"}
) )
# Extract content from response
content = response.choices[0].message.content content = response.choices[0].message.content
# Check if content is None or empty
if content is None or content.strip() == "": if content is None or content.strip() == "":
raise ValueError("API returned empty or None response content") raise ValueError("API returned empty or None response content")
# Parse the JSON response
parsed = json.loads(content) parsed = json.loads(content)
inference_time = time.time() - start_time inference_time = time.time() - start_time
return { return {
@@ -96,8 +95,14 @@ Patientenbericht:
"result": parsed, "result": parsed,
"inference_time_sec": inference_time "inference_time_sec": inference_time
} }
except Exception as e: except Exception as e:
print(f"Inference error: {e}") print(f"Attempt {attempt + 1} failed: {e}")
if attempt < max_retries:
time.sleep(2 ** attempt) # Exponential backoff
continue
else:
print("All retries exhausted.")
return { return {
"success": False, "success": False,
"error": str(e), "error": str(e),