|
| 1 | +""" |
| 2 | +Meta-prompting agent for dynamically rewriting prompts. |
| 3 | +
|
| 4 | +This module provides the MetaPromptingAgent class that can analyze the current task |
| 5 | +and dynamically rewrite a specific prompt to better suit the requirements. |
| 6 | +""" |
| 7 | + |
| 8 | +import logging |
| 9 | + |
| 10 | +from ..prompts import MetaPromptingPrompt |
| 11 | +from .base_agent import BaseAgent |
| 12 | +from .utils import init_llm |
| 13 | + |
| 14 | +logger = logging.getLogger(__name__) |
| 15 | + |
| 16 | + |
| 17 | +class MetaPromptingAgent(BaseAgent): |
| 18 | + """ |
| 19 | + Meta-prompting agent that dynamically rewrites prompts based on the current task. |
| 20 | +
|
| 21 | + This agent is designed to be instantiated once and used for all prompts that need rewriting, |
| 22 | + handling any prompt template provided during the call. |
| 23 | +
|
| 24 | + Agent Input: |
| 25 | + - Target prompt template to rewrite |
| 26 | + - Target prompt class for meta-instructions |
| 27 | + - Current task description and user input |
| 28 | + - Available template variables |
| 29 | +
|
| 30 | + Agent Output: |
| 31 | + - Rewritten prompt template customized for the current task |
| 32 | + """ |
| 33 | + |
| 34 | + def __init__(self, config, manager, llm_config, meta_prompt_template=None): |
| 35 | + """ |
| 36 | + Initialize the MetaPromptingAgent for all prompts. |
| 37 | +
|
| 38 | + Args: |
| 39 | + config: Configuration object |
| 40 | + manager: Manager that provides state and variable values |
| 41 | + llm_config: Configuration for the language model |
| 42 | + meta_prompt_template: Optional custom template for the meta-prompting prompt |
| 43 | + """ |
| 44 | + super().__init__(config=config, manager=manager) |
| 45 | + |
| 46 | + self.llm_config = llm_config |
| 47 | + |
| 48 | + # Initialize the meta-prompting prompt |
| 49 | + self.meta_prompt = MetaPromptingPrompt( |
| 50 | + llm_config=self.llm_config, manager=self.manager, template=meta_prompt_template |
| 51 | + ) |
| 52 | + |
| 53 | + # Initialize the LLM lazily |
| 54 | + self.llm = None |
| 55 | + # Store rewritten templates in a dictionary keyed by prompt class name |
| 56 | + self._rewritten_templates = {} |
| 57 | + |
| 58 | + def __call__(self, target_prompt_instance, force_rewrite=False): |
| 59 | + """ |
| 60 | + Generate a rewritten prompt template for the specified prompt class. |
| 61 | +
|
| 62 | + Args: |
| 63 | + target_prompt_instance: Instance of the prompt (to access its meta_instructions) |
| 64 | + force_rewrite: If True, rewrite the template even if previously rewritten |
| 65 | +
|
| 66 | + Returns: |
| 67 | + Rewritten prompt template |
| 68 | + """ |
| 69 | + assert self.manager.target_prompt_instance is None, f"{self.manager.target_prompt_instance.__class__}" |
| 70 | + self.manager.target_prompt_instance = target_prompt_instance |
| 71 | + # Generate a key to identify this prompt in our cache |
| 72 | + prompt_name = target_prompt_instance.__class__.__name__ |
| 73 | + |
| 74 | + # If already rewritten and not forcing a rewrite, return the cached version |
| 75 | + if not force_rewrite and prompt_name in self._rewritten_templates: |
| 76 | + self.manager.target_prompt_instance = None |
| 77 | + return self._rewritten_templates[prompt_name] |
| 78 | + |
| 79 | + self.manager.log_agent_start( |
| 80 | + f"MetaPromptingAgent: starting to analyze task and rewrite {prompt_name} template." |
| 81 | + ) |
| 82 | + |
| 83 | + # Build the meta-prompting prompt |
| 84 | + prompt = self.meta_prompt.build() |
| 85 | + |
| 86 | + # Initialize LLM if not already done |
| 87 | + if self.llm is None: |
| 88 | + self.llm = init_llm( |
| 89 | + llm_config=self.llm_config, agent_name="meta_prompting", multi_turn=self.llm_config.multi_turn |
| 90 | + ) |
| 91 | + |
| 92 | + # Get response from LLM |
| 93 | + response = self.llm.assistant_chat(prompt) |
| 94 | + |
| 95 | + # Parse the response to get the rewritten template |
| 96 | + rewritten_template = self.meta_prompt.parse(response) |
| 97 | + |
| 98 | + # Cache the rewritten template |
| 99 | + self._rewritten_templates[prompt_name] = rewritten_template |
| 100 | + |
| 101 | + # Save the rewritten template for debugging |
| 102 | + self.manager.save_and_log_states( |
| 103 | + content=rewritten_template, |
| 104 | + save_name=f"rewritten_{prompt_name}_template.txt", |
| 105 | + per_iteration=False, |
| 106 | + add_uuid=False, |
| 107 | + ) |
| 108 | + |
| 109 | + self.manager.log_agent_end(f"MetaPromptingAgent: finished rewriting {prompt_name} template.") |
| 110 | + |
| 111 | + self.manager.target_prompt_instance = None |
| 112 | + |
| 113 | + return rewritten_template |
0 commit comments