Integrate Guardrails into Your Application

View as Markdown

The NeMo Guardrails library provides the following tools to integrate guardrails into your applications.

  • Use the NeMo Guardrails Python SDK to add guardrails directly into your Python application.

    1from nemoguardrails import LLMRails, RailsConfig
    2
    3config = RailsConfig.from_path("path/to/config")
    4rails = LLMRails(config)
    5
    6# Use in your application
    7response = rails.generate(messages=[...])
  • Use the NeMo Guardrails LangChain integration to wrap guardrails around LangChain chains or use chains within guardrails.

    Starting in 0.22, the LangChain integration is opt-in. To enable it, set the NEMOGUARDRAILS_LLM_FRAMEWORK=langchain environment variable or call set_default_framework("langchain"). Install langchain and langchain-community (or the matching langchain-<provider> package) before importing. For background, see Migrating to 0.22.

    1from nemoguardrails.integrations.langchain.runnable_rails import RunnableRails
    2
    3guardrails = RunnableRails(config)
    4chain_with_guardrails = prompt | guardrails | model | output_parser

    For more information, refer to the LangChain Integration Guide.

  • Integrate the NeMo Guardrails API server into your application to add protection to applications in any programming language.

    $nemoguardrails server --config path/to/configs

    You can then use the API server in your application by sending requests to the server’s endpoint.

    $curl -X POST http://localhost:8000/v1/chat/completions \
    > -H "Content-Type: application/json" \
    > -d '{
    > "config_id": "content_safety",
    > "messages": [{"role": "user", "content": "Hello!"}]
    > }'

    For more information, refer to Guardrails API Server.

  • Use the NeMo Guardrails Docker deployment capabilities to deploy guardrails as a containerized service. For more information, refer to Docker.

For more examples and detailed integration patterns, refer to the examples directory in the NeMo Guardrails GitHub repository.