You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_agentchat_utils.py 2.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. from typing import Dict, List, Union
  2. import pytest
  3. from autogen import agentchat
  4. TAG_PARSING_TESTS = [
  5. {
  6. "message": "Hello agent, can you take a look at this image <img http://example.com/image.png>",
  7. "expected": [{"tag": "img", "attr": {"src": "http://example.com/image.png"}}],
  8. },
  9. {
  10. "message": "Can you transcribe this audio? <audio http://example.com/au=dio.mp3>",
  11. "expected": [{"tag": "audio", "attr": {"src": "http://example.com/au=dio.mp3"}}],
  12. },
  13. {
  14. "message": "Can you describe what's in this image <img url='http://example.com/=image.png'>",
  15. "expected": [{"tag": "img", "attr": {"url": "http://example.com/=image.png"}}],
  16. },
  17. {
  18. "message": "Can you describe what's in this image <img http://example.com/image.png> and transcribe this audio? <audio http://example.com/audio.mp3>",
  19. "expected": [
  20. {"tag": "img", "attr": {"src": "http://example.com/image.png"}},
  21. {"tag": "audio", "attr": {"src": "http://example.com/audio.mp3"}},
  22. ],
  23. },
  24. {
  25. "message": "Can you generate this audio? <audio text='Hello I'm a robot' prompt='whisper'>",
  26. "expected": [{"tag": "audio", "attr": {"text": "Hello I'm a robot", "prompt": "whisper"}}],
  27. },
  28. {
  29. "message": "Can you describe what's in this image <img http://example.com/image.png width='100'> and this image <img http://hello.com/image=.png>?",
  30. "expected": [
  31. {"tag": "img", "attr": {"src": "http://example.com/image.png", "width": "100"}},
  32. {"tag": "img", "attr": {"src": "http://hello.com/image=.png"}},
  33. ],
  34. },
  35. {
  36. "message": "Text with no tags",
  37. "expected": [],
  38. },
  39. ]
  40. def _delete_unused_keys(d: Dict) -> None:
  41. if "match" in d:
  42. del d["match"]
  43. @pytest.mark.parametrize("test_case", TAG_PARSING_TESTS)
  44. def test_tag_parsing(test_case: Dict[str, Union[str, List[Dict[str, Union[str, Dict[str, str]]]]]]) -> None:
  45. """Test the tag_parsing function."""
  46. message = test_case["message"]
  47. expected = test_case["expected"]
  48. tags = ["img", "audio", "random"]
  49. result = []
  50. for tag in tags:
  51. parsed_tags = agentchat.utils.parse_tags_from_content(tag, message)
  52. for item in parsed_tags:
  53. _delete_unused_keys(item)
  54. result.extend(parsed_tags)
  55. assert result == expected
  56. result = []
  57. for tag in tags:
  58. content = [{"type": "text", "text": message}]
  59. parsed_tags = agentchat.utils.parse_tags_from_content(tag, content)
  60. for item in parsed_tags:
  61. _delete_unused_keys(item)
  62. result.extend(parsed_tags)
  63. assert result == expected
  64. if __name__ == "__main__":
  65. test_tag_parsing(TAG_PARSING_TESTS[0])