|
| 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 | +from nemoguardrails import RailsConfig |
| 17 | +from tests.utils import TestChat |
| 18 | + |
| 19 | +yaml_content = """ |
| 20 | +colang_version: "2.x" |
| 21 | +models: |
| 22 | + - type: main |
| 23 | + engine: openai |
| 24 | + model: gpt-4-turbo |
| 25 | +""" |
| 26 | + |
| 27 | + |
| 28 | +def test_1(): |
| 29 | + """Test input and output rails transformations.""" |
| 30 | + |
| 31 | + colang_content = """ |
| 32 | + import core |
| 33 | + import guardrails |
| 34 | +
|
| 35 | + flow input rails $input_text |
| 36 | + global $user_message |
| 37 | + $user_message = "{$input_text}, Dick" |
| 38 | +
|
| 39 | + flow output rails $output_text |
| 40 | + global $user_message |
| 41 | + global $bot_message |
| 42 | + $bot_message = "{$user_message}, and Harry" |
| 43 | +
|
| 44 | + flow main |
| 45 | + global $last_bot_message |
| 46 | + await user said "Tom" |
| 47 | + bot say "{$last_bot_message}" |
| 48 | + """ |
| 49 | + |
| 50 | + config = RailsConfig.from_content(colang_content, yaml_content) |
| 51 | + chat = TestChat( |
| 52 | + config, |
| 53 | + llm_completions=[], |
| 54 | + ) |
| 55 | + chat >> "Tom" |
| 56 | + chat << "Tom, Dick, and Harry" |
| 57 | + |
| 58 | + |
| 59 | +if __name__ == "__main__": |
| 60 | + test_1() |
0 commit comments