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()
# === 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.
### Regeln für die Ausgabe:
1. **Reason**: Erstelle eine prägnante Zusammenfassung (max. 400 Zeichen) der Befunde auf **DEUTSCH**, die zur Einstufung führen.
@@ -65,44 +65,49 @@ Patientenbericht:
'''
start_time = time.time()
try:
# Make API call using OpenAI client
response = client.chat.completions.create(
messages=[
{
"role": "system",
"content": "You extract EDSS scores and all subcategories. You prioritize providing values even if data is partial, by using clinical inference."
},
{
"role": "user",
"content": prompt
for attempt in range(max_retries + 1):
try:
response = client.chat.completions.create(
messages=[
{
"role": "system",
"content": "You extract EDSS scores and all subcategories. You prioritize providing values even if data is partial, by using clinical inference."
},
{
"role": "user",
"content": prompt
}
],
model=MODEL_NAME,
max_tokens=2048,
temperature=0.0,
response_format={"type": "json_object"}
)
content = response.choices[0].message.content
if content is None or content.strip() == "":
raise ValueError("API returned empty or None response content")
parsed = json.loads(content)
inference_time = time.time() - start_time
return {
"success": True,
"result": parsed,
"inference_time_sec": inference_time
}
except Exception as 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 {
"success": False,
"error": str(e),
"inference_time_sec": -1
}
],
model=MODEL_NAME,
max_tokens=2048,
temperature=0.0,
response_format={"type": "json_object"}
)
# Extract content from response
content = response.choices[0].message.content
# Check if content is None or empty
if content is None or content.strip() == "":
raise ValueError("API returned empty or None response content")
# Parse the JSON response
parsed = json.loads(content)
inference_time = time.time() - start_time
return {
"success": True,
"result": parsed,
"inference_time_sec": inference_time
}
except Exception as e:
print(f"Inference error: {e}")
return {
"success": False,
"error": str(e),
"inference_time_sec": -1
}
# === BUILD PATIENT TEXT ===
def build_patient_text(row):