|
| 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 | +import pytest |
| 17 | +from pydantic import ValidationError |
| 18 | + |
| 19 | +from nemoguardrails.rails.llm.config import Model |
| 20 | + |
| 21 | + |
| 22 | +def test_explicit_model_param(): |
| 23 | + """Test model specified directly via the model parameter.""" |
| 24 | + model = Model(type="main", engine="test_engine", model="test_model") |
| 25 | + assert model.model == "test_model" |
| 26 | + assert "model" not in model.parameters |
| 27 | + |
| 28 | + |
| 29 | +def test_model_in_parameters(): |
| 30 | + """Test model specified via parameters dictionary.""" |
| 31 | + model = Model(type="main", engine="test_engine", parameters={"model": "test_model"}) |
| 32 | + assert model.model == "test_model" |
| 33 | + assert "model" not in model.parameters |
| 34 | + |
| 35 | + |
| 36 | +def test_model_name_in_parameters(): |
| 37 | + """Test model specified via model_name in parameters dictionary.""" |
| 38 | + model = Model( |
| 39 | + type="main", engine="test_engine", parameters={"model_name": "test_model"} |
| 40 | + ) |
| 41 | + assert model.model == "test_model" |
| 42 | + assert "model_name" not in model.parameters |
| 43 | + |
| 44 | + |
| 45 | +def test_model_equivalence(): |
| 46 | + """Test that models defined in different ways are considered equivalent.""" |
| 47 | + model1 = Model(type="main", engine="test_engine", model="test_model") |
| 48 | + model2 = Model( |
| 49 | + type="main", engine="test_engine", parameters={"model": "test_model"} |
| 50 | + ) |
| 51 | + assert model1 == model2 |
| 52 | + |
| 53 | + |
| 54 | +def test_empty_model_and_parameters(): |
| 55 | + """Test that an empty model and parameters dict fails validation.""" |
| 56 | + with pytest.raises(ValueError, match="Model name must be specified"): |
| 57 | + Model(type="main", engine="openai", parameters={}) |
| 58 | + |
| 59 | + |
| 60 | +def test_none_model_and_parameters(): |
| 61 | + """Test that None model and empty parameters dict fails validation.""" |
| 62 | + with pytest.raises(ValueError, match="Model name must be specified"): |
| 63 | + Model(type="main", engine="openai", model=None, parameters={}) |
| 64 | + |
| 65 | + |
| 66 | +def test_none_model_and_none_parameters(): |
| 67 | + """Test that None model and None parameters fails validation.""" |
| 68 | + with pytest.raises(ValueError): |
| 69 | + Model(type="main", engine="openai", model=None, parameters=None) |
| 70 | + |
| 71 | + |
| 72 | +def test_model_and_model_name_in_parameters(): |
| 73 | + """Test that having both model and model_name in parameters raises an error.""" |
| 74 | + with pytest.raises( |
| 75 | + ValueError, match="Model name must be specified in exactly one place" |
| 76 | + ): |
| 77 | + Model( |
| 78 | + type="main", |
| 79 | + engine="openai", |
| 80 | + model="gpt-4", |
| 81 | + parameters={"model_name": "gpt-3.5-turbo"}, |
| 82 | + ) |
| 83 | + |
| 84 | + |
| 85 | +def test_model_and_model_in_parameters(): |
| 86 | + """Test that having both model field and model in parameters raises an error.""" |
| 87 | + with pytest.raises( |
| 88 | + ValueError, match="Model name must be specified in exactly one place" |
| 89 | + ): |
| 90 | + Model( |
| 91 | + type="main", |
| 92 | + engine="openai", |
| 93 | + model="gpt-4", |
| 94 | + parameters={"model": "gpt-3.5-turbo"}, |
| 95 | + ) |
| 96 | + |
| 97 | + |
| 98 | +def test_empty_string_model(): |
| 99 | + """Test that an empty string model fails validation.""" |
| 100 | + with pytest.raises(ValueError, match="Model name must be specified"): |
| 101 | + Model(type="main", engine="openai", model="", parameters={}) |
| 102 | + |
| 103 | + |
| 104 | +def test_whitespace_only_model(): |
| 105 | + """Test that a whitespace-only model fails validation.""" |
| 106 | + with pytest.raises(ValueError, match="Model name must be specified"): |
| 107 | + Model(type="main", engine="openai", model=" ", parameters={}) |
| 108 | + |
| 109 | + |
| 110 | +def test_empty_string_model_name_in_parameters(): |
| 111 | + """Test that an empty string model_name in parameters fails validation.""" |
| 112 | + with pytest.raises(ValueError, match="Model name must be specified"): |
| 113 | + Model(type="main", engine="openai", parameters={"model_name": ""}) |
| 114 | + |
| 115 | + |
| 116 | +def test_whitespace_only_model_in_parameters(): |
| 117 | + """Test that a whitespace-only model in parameters fails validation.""" |
| 118 | + with pytest.raises(ValueError, match="Model name must be specified"): |
| 119 | + Model(type="main", engine="openai", parameters={"model": " "}) |
| 120 | + |
| 121 | + |
| 122 | +def test_model_name_none_in_parameters(): |
| 123 | + """Test that None model_name in parameters fails validation.""" |
| 124 | + with pytest.raises(ValueError, match="Model name must be specified"): |
| 125 | + Model(type="main", engine="openai", parameters={"model_name": None}) |
0 commit comments