test_response_parsing.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import pytest
  2. from agenthub.micro.agent import parse_response as parse_response_micro
  3. from agenthub.monologue_agent.utils.prompts import (
  4. parse_action_response as parse_response_monologue,
  5. )
  6. from agenthub.planner_agent.prompt import parse_response as parse_response_planner
  7. from opendevin.core.exceptions import LLMOutputError
  8. from opendevin.core.utils.json import loads as custom_loads
  9. from opendevin.events.action import (
  10. FileWriteAction,
  11. MessageAction,
  12. )
  13. @pytest.mark.parametrize(
  14. 'parse_response_module',
  15. [parse_response_micro, parse_response_planner, parse_response_monologue],
  16. )
  17. def test_parse_single_complete_json(parse_response_module):
  18. input_response = """
  19. {
  20. "action": "message",
  21. "args": {
  22. "content": "The following typos were fixed:\\n* 'futur' -> 'future'\\n* 'imagin' -> 'imagine'\\n* 'techological' -> 'technological'\\n* 'responsability' -> 'responsibility'\\nThe corrected file is ./short_essay.txt."
  23. }
  24. }
  25. """
  26. expected = MessageAction(
  27. "The following typos were fixed:\n* 'futur' -> 'future'\n* 'imagin' -> 'imagine'\n* 'techological' -> 'technological'\n* 'responsability' -> 'responsibility'\nThe corrected file is ./short_essay.txt."
  28. )
  29. result = parse_response_module(input_response)
  30. assert result == expected
  31. @pytest.mark.parametrize(
  32. 'parse_response_module',
  33. [parse_response_micro, parse_response_planner, parse_response_monologue],
  34. )
  35. def test_parse_json_with_surrounding_text(parse_response_module):
  36. input_response = """
  37. Some initial text that is not JSON formatted.
  38. {
  39. "action": "write",
  40. "args": {
  41. "path": "./updated_file.txt",
  42. "content": "Updated text content here..."
  43. }
  44. }
  45. Some trailing text that is also not JSON formatted.
  46. """
  47. expected = FileWriteAction(
  48. path='./updated_file.txt', content='Updated text content here...'
  49. )
  50. result = parse_response_module(input_response)
  51. assert result == expected
  52. @pytest.mark.parametrize(
  53. 'parse_response_module',
  54. [parse_response_micro, parse_response_planner, parse_response_monologue],
  55. )
  56. def test_parse_first_of_multiple_jsons(parse_response_module):
  57. input_response = """
  58. I will firstly do
  59. {
  60. "action": "write",
  61. "args": {
  62. "path": "./short_essay.txt",
  63. "content": "Text content here..."
  64. }
  65. }
  66. Then I will continue with
  67. {
  68. "action": "think",
  69. "args": {
  70. "thought": "This should not be parsed."
  71. }
  72. }
  73. """
  74. expected = FileWriteAction(path='./short_essay.txt', content='Text content here...')
  75. result = parse_response_module(input_response)
  76. assert result == expected
  77. def test_invalid_json_raises_error():
  78. # This should fail if repair_json is able to fix this faulty JSON
  79. input_response = '{"action": "write", "args": { "path": "./short_essay.txt", "content": "Missing closing brace" }'
  80. with pytest.raises(LLMOutputError):
  81. custom_loads(input_response)
  82. def test_no_json_found():
  83. input_response = 'This is just a string with no JSON object.'
  84. with pytest.raises(LLMOutputError):
  85. custom_loads(input_response)