Spaces:
Running
Running
| """ | |
| Script to export all prompts used in the HBV AI Assistant project to a Word document. | |
| Includes: Agent System Prompt, Validation Prompt, and HBV Assessment Prompt. | |
| """ | |
| from docx import Document | |
| from docx.shared import Pt, RGBColor, Inches | |
| from docx.enum.text import WD_ALIGN_PARAGRAPH | |
| import sys | |
| import os | |
| import re | |
| # Add the project root to the path | |
| sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) | |
| def extract_system_message_from_agent(): | |
| """Extract SYSTEM_MESSAGE from core/agent.py without importing it.""" | |
| agent_path = os.path.join(os.path.dirname(__file__), 'core', 'agent.py') | |
| with open(agent_path, 'r', encoding='utf-8') as f: | |
| content = f.read() | |
| # Extract SYSTEM_MESSAGE using regex | |
| match = re.search(r'SYSTEM_MESSAGE = """(.*?)"""', content, re.DOTALL) | |
| if match: | |
| return match.group(1).strip() | |
| else: | |
| raise ValueError("Could not extract SYSTEM_MESSAGE from core/agent.py") | |
| def extract_validation_prompt(): | |
| """Extract validation prompt from core/validation.py without importing it.""" | |
| validation_path = os.path.join(os.path.dirname(__file__), 'core', 'validation.py') | |
| with open(validation_path, 'r', encoding='utf-8') as f: | |
| content = f.read() | |
| # Find the _create_validation_system_prompt method and extract the return string | |
| match = re.search(r'def _create_validation_system_prompt\(self\) -> str:.*?return """(.*?)"""', content, re.DOTALL) | |
| if match: | |
| return match.group(1).strip() | |
| else: | |
| raise ValueError("Could not extract validation prompt from core/validation.py") | |
| def extract_hbv_assessment_prompt(): | |
| """Extract HBV assessment prompt from core/hbv_assessment.py without importing it.""" | |
| assessment_path = os.path.join(os.path.dirname(__file__), 'core', 'hbv_assessment.py') | |
| with open(assessment_path, 'r', encoding='utf-8') as f: | |
| content = f.read() | |
| # Find the analysis_prompt in assess_hbv_eligibility function | |
| match = re.search(r'analysis_prompt = f"""(.*?)"""', content, re.DOTALL) | |
| if match: | |
| # Extract the prompt template (without f-string variables) | |
| prompt_template = match.group(1).strip() | |
| return prompt_template | |
| else: | |
| raise ValueError("Could not extract HBV assessment prompt from core/hbv_assessment.py") | |
| def create_prompts_document(): | |
| """Create a Word document with both prompts.""" | |
| # Create a new Document | |
| doc = Document() | |
| # Add title | |
| title = doc.add_heading('HBV AI Assistant - System Prompts', 0) | |
| title.alignment = WD_ALIGN_PARAGRAPH.CENTER | |
| # Add metadata | |
| metadata = doc.add_paragraph() | |
| metadata.add_run('Project: HBV Clinical Decision Support System\n').bold = True | |
| metadata.add_run('Generated: October 30, 2025\n') | |
| metadata.add_run('Description: This document contains all system prompts used by the HBV AI Assistant, including the main agent prompt, validation prompt, and HBV assessment prompt.') | |
| doc.add_page_break() | |
| # ==================== HBV ASSESSMENT PROMPT ==================== | |
| doc.add_heading('1. HBV Assessment System Prompt', 1) | |
| # Add description | |
| desc3 = doc.add_paragraph() | |
| desc3.add_run('Purpose: ').bold = True | |
| desc3.add_run('Evaluates patient eligibility for HBV treatment based on SASLT 2021 guidelines\n') | |
| desc3.add_run('Location: ').bold = True | |
| desc3.add_run('core/hbv_assessment.py\n') | |
| desc3.add_run('Model: ').bold = True | |
| desc3.add_run('GPT-4 (configurable)\n') | |
| desc3.add_run('Function: ').bold = True | |
| desc3.add_run('assess_hbv_eligibility()') | |
| doc.add_paragraph() # Spacing | |
| # Add assessment overview | |
| doc.add_heading('Assessment Process:', 2) | |
| process = doc.add_paragraph() | |
| process.add_run('The HBV assessment prompt analyzes patient data against SASLT 2021 guidelines:\n') | |
| process_list = [ | |
| 'Evaluates patient parameters (HBV DNA, ALT, fibrosis stage, etc.)', | |
| 'Compares against treatment eligibility criteria from SASLT 2021', | |
| 'Determines eligibility status (Eligible/Not Eligible/Borderline)', | |
| 'Recommends first-line antiviral agents (ETV, TDF, TAF) if eligible', | |
| 'Provides comprehensive assessment with inline citations', | |
| 'Includes special considerations (pregnancy, immunosuppression, coinfections)' | |
| ] | |
| for item in process_list: | |
| doc.add_paragraph(item, style='List Bullet') | |
| doc.add_paragraph() # Spacing | |
| # Add the actual HBV assessment prompt | |
| doc.add_heading('Prompt Content:', 2) | |
| hbv_assessment_prompt = extract_hbv_assessment_prompt() | |
| assessment_para = doc.add_paragraph(hbv_assessment_prompt) | |
| assessment_para.style = 'Normal' | |
| # Format the assessment prompt text | |
| for run in assessment_para.runs: | |
| run.font.size = Pt(10) | |
| run.font.name = 'Courier New' | |
| doc.add_page_break() | |
| # ==================== AGENT PROMPT ==================== | |
| doc.add_heading('2. Agent System Prompt', 1) | |
| # Add description | |
| desc1 = doc.add_paragraph() | |
| desc1.add_run('Purpose: ').bold = True | |
| desc1.add_run('Main conversational AI agent for clinical decision support\n') | |
| desc1.add_run('Location: ').bold = True | |
| desc1.add_run('core/agent.py\n') | |
| desc1.add_run('Model: ').bold = True | |
| desc1.add_run('GPT-4 (configurable)') | |
| doc.add_paragraph() # Spacing | |
| # Add the actual prompt | |
| doc.add_heading('Prompt Content:', 2) | |
| system_message = extract_system_message_from_agent() | |
| prompt_para = doc.add_paragraph(system_message) | |
| prompt_para.style = 'Normal' | |
| # Format the prompt text | |
| for run in prompt_para.runs: | |
| run.font.size = Pt(10) | |
| run.font.name = 'Courier New' | |
| doc.add_page_break() | |
| # ==================== VALIDATION PROMPT ==================== | |
| doc.add_heading('3. Validation System Prompt', 1) | |
| # Add description | |
| desc2 = doc.add_paragraph() | |
| desc2.add_run('Purpose: ').bold = True | |
| desc2.add_run('Validates generated medical answers for quality assurance\n') | |
| desc2.add_run('Location: ').bold = True | |
| desc2.add_run('core/validation.py\n') | |
| desc2.add_run('Model: ').bold = True | |
| desc2.add_run('GPT-4o') | |
| doc.add_paragraph() # Spacing | |
| # Add validation criteria overview | |
| doc.add_heading('Validation Criteria:', 2) | |
| criteria = doc.add_paragraph() | |
| criteria.add_run('The validation prompt evaluates answers on 6 dimensions:\n') | |
| criteria_list = [ | |
| 'Accuracy (0-100%): Factual correctness based on provided documents', | |
| 'Coherence (0-100%): Logical structure, clarity, and readability', | |
| 'Relevance (0-100%): Addresses user\'s question without off-topic information', | |
| 'Completeness (0-100%): Includes all necessary information from documents', | |
| 'Citations/Attribution (0-100%): Proper citation of all claims', | |
| 'Length (0-100%): Appropriate detail without being too brief or verbose' | |
| ] | |
| for criterion in criteria_list: | |
| doc.add_paragraph(criterion, style='List Bullet') | |
| doc.add_paragraph() # Spacing | |
| # Add the actual validation prompt | |
| doc.add_heading('Prompt Content:', 2) | |
| validation_prompt = extract_validation_prompt() | |
| validation_para = doc.add_paragraph(validation_prompt) | |
| validation_para.style = 'Normal' | |
| # Format the validation prompt text | |
| for run in validation_para.runs: | |
| run.font.size = Pt(10) | |
| run.font.name = 'Courier New' | |
| # Save the document | |
| output_path = 'HBV_AI_Assistant_System_Prompts.docx' | |
| doc.save(output_path) | |
| print(f"β Document created successfully: {output_path}") | |
| print(f"β File location: {os.path.abspath(output_path)}") | |
| print(f"\nπ Exported Prompts:") | |
| print(f" 1. HBV Assessment Prompt (core/hbv_assessment.py)") | |
| print(f" 2. Agent System Prompt (core/agent.py)") | |
| print(f" 3. Validation System Prompt (core/validation.py)") | |
| return output_path | |
| if __name__ == "__main__": | |
| try: | |
| create_prompts_document() | |
| except Exception as e: | |
| print(f"β Error creating document: {str(e)}") | |
| import traceback | |
| traceback.print_exc() | |