|
| 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 | +import logging |
| 16 | +import unittest |
| 17 | + |
| 18 | +from nemoguardrails import RailsConfig |
| 19 | +from tests.utils import TestChat |
| 20 | + |
| 21 | +colang_content = ''' |
| 22 | + import core |
| 23 | + import passthrough |
| 24 | +
|
| 25 | + flow main |
| 26 | + activate llm continuation |
| 27 | + activate greeting |
| 28 | + activate other reactions |
| 29 | +
|
| 30 | + flow greeting |
| 31 | + user expressed greeting |
| 32 | + bot say "Hello world!" |
| 33 | +
|
| 34 | + flow other reactions |
| 35 | + user expressed to be bored |
| 36 | + bot say "No problem!" |
| 37 | +
|
| 38 | + flow user expressed greeting |
| 39 | + """"User expressed greeting in any way or form.""" |
| 40 | + user said "hi" |
| 41 | +
|
| 42 | + flow user expressed to be bored |
| 43 | + """"User expressed to be bored.""" |
| 44 | + user said "This is boring" |
| 45 | + ''' |
| 46 | + |
| 47 | +yaml_content = """ |
| 48 | +colang_version: "2.x" |
| 49 | +models: |
| 50 | + - type: main |
| 51 | + engine: openai |
| 52 | + model: gpt-3.5-turbo-instruct |
| 53 | +
|
| 54 | + """ |
| 55 | + |
| 56 | + |
| 57 | +config = RailsConfig.from_content(colang_content, yaml_content) |
| 58 | + |
| 59 | + |
| 60 | +class TestPassthroughLLMActionLogging(unittest.IsolatedAsyncioTestCase): |
| 61 | + def test_passthrough_llm_action_not_invoked_via_logs(self): |
| 62 | + chat = TestChat( |
| 63 | + config, |
| 64 | + llm_completions=["user expressed greeting"], |
| 65 | + ) |
| 66 | + rails = chat.app |
| 67 | + |
| 68 | + logger = logging.getLogger("nemoguardrails.colang.v2_x.runtime.statemachine") |
| 69 | + |
| 70 | + with self.assertLogs(logger, level="INFO") as log: |
| 71 | + messages = [{"role": "user", "content": "hi"}] |
| 72 | + response = rails.generate(messages=messages) |
| 73 | + # Check that 'StartPassthroughLLMAction' is not in the logs |
| 74 | + passthrough_invoked = any( |
| 75 | + "PassthroughLLMActionFinished" in message for message in log.output |
| 76 | + ) |
| 77 | + self.assertFalse( |
| 78 | + passthrough_invoked, "PassthroughLLMAction was invoked unexpectedly." |
| 79 | + ) |
| 80 | + |
| 81 | + self.assertIn("content", response) |
| 82 | + self.assertIsInstance(response["content"], str) |
| 83 | + |
| 84 | + def test_passthrough_llm_action_invoked_via_logs(self): |
| 85 | + chat = TestChat( |
| 86 | + config, |
| 87 | + llm_completions=["user asked about capabilites", "a random text from llm"], |
| 88 | + ) |
| 89 | + rails = chat.app |
| 90 | + |
| 91 | + logger = logging.getLogger("nemoguardrails.colang.v2_x.runtime.statemachine") |
| 92 | + |
| 93 | + with self.assertLogs(logger, level="INFO") as log: |
| 94 | + messages = [{"role": "user", "content": "What can you do?"}] |
| 95 | + response = rails.generate(messages=messages) |
| 96 | + # Check that 'StartPassthroughLLMAction' is in the logs |
| 97 | + passthrough_invoked = any( |
| 98 | + "StartPassthroughLLMAction" in message for message in log.output |
| 99 | + ) |
| 100 | + self.assertTrue( |
| 101 | + passthrough_invoked, "PassthroughLLMAction was not invoked." |
| 102 | + ) |
| 103 | + |
| 104 | + self.assertIn("content", response) |
| 105 | + self.assertIsInstance(response["content"], str) |
0 commit comments