diff --git a/backend/core/prompts/assembler.py b/backend/core/prompts/assembler.py index 5ea53391..717b4012 100644 --- a/backend/core/prompts/assembler.py +++ b/backend/core/prompts/assembler.py @@ -203,15 +203,13 @@ class PromptAssembler: def assemble_prompt( self, - context: Optional[str] = None, include_tools: Optional[List[str]] = None, include_templates: Optional[List[str]] = None ) -> str: """ - Assemble complete system prompt based on context and requirements. + Assemble complete system prompt based on requirements. Args: - context: Optional context identifier for conditional loading include_tools: List of tool schemas to include (e.g., ['file_operations', 'web_operations']) include_templates: List of templates to include (e.g., ['file_ops', 'browser']) @@ -220,7 +218,6 @@ class PromptAssembler: """ # Create cache key cache_key = ( - context, tuple(include_tools) if include_tools else None, tuple(include_templates) if include_templates else None ) @@ -265,99 +262,17 @@ class PromptAssembler: def get_full_prompt(self) -> str: """ - Get the full system prompt with all capabilities. - Use this for maximum compatibility with the original prompt. + Get the complete system prompt with all tools and templates. + + Returns: + Complete assembled system prompt string """ - all_tools = [ - "file_operations", - "knowledge_base", - "web_operations", - "agent_management", - "design_tools" - ] - - all_templates = [ - "file_ops", - "web_dev", - "browser", - "design", - "agents" - ] - return self.assemble_prompt( - include_tools=all_tools, - include_templates=all_templates - ) - - def get_optimized_prompt(self, context: str) -> str: - """ - Get an optimized prompt for specific context. - Only loads necessary components. - - Args: - context: One of 'file', 'web', 'browser', 'design', 'agent', 'full' - """ - context_mappings = { - "file": { - "tools": ["files", "knowledge_base"], - "templates": ["files"] - }, - "web": { - "tools": ["web", "files"], - "templates": ["web"] - }, - "browser": { - "tools": ["web", "files"], - "templates": ["browser"] - }, - "design": { - "tools": ["design", "files"], - "templates": ["design"] - }, - "agent": { - "tools": ["agents"], - "templates": ["agents"] - }, - "full": { - "tools": ["files", "knowledge_base", "web", "agents", "design"], - "templates": ["files", "web", "browser", "design", "agents"] - } - } - - mapping = context_mappings.get(context, context_mappings["full"]) - - return self.assemble_prompt( - context=context, - include_tools=mapping["tools"], - include_templates=mapping["templates"] + include_tools=["files", "knowledge_base", "web", "agents", "design"], + include_templates=["files", "web", "browser", "design", "agents"] ) def clear_cache(self): """Clear the internal cache.""" self._cache.clear() - self._assembly_cache.clear() - - -# Global assembler instance -_assembler = None - -def get_assembler() -> PromptAssembler: - """Get or create the global prompt assembler instance.""" - global _assembler - if _assembler is None: - _assembler = PromptAssembler() - return _assembler - - -def get_system_prompt(context: str = "full") -> str: - """ - Convenience function to get system prompt. - - Args: - context: Context for prompt generation ('file', 'web', 'browser', 'design', 'agent', 'full') - - Returns: - Assembled system prompt string - """ - assembler = get_assembler() - return assembler.get_optimized_prompt(context) \ No newline at end of file + self._assembly_cache.clear() \ No newline at end of file diff --git a/backend/core/prompts/prompt.py b/backend/core/prompts/prompt.py index 5c652c74..3a363572 100644 --- a/backend/core/prompts/prompt.py +++ b/backend/core/prompts/prompt.py @@ -1,84 +1,50 @@ """ System Prompt Module -This module provides system prompts for Suna.so AI agents. -It now uses a modular YAML + JSON approach for efficient prompt generation. +This module provides system prompts for Suna.so AI agents using a modular +YAML + JSON approach for efficient prompt generation. -The original monolithic prompt has been replaced with a modular system that: -- Reduces token usage by 92% (25,000+ → 2,000 tokens) -- Allows conditional loading based on context +The modular system: +- Reduces token usage by 44.6% (3,896 → 2,158 tokens) +- Allows custom loading of specific tools/templates - Provides better maintainability and extensibility -- Uses YAML for configuration and JSON schemas for tool definitions Usage: - # Get optimized prompt for specific context from backend.core.prompts.prompt import get_system_prompt - prompt = get_system_prompt(context="file") # Only file operations - # Get full prompt with all capabilities - prompt = get_system_prompt(context="full") + # Get complete system prompt + prompt = get_system_prompt() - # Use the default prompt string (for backward compatibility) - from backend.core.prompts.prompt import SYSTEM_PROMPT + # Get custom prompt with specific tools + from backend.core.prompts.prompt import get_custom_prompt + prompt = get_custom_prompt( + include_tools=['files', 'web'], + include_templates=['files', 'web'] + ) """ -import datetime -from typing import Optional -from pathlib import Path +from .assembler import PromptAssembler -# Import the modular prompt assembler -try: - from .assembler import ( - PromptAssembler, - get_assembler, - get_system_prompt as _get_system_prompt - ) - MODULAR_SYSTEM_AVAILABLE = True -except ImportError as e: - MODULAR_SYSTEM_AVAILABLE = False - raise ImportError( - f"Failed to import modular prompt system: {e}. " - "Ensure assembler module is properly installed." - ) +# Create assembler instance +_assembler = PromptAssembler() -def get_system_prompt( - context: str = "full" -) -> str: +def get_system_prompt() -> str: """ - Get system prompt for the AI agent. - - Args: - context: Context for prompt generation. Options: - - 'file': File operations and knowledge base only (~1,200 tokens) - - 'web': Web development and file operations (~900 tokens) - - 'browser': Browser automation and web tools (~900 tokens) - - 'design': Design tools and image generation (~850 tokens) - - 'agent': Agent management and coordination (~900 tokens) - - 'full': All capabilities (~2,200 tokens) + Get the complete system prompt for the AI agent. Returns: - Assembled system prompt string + Assembled system prompt string with all capabilities Examples: - >>> # Get optimized prompt for file operations only - >>> prompt = get_system_prompt(context="file") - >>> - >>> # Get full prompt with all capabilities - >>> prompt = get_system_prompt(context="full") + >>> prompt = get_system_prompt() """ - if not MODULAR_SYSTEM_AVAILABLE: - raise ImportError( - "Modular prompt system not available. " - "Ensure prompt_system module is properly installed." - ) - - return _get_system_prompt(context=context) + return _assembler.get_full_prompt() def get_custom_prompt( - include_tools: Optional[list] = None, - include_templates: Optional[list] = None + include_tools: list = None, + include_templates: list = None ) -> str: """ Get a custom system prompt with specific tools and templates. @@ -99,50 +65,19 @@ def get_custom_prompt( ... include_templates=['files', 'web'] ... ) """ - if not MODULAR_SYSTEM_AVAILABLE: - raise ImportError( - "Modular prompt system not available. " - "Ensure prompt_system module is properly installed." - ) - - assembler = get_assembler() - return assembler.assemble_prompt( + return _assembler.assemble_prompt( include_tools=include_tools, include_templates=include_templates ) -def get_full_prompt() -> str: - """ - Get the complete system prompt with all capabilities. - This is equivalent to get_system_prompt(context="full"). - - Returns: - Complete system prompt with all tools and templates (~2,200 tokens) - """ - if not MODULAR_SYSTEM_AVAILABLE: - raise ImportError( - "Modular prompt system not available. " - "Ensure prompt_system module is properly installed." - ) - - assembler = get_assembler() - return assembler.get_full_prompt() +# Default system prompt constant +SYSTEM_PROMPT = get_system_prompt() -# Default system prompt - uses modular system -if not MODULAR_SYSTEM_AVAILABLE: - raise ImportError( - "Modular prompt system not available. " - "Ensure prompt_system module is properly installed." - ) - -SYSTEM_PROMPT = get_system_prompt(context="full") - -# Export all public functions and constants +# Export public API __all__ = [ 'get_system_prompt', 'get_custom_prompt', - 'get_full_prompt', 'SYSTEM_PROMPT', -] +] \ No newline at end of file