Wire CortexDB recall and capture into NVIDIA NeMo Guardrails as registered actions.
NeMo Guardrails Integration
NeMo Guardrails uses Colang flows to constrain LLM behavior. Register CortexDB as custom actions so flows can recall context and capture audit-relevant turns.
Install
pip install cortexdbai[nemo-guardrails]
Knowledge base + actions
import os
from cortexdb import Cortex
from cortexdb.integrations.nemo_guardrails import (
CortexDBKnowledgeBase,
register_cortexdb_actions,
)
from nemoguardrails import LLMRails, RailsConfig
client = Cortex(
api_url="https://api-v1.cortexdb.ai",
actor="user:alice",
bearer=os.environ["CORTEX_TOKEN"],
)
kb = CortexDBKnowledgeBase(client=client, scope="org:acme/user:alice")
kb.add("Refunds must be requested within 30 days of purchase.")
rails = LLMRails(RailsConfig.from_path("config/"))
register_cortexdb_actions(rails, client, scope="org:acme/user:alice")
Registers cortexdb_retrieve_context, cortexdb_check_facts, cortexdb_log_guardrail_decision, and cortexdb_store_interaction actions on the rails.
Prefer manual control?
# config/actions.py
import os
from datetime import datetime, timezone
from uuid import uuid4
from nemoguardrails.actions import action
from cortexdb.v1 import V1Client
client = V1Client(api_url="https://api-v1.cortexdb.ai", actor="user:alice",
bearer=os.environ["CORTEX_TOKEN"])
SCOPE = "org:acme/user:alice"
@action()
async def cortex_recall(query: str) -> str:
pack = client.recall(scope=SCOPE, view="holistic", query=query,
include=["events", "beliefs", "facts", "episodes"],
budgets={"max_tokens": 3000})
return pack.get("context_block", "") or ""
@action()
async def cortex_capture(text: str, role: str = "user") -> bool:
client.experience(scope=SCOPE, text=text, role=role,
observed_at=datetime.now(timezone.utc).isoformat(),
idempotency_key=f"nemo-{role}-{uuid4()}")
return True
# config/rails.co
define flow recall_and_answer
user said something
$context = execute cortex_recall(query=$user_message)
bot $context # prepend to system
execute cortex_capture(text=$user_message, role="user")
The forget surface (/v1/forget, /v1/erasures) is useful when guardrails detect a violation — call client.forget(...) from a block_message flow to drop the offending turn from derived layers.