Skip to content

Commit

Permalink
Merge pull request #350 from NVIDIA/fix/fix-incorrect-logging-of-acti…
Browse files Browse the repository at this point in the history
…vated-rails

Fix the incorrect logging of an extra dialog rail
  • Loading branch information
drazvan authored Feb 23, 2024
2 parents 1331619 + 287022e commit 24114e9
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ eval_outputs
/**/chitchat/user.co
/**/banking/user.co
.mypy_cache
.python-version
17 changes: 11 additions & 6 deletions nemoguardrails/logging/processing_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def compute_generation_log(processing_log: List[dict]) -> GenerationLog:
ignored_flows = [
"process user input",
"run input rails",
"run dialog rails",
"process bot message",
"run output rails",
]
Expand Down Expand Up @@ -69,9 +70,11 @@ def compute_generation_log(processing_log: List[dict]) -> GenerationLog:
continue

activated_rail = ActivatedRail(
type="dialog"
if event["flow_id"] not in generation_flows
else "generation",
type=(
"dialog"
if event["flow_id"] not in generation_flows
else "generation"
),
name=event["flow_id"],
started_at=event["timestamp"],
)
Expand All @@ -88,9 +91,11 @@ def compute_generation_log(processing_log: List[dict]) -> GenerationLog:
continue

activated_rail = ActivatedRail(
type="dialog"
if event["flow_id"] not in generation_flows
else "generation",
type=(
"dialog"
if event["flow_id"] not in generation_flows
else "generation"
),
name=event["flow_id"],
started_at=event["timestamp"],
)
Expand Down
9 changes: 7 additions & 2 deletions nemoguardrails/rails/llm/llm_flows.co
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ define parallel flow process user input
create event UserMessage(text=$user_message)


define flow generate user intent
define flow run dialog rails
"""Generate the user's intent based on the text."""
event UserMessage(text="...")

Expand All @@ -36,7 +36,12 @@ define flow generate user intent
create event BotMessage(text=$bot_message)
else
# If not, we continue the usual process
execute generate_user_intent
do generate user intent


define subflow generate user intent
"""Generates the user intent."""
execute generate_user_intent


define subflow run input rails
Expand Down
4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ dependencies = [
"fastembed>=0.1.3",
"httpx>=0.24.1",
"jinja2>=3.1.3",
"langchain>=0.1.0,<0.2.0",
# The 0.1.9 has a bug related to SparkLLM which breaks everything.
"langchain>=0.1.0,<0.2.0,!=0.1.9",
"langchain-core>=0.1.0,!=0.1.26",
"langchain-community>=0.0.16,<0.1.0",
"lark~=1.1.7",
"nest-asyncio>=1.5.6",
Expand Down
52 changes: 52 additions & 0 deletions tests/test_input_rails_only.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# 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 tests.utils import TestChat


def test_1():
config = RailsConfig.from_content(
"""
define flow example input rail
done
""",
"""
rails:
input:
flows:
- example input rail
""",
)

chat = TestChat(
config,
llm_completions=[],
)

messages = [{"role": "user", "content": "Hello! What can you do for me?"}]

response = chat.app.generate(
messages=messages,
options={
"rails": ["input"],
"log": {
"activated_rails": True,
},
},
)

# We should only get the input rail here.
assert len(response.log.activated_rails) == 1

0 comments on commit 24114e9

Please sign in to comment.