Spaces:
Build error
Build error
Update hedis_engine.py
Browse files- hedis_engine.py +89 -28
hedis_engine.py
CHANGED
|
@@ -75,33 +75,34 @@ class InteractiveHedisComplianceEngine:
|
|
| 75 |
Get HEDIS criteria from expert and present to user for feedback.
|
| 76 |
Returns approved criteria or None if user exits.
|
| 77 |
"""
|
| 78 |
-
#
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
if suggestions:
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
|
|
|
|
|
|
| 101 |
|
| 102 |
hedis_task = Task(
|
| 103 |
-
description=
|
| 104 |
-
expected_output="Valid JSON object (no markdown, no code blocks)
|
| 105 |
agent=self.hedis_expert,
|
| 106 |
)
|
| 107 |
|
|
@@ -119,7 +120,13 @@ class InteractiveHedisComplianceEngine:
|
|
| 119 |
"measurement_year": self.measurement_year,
|
| 120 |
}
|
| 121 |
|
| 122 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
|
| 124 |
try:
|
| 125 |
# Clean the result string - remove markdown code blocks and extra whitespace
|
|
@@ -153,12 +160,66 @@ class InteractiveHedisComplianceEngine:
|
|
| 153 |
# Try to parse as JSON
|
| 154 |
criteria_json = json.loads(result_str)
|
| 155 |
return criteria_json
|
| 156 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
print(f"JSON parsing error: {e}")
|
|
|
|
| 158 |
print(f"Raw result: {repr(str(result))}")
|
| 159 |
-
print(f"Cleaned result: {repr(result_str)}")
|
| 160 |
# If not valid JSON, return as string
|
| 161 |
-
return {"raw_output": str(result), "parse_error": str(e)}
|
| 162 |
|
| 163 |
def display_criteria(self, criteria: dict) -> None:
|
| 164 |
"""Display the HEDIS criteria in a user-friendly format."""
|
|
|
|
| 75 |
Get HEDIS criteria from expert and present to user for feedback.
|
| 76 |
Returns approved criteria or None if user exits.
|
| 77 |
"""
|
| 78 |
+
# Build the task description carefully to avoid string formatting issues
|
| 79 |
+
base_description = f"""Provide the official criteria for HEDIS measure {self.measure_code}, including:
|
| 80 |
+
- Required tests or procedures
|
| 81 |
+
- Diagnosis/procedure codes
|
| 82 |
+
- Required medications
|
| 83 |
+
- Inclusion and exclusion criteria
|
| 84 |
+
- Timeframes for each requirement (compute absolute date ranges using {self.measurement_year})
|
| 85 |
+
|
| 86 |
+
You MUST compute and include absolute date ranges using {self.measurement_year}."""
|
| 87 |
+
|
|
|
|
| 88 |
if suggestions:
|
| 89 |
+
base_description += f"\n\nIncorporate these suggestions: {suggestions}"
|
| 90 |
+
|
| 91 |
+
formatting_requirements = """
|
| 92 |
+
|
| 93 |
+
CRITICAL FORMATTING REQUIREMENTS:
|
| 94 |
+
- Return ONLY valid JSON without any markdown formatting, code blocks, or extra text
|
| 95 |
+
- Do not wrap the JSON in code blocks
|
| 96 |
+
- Do not add any explanatory text before or after the JSON
|
| 97 |
+
- The response must start with {{ and end with }}
|
| 98 |
+
- Ensure all JSON keys and string values are properly quoted
|
| 99 |
+
- Required JSON fields: measure, required_tests, required_medications, codes, timeframes, inclusions, exclusions"""
|
| 100 |
+
|
| 101 |
+
full_description = base_description + formatting_requirements
|
| 102 |
|
| 103 |
hedis_task = Task(
|
| 104 |
+
description=full_description,
|
| 105 |
+
expected_output="Valid JSON object with required fields (no markdown, no code blocks)",
|
| 106 |
agent=self.hedis_expert,
|
| 107 |
)
|
| 108 |
|
|
|
|
| 120 |
"measurement_year": self.measurement_year,
|
| 121 |
}
|
| 122 |
|
| 123 |
+
try:
|
| 124 |
+
result = criteria_crew.kickoff(inputs=inputs)
|
| 125 |
+
print(f"Raw crew result type: {type(result)}")
|
| 126 |
+
print(f"Raw crew result: {repr(str(result)[:200])}...") # Show first 200 chars
|
| 127 |
+
except Exception as e:
|
| 128 |
+
print(f"Crew execution error: {e}")
|
| 129 |
+
return {"raw_output": f"Crew execution failed: {str(e)}", "parse_error": str(e)}
|
| 130 |
|
| 131 |
try:
|
| 132 |
# Clean the result string - remove markdown code blocks and extra whitespace
|
|
|
|
| 160 |
# Try to parse as JSON
|
| 161 |
criteria_json = json.loads(result_str)
|
| 162 |
return criteria_json
|
| 163 |
+
def get_hedis_criteria_simple(self, suggestions: str = "") -> dict:
|
| 164 |
+
"""
|
| 165 |
+
Simplified method to get HEDIS criteria with minimal string formatting.
|
| 166 |
+
"""
|
| 167 |
+
try:
|
| 168 |
+
# Create a simple, direct prompt
|
| 169 |
+
prompt_parts = [
|
| 170 |
+
f"Provide HEDIS measure {self.measure_code} criteria for year {self.measurement_year}.",
|
| 171 |
+
"Include: required tests, medications, codes, timeframes, inclusions, exclusions.",
|
| 172 |
+
]
|
| 173 |
+
|
| 174 |
+
if suggestions:
|
| 175 |
+
prompt_parts.append(f"Suggestions: {suggestions}")
|
| 176 |
+
|
| 177 |
+
prompt_parts.append("Respond with valid JSON only. No markdown, no code blocks, just JSON.")
|
| 178 |
+
|
| 179 |
+
simple_description = " ".join(prompt_parts)
|
| 180 |
+
|
| 181 |
+
hedis_task = Task(
|
| 182 |
+
description=simple_description,
|
| 183 |
+
expected_output="JSON object with measure criteria",
|
| 184 |
+
agent=self.hedis_expert,
|
| 185 |
+
)
|
| 186 |
+
|
| 187 |
+
# Create temporary crew
|
| 188 |
+
criteria_crew = Crew(
|
| 189 |
+
agents=[self.hedis_expert],
|
| 190 |
+
tasks=[hedis_task],
|
| 191 |
+
process=Process.sequential,
|
| 192 |
+
verbose=True,
|
| 193 |
+
)
|
| 194 |
+
|
| 195 |
+
# Execute
|
| 196 |
+
inputs = {"measure_code": self.measure_code, "measurement_year": self.measurement_year}
|
| 197 |
+
result = criteria_crew.kickoff(inputs=inputs)
|
| 198 |
+
|
| 199 |
+
# Try to parse the result
|
| 200 |
+
result_str = str(result).strip()
|
| 201 |
+
|
| 202 |
+
# Basic cleaning
|
| 203 |
+
if result_str.startswith("```"):
|
| 204 |
+
lines = result_str.split('\n')
|
| 205 |
+
# Remove first and last lines if they contain ```
|
| 206 |
+
if lines[0].strip().startswith('```'):
|
| 207 |
+
lines = lines[1:]
|
| 208 |
+
if lines and lines[-1].strip() == '```':
|
| 209 |
+
lines = lines[:-1]
|
| 210 |
+
result_str = '\n'.join(lines).strip()
|
| 211 |
+
|
| 212 |
+
return json.loads(result_str)
|
| 213 |
+
|
| 214 |
+
except Exception as e:
|
| 215 |
+
print(f"Simple method error: {e}")
|
| 216 |
+
return {"raw_output": str(result) if 'result' in locals() else "No result", "parse_error": str(e)}
|
| 217 |
print(f"JSON parsing error: {e}")
|
| 218 |
+
print(f"Raw result type: {type(result)}")
|
| 219 |
print(f"Raw result: {repr(str(result))}")
|
| 220 |
+
print(f"Cleaned result: {repr(result_str if 'result_str' in locals() else 'N/A')}")
|
| 221 |
# If not valid JSON, return as string
|
| 222 |
+
return {"raw_output": str(result), "parse_error": f"JSON parsing failed: {str(e)}"}
|
| 223 |
|
| 224 |
def display_criteria(self, criteria: dict) -> None:
|
| 225 |
"""Display the HEDIS criteria in a user-friendly format."""
|