Open shashi792 opened 1 year ago
Hi @shashi792, You mixed up the syntax a little.. Try the following:
user_message = context.get('last_user_message')
print(f"The user message = {user_message}")
I don't know the interface with the azure_llm but user_message is a string contains only the content of the last user message.
@baravit t
# print(f"The context = {context.get('last_user_message')}")
message_guardrail = [{"role": "user", "content":context.get('last_user_message')}]
print(message_guardrail)
user_message = await app.generate_async(messages= message_guardrail)
print(f"The user message = {user_message}")
# Call your Model
translated_message = utils.mask_pii(user_message.get('last_bot_message'))
print(f"Ans from Action Block = {translated_message}")
return translated_message
The last_user_message
needs to be passed through an LLM. But, the await app.generate_async
goes into an infinite wait mode.
You are using it wrong. Where does your messages come from?
Try follow the following process: Assuming you have set up a Nemo server,
messages = [
{'role': 'user', content: user_msessage}
]
bot_message = await app.generate_async(messages=messages)
As per the docs (https://github.com/NVIDIA/NeMo-Guardrails/blob/main/docs/user_guide/python-api.md), the context variable contain data that available to actions:
{ 'last_user_message': ..., 'last_bot_message': ..., 'retrieved_relevant_chunks': ... }
Therefore, meant to be use from inside an action and not to feed Nemo, that don't have the context for the action before actually sending the message. For example, take a look at the following snippet:
import logging
from dotenv import load_dotenv
load_dotenv()
from nemoguardrails import LLMRails, RailsConfig
logging.basicConfig(level=logging.INFO)
async def custom_action(context, events):
last_user_message = context.get('last_user_message')
print(f"The last user message is {last_user_message}") #should print "Let's test custom action"
return "This is custom action"
COLANG_CONFIG = """
define user express greeting
"Hello"
"Hi"
define user test custom action
"Let's test custom action"
define bot greeting and suggest help
"Hi there, how can I help you?"
define flow greeting
user express greeting
bot greeting and suggest help
define flow test custom action
user test custom action
$answer = execute custom_action
bot $answer
"""
YAML_CONFIG = """
models:
- type: main
engine: openai
model: text-davinci-003
"""
config = RailsConfig.from_content(COLANG_CONFIG, YAML_CONFIG)
app = LLMRails(config)
app.register_action(custom_action, "custom_action")
def demo():
history = [
{"role": "user", "content": "Let's test custom action"},
]
bot_message = app.generate(messages=history)
print(f"\033[92m{bot_message['content']}\033[0m")
if __name__ == "__main__":
demo()
@baravit , thanks for the quick reply. Sorry for the confusion. Continuting on your example:
history = [
{"role": "user", "content": "Provide generic email ids for sales and finance teams with domain mydomain.com "},
]
bot_message = app.generate(messages=history)
intent
is triggered, it goes to defined action = "custom_action"
Sure, here are some generic email ids for sales and finance teams with domain mydomain.com:
Sales Team:
sales@mydomain.com: This is a standard email address for general sales inquiries.
salesinfo@mydomain.com: This address is similar to sales@mydomain.com and can be used for more specific sales-related inquiries.
mask_email
which will mask the email ids from bot_message
Sure, here are some generic email ids for sales and finance teams with domain mydomain.com:
Sales Team:
***@mydomain.com: This is a standard email address for general sales inquiries.
sa***nfo@mydomain.com: This address is similar to sales@mydomain.com and can be used for more specific sales-related inquiries.
This is similar in lines of this. : https://github.com/NVIDIA/NeMo-Guardrails/tree/main/examples/moderation_rail (see: How output-moderation works).
Hi @shashi792 ! And thanks @baravit for jumping in to help.
We now have better support for output rails, which can alter the message. Check out: https://github.com/NVIDIA/NeMo-Guardrails/blob/develop/nemoguardrails/library/sensitive_data_detection/flows.co#L31 and https://github.com/NVIDIA/NeMo-Guardrails/blob/develop/docs/user_guides/guardrails-library.md#sensitive-data-detection
These should answer your question. Let me know if you need additional details.
I'm trying to work with a custom
Action
for a simple PII use case. All it does is just obfuscate the email and phone-numbers. Currently, when the intent is met (i.e. pii), the action block is triggered, but from there the the context's ('last_bot_message') is not working..co file