Use CortexDB as a knowledge base and audit logger for NVIDIA NeMo Guardrails.

NeMo Guardrails Integration

CortexDB integrates with NVIDIA's NeMo Guardrails as a knowledge base provider for fact-checking and as an audit logger for guardrail decisions. This enables guardrails to verify claims against stored knowledge and maintain a complete audit trail of all guardrail evaluations.

Installation

pip install cortexdb[nemo-guardrails]

Quick Start

from nemoguardrails import LLMRails, RailsConfig
from cortexdb import Cortex
from cortexdb_nemo_guardrails import CortexDBKnowledgeBase, register_cortexdb_actions

client = Cortex(base_url="http://localhost:3141", api_key="your-cortex-api-key")

# Set up knowledge base for fact-checking
kb = CortexDBKnowledgeBase(
    client=client,
    tenant_id="my-app",
    namespace="guardrails-kb",
)

# Load authoritative knowledge
kb.add("Refunds must be requested within 30 days of purchase.")
kb.add("Premium support is available for Enterprise tier customers only.")

# Register actions with NeMo Guardrails
config = RailsConfig.from_path("config/")
rails = LLMRails(config)
register_cortexdb_actions(rails, client, tenant_id="my-app")

As Memory Backend

Knowledge Base

The CortexDBKnowledgeBase provides a searchable knowledge store for guardrails fact-checking:

kb = CortexDBKnowledgeBase(
    client=client,
    tenant_id="my-app",
    namespace="company-policies",
    top_k=5,
)

# Add authoritative knowledge
kb.add("The company was founded in 2020.")
kb.add("Our headquarters is in San Francisco, CA.")
kb.add_many([
    "Free tier includes 1,000 API calls per month.",
    "Pro tier includes 100,000 API calls per month.",
    "Enterprise tier has unlimited API calls.",
])

# Search for facts
results = kb.search("API call limits")

# Get plain text chunks for prompt injection
chunks = kb.search_relevant_chunks("company founding date")
# Returns: ["The company was founded in 2020."]

As Agent Tools

Custom Guardrail Actions

Register CortexDB actions that guardrails can invoke during evaluation:

from cortexdb_nemo_guardrails import register_cortexdb_actions

register_cortexdb_actions(rails, client, tenant_id="my-app")

This registers four actions:

  • cortexdb_retrieve_context — Retrieve relevant context for grounding responses
  • cortexdb_check_facts — Verify claims against stored knowledge
  • cortexdb_log_guardrail_decision — Log guardrail decisions for audit
  • cortexdb_store_interaction — Store user-bot interactions for future reference

Use them in your Colang flows:

define flow check response accuracy
  $context = execute cortexdb_retrieve_context(query=$bot_message)
  $facts = execute cortexdb_check_facts(claim=$bot_message)
  if not $facts.supported
    bot refuse to answer
    execute cortexdb_log_guardrail_decision(rail_name="fact_check", decision="blocked", input_text=$user_message, reason="Unsupported claim")
  else
    execute cortexdb_log_guardrail_decision(rail_name="fact_check", decision="allowed", input_text=$user_message, output_text=$bot_message)

Individual Action Usage

You can also use actions directly in Python:

from cortexdb_nemo_guardrails import cortexdb_check_facts, cortexdb_log_guardrail_decision

# Fact-check a claim
result = await cortexdb_check_facts(
    client=client,
    claim="Our free tier includes 5,000 API calls.",
    tenant_id="my-app",
)
print(result)
# {"supported": False, "evidence": [...], "evidence_count": 0}

# Log a guardrail decision
await cortexdb_log_guardrail_decision(
    client=client,
    rail_name="accuracy",
    decision="blocked",
    input_text="What's our free tier limit?",
    output_text="Our free tier includes 5,000 API calls.",
    reason="Claim not supported by knowledge base",
    tenant_id="my-app",
)

Configuration

| Parameter | Default | Description | |---|---|---| | base_url | http://localhost:3141 | CortexDB server URL | | api_key | None | CortexDB API key | | tenant_id | "default" | Tenant identifier | | namespace | None | Knowledge base namespace | | top_k | 5 | Results per search query | | min_confidence | 0.0 | Minimum confidence for fact-checking |

Complete Example

from nemoguardrails import LLMRails, RailsConfig
from cortexdb import Cortex
from cortexdb_nemo_guardrails import (
    CortexDBKnowledgeBase,
    register_cortexdb_actions,
)

client = Cortex(base_url="http://localhost:3141", api_key="your-cortex-api-key")

# 1. Set up the knowledge base with authoritative facts
kb = CortexDBKnowledgeBase(
    client=client,
    tenant_id="customer-support",
    namespace="policies",
)

kb.add_many([
    "Refunds are processed within 5-7 business days.",
    "Refund requests must be submitted within 30 days.",
    "Digital products are non-refundable after download.",
    "Premium support is available Mon-Fri 9am-6pm EST.",
    "Enterprise customers have 24/7 dedicated support.",
])

# 2. Configure NeMo Guardrails
config = RailsConfig.from_path("config/")
rails = LLMRails(config)

# 3. Register CortexDB actions
register_cortexdb_actions(rails, client, tenant_id="customer-support")

# 4. Use the guardrailed LLM
response = rails.generate(
    messages=[{
        "role": "user",
        "content": "Can I get a refund on a digital product I downloaded last week?",
    }]
)

# The guardrail checks the response against the knowledge base,
# ensuring the bot correctly states that digital products are
# non-refundable after download. All decisions are logged to
# CortexDB for audit compliance.
print(response)