Spaces:
Running
Running
feat: sanitize response by escaping braces to prevent formatting errors & add exception
Browse files- patientsim/checker.py +4 -1
- patientsim/patient.py +7 -1
- patientsim/utils/common_utils.py +3 -3
patientsim/checker.py
CHANGED
|
@@ -68,8 +68,11 @@ class CheckerAgent:
|
|
| 68 |
raise ValueError(colorstr("red", f"Invalid visiting type: {self.visit_type}. Supported types: {', '.join(VISIT_TYPE)}"))
|
| 69 |
|
| 70 |
def __call__(self, response: str, **kwargs) -> str:
|
|
|
|
|
|
|
|
|
|
| 71 |
return self.client(
|
| 72 |
-
user_prompt=self.prompt_template.format(response=
|
| 73 |
using_multi_turn=False,
|
| 74 |
verbose=False,
|
| 75 |
temperature=self.temperature,
|
|
|
|
| 68 |
raise ValueError(colorstr("red", f"Invalid visiting type: {self.visit_type}. Supported types: {', '.join(VISIT_TYPE)}"))
|
| 69 |
|
| 70 |
def __call__(self, response: str, **kwargs) -> str:
|
| 71 |
+
# Escape braces in `response` before substitution to prevent unintended
|
| 72 |
+
# format-string expansion if the LLM output contains `{...}` patterns.
|
| 73 |
+
safe_response = response.replace("{", "{{").replace("}", "}}")
|
| 74 |
return self.client(
|
| 75 |
+
user_prompt=self.prompt_template.format(response=safe_response),
|
| 76 |
using_multi_turn=False,
|
| 77 |
verbose=False,
|
| 78 |
temperature=self.temperature,
|
patientsim/patient.py
CHANGED
|
@@ -187,7 +187,13 @@ class PatientAgent:
|
|
| 187 |
'reminder': reminder_desc,
|
| 188 |
'sentence_limit': sentence_limit,
|
| 189 |
}
|
| 190 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
system_prompt = system_prompt_template.format(**prompt_kwargs)
|
| 192 |
prompt_valid_check(system_prompt, self.patient_conditions)
|
| 193 |
return system_prompt
|
|
|
|
| 187 |
'reminder': reminder_desc,
|
| 188 |
'sentence_limit': sentence_limit,
|
| 189 |
}
|
| 190 |
+
# Escape braces in patient data values to prevent accidental format-string
|
| 191 |
+
# expansion when a field contains text like "{diagnosis}" or "{key}".
|
| 192 |
+
safe_conditions = {
|
| 193 |
+
k: str(v).replace("{", "{{").replace("}", "}}")
|
| 194 |
+
for k, v in self.patient_conditions.items()
|
| 195 |
+
}
|
| 196 |
+
prompt_kwargs.update(safe_conditions)
|
| 197 |
system_prompt = system_prompt_template.format(**prompt_kwargs)
|
| 198 |
prompt_valid_check(system_prompt, self.patient_conditions)
|
| 199 |
return system_prompt
|
patientsim/utils/common_utils.py
CHANGED
|
@@ -45,7 +45,7 @@ def detect_op_termination(text: str) -> bool:
|
|
| 45 |
try:
|
| 46 |
pattern = re.compile(r'Answer:\s*\d+\.\s*(.+)')
|
| 47 |
return bool(pattern.search(text))
|
| 48 |
-
except:
|
| 49 |
return False
|
| 50 |
|
| 51 |
|
|
@@ -54,7 +54,7 @@ def str_to_datetime(iso_time: Union[str, datetime]) -> datetime:
|
|
| 54 |
if isinstance(iso_time, str):
|
| 55 |
return datetime.fromisoformat(iso_time)
|
| 56 |
return iso_time
|
| 57 |
-
except:
|
| 58 |
raise ValueError(colorstr("red", f"`iso_time` must be str or date format, but got {type(iso_time)}"))
|
| 59 |
|
| 60 |
|
|
@@ -63,7 +63,7 @@ def datetime_to_str(iso_time: Union[str, datetime], format: str) -> str:
|
|
| 63 |
if not isinstance(iso_time, str):
|
| 64 |
return iso_time.strftime(format)
|
| 65 |
return iso_time
|
| 66 |
-
except:
|
| 67 |
raise ValueError(colorstr("red", f"`iso_time` must be str or date format, but got {type(iso_time)}"))
|
| 68 |
|
| 69 |
|
|
|
|
| 45 |
try:
|
| 46 |
pattern = re.compile(r'Answer:\s*\d+\.\s*(.+)')
|
| 47 |
return bool(pattern.search(text))
|
| 48 |
+
except Exception:
|
| 49 |
return False
|
| 50 |
|
| 51 |
|
|
|
|
| 54 |
if isinstance(iso_time, str):
|
| 55 |
return datetime.fromisoformat(iso_time)
|
| 56 |
return iso_time
|
| 57 |
+
except Exception:
|
| 58 |
raise ValueError(colorstr("red", f"`iso_time` must be str or date format, but got {type(iso_time)}"))
|
| 59 |
|
| 60 |
|
|
|
|
| 63 |
if not isinstance(iso_time, str):
|
| 64 |
return iso_time.strftime(format)
|
| 65 |
return iso_time
|
| 66 |
+
except Exception:
|
| 67 |
raise ValueError(colorstr("red", f"`iso_time` must be str or date format, but got {type(iso_time)}"))
|
| 68 |
|
| 69 |
|