You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
from langchain.chat_models import init_chat_model
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from nemoguardrails import RailsConfig
from nemoguardrails.integrations.langchain.runnable_rails import RunnableRails
config = RailsConfig.from_path("./config")
guardrails = RunnableRails(
config,
output_key="user_question",
passthrough=True,
verbose=False
)
prompt = ChatPromptTemplate.from_template("Respond to user question in short. User question: {user_question}")
model = init_chat_model("gpt-4.1") # gpt-4o-mini
output_parser = StrOutputParser()
# guardrails around the entire chain
# unsafe question
base_chain = prompt | model | output_parser
validate_base_chain = guardrails | base_chain
response = validate_base_chain.invoke({"user_question":"Are politicians good or bad people?"})
print(response)
### Configuration used for the above code (flow) -
models:
- type: main
engine: openai
model: gpt-4o
rails:
input:
flows:
- self check input
output:
flows:
- self check output
prompts:
- task: self_check_input
content: |
Your task is to filter out messages about politics.
Block messages that discuss, ask about, or reference:
- Political parties, politicians, or elections
- Government policy debates or political ideologies
- Political news, campaigns, or voting
User message: "{{ user_input }}"
Answer only: Yes (block) or No (allow)
- task: self_check_output
content: |
Your task is to filter model responses that contain the word "perspective".
Model response: {{ bot_response }}
Answer only: Yes (block) or No (allow)
Output -
No problem! Let me know if you have any other questions.
The guardrails layer returns the string I’m sorry, I can’t respond to that. in the user_question field. How should we handle this kind of output correctly when integrating with LangChain chains that expect the original user_question input? I’ve spent quite a bit of time reading through the NeMo Guardrails documentation, but I’m still not sure what the recommended pattern is for this case.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
### Code -
### Configuration used for the above code (flow) -
Output -
No problem! Let me know if you have any other questions.The guardrails layer returns the string
I’m sorry, I can’t respond to that.in theuser_questionfield. How should we handle this kind of output correctly when integrating with LangChain chains that expect the original user_question input? I’ve spent quite a bit of time reading through the NeMo Guardrails documentation, but I’m still not sure what the recommended pattern is for this case.Beta Was this translation helpful? Give feedback.
All reactions