Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

NeMo Guardrails is an open-source toolkit for easily adding programmable guardrails to LLM-based conversational systems.

License

NotificationsYou must be signed in to change notification settings

NVIDIA-NeMo/Guardrails

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TestsLicensePyPI versionPython 3.8+Code style: blackarXiv

LATEST RELEASE / DEVELOPMENT VERSION: Themain branch tracks the latest released beta version:0.18.0. For the latest development version, checkout thedevelop branch.

✨✨✨

📌The official NeMo Guardrails documentation has moved todocs.nvidia.com/nemo/guardrails.

✨✨✨

NeMo Guardrails is an open-source toolkit for easily addingprogrammable guardrails to LLM-based conversational applications. Guardrails (or "rails" for short) are specific ways of controlling the output of a large language model, such as not talking about politics, responding in a particular way to specific user requests, following a predefined dialog path, using a particular language style, extracting structured data, and more.

This paper introduces NeMo Guardrails and contains a technical overview of the system and the current evaluation.

Requirements

Python 3.10, 3.11, 3.12 or 3.13.

NeMo Guardrails usesannoy which is a C++ library with Python bindings. To install NeMo Guardrails you will need to have the C++ compiler and dev tools installed. Check out theInstallation Guide for platform-specific instructions.

Installation

To install using pip:

> pip install nemoguardrails

For more detailed instructions, see theInstallation Guide.

Overview

NeMo Guardrails enables developers building LLM-based applications to easily addprogrammable guardrails between the application code and the LLM.

Programmable Guardrails

Key benefits of addingprogrammable guardrails include:

  • Building Trustworthy, Safe, and Secure LLM-based Applications: you can define rails to guide and safeguard conversations; you can choose to define the behavior of your LLM-based application on specific topics and prevent it from engaging in discussions on unwanted topics.

  • Connecting models, chains, and other services securely: you can connect an LLM to other services (a.k.a. tools) seamlessly and securely.

  • Controllable dialog: you can steer the LLM to follow pre-defined conversational paths, allowing you to design the interaction following conversation design best practices and enforce standard operating procedures (e.g., authentication, support).

Protecting against LLM Vulnerabilities

NeMo Guardrails provides several mechanisms for protecting an LLM-powered chat application against common LLM vulnerabilities, such as jailbreaks and prompt injections. Below is a sample overview of the protection offered by different guardrails configuration for the exampleABC Bot included in this repository. For more details, please refer to theLLM Vulnerability Scanning page.

Use Cases

You can use programmable guardrails in different types of use cases:

  1. Question Answering over a set of documents (a.k.a. Retrieval Augmented Generation): Enforce fact-checking and output moderation.
  2. Domain-specific Assistants (a.k.a. chatbots): Ensure the assistant stays on topic and follows the designed conversational flows.
  3. LLM Endpoints: Add guardrails to your custom LLM for safer customer interaction.
  4. LangChain Chains: If you use LangChain for any use case, you can add a guardrails layer around your chains.

Usage

To add programmable guardrails to your application you can use the Python API or a guardrails server (see theServer Guide for more details). Using the Python API is similar to using the LLM directly. Calling the guardrails layer instead of the LLM requires only minimal changes to the code base, and it involves two simple steps:

  1. Loading a guardrails configuration and creating anLLMRails instance.
  2. Making the calls to the LLM using thegenerate/generate_async methods.
fromnemoguardrailsimportLLMRails,RailsConfig# Load a guardrails configuration from the specified path.config=RailsConfig.from_path("PATH/TO/CONFIG")rails=LLMRails(config)completion=rails.generate(messages=[{"role":"user","content":"Hello world!"}])

Sample output:

{"role":"assistant","content":"Hi! How can I help you?"}

The input and output format for thegenerate method is similar to theChat Completions API from OpenAI.

Async API

NeMo Guardrails is an async-first toolkit as the core mechanics are implemented using the Python async model. The public methods have both a sync and an async version. For example:LLMRails.generate andLLMRails.generate_async.

Supported LLMs

