# utils/prompts.py - Custom prompt management for PDF Analysis & Orchestrator import json import os from pathlib import Path from typing import Dict, List, Optional from config import Config class PromptManager: """Manage custom prompts for analysis""" def __init__(self, prompts_dir: str = None): self.prompts_dir = Path(prompts_dir or Config.PROMPTS_DIR) self.prompts_dir.mkdir(parents=True, exist_ok=True) self.prompts_file = self.prompts_dir / "custom_prompts.json" self._load_prompts() def _load_prompts(self) -> None: """Load prompts from file""" if self.prompts_file.exists(): try: with open(self.prompts_file, 'r', encoding='utf-8') as f: self.prompts = json.load(f) except Exception: self.prompts = {} else: self.prompts = self._get_default_prompts() self._save_prompts() def _get_default_prompts(self) -> Dict[str, Dict[str, str]]: """Get default prompt templates""" return { "summarize": { "name": "Summarize Document", "description": "Create a concise summary of the document", "template": "Summarize this document in 3-5 key points, highlighting the main ideas and conclusions.", "category": "basic" }, "explain_simple": { "name": "Explain Simply", "description": "Explain complex content for a general audience", "template": "Explain this document in simple terms that a 10-year-old could understand. Use analogies and examples where helpful.", "category": "explanation" }, "executive_summary": { "name": "Executive Summary", "description": "Create an executive summary for decision makers", "template": "Create an executive summary of this document, focusing on key findings, recommendations, and business implications.", "category": "business" }, "technical_analysis": { "name": "Technical Analysis", "description": "Provide detailed technical analysis", "template": "Provide a detailed technical analysis of this document, including methodology, data analysis, and technical conclusions.", "category": "technical" }, "theme_segmentation": { "name": "Theme Segmentation", "description": "Break down document by themes and topics", "template": "Segment this document by main themes and topics. Identify key themes and provide a brief summary of each section.", "category": "organization" }, "key_findings": { "name": "Key Findings", "description": "Extract key findings and insights", "template": "Extract and analyze the key findings, insights, and recommendations from this document. Highlight the most important points.", "category": "analysis" }, "research_pipeline": { "name": "R&D Pipeline Analysis", "description": "Extract high-value insights for R&D pipeline development", "template": "Act as a senior research analyst: identify novel ideas, breakthrough concepts, and innovative approaches with high product/engineering impact. Convert insights into concrete R&D pipeline outcomes: specific experiments to test, prototypes to build, and product decisions to make. Prioritize by transformative potential and measurable business value.", "category": "research" }, "innovation_assessment": { "name": "Innovation Opportunity Assessment", "description": "Assess commercial viability and innovation potential", "template": "Analyze this document for breakthrough innovation opportunities. Identify novel technical concepts, assess their commercial viability, market readiness, and competitive advantage potential. Generate specific recommendations for experimental validation, prototype development, and strategic product decisions.", "category": "research" }, "experimental_design": { "name": "Experimental Design Framework", "description": "Design specific experiments and validation methodologies", "template": "Extract technical concepts and methodologies from this document. Design specific experimental frameworks to validate key hypotheses, including success metrics, validation criteria, and implementation timelines. Focus on experiments that could drive significant product/engineering advancement.", "category": "research" }, "prototype_roadmap": { "name": "Prototype Development Roadmap", "description": "Create technical implementation roadmap for prototypes", "template": "Identify technical concepts suitable for prototype development. Create a structured roadmap for building technical implementations that demonstrate key innovations. Include technical specifications, development phases, resource requirements, and success criteria for each prototype.", "category": "research" } } def _save_prompts(self) -> None: """Save prompts to file""" try: with open(self.prompts_file, 'w', encoding='utf-8') as f: json.dump(self.prompts, f, indent=2, ensure_ascii=False) except Exception as e: print(f"Error saving prompts: {e}") def get_prompt(self, prompt_id: str) -> Optional[str]: """Get a specific prompt template""" return self.prompts.get(prompt_id, {}).get("template") def get_all_prompts(self) -> Dict[str, Dict[str, str]]: """Get all available prompts""" return self.prompts.copy() def get_prompts_by_category(self, category: str) -> Dict[str, Dict[str, str]]: """Get prompts filtered by category""" return { pid: prompt for pid, prompt in self.prompts.items() if prompt.get("category") == category } def add_prompt(self, prompt_id: str, name: str, description: str, template: str, category: str = "custom") -> bool: """Add a new custom prompt""" try: self.prompts[prompt_id] = { "name": name, "description": description, "template": template, "category": category } self._save_prompts() return True except Exception: return False def update_prompt(self, prompt_id: str, **kwargs) -> bool: """Update an existing prompt""" if prompt_id not in self.prompts: return False try: self.prompts[prompt_id].update(kwargs) self._save_prompts() return True except Exception: return False def delete_prompt(self, prompt_id: str) -> bool: """Delete a custom prompt (cannot delete default prompts)""" if prompt_id in self.prompts and self.prompts[prompt_id].get("category") == "custom": try: del self.prompts[prompt_id] self._save_prompts() return True except Exception: return False return False def get_categories(self) -> List[str]: """Get all available categories""" categories = set() for prompt in self.prompts.values(): categories.add(prompt.get("category", "uncategorized")) return sorted(list(categories))