prompt.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import os
  2. from itertools import islice
  3. from jinja2 import Template
  4. from openhands.controller.state.state import State
  5. from openhands.core.message import Message, TextContent
  6. from openhands.utils.microagent import MicroAgent
  7. class PromptManager:
  8. """
  9. Manages prompt templates and micro-agents for AI interactions.
  10. This class handles loading and rendering of system and user prompt templates,
  11. as well as loading micro-agent specifications. It provides methods to access
  12. rendered system and initial user messages for AI interactions.
  13. Attributes:
  14. prompt_dir (str): Directory containing prompt templates.
  15. agent_skills_docs (str): Documentation of agent skills.
  16. microagent_dir (str): Directory containing microagent specifications.
  17. disabled_microagents (list[str] | None): List of microagents to disable. If None, all microagents are enabled.
  18. """
  19. def __init__(
  20. self,
  21. prompt_dir: str,
  22. microagent_dir: str | None = None,
  23. agent_skills_docs: str = '',
  24. disabled_microagents: list[str] | None = None,
  25. ):
  26. self.prompt_dir: str = prompt_dir
  27. self.agent_skills_docs: str = agent_skills_docs
  28. self.system_template: Template = self._load_template('system_prompt')
  29. self.user_template: Template = self._load_template('user_prompt')
  30. self.microagents: dict = {}
  31. microagent_files = []
  32. if microagent_dir:
  33. microagent_files = [
  34. os.path.join(microagent_dir, f)
  35. for f in os.listdir(microagent_dir)
  36. if f.endswith('.md')
  37. ]
  38. for microagent_file in microagent_files:
  39. microagent = MicroAgent(microagent_file)
  40. if (
  41. disabled_microagents is None
  42. or microagent.name not in disabled_microagents
  43. ):
  44. self.microagents[microagent.name] = microagent
  45. def _load_template(self, template_name: str) -> Template:
  46. if self.prompt_dir is None:
  47. raise ValueError('Prompt directory is not set')
  48. template_path = os.path.join(self.prompt_dir, f'{template_name}.j2')
  49. if not os.path.exists(template_path):
  50. raise FileNotFoundError(f'Prompt file {template_path} not found')
  51. with open(template_path, 'r') as file:
  52. return Template(file.read())
  53. def get_system_message(self) -> str:
  54. rendered = self.system_template.render(
  55. agent_skills_docs=self.agent_skills_docs,
  56. ).strip()
  57. return rendered
  58. def get_example_user_message(self) -> str:
  59. """This is the initial user message provided to the agent
  60. before *actual* user instructions are provided.
  61. It is used to provide a demonstration of how the agent
  62. should behave in order to solve the user's task. And it may
  63. optionally contain some additional context about the user's task.
  64. These additional context will convert the current generic agent
  65. into a more specialized agent that is tailored to the user's task.
  66. """
  67. return self.user_template.render().strip()
  68. def enhance_message(self, message: Message) -> None:
  69. """Enhance the user message with additional context.
  70. This method is used to enhance the user message with additional context
  71. about the user's task. The additional context will convert the current
  72. generic agent into a more specialized agent that is tailored to the user's task.
  73. """
  74. if not message.content:
  75. return
  76. message_content = message.content[0].text
  77. for microagent in self.microagents.values():
  78. trigger = microagent.get_trigger(message_content)
  79. if trigger:
  80. micro_text = f'<extra_info>\nThe following information has been included based on a keyword match for "{trigger}". It may or may not be relevant to the user\'s request.'
  81. micro_text += '\n\n' + microagent.content
  82. micro_text += '\n</extra_info>'
  83. message.content.append(TextContent(text=micro_text))
  84. def add_turns_left_reminder(self, messages: list[Message], state: State) -> None:
  85. latest_user_message = next(
  86. islice(
  87. (
  88. m
  89. for m in reversed(messages)
  90. if m.role == 'user'
  91. and any(isinstance(c, TextContent) for c in m.content)
  92. ),
  93. 1,
  94. ),
  95. None,
  96. )
  97. if latest_user_message:
  98. reminder_text = f'\n\nENVIRONMENT REMINDER: You have {state.max_iterations - state.iteration} turns left to complete the task. When finished reply with <finish></finish>.'
  99. latest_user_message.content.append(TextContent(text=reminder_text))