Skip to content
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

Fix/issue 216 bot utterance (Enable Parser to Handle Multiline Strings and Newlines) #544

Merged
merged 3 commits into from
Jul 10, 2024
Merged
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
20 changes: 20 additions & 0 deletions nemoguardrails/actions/llm/generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# limitations under the License.

"""A set of actions for generating various types of completions using an LLMs."""

import asyncio
import logging
import random
Expand Down Expand Up @@ -910,6 +911,7 @@ async def generate_bot_message(
log.info(f"Generated bot message: {bot_utterance}")

if bot_utterance:
bot_utterance = clean_utterance_content(bot_utterance)
# In streaming mode, we also push this.
if streaming_handler:
await streaming_handler.push_chunk(bot_utterance)
Expand Down Expand Up @@ -1279,3 +1281,21 @@ async def generate_intent_steps_message(
return ActionResult(
events=[new_event_dict("BotMessage", text=text)],
)


def clean_utterance_content(utterance: str) -> str:
"""
Clean an utterance by performing the following operations:
- replacing "\\n" with "\n".

Args:
utterance (str): The utterance to clean.

Returns:
str: The cleaned utterance.
"""
if utterance:
# If "\n" is used inside a predefined message, it will be returned as is as part of the message.
# It should be translated to an actual \n character.
utterance = utterance.replace("\\n", "\n")
return utterance
29 changes: 29 additions & 0 deletions nemoguardrails/colang/v1_0/lang/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,38 @@ def get_numbered_lines(content: str):
i = 0
multiline_comment = False
current_comment = None
multiline_string = False
current_string = None
while i < len(raw_lines):
raw_line = raw_lines[i].strip()

# handle multiline string
if multiline_string:
current_string += "\n" + raw_line
if raw_line.endswith('"'):
multiline_string = False
lines.append(
{
"text": current_string,
"number": i + 1,
"indentation": multiline_indentation,
"comment": current_comment,
}
)
current_string = None
i += 1
continue
if (
raw_line.startswith('"')
and not raw_line.startswith('"""')
and not raw_line.endswith('"')
):
multiline_string = True
current_string = raw_line
multiline_indentation = len(raw_lines[i]) - len(raw_line.lstrip())
i += 1
continue

# If we have a line comment, we record it
if raw_line.startswith("#"):
if current_comment is None:
Expand Down
79 changes: 79 additions & 0 deletions tests/test_issue_216.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# 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 nemoguardrails import LLMRails, RailsConfig


@pytest.mark.asyncio
async def test_new_line_in_bot_message():
config = RailsConfig.from_content(
colang_content="""
define user express greeting
"hello"

define flow
user express greeting
bot express greeting

define bot express greeting
"Hello World!\\n NewLine World!"
""",
config={
"models": [],
"rails": {"dialog": {"user_messages": {"embeddings_only": True}}},
},
)
rails = LLMRails(config)

# This should not raise an exception
res = await rails.generate_async(
messages=[
{"role": "user", "content": "hi!"},
{"role": "assistant", "content": "hi!"},
{"role": "user", "content": "hi!"},
]
)

assert res["content"] == "Hello World!\n NewLine World!"


@pytest.mark.asyncio
async def test_new_line_in_user_message():
config = RailsConfig.from_content(
colang_content="""
define user express greeting
"hello\nworld"
define flow
user express greeting
bot express greeting
define bot express greeting
"Hello World!\\n NewLine World!"
""",
config={
"models": [],
"rails": {"dialog": {"user_messages": {"embeddings_only": True}}},
},
)
rails = LLMRails(config)
# This should not raise an exception
await rails.generate_async(
messages=[
{"role": "user", "content": "hi!"},
{"role": "assistant", "content": "hi!"},
{"role": "user", "content": "hi!"},
]
)
Loading