You can use NeMo Guardrails with multiple LLMs like OpenAI GPT-3.5, GPT-4, LLaMa-2, Falcon, Vicuna, or Mosaic. For more details, check out theSupported LLM Models section in the Configuration Guide.

Types of Guardrails

NeMo Guardrails supports five main types of guardrails:

Programmable Guardrails Flow
  1. Input rails: applied to the input from the user; an input rail can reject the input, stopping any additional processing, or alter the input (e.g., to mask potentially sensitive data, to rephrase).

  2. Dialog rails: influence how the LLM is prompted; dialog rails operate on canonical form messages for details seeColang Guide) and determine if an action should be executed, if the LLM should be invoked to generate the next step or a response, if a predefined response should be used instead, etc.

  3. Retrieval rails: applied to the retrieved chunks in the case of a RAG (Retrieval Augmented Generation) scenario; a retrieval rail can reject a chunk, preventing it from being used to prompt the LLM, or alter the relevant chunks (e.g., to mask potentially sensitive data).

  4. Execution rails: applied to input/output of the custom actions (a.k.a. tools), that need to be called by the LLM.

  5. Output rails: applied to the output generated by the LLM; an output rail can reject the output, preventing it from being returned to the user, or alter it (e.g., removing sensitive data).

Guardrails Configuration

A guardrails configuration defines theLLM(s) to be used andone or more guardrails. A guardrails configuration can include any number of input/dialog/output/retrieval/execution rails. A configuration without any configured rails will essentially forward the requests to the LLM.

The standard structure for a guardrails configuration folder looks like this:

.├── config│   ├── actions.py│   ├── config.py│   ├── config.yml│   ├── rails.co│   ├── ...

Theconfig.yml contains all the general configuration options, such as LLM models, active rails, and custom configuration data". Theconfig.py file contains any custom initialization code and theactions.py contains any custom python actions. For a complete overview, see theConfiguration Guide.

Below is an exampleconfig.yml:

# config.ymlmodels:  -type:mainengine:openaimodel:gpt-3.5-turbo-instructrails:# Input rails are invoked when new input from the user is received.input:flows:      -check jailbreak      -mask sensitive data on input# Output rails are triggered after a bot message has been generated.output:flows:      -self check facts      -self check hallucination      -activefence moderation on inputconfig:# Configure the types of entities that should be masked on user input.sensitive_data_detection:input:entities:          -PERSON          -EMAIL_ADDRESS

The.co files included in a guardrails configuration contain the Colang definitions (see the next section for a quick overview of what Colang is) that define various types of rails. Below is an examplegreeting.co file which defines the dialog rails for greeting the user.

define user express greeting  "Hello!"  "Good afternoon!"define flow  user express greeting  bot express greeting  bot offer to helpdefine bot express greeting  "Hello there!"define bot offer to help  "How can I help you today?"

Below is an additional example of Colang definitions for a dialog rail against insults:

define user express insult  "You are stupid"define flow  user express insult  bot express calmly willingness to help

Colang

To configure and implement various types of guardrails, this toolkit introducesColang, a modeling language specifically created for designing flexible, yet controllable, dialogue flows. Colang has a python-like syntax and is designed to be simple and intuitive, especially for developers.

Two versions of Colang, 1.0 and 2.0, are supported and Colang 1.0 is the default.

For a brief introduction to the Colang 1.0 syntax, see theColang 1.0 Language Syntax Guide.

To get started with Colang 2.0, see theColang 2.0 Documentation.

Guardrails Library

NeMo Guardrails comes with a set ofbuilt-in guardrails.

The built-in guardrails may or may not be suitable for a given production use case. As always, developers should work with their internal application team to ensure guardrails meets requirements for the relevant industry and use case and address unforeseen product misuse.

The library includes guardrails for LLM self-checking (input/output moderation, fact-checking, hallucination detection), NVIDIA safety models (content safety, topic safety), jailbreak and injection detection, and integrations with community models and third-party APIs. For the complete list, see theGuardrails Library documentation.

CLI

