Skip to content

Commit

Permalink
Fix general instructions override bug (#7).
Browse files Browse the repository at this point in the history
  • Loading branch information
drazvan committed Apr 28, 2023
1 parent 9dc5d35 commit 12e011d
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
12 changes: 10 additions & 2 deletions nemoguardrails/rails/llm/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def from_path(config_path: str):

elif os.path.isdir(config_path):
# Iterate all .yml files and join them
raw_config = {"instructions": default_config["instructions"]}
raw_config = {}

for root, dirs, files in os.walk(config_path):
for file in files:
Expand Down Expand Up @@ -192,6 +192,10 @@ def from_path(config_path: str):
else:
raise ValueError(f"Invalid config path {config_path}.")

# If there are no instructions, we use the default ones.
if len(raw_config.get("instructions", [])) == 0:
raw_config["instructions"] = default_config["instructions"]

raw_config["config_path"] = config_path

return RailsConfig.parse_object(raw_config)
Expand All @@ -201,7 +205,7 @@ def from_content(
colang_content: Optional[str] = None, yaml_content: Optional[str] = None
):
"""Loads a configuration from the provided colang/YAML content."""
raw_config = {"instructions": default_config["instructions"]}
raw_config = {}

if colang_content:
_join_config(
Expand All @@ -211,6 +215,10 @@ def from_content(
if yaml_content:
_join_config(raw_config, yaml.safe_load(yaml_content))

# If there are no instructions, we use the default ones.
if len(raw_config.get("instructions", [])) == 0:
raw_config["instructions"] = default_config["instructions"]

return RailsConfig.parse_object(raw_config)

@classmethod
Expand Down
58 changes: 58 additions & 0 deletions tests/test_config_loading.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# 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.

from nemoguardrails import RailsConfig
from nemoguardrails.rails.llm.config import Instruction


def test_default_instructions():
config = RailsConfig.from_content(
"""
define user express greeting
"hello"
"""
)

assert config.instructions == [
Instruction(
type="general",
content="Below is a conversation between a helpful AI assistant and a user. "
"The bot is designed to generate human-like text based on the input that it receives. "
"The bot is talkative and provides lots of specific details. "
"If the bot does not know the answer to a question, it truthfully says it does not know.",
)
]


def test_instructions_override():
config = RailsConfig.from_content(
"""
define user express greeting
"hello"
""",
"""
instructions:
- type: "general"
content: |
Below is a conversation between a helpful AI assistant and a user.
""",
)

assert config.instructions == [
Instruction(
type="general",
content="Below is a conversation between a helpful AI assistant and a user.\n",
)
]

0 comments on commit 12e011d

Please sign in to comment.