test_message_serialization.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. from openhands.core.message import ImageContent, Message, TextContent
  2. def test_message_with_vision_enabled():
  3. text_content1 = TextContent(text='This is a text message')
  4. image_content1 = ImageContent(
  5. image_urls=['http://example.com/image1.png', 'http://example.com/image2.png']
  6. )
  7. text_content2 = TextContent(text='This is another text message')
  8. image_content2 = ImageContent(
  9. image_urls=['http://example.com/image3.png', 'http://example.com/image4.png']
  10. )
  11. message: Message = Message(
  12. role='user',
  13. content=[text_content1, image_content1, text_content2, image_content2],
  14. vision_enabled=True,
  15. )
  16. serialized_message: dict = message.serialize_model()
  17. expected_serialized_message = {
  18. 'role': 'user',
  19. 'content': [
  20. {'type': 'text', 'text': 'This is a text message'},
  21. {
  22. 'type': 'image_url',
  23. 'image_url': {'url': 'http://example.com/image1.png'},
  24. },
  25. {
  26. 'type': 'image_url',
  27. 'image_url': {'url': 'http://example.com/image2.png'},
  28. },
  29. {'type': 'text', 'text': 'This is another text message'},
  30. {
  31. 'type': 'image_url',
  32. 'image_url': {'url': 'http://example.com/image3.png'},
  33. },
  34. {
  35. 'type': 'image_url',
  36. 'image_url': {'url': 'http://example.com/image4.png'},
  37. },
  38. ],
  39. }
  40. assert serialized_message == expected_serialized_message
  41. assert message.contains_image is True
  42. def test_message_with_only_text_content_and_vision_enabled():
  43. text_content1 = TextContent(text='This is a text message')
  44. text_content2 = TextContent(text='This is another text message')
  45. message: Message = Message(
  46. role='user', content=[text_content1, text_content2], vision_enabled=True
  47. )
  48. serialized_message: dict = message.serialize_model()
  49. expected_serialized_message = {
  50. 'role': 'user',
  51. 'content': [
  52. {'type': 'text', 'text': 'This is a text message'},
  53. {'type': 'text', 'text': 'This is another text message'},
  54. ],
  55. }
  56. assert serialized_message == expected_serialized_message
  57. assert message.contains_image is False
  58. def test_message_with_only_text_content_and_vision_disabled():
  59. text_content1 = TextContent(text='This is a text message')
  60. text_content2 = TextContent(text='This is another text message')
  61. message: Message = Message(
  62. role='user', content=[text_content1, text_content2], vision_enabled=False
  63. )
  64. serialized_message: dict = message.serialize_model()
  65. expected_serialized_message = {
  66. 'role': 'user',
  67. 'content': 'This is a text message\nThis is another text message',
  68. }
  69. assert serialized_message == expected_serialized_message
  70. assert message.contains_image is False
  71. def test_message_with_mixed_content_and_vision_disabled():
  72. # Create a message with both text and image content
  73. text_content1 = TextContent(text='This is a text message')
  74. image_content1 = ImageContent(
  75. image_urls=['http://example.com/image1.png', 'http://example.com/image2.png']
  76. )
  77. text_content2 = TextContent(text='This is another text message')
  78. image_content2 = ImageContent(
  79. image_urls=['http://example.com/image3.png', 'http://example.com/image4.png']
  80. )
  81. # Initialize Message with vision disabled
  82. message: Message = Message(
  83. role='user',
  84. content=[text_content1, image_content1, text_content2, image_content2],
  85. vision_enabled=False,
  86. )
  87. serialized_message: dict = message.serialize_model()
  88. # Expected serialization ignores images and concatenates text
  89. expected_serialized_message = {
  90. 'role': 'user',
  91. 'content': 'This is a text message\nThis is another text message',
  92. }
  93. # Assert serialized message matches expectation
  94. assert serialized_message == expected_serialized_message
  95. # Assert that images exist in the original message
  96. assert message.contains_image