test_parsing.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. from __future__ import annotations
  2. import pytest
  3. from sweagent.agent.commands import Command
  4. from sweagent.agent.parsing import (
  5. ActionParser,
  6. EditFormat,
  7. FormatError,
  8. Identity,
  9. JsonParser,
  10. ParseFunction,
  11. ThoughtActionParser,
  12. XMLThoughtActionParser,
  13. )
  14. def test_parse_function_registry():
  15. assert isinstance(ParseFunction.get("ActionParser"), ActionParser)
  16. assert isinstance(ParseFunction.get("ThoughtActionParser"), ThoughtActionParser)
  17. assert isinstance(ParseFunction.get("XMLThoughtActionParser"), XMLThoughtActionParser)
  18. assert isinstance(ParseFunction.get("EditFormat"), EditFormat)
  19. assert isinstance(ParseFunction.get("Identity"), Identity)
  20. assert isinstance(ParseFunction.get("JsonParser"), JsonParser)
  21. with pytest.raises(ValueError):
  22. ParseFunction.get("InvalidParser")
  23. def test_action_parser():
  24. parser = ActionParser()
  25. command = Command(code="ls", name="ls")
  26. thought, action = parser("ls -l", [command])
  27. assert thought == "ls -l"
  28. assert action == "ls -l"
  29. with pytest.raises(FormatError):
  30. parser("invalid command", [command])
  31. def test_thought_action_parser():
  32. parser = ThoughtActionParser()
  33. model_response = "Let's look at the files in the current directory.\n```\nls -l\n```"
  34. thought, action = parser(model_response, [])
  35. assert thought == "Let's look at the files in the current directory.\n"
  36. assert action == "ls -l\n"
  37. with pytest.raises(FormatError):
  38. parser("No code block", [])
  39. def test_xml_thought_action_parser():
  40. parser = XMLThoughtActionParser()
  41. model_response = "Let's look at the files in the current directory.\n<command>\nls -l\n</command>"
  42. thought, action = parser(model_response, [])
  43. assert thought == "Let's look at the files in the current directory."
  44. assert action == "ls -l"
  45. with pytest.raises(FormatError):
  46. parser("No command tags", [])
  47. def test_edit_format_parser():
  48. parser = EditFormat()
  49. model_response = "Let's replace the contents.\n```\nimport os\nos.listdir()\n```"
  50. thought, action = parser(model_response, [])
  51. assert thought == "Let's replace the contents.\n"
  52. assert action == "import os\nos.listdir()\n"
  53. with pytest.raises(FormatError):
  54. parser("No code block", [])
  55. def test_identity_parser():
  56. parser = Identity()
  57. model_response = "Return as is"
  58. thought, action = parser(model_response, [])
  59. assert thought == model_response
  60. assert action == model_response
  61. def test_json_parser():
  62. parser = JsonParser()
  63. model_response = '{"thought": "List files", "command": {"name": "ls", "arguments": {"path": "."}}}'
  64. thought, action = parser(model_response, [])
  65. assert thought == "List files"
  66. assert action == "ls ."
  67. invalid_json = "Not a JSON"
  68. with pytest.raises(FormatError):
  69. parser(invalid_json, [])
  70. missing_keys = '{"thought": "Missing command key"}'
  71. with pytest.raises(FormatError):
  72. parser(missing_keys, [])