Spaces:
Sleeping
Sleeping
return to the old version
Browse files- core/config.py +2 -2
- core/hbv_assessment.py +24 -40
core/config.py
CHANGED
|
@@ -87,7 +87,7 @@ def get_llm():
|
|
| 87 |
|
| 88 |
try:
|
| 89 |
_llm = ChatOpenAI(
|
| 90 |
-
model="gpt-
|
| 91 |
api_key=openai_key,
|
| 92 |
base_url=settings.OPENAI_BASE_URL,
|
| 93 |
temperature=0.0,
|
|
@@ -153,4 +153,4 @@ try:
|
|
| 153 |
validate_config()
|
| 154 |
except Exception as e:
|
| 155 |
logger.error(f"Configuration validation failed: {e}")
|
| 156 |
-
raise e
|
|
|
|
| 87 |
|
| 88 |
try:
|
| 89 |
_llm = ChatOpenAI(
|
| 90 |
+
model="gpt-4o",
|
| 91 |
api_key=openai_key,
|
| 92 |
base_url=settings.OPENAI_BASE_URL,
|
| 93 |
temperature=0.0,
|
|
|
|
| 153 |
validate_config()
|
| 154 |
except Exception as e:
|
| 155 |
logger.error(f"Configuration validation failed: {e}")
|
| 156 |
+
raise e
|
core/hbv_assessment.py
CHANGED
|
@@ -123,21 +123,14 @@ SASLT_GUIDELINES = """
|
|
| 123 |
• Patients with HBV DNA > 20,000 IU/mL and ALT > 2xULN, regardless of the degree of fibrosis (Grade B) [SASLT 2021, p. 6]
|
| 124 |
• Patients with HBeAg-positive chronic HBV infection (persistently normal ALT and high HBV DNA levels) may be treated if they are > 30 years, regardless of the severity of liver histological lesions (Grade D) [SASLT 2021, p. 6]
|
| 125 |
• Patients with chronic HBV infection (HBV DNA > 2,000 IU/mL, ALT > ULN), regardless of HBeAg status, and a family history of HCC or cirrhosis and extrahepatic manifestations (Grade D) [SASLT 2021, p. 6]
|
| 126 |
-
• Non‑cirrhotic patients should be considered for treatment if they have HBV DNA levels >2,000 IU/mL, serum ALT >~40 IU/L and severity of liver disease assessed by liver biopsy showing at least moderate necroinflammation and/or at least moderate fibrosis.
|
| 127 |
|
| 128 |
-
Patients with HBV DNA greater than 20,000 IU/mL and ALT greater than 2x ULN can begin treatment without a liver biopsy.
|
| 129 |
-
|
| 130 |
-
Patients with HBV DNA >2,000 IU/mL and at least moderate fibrosis may initiate treatment even if ALT levels are normal.
|
| 131 |
|
| 132 |
### 2. MANAGEMENT ALGORITHM [SASLT 2021, p. 6]
|
| 133 |
-
|
| 134 |
-
(HBsAg, HBV ALT, fibrosis assessment). Consider: risk
|
| 135 |
-
|
| 136 |
-
• HBsAg positive
|
| 137 |
-
|
| 138 |
-
• HBsAg negative, anti-HBc positive —i No specialist follow-up (inform about HBV
|
| 139 |
-
reactivation risk). In case of immunosuppression: start oral antiviral prophylaxis or monitor
|
| 140 |
-
ISASLT 2021, p. 61
|
| 141 |
|
| 142 |
### 3. MONITORING OF UNTREATED PATIENTS [SASLT 2021, p. 6-7]
|
| 143 |
|
|
@@ -203,8 +196,6 @@ ISASLT 2021, p. 61
|
|
| 203 |
|
| 204 |
* **ALT (Alanine Aminotransferase):** Normal range in Adults falls between 4-42 units/L (U/L).
|
| 205 |
* **ULN (Upper Limit of Normal):** ULN means Upper Limit of Normal for ALT which is set at 40 units/L for the purpose of these guidelines.
|
| 206 |
-
* **Necroinflammatory Activity Scale:** A1 = mild, A2 = moderate, A3 = severe.
|
| 207 |
-
* **Liver Fibrosis Stages:** F0—no fibrosis; F1—mild fibrosis, pericellular collagen deposits; F2—moderate fibrosis, beginning bridging fibrosis; F3—severe fibrosis, defined as presence of numerous bridges and septa; F4—cirrhosis.
|
| 208 |
"""
|
| 209 |
|
| 210 |
|
|
@@ -255,15 +246,27 @@ def assess_hbv_eligibility(patient_data: Dict[str, Any]) -> Dict[str, Any]:
|
|
| 255 |
|
| 256 |
# Create prompt for LLM to analyze patient against guidelines
|
| 257 |
analysis_prompt = f"""You are an HBV treatment eligibility assessment system. Analyze the patient data against the SASLT 2021 guidelines.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 258 |
|
| 259 |
|
| 260 |
SASLT 2021 GUIDELINES (Retrieved Context):
|
| 261 |
{SASLT_GUIDELINES}
|
| 262 |
Based STRICTLY on the SASLT 2021 guidelines and criteria provided above, assess this patient's eligibility for HBV antiviral treatment.
|
| 263 |
-
NUMERIC DATA AND COMPARISON RULES (MUST FOLLOW):
|
| 264 |
-
- Pay EXTREME attention to all numeric values in PATIENT DATA (e.g., HBV DNA, ALT, age, durations) and all comparison signs in the guidelines (>, <, ≥, ≤).
|
| 265 |
-
- When checking criteria (for example "HBV DNA > 2,000 IU/mL" or "ALT > 2xULN"), you MUST compare the patient numbers exactly against those thresholds and operators.
|
| 266 |
-
- NEVER ignore available numeric patient data, NEVER assume missing values, and NEVER relax or tighten thresholds beyond what the SASLT text explicitly states.
|
| 267 |
You MUST respond with a valid JSON object in this exact format:
|
| 268 |
{{
|
| 269 |
"eligible": true or false,
|
|
@@ -283,8 +286,7 @@ CRITICAL ELIGIBILITY HIERARCHY:
|
|
| 283 |
* If patient has **HBV-HCV coinfection** AND meets HBV treatment criteria (Page 9): Set **"eligible": true"**.
|
| 284 |
2. **Check Standard Criteria SECOND (Page 6):**
|
| 285 |
* If *not* eligible based on special populations, check standard criteria (HBV DNA, ALT, fibrosis, age, family history).
|
| 286 |
-
*
|
| 287 |
-
* If any other Page 6 criteria are met (e.g., Cirrhosis, or HBV DNA > 2,000 + ALT > ULN + moderate fibrosis, etc.): Set **"eligible": true"**.
|
| 288 |
3. **If NO criteria from (1) or (2) are met:** Set **"eligible": false"**.
|
| 289 |
4. The "eligible" flag MUST be consistent with the "Eligibility and Rationale" bullet. Do not contradict yourself.
|
| 290 |
|
|
@@ -327,7 +329,7 @@ EXAMPLE OUTPUT (ELIGIBLE - STANDARD CRITERIA) - Use SINGLE \\n only:
|
|
| 327 |
EXAMPLE OUTPUT (NOT ELIGIBLE - STANDARD CRITERIA) - Use SINGLE \\n only:
|
| 328 |
{{
|
| 329 |
"eligible": false,
|
| 330 |
-
"recommendations": "Eligibility and Rationale:\\n- Not eligible: HBV DNA < 2,000 IU/mL, ALT ≤ ULN,
|
| 331 |
}}
|
| 332 |
---
|
| 333 |
EXAMPLE OUTPUT (ELIGIBLE - IMMUNOSUPPRESSION) - Use SINGLE \\n only:
|
|
@@ -349,24 +351,6 @@ CRITICAL REQUIREMENTS:
|
|
| 349 |
4. Use ONLY the 5 sections listed above.
|
| 350 |
5. Cite exact page at end of each bullet: [SASLT 2021, Page X].
|
| 351 |
6. Return ONLY valid JSON, no markdown, no extra text.
|
| 352 |
-
|
| 353 |
-
PATIENT DATA:
|
| 354 |
-
- Sex: {sex}
|
| 355 |
-
- Age: {age} years
|
| 356 |
-
- Pregnancy Status: {pregnancy_status}
|
| 357 |
-
- HBsAg Status: {hbsag_status}
|
| 358 |
-
- HBsAg Duration: {duration_hbsag} months
|
| 359 |
-
- HBV DNA Level: {hbv_dna:,.0f} IU/mL
|
| 360 |
-
- HBeAg Status: {hbeag_status}
|
| 361 |
-
- ALT Level: {alt_level}
|
| 362 |
-
- Fibrosis Stage: {fibrosis_stage}
|
| 363 |
-
- Necroinflammatory Activity: {necroinflammatory}
|
| 364 |
-
- Extrahepatic Manifestations: {extrahepatic}
|
| 365 |
-
- Immunosuppression: {immunosuppression}
|
| 366 |
-
- Coinfections: {', '.join(coinfections) if coinfections else 'None'}
|
| 367 |
-
- Family History (Cirrhosis/HCC): {family_history}
|
| 368 |
-
- Other Comorbidities: {', '.join(comorbidities) if comorbidities else 'None'}
|
| 369 |
-
|
| 370 |
"""
|
| 371 |
|
| 372 |
|
|
@@ -450,4 +434,4 @@ PATIENT DATA:
|
|
| 450 |
|
| 451 |
except Exception as e:
|
| 452 |
logger.error(f"Error in assess_hbv_eligibility: {str(e)}")
|
| 453 |
-
raise
|
|
|
|
| 123 |
• Patients with HBV DNA > 20,000 IU/mL and ALT > 2xULN, regardless of the degree of fibrosis (Grade B) [SASLT 2021, p. 6]
|
| 124 |
• Patients with HBeAg-positive chronic HBV infection (persistently normal ALT and high HBV DNA levels) may be treated if they are > 30 years, regardless of the severity of liver histological lesions (Grade D) [SASLT 2021, p. 6]
|
| 125 |
• Patients with chronic HBV infection (HBV DNA > 2,000 IU/mL, ALT > ULN), regardless of HBeAg status, and a family history of HCC or cirrhosis and extrahepatic manifestations (Grade D) [SASLT 2021, p. 6]
|
|
|
|
| 126 |
|
|
|
|
|
|
|
|
|
|
| 127 |
|
| 128 |
### 2. MANAGEMENT ALGORITHM [SASLT 2021, p. 6]
|
| 129 |
+
|
| 130 |
+
• HBsAg positive with chronic HBV infection and no signs of chronic hepatitis → Monitor (HBsAg, HBeAg, HBV DNA, ALT, fibrosis assessment). Consider: risk of HCC, risk of HBV reactivation, extrahepatic manifestations, risk of HBV transmission [SASLT 2021, p. 6]
|
| 131 |
+
• CHB (with/without cirrhosis) → Start antiviral treatment if indicated, otherwise return to monitoring [SASLT 2021, p. 6]
|
| 132 |
+
• HBsAg negative, anti-HBc positive → No specialist follow-up (inform about HBV reactivation risk). In case of immunosuppression: start oral antiviral prophylaxis or monitor [SASLT 2021, p. 6]
|
| 133 |
+
|
|
|
|
|
|
|
|
|
|
| 134 |
|
| 135 |
### 3. MONITORING OF UNTREATED PATIENTS [SASLT 2021, p. 6-7]
|
| 136 |
|
|
|
|
| 196 |
|
| 197 |
* **ALT (Alanine Aminotransferase):** Normal range in Adults falls between 4-42 units/L (U/L).
|
| 198 |
* **ULN (Upper Limit of Normal):** ULN means Upper Limit of Normal for ALT which is set at 40 units/L for the purpose of these guidelines.
|
|
|
|
|
|
|
| 199 |
"""
|
| 200 |
|
| 201 |
|
|
|
|
| 246 |
|
| 247 |
# Create prompt for LLM to analyze patient against guidelines
|
| 248 |
analysis_prompt = f"""You are an HBV treatment eligibility assessment system. Analyze the patient data against the SASLT 2021 guidelines.
|
| 249 |
+
PATIENT DATA:
|
| 250 |
+
- Sex: {sex}
|
| 251 |
+
- Age: {age} years
|
| 252 |
+
- Pregnancy Status: {pregnancy_status}
|
| 253 |
+
- HBsAg Status: {hbsag_status}
|
| 254 |
+
- HBsAg Duration: {duration_hbsag} months
|
| 255 |
+
- HBV DNA Level: {hbv_dna:,.0f} IU/mL
|
| 256 |
+
- HBeAg Status: {hbeag_status}
|
| 257 |
+
- ALT Level: {alt_level}
|
| 258 |
+
- Fibrosis Stage: {fibrosis_stage}
|
| 259 |
+
- Necroinflammatory Activity: {necroinflammatory}
|
| 260 |
+
- Extrahepatic Manifestations: {extrahepatic}
|
| 261 |
+
- Immunosuppression: {immunosuppression}
|
| 262 |
+
- Coinfections: {', '.join(coinfections) if coinfections else 'None'}
|
| 263 |
+
- Family History (Cirrhosis/HCC): {family_history}
|
| 264 |
+
- Other Comorbidities: {', '.join(comorbidities) if comorbidities else 'None'}
|
| 265 |
|
| 266 |
|
| 267 |
SASLT 2021 GUIDELINES (Retrieved Context):
|
| 268 |
{SASLT_GUIDELINES}
|
| 269 |
Based STRICTLY on the SASLT 2021 guidelines and criteria provided above, assess this patient's eligibility for HBV antiviral treatment.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 270 |
You MUST respond with a valid JSON object in this exact format:
|
| 271 |
{{
|
| 272 |
"eligible": true or false,
|
|
|
|
| 286 |
* If patient has **HBV-HCV coinfection** AND meets HBV treatment criteria (Page 9): Set **"eligible": true"**.
|
| 287 |
2. **Check Standard Criteria SECOND (Page 6):**
|
| 288 |
* If *not* eligible based on special populations, check standard criteria (HBV DNA, ALT, fibrosis, age, family history).
|
| 289 |
+
* If any Page 6 criteria are met (e.g., Cirrhosis, or HBV DNA > 2,000 + ALT > ULN + moderate fibrosis, etc.): Set **"eligible": true"**.
|
|
|
|
| 290 |
3. **If NO criteria from (1) or (2) are met:** Set **"eligible": false"**.
|
| 291 |
4. The "eligible" flag MUST be consistent with the "Eligibility and Rationale" bullet. Do not contradict yourself.
|
| 292 |
|
|
|
|
| 329 |
EXAMPLE OUTPUT (NOT ELIGIBLE - STANDARD CRITERIA) - Use SINGLE \\n only:
|
| 330 |
{{
|
| 331 |
"eligible": false,
|
| 332 |
+
"recommendations": "Eligibility and Rationale:\\n- Not eligible: HBV DNA < 2,000 IU/mL, ALT ≤ ULN, no significant fibrosis [SASLT 2021, Page 6]\\nTreatment Recommendations:\\n- Treatment not indicated at this time [SASLT 2021, Page 6]\\nMonitoring and Follow-up:\\n- Monitor every 6-12 months (HBeAg-negative, HBV DNA < 2,000 IU/mL) (Grade B) [SASLT 2021, Page 7]\\nReferences:\\n- Pages 6, 7: Treatment criteria, monitoring protocols"
|
| 333 |
}}
|
| 334 |
---
|
| 335 |
EXAMPLE OUTPUT (ELIGIBLE - IMMUNOSUPPRESSION) - Use SINGLE \\n only:
|
|
|
|
| 351 |
4. Use ONLY the 5 sections listed above.
|
| 352 |
5. Cite exact page at end of each bullet: [SASLT 2021, Page X].
|
| 353 |
6. Return ONLY valid JSON, no markdown, no extra text.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 354 |
"""
|
| 355 |
|
| 356 |
|
|
|
|
| 434 |
|
| 435 |
except Exception as e:
|
| 436 |
logger.error(f"Error in assess_hbv_eligibility: {str(e)}")
|
| 437 |
+
raise
|