test_prompt_manager.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import os
  2. import shutil
  3. from unittest.mock import Mock
  4. import pytest
  5. from openhands.utils.microagent import MicroAgent
  6. from openhands.utils.prompt import PromptManager
  7. @pytest.fixture
  8. def prompt_dir(tmp_path):
  9. # Copy contents from "openhands/agenthub/codeact_agent" to the temp directory
  10. shutil.copytree('openhands/agenthub/codeact_agent', tmp_path, dirs_exist_ok=True)
  11. # Return the temporary directory path
  12. return tmp_path
  13. SAMPLE_AGENT_SKILLS_DOCS = """Sample agent skills documentation"""
  14. @pytest.fixture
  15. def agent_skills_docs():
  16. return SAMPLE_AGENT_SKILLS_DOCS
  17. def test_prompt_manager_without_micro_agent(prompt_dir, agent_skills_docs):
  18. manager = PromptManager(prompt_dir, agent_skills_docs)
  19. assert manager.prompt_dir == prompt_dir
  20. assert manager.agent_skills_docs == agent_skills_docs
  21. assert manager.micro_agent is None
  22. assert isinstance(manager.system_message, str)
  23. assert (
  24. "A chat between a curious user and an artificial intelligence assistant. The assistant gives helpful, detailed answers to the user's questions."
  25. in manager.system_message
  26. )
  27. assert SAMPLE_AGENT_SKILLS_DOCS in manager.system_message
  28. assert isinstance(manager.initial_user_message, str)
  29. assert '--- BEGIN OF GUIDELINE ---' not in manager.initial_user_message
  30. assert '--- END OF GUIDELINE ---' not in manager.initial_user_message
  31. assert "NOW, LET'S START!" in manager.initial_user_message
  32. assert 'micro_agent' not in manager.initial_user_message
  33. def test_prompt_manager_with_micro_agent(prompt_dir, agent_skills_docs):
  34. micro_agent_name = 'test_micro_agent'
  35. micro_agent_content = (
  36. '## Micro Agent\n'
  37. 'This is a test micro agent.\n'
  38. 'It is used to test the prompt manager.\n'
  39. )
  40. # Create a temporary micro agent file
  41. os.makedirs(os.path.join(prompt_dir, 'micro'), exist_ok=True)
  42. with open(os.path.join(prompt_dir, 'micro', f'{micro_agent_name}.md'), 'w') as f:
  43. f.write(micro_agent_content)
  44. # Mock MicroAgent
  45. mock_micro_agent = Mock(spec=MicroAgent)
  46. mock_micro_agent.content = micro_agent_content
  47. manager = PromptManager(
  48. prompt_dir=prompt_dir,
  49. agent_skills_docs=agent_skills_docs,
  50. micro_agent=mock_micro_agent,
  51. )
  52. assert manager.prompt_dir == prompt_dir
  53. assert manager.agent_skills_docs == agent_skills_docs
  54. assert manager.micro_agent == mock_micro_agent
  55. assert isinstance(manager.system_message, str)
  56. assert (
  57. "A chat between a curious user and an artificial intelligence assistant. The assistant gives helpful, detailed answers to the user's questions."
  58. in manager.system_message
  59. )
  60. assert SAMPLE_AGENT_SKILLS_DOCS in manager.system_message
  61. assert isinstance(manager.initial_user_message, str)
  62. assert (
  63. '--- BEGIN OF GUIDELINE ---\n'
  64. + 'The following information may assist you in completing your task:\n\n'
  65. + micro_agent_content
  66. + '\n'
  67. + '--- END OF GUIDELINE ---\n'
  68. + "\n\nNOW, LET'S START!"
  69. ) in manager.initial_user_message
  70. assert micro_agent_content in manager.initial_user_message
  71. # Clean up the temporary file
  72. os.remove(os.path.join(prompt_dir, 'micro', f'{micro_agent_name}.md'))
  73. def test_prompt_manager_file_not_found(prompt_dir, agent_skills_docs):
  74. with pytest.raises(FileNotFoundError):
  75. MicroAgent(os.path.join(prompt_dir, 'micro', 'non_existent_micro_agent.md'))
  76. def test_prompt_manager_template_rendering(prompt_dir, agent_skills_docs):
  77. # Create temporary template files
  78. with open(os.path.join(prompt_dir, 'system_prompt.j2'), 'w') as f:
  79. f.write('System prompt: {{ agent_skills_docs }}')
  80. with open(os.path.join(prompt_dir, 'user_prompt.j2'), 'w') as f:
  81. f.write('User prompt: {{ micro_agent }}')
  82. manager = PromptManager(prompt_dir, agent_skills_docs)
  83. assert manager.system_message == f'System prompt: {agent_skills_docs}'
  84. assert manager.initial_user_message == 'User prompt: None'
  85. # Clean up temporary files
  86. os.remove(os.path.join(prompt_dir, 'system_prompt.j2'))
  87. os.remove(os.path.join(prompt_dir, 'user_prompt.j2'))