-
Notifications
You must be signed in to change notification settings - Fork 559
fix: use ValueError in TaskPrompt to resolve TypeError raised by Pydantic #1132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3d9eb22
feat(config): add validation for prompt fields
Pouyanpi f6b3d89
fix(config): replace ValidationError with ValueError
Pouyanpi 6e0f7d2
refactor(config): remove unused Flow import
Pouyanpi 81236d6
feat(tests): add unit tests for TaskPrompt validation
Pouyanpi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import pytest | ||
| from pydantic import ValidationError | ||
|
|
||
| from nemoguardrails.rails.llm.config import TaskPrompt | ||
|
|
||
|
|
||
| def test_task_prompt_valid_content(): | ||
| prompt = TaskPrompt(task="example_task", content="This is a valid prompt.") | ||
| assert prompt.task == "example_task" | ||
| assert prompt.content == "This is a valid prompt." | ||
| assert prompt.messages is None | ||
|
|
||
|
|
||
| def test_task_prompt_valid_messages(): | ||
| prompt = TaskPrompt(task="example_task", messages=["Hello", "How can I help you?"]) | ||
| assert prompt.task == "example_task" | ||
| assert prompt.messages == ["Hello", "How can I help you?"] | ||
| assert prompt.content is None | ||
|
|
||
|
|
||
| def test_task_prompt_missing_content_and_messages(): | ||
| with pytest.raises(ValidationError) as excinfo: | ||
| TaskPrompt(task="example_task") | ||
| assert "One of `content` or `messages` must be provided." in str(excinfo.value) | ||
|
|
||
|
|
||
| def test_task_prompt_both_content_and_messages(): | ||
| with pytest.raises(ValidationError) as excinfo: | ||
| TaskPrompt( | ||
| task="example_task", | ||
| content="This is a prompt.", | ||
| messages=["Hello", "How can I help you?"], | ||
| ) | ||
| assert "Only one of `content` or `messages` must be provided." in str(excinfo.value) | ||
|
|
||
|
|
||
| def test_task_prompt_models_validation(): | ||
| prompt = TaskPrompt( | ||
| task="example_task", | ||
| content="Test prompt", | ||
| models=["openai", "openai/gpt-3.5-turbo"], | ||
| ) | ||
| assert prompt.models == ["openai", "openai/gpt-3.5-turbo"] | ||
|
|
||
| prompt = TaskPrompt(task="example_task", content="Test prompt", models=[]) | ||
| assert prompt.models == [] | ||
|
|
||
| prompt = TaskPrompt(task="example_task", content="Test prompt", models=None) | ||
| assert prompt.models is None | ||
|
|
||
|
|
||
| def test_task_prompt_max_length_validation(): | ||
| prompt = TaskPrompt(task="example_task", content="Test prompt") | ||
| assert prompt.max_length == 16000 | ||
|
|
||
| prompt = TaskPrompt(task="example_task", content="Test prompt", max_length=1000) | ||
| assert prompt.max_length == 1000 | ||
|
|
||
| with pytest.raises(ValidationError) as excinfo: | ||
| TaskPrompt(task="example_task", content="Test prompt", max_length=0) | ||
| assert "Input should be greater than or equal to 1" in str(excinfo.value) | ||
|
|
||
| with pytest.raises(ValidationError) as excinfo: | ||
| TaskPrompt(task="example_task", content="Test prompt", max_length=-1) | ||
| assert "Input should be greater than or equal to 1" in str(excinfo.value) | ||
|
|
||
|
|
||
| def test_task_prompt_mode_validation(): | ||
| prompt = TaskPrompt(task="example_task", content="Test prompt") | ||
| # default mode is "standard" | ||
| assert prompt.mode == "standard" | ||
|
|
||
| prompt = TaskPrompt(task="example_task", content="Test prompt", mode="chat") | ||
| assert prompt.mode == "chat" | ||
|
|
||
| prompt = TaskPrompt(task="example_task", content="Test prompt", mode=None) | ||
| assert prompt.mode is None | ||
|
|
||
|
|
||
| def test_task_prompt_stop_tokens_validation(): | ||
| prompt = TaskPrompt( | ||
| task="example_task", content="Test prompt", stop=["\n", "Human:", "Assistant:"] | ||
| ) | ||
| assert prompt.stop == ["\n", "Human:", "Assistant:"] | ||
|
|
||
| prompt = TaskPrompt(task="example_task", content="Test prompt", stop=[]) | ||
| assert prompt.stop == [] | ||
|
|
||
| prompt = TaskPrompt(task="example_task", content="Test prompt", stop=None) | ||
| assert prompt.stop is None | ||
|
|
||
| with pytest.raises(ValidationError) as excinfo: | ||
| TaskPrompt(task="example_task", content="Test prompt", stop=[1, 2, 3]) | ||
| assert "Input should be a valid string" in str(excinfo.value) | ||
|
|
||
|
|
||
| def test_task_prompt_max_tokens_validation(): | ||
| prompt = TaskPrompt(task="example_task", content="Test prompt") | ||
| assert prompt.max_tokens is None | ||
|
|
||
| prompt = TaskPrompt(task="example_task", content="Test prompt", max_tokens=1000) | ||
| assert prompt.max_tokens == 1000 | ||
|
|
||
| with pytest.raises(ValidationError) as excinfo: | ||
| TaskPrompt(task="example_task", content="Test prompt", max_tokens=0) | ||
| assert "Input should be greater than or equal to 1" in str(excinfo.value) | ||
|
|
||
| with pytest.raises(ValidationError) as excinfo: | ||
| TaskPrompt(task="example_task", content="Test prompt", max_tokens=-1) | ||
| assert "Input should be greater than or equal to 1" in str(excinfo.value) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.