Use this file to discover all available pages before exploring further.
This example demonstrates how to build a RAG (Retrieval-Augmented Generation) system with fact-checking and hallucination detection using NeMo Guardrails.
Define your models and enable output rails for fact-checking and hallucination detection.
models: - type: main engine: openai model: gpt-3.5-turborails: output: flows: - self check facts - self check hallucinationprompts: - task: self_check_facts content: |- You are given a task to identify if the hypothesis is grounded and entailed to the evidence. You will only use the contents of the evidence and not rely on external knowledge. Answer with yes/no. "evidence": {{ evidence }} "hypothesis": {{ response }} "entails": - task: self_check_hallucinations content: |- You are given a task to identify if the hypothesis is in agreement with the context below. You will only use the contents of the context and not rely on external knowledge. Answer with yes/no. "context": {{ paragraph }} "hypothesis": {{ statement }} "agreement":
2
Implement the custom RAG action
Create a custom action that performs retrieval and prepares context for fact-checking.
from langchain_core.language_models import BaseLLMfrom langchain_core.output_parsers import StrOutputParserfrom langchain_core.prompts import PromptTemplatefrom nemoguardrails import LLMRailsfrom nemoguardrails.actions.actions import ActionResultfrom nemoguardrails.kb.kb import KnowledgeBaseTEMPLATE = """Use the following pieces of context to answer the question at the end.If you don't know the answer, just say that you don't know, don't try to make up an answer.Use three sentences maximum and keep the answer as concise as possible.Always say "thanks for asking!" at the end of the answer.{context}Question: {question}Helpful Answer:"""async def rag(context: dict, llm: BaseLLM, kb: KnowledgeBase) -> ActionResult: user_message = context.get("last_user_message") context_updates = {} # For our custom RAG, we re-use the built-in retrieval chunks = await kb.search_relevant_chunks(user_message) relevant_chunks = "\n".join([chunk["body"] for chunk in chunks]) # Store the chunks for fact-checking context_updates["relevant_chunks"] = relevant_chunks # Use a custom prompt template prompt_template = PromptTemplate.from_template(TEMPLATE) input_variables = {"question": user_message, "context": relevant_chunks} # Store the template for hallucination-checking context_updates["_last_bot_prompt"] = prompt_template.format(**input_variables) print(f"RAG :: prompt_template: {context_updates['_last_bot_prompt']}") # Put together a simple LangChain chain output_parser = StrOutputParser() chain = prompt_template | llm | output_parser answer = await chain.ainvoke(input_variables) return ActionResult(return_value=answer, context_updates=context_updates)def init(app: LLMRails): app.register_action(rag, "rag")
3
Define the output rails flow
Create flows that trigger RAG and enable fact-checking.
define user ask about report "What was last month's unemployment rate?" "Which industry added the most jobs?" "How many jobs were added in the transportation industry?"define flow answer report question user ... $answer = execute rag() $check_facts = True $check_hallucination = True bot $answer
4
Add your knowledge base
Create a kb/ folder and add your documents. For example: