|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +from nemoguardrails.actions.llm.utils import ( |
| 17 | + get_colang_history, |
| 18 | + get_last_user_utterance, |
| 19 | +) |
| 20 | + |
| 21 | +FAKE_BASE64 = "iVBORw0KGgoAAAANSUhEUg" * 5000 |
| 22 | + |
| 23 | + |
| 24 | +def _multimodal_content(text=None, image_b64=None): |
| 25 | + parts = [] |
| 26 | + if text is not None: |
| 27 | + parts.append({"type": "text", "text": text}) |
| 28 | + if image_b64 is not None: |
| 29 | + parts.append({"type": "image_url", "image_url": {"url": f"data:image/png;base64,{image_b64}"}}) |
| 30 | + return parts |
| 31 | + |
| 32 | + |
| 33 | +class TestGetColangHistoryMultimodal: |
| 34 | + def test_text_only_message_unchanged(self): |
| 35 | + events = [{"type": "UserMessage", "text": "Hello there"}] |
| 36 | + result = get_colang_history(events) |
| 37 | + assert 'user "Hello there"' in result |
| 38 | + |
| 39 | + def test_multimodal_text_and_image(self): |
| 40 | + events = [{"type": "UserMessage", "text": _multimodal_content("Describe this", FAKE_BASE64)}] |
| 41 | + result = get_colang_history(events) |
| 42 | + assert FAKE_BASE64 not in result |
| 43 | + assert "Describe this [+ image]" in result |
| 44 | + |
| 45 | + def test_multimodal_image_only(self): |
| 46 | + events = [{"type": "UserMessage", "text": _multimodal_content(image_b64=FAKE_BASE64)}] |
| 47 | + result = get_colang_history(events) |
| 48 | + assert FAKE_BASE64 not in result |
| 49 | + assert 'user "[+ image]"' in result |
| 50 | + |
| 51 | + def test_multimodal_multiple_text_parts(self): |
| 52 | + content = [ |
| 53 | + {"type": "text", "text": "First part"}, |
| 54 | + {"type": "text", "text": "Second part"}, |
| 55 | + {"type": "image_url", "image_url": {"url": f"data:image/png;base64,{FAKE_BASE64}"}}, |
| 56 | + ] |
| 57 | + events = [{"type": "UserMessage", "text": content}] |
| 58 | + result = get_colang_history(events) |
| 59 | + assert FAKE_BASE64 not in result |
| 60 | + assert "First part Second part [+ image]" in result |
| 61 | + |
| 62 | + def test_multimodal_does_not_bloat_history(self): |
| 63 | + events = [{"type": "UserMessage", "text": _multimodal_content("Describe this", FAKE_BASE64)}] |
| 64 | + result = get_colang_history(events) |
| 65 | + assert FAKE_BASE64 not in result |
| 66 | + assert len(result) < 1000 |
| 67 | + |
| 68 | + def test_mixed_text_and_multimodal_conversation(self): |
| 69 | + events = [ |
| 70 | + {"type": "UserMessage", "text": "Hi"}, |
| 71 | + {"type": "UserIntent", "intent": "express greeting"}, |
| 72 | + {"type": "BotIntent", "intent": "express greeting"}, |
| 73 | + {"type": "StartUtteranceBotAction", "script": "Hello!"}, |
| 74 | + {"type": "UserMessage", "text": _multimodal_content("What is this?", FAKE_BASE64)}, |
| 75 | + ] |
| 76 | + result = get_colang_history(events) |
| 77 | + assert 'user "Hi"' in result |
| 78 | + assert FAKE_BASE64 not in result |
| 79 | + assert 'user "What is this? [+ image]"' in result |
| 80 | + |
| 81 | + |
| 82 | +class TestGetLastUserUtteranceMultimodal: |
| 83 | + def test_text_returns_string(self): |
| 84 | + events = [{"type": "UserMessage", "text": "Plain text"}] |
| 85 | + result = get_last_user_utterance(events) |
| 86 | + assert result == "Plain text" |
| 87 | + assert isinstance(result, str) |
| 88 | + |
| 89 | + def test_multimodal_returns_string(self): |
| 90 | + events = [{"type": "UserMessage", "text": _multimodal_content("Describe this", FAKE_BASE64)}] |
| 91 | + result = get_last_user_utterance(events) |
| 92 | + assert isinstance(result, str) |
| 93 | + assert FAKE_BASE64 not in result |
| 94 | + assert "[+ image]" in result |
| 95 | + |
| 96 | + def test_multimodal_image_only(self): |
| 97 | + events = [{"type": "UserMessage", "text": _multimodal_content(image_b64=FAKE_BASE64)}] |
| 98 | + result = get_last_user_utterance(events) |
| 99 | + assert isinstance(result, str) |
| 100 | + assert FAKE_BASE64 not in result |
| 101 | + assert result == "[+ image]" |
| 102 | + |
| 103 | + def test_multimodal_none_text_part_does_not_crash(self): |
| 104 | + events = [ |
| 105 | + { |
| 106 | + "type": "UserMessage", |
| 107 | + "text": [ |
| 108 | + {"type": "text", "text": None}, |
| 109 | + {"type": "image_url", "image_url": {"url": f"data:image/png;base64,{FAKE_BASE64}"}}, |
| 110 | + ], |
| 111 | + } |
| 112 | + ] |
| 113 | + result = get_last_user_utterance(events) |
| 114 | + assert isinstance(result, str) |
| 115 | + assert FAKE_BASE64 not in result |
| 116 | + assert result == "[+ image]" |
| 117 | + |
| 118 | + def test_empty_list_returns_empty_string(self): |
| 119 | + events = [{"type": "UserMessage", "text": []}] |
| 120 | + assert get_last_user_utterance(events) == "" |
| 121 | + |
| 122 | + def test_multiple_images_single_placeholder(self): |
| 123 | + events = [ |
| 124 | + { |
| 125 | + "type": "UserMessage", |
| 126 | + "text": [ |
| 127 | + {"type": "image_url", "image_url": {"url": "data:image/png;base64,AAA"}}, |
| 128 | + {"type": "image_url", "image_url": {"url": "data:image/png;base64,BBB"}}, |
| 129 | + ], |
| 130 | + } |
| 131 | + ] |
| 132 | + assert get_last_user_utterance(events) == "[+ image]" |
0 commit comments