Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions nemoguardrails/rails/llm/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -1566,8 +1566,11 @@ def _load_path(
full_path = os.path.join(root, file)
rel_path = os.path.relpath(full_path, config_path)

# If it's a file in the `kb` folder we need to append it to the docs
if rel_path.startswith("kb"):
# If it's a file in the `kb` folder we need to append it to the docs.
# Match the folder as a path component, not a name prefix, so that
# files/folders merely named `kb...` (e.g. `kbsettings.yml`) are not
# misclassified as knowledge-base docs and dropped.
if rel_path == "kb" or rel_path.startswith("kb" + os.sep):
_raw_config = {"docs": []}
if rel_path.endswith(".md"):
with open(full_path, encoding="utf-8") as f:
Expand Down
23 changes: 22 additions & 1 deletion tests/test_config_loading.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# limitations under the License.

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


def test_default_instructions():
Expand Down Expand Up @@ -56,3 +56,24 @@ def test_instructions_override():
content="Below is a conversation between a helpful AI assistant and a user.\n",
)
]


def test_load_path_kb_prefixed_names_are_not_dropped(tmp_path):
# A file or folder whose name merely starts with "kb" (but is not inside the
# `kb/` knowledge-base folder) must be loaded normally, not silently dropped
# as an empty knowledge-base doc.
(tmp_path / "kbsettings.yml").write_text('sample_conversation: "hello world"\n')
(tmp_path / "kbase").mkdir()
(tmp_path / "kbase" / "greeting.co").write_text("define flow greeting\n bot express greeting\n")
# A genuine kb/ document must still be collected as a doc (control).
(tmp_path / "kb").mkdir()
(tmp_path / "kb" / "doc.md").write_text("# real kb doc\n")

raw_config, colang_files = _load_path(str(tmp_path))

# kb-prefixed config file is parsed, not dropped.
assert raw_config.get("sample_conversation") == "hello world"
# kb-prefixed subfolder's colang file is registered.
assert any(name == "greeting.co" for name, _ in colang_files)
# a genuine kb/ document is still collected as a doc (control).
assert {"format": "md", "content": "# real kb doc\n"} in raw_config.get("docs", [])