Spaces:
Sleeping
Sleeping
| """ | |
| Prompt Loader - Loads system prompts from markdown files. | |
| This module provides a centralized way to load prompts from external files | |
| while maintaining backward compatibility with the Customize AI Prompts feature. | |
| """ | |
| import os | |
| from pathlib import Path | |
| from typing import Dict, Optional | |
| # Base directory for prompt files | |
| PROMPTS_DIR = Path(__file__).parent / "prompts" | |
| def load_prompt_from_file(filename: str) -> str: | |
| """ | |
| Load a prompt from a file. | |
| Args: | |
| filename: Name of the prompt file (e.g., "spiritual_monitor.txt") | |
| Returns: | |
| Content of the prompt file as a string | |
| Raises: | |
| FileNotFoundError: If the prompt file doesn't exist | |
| """ | |
| filepath = PROMPTS_DIR / filename | |
| if not filepath.exists(): | |
| raise FileNotFoundError(f"Prompt file not found: {filepath}") | |
| with open(filepath, 'r', encoding='utf-8') as f: | |
| return f.read() | |
| def load_all_spiritual_prompts() -> Dict[str, str]: | |
| """ | |
| Load all spiritual-related prompts. | |
| Returns: | |
| Dictionary mapping prompt names to their content | |
| """ | |
| prompts = {} | |
| try: | |
| prompts['spiritual_monitor'] = load_prompt_from_file('spiritual_monitor.txt') | |
| except FileNotFoundError: | |
| prompts['spiritual_monitor'] = "" | |
| try: | |
| prompts['triage_question'] = load_prompt_from_file('triage_question.txt') | |
| except FileNotFoundError: | |
| prompts['triage_question'] = "" | |
| try: | |
| prompts['triage_evaluator'] = load_prompt_from_file('triage_evaluator.txt') | |
| except FileNotFoundError: | |
| prompts['triage_evaluator'] = "" | |
| return prompts | |
| def load_all_medical_prompts() -> Dict[str, str]: | |
| """ | |
| Load all medical-related prompts. | |
| Returns: | |
| Dictionary mapping prompt names to their content | |
| """ | |
| prompts = {} | |
| try: | |
| prompts['medical_assistant'] = load_prompt_from_file('medical_assistant.txt') | |
| except FileNotFoundError: | |
| prompts['medical_assistant'] = "" | |
| try: | |
| prompts['soft_medical_triage'] = load_prompt_from_file('soft_medical_triage.txt') | |
| except FileNotFoundError: | |
| prompts['soft_medical_triage'] = "" | |
| return prompts | |
| # Cache for loaded prompts to avoid repeated file I/O | |
| _prompt_cache: Optional[Dict[str, str]] = None | |
| def get_cached_prompts() -> Dict[str, str]: | |
| """ | |
| Get cached prompts, loading them if necessary. | |
| Returns: | |
| Dictionary of all loaded prompts | |
| """ | |
| global _prompt_cache | |
| if _prompt_cache is None: | |
| _prompt_cache = {} | |
| _prompt_cache.update(load_all_spiritual_prompts()) | |
| _prompt_cache.update(load_all_medical_prompts()) | |
| return _prompt_cache | |
| def reload_prompts() -> Dict[str, str]: | |
| """ | |
| Force reload of all prompts from files. | |
| Useful for development or when prompts are updated. | |
| Returns: | |
| Dictionary of all reloaded prompts | |
| """ | |
| global _prompt_cache | |
| _prompt_cache = None | |
| return get_cached_prompts() | |