NeMo Guardrails also comes with a built-in CLI.

$ nemoguardrails --helpUsage: nemoguardrails [OPTIONS] COMMAND [ARGS]...actions-server    Start a NeMo Guardrails actions server.chat              Start an interactive chat session.evaluate          Run an evaluation task.server            Start a NeMo Guardrails server.

Guardrails Server

You can use the NeMo Guardrails CLI to start a guardrails server. The server can load one or more configurations from the specified folder and expose and HTTP API for using them.

nemoguardrails server [--config PATH/TO/CONFIGS] [--port PORT]

For example, to get a chat completion for asample config, you can use the/v1/chat/completions endpoint:

POST /v1/chat/completions
{"config_id":"sample","messages": [{"role":"user","content":"Hello! What can you do for me?"    }]}

Sample output:

{"role":"assistant","content":"Hi! How can I help you?"}

Docker

To start a guardrails server, you can also use a Docker container. NeMo Guardrails provides aDockerfile that you can use to build anemoguardrails image. For further information, see theusing Docker section.

Integration with LangChain

NeMo Guardrails integrates seamlessly with LangChain. You can easily wrap a guardrails configuration around a LangChain chain (or anyRunnable). You can also call a LangChain chain from within a guardrails configuration. For more details, check out theLangChain Integration Documentation

Evaluation

Evaluating the safety of a LLM-based conversational application is a complex task and still an open research question. To support proper evaluation, NeMo Guardrails provides the following:

  1. Anevaluation tool, i.e.nemoguardrails evaluate, with support for topical rails, fact-checking, moderation (jailbreak and output moderation) and hallucination.
  2. Sample LLM Vulnerability Scanning Reports, e.g,ABC Bot - LLM Vulnerability Scan Results

How is this different?

There are many ways guardrails can be added to an LLM-based conversational application. For example: explicit moderation endpoints (e.g., OpenAI, ActiveFence), critique chains (e.g. constitutional chain), parsing the output (e.g. guardrails.ai), individual guardrails (e.g., LLM-Guard), hallucination detection for RAG applications (e.g., Got It AI, Patronus Lynx).

NeMo Guardrails aims to provide a flexible toolkit that can integrate all these complementary approaches into a cohesive LLM guardrails layer. For example, the toolkit provides out-of-the-box integration with ActiveFence, AlignScore and LangChain chains.

To the best of our knowledge, NeMo Guardrails is the only guardrails toolkit that also offers a solution for modeling the dialog between the user and the LLM. This enables on one hand the ability to guide the dialog in a precise way. On the other hand it enables fine-grained control for when certain guardrails should be used, e.g., use fact-checking only for certain types of questions.

Learn More

Inviting the community to contribute

The example rails residing in the repository are excellent starting points. We enthusiastically invite the community to contribute towards making the power of trustworthy, safe, and secure LLMs accessible to everyone. For guidance on setting up a development environment and how to contribute to NeMo Guardrails, see thecontributing guidelines.

License

This toolkit is licensed under theApache License, Version 2.0.

How to cite

If you use this work, please cite theEMNLP 2023 paper that introduces it.

@inproceedings{rebedea-etal-2023-nemo,title ="{N}e{M}o Guardrails: A Toolkit for Controllable and Safe {LLM} Applications with Programmable Rails",author ="Rebedea, Traian  and      Dinu, Razvan  and      Sreedhar, Makesh Narsimhan  and      Parisien, Christopher  and      Cohen, Jonathan",editor ="Feng, Yansong  and      Lefever, Els",booktitle ="Proceedings of the 2023 Conference on Empirical Methods in Natural Language Processing: System Demonstrations",month = dec,year ="2023",address ="Singapore",publisher ="Association for Computational Linguistics",url ="https://aclanthology.org/2023.emnlp-demo.40",doi ="10.18653/v1/2023.emnlp-demo.40",pages ="431--445",}

About

NeMo Guardrails is an open-source toolkit for easily adding programmable guardrails to LLM-based conversational systems.

Topics

Resources

License

Contributing

Security policy

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp