|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2023 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 | +"""Test for InternalEvent handling in topic_safety_check_input action.""" |
| 17 | + |
| 18 | +from unittest.mock import AsyncMock, patch |
| 19 | + |
| 20 | +import pytest |
| 21 | + |
| 22 | +from nemoguardrails.colang.v2_x.runtime.flows import InternalEvent |
| 23 | +from nemoguardrails.library.topic_safety.actions import topic_safety_check_input |
| 24 | + |
| 25 | + |
| 26 | +@pytest.mark.asyncio |
| 27 | +async def test_topic_safety_check_input_with_internal_events(): |
| 28 | + """Test that topic_safety_check_input can handle InternalEvent objects without failing. |
| 29 | +
|
| 30 | + This test would fail before the fix with: |
| 31 | + TypeError: 'InternalEvent' object is not subscriptable |
| 32 | + """ |
| 33 | + internal_events = [ |
| 34 | + InternalEvent( |
| 35 | + name="UtteranceUserActionFinished", |
| 36 | + arguments={"final_transcript": "Hello, how are you?"}, |
| 37 | + ), |
| 38 | + InternalEvent( |
| 39 | + name="StartUtteranceBotAction", |
| 40 | + arguments={"script": "I'm doing well, thank you!"}, |
| 41 | + ), |
| 42 | + ] |
| 43 | + |
| 44 | + class MockTaskManager: |
| 45 | + def render_task_prompt(self, task): |
| 46 | + return "Check if the conversation is on topic." |
| 47 | + |
| 48 | + def get_stop_tokens(self, task): |
| 49 | + return [] |
| 50 | + |
| 51 | + def get_max_tokens(self, task): |
| 52 | + return 10 |
| 53 | + |
| 54 | + llms = {"topic_control": "mock_llm"} |
| 55 | + llm_task_manager = MockTaskManager() |
| 56 | + |
| 57 | + with patch( |
| 58 | + "nemoguardrails.library.topic_safety.actions.llm_call", new_callable=AsyncMock |
| 59 | + ) as mock_llm_call: |
| 60 | + mock_llm_call.return_value = "on-topic" |
| 61 | + |
| 62 | + # should not raise TypeError: 'InternalEvent' object is not subscriptable |
| 63 | + result = await topic_safety_check_input( |
| 64 | + llms=llms, |
| 65 | + llm_task_manager=llm_task_manager, |
| 66 | + model_name="topic_control", |
| 67 | + context={"user_message": "Hello"}, |
| 68 | + events=internal_events, |
| 69 | + ) |
| 70 | + |
| 71 | + assert isinstance(result, dict) |
| 72 | + assert "on_topic" in result |
| 73 | + assert isinstance(result["on_topic"], bool) |
| 74 | + assert result["on_topic"] is True |
0 commit comments