test_prompt_manager.py 3.8 KB

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