Skip to content

Commit 1cfcc76

Browse files
committed
docs: initial Guardian documentation migration from deprecated GuardianCheck to Intrinsics API
Migrates docs, examples, and cross-links from the deprecated GuardianCheck/GuardianRisk API to the current Guardian Intrinsics API (guardian_check(), policy_guardrails(), factuality_detection(), factuality_correction()). - New how-to/safety-guardrails.md: full reference for all four Intrinsic functions, CRITERIA_BANK keys, and the target_role="user" input-gating pattern - Tutorial 04 steps 4–7 rewritten to use Intrinsics; prerequisites updated - Glossary: 5 new entries; GuardianCheck/GuardianRisk entries marked deprecated - Deprecation banners added to security-and-taint-tracking.md and three example files - docs.json: safety-guardrails added to nav; temporary redirect removed - Cross-links updated in intrinsics.md, index.mdx, build-a-rag-pipeline.md, use-context-and-sessions.md, common-errors.md, architecture-vs-agents.md, plugins.mdx Partially addresses #639, #802. Assisted-by: Claude Code
1 parent 3901c0e commit 1cfcc76

17 files changed

Lines changed: 591 additions & 201 deletions

docs/docs/advanced/intrinsics.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,3 +209,12 @@ print(out) # {"requirement_likelihood": 1.0}
209209

210210
The `Intrinsic` component loads aLoRA adapters (falling back to LoRA) by task name.
211211
Output format is task-specific — `requirement_check` returns a likelihood score.
212+
213+
---
214+
215+
## Guardian Intrinsics
216+
217+
Safety and factuality checks use a separate set of Guardian-specific intrinsics:
218+
`guardian_check()`, `policy_guardrails()`, `factuality_detection()`, and
219+
`factuality_correction()`. These are documented in the
220+
[Safety Guardrails](../how-to/safety-guardrails) how-to guide.

docs/docs/advanced/security-and-taint-tracking.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ description: "Use GuardianCheck with IBM Granite Guardian to validate LLM output
44
# diataxis: how-to
55
---
66

7+
> **Deprecated API.** The `GuardianCheck` class documented here is deprecated as
8+
> of Mellea v0.4 and will emit `DeprecationWarning` on use. For new code, use the
9+
> [Guardian Intrinsics](../how-to/safety-guardrails)`guardian_check()`,
10+
> `policy_guardrails()`, `factuality_detection()`, and `factuality_correction()`
11+
> which are faster, require no separate Guardian model pull, and produce consistent
12+
> structured output.
13+
714
**Prerequisites:** [Instruct, Validate, Repair](../concepts/instruct-validate-repair)
815
complete, `pip install mellea`, Ollama running locally with a Granite Guardian model
916
pulled.

docs/docs/concepts/architecture-vs-agents.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ orchestrator:
135135
with [`ChatContext`](../reference/glossary#chatcontext) and the `@tool` decorator. See
136136
[Tools and Agents](../how-to/tools-and-agents).
137137
- **Guarded agents** — combine the ReACT pattern with `requirements` and
138-
`GuardianCheck` to enforce safety constraints at every step. See
139-
[Security and Taint Tracking](../advanced/security-and-taint-tracking).
138+
[Guardian Intrinsics](../how-to/safety-guardrails) to enforce safety constraints
139+
at every step.
140140
- **Structured outputs** — use `@generative` with Pydantic models or `Literal` types
141141
to enforce type-safe structured output at each step. See
142142
[Generative Functions](../how-to/generative-functions).
@@ -212,4 +212,4 @@ tools or steps.
212212
---
213213

214214
**See also:** [Tools and Agents](../how-to/tools-and-agents) |
215-
[Security and Taint Tracking](../advanced/security-and-taint-tracking)
215+
[Safety Guardrails](../how-to/safety-guardrails)

docs/docs/concepts/plugins.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1003,4 +1003,4 @@ from mellea.plugins import (
10031003

10041004
---
10051005

1006-
**See also:** [Glossary](../reference/glossary), [Tools and Agents](../how-to/tools-and-agents), [Security and Taint Tracking](../advanced/security-and-taint-tracking), [OpenTelemetry Tracing](../observability/tracing)
1006+
**See also:** [Glossary](../reference/glossary), [Tools and Agents](../how-to/tools-and-agents), [Safety Guardrails](../how-to/safety-guardrails), [OpenTelemetry Tracing](../evaluation-and-observability/opentelemetry-tracing)

docs/docs/docs.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
"how-to/configure-model-options",
6969
"how-to/use-images-and-vision",
7070
"how-to/build-a-rag-pipeline",
71+
"how-to/safety-guardrails",
7172
"how-to/refactor-prompts-with-cli",
7273
"how-to/unit-test-generative-code",
7374
"how-to/handling-exceptions"
@@ -483,10 +484,6 @@
483484
"source": "/integrations/langchain-and-smolagents",
484485
"destination": "/integrations/langchain"
485486
},
486-
{
487-
"source": "/how-to/safety-guardrails",
488-
"destination": "/advanced/security-and-taint-tracking"
489-
},
490487
{
491488
"source": "/dev/constrained-decoding",
492489
"destination": "/advanced/mellea-core-internals"

docs/docs/examples/index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ to run.
6060

6161
| Category | What it shows |
6262
| -------- | ------------- |
63-
| `safety/` | `GuardianCheck` for harm, jailbreak, profanity, social bias, violence, and groundedness; shared backend pattern |
63+
| `intrinsics/` | [Guardian Intrinsics](../how-to/safety-guardrails): `guardian_check()` for harm, jailbreak, social bias, groundedness; `policy_guardrails()`; `factuality_detection()` / `factuality_correction()` |
64+
| `safety/` | *(Deprecated)* `GuardianCheck` examples — see `intrinsics/` for the current API |
6465

6566
### Integration and deployment
6667

docs/docs/guide/CONTRIBUTING.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,8 @@ Terms that **must** be linked on first use wherever they appear in guide pages (
208208
| `ReAct` | `#react` |
209209
| `RichDocument` | `#richdocument` |
210210
| `LiteLLM` / `LiteLLMBackend` | `#litellm--litellmbackend` |
211-
| `GuardianCheck` / `GuardianRisk` | `#guardiancheck` |
211+
| `guardian_check()` / `CRITERIA_BANK` | `#guardian_check` / `#criteria_bank` |
212+
| `GuardianCheck` / `GuardianRisk` *(deprecated)* | `#guardiancheck` / `#guardianrisk` |
212213
| `m decompose` | `#m-decompose` |
213214

214215
Linking within the **glossary page itself** is not required (the glossary is the definition source).

docs/docs/how-to/build-a-rag-pipeline.md

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Embedding model → vector search → top-k candidates
3030
|
3131
v
3232
Final answer
33-
(optional: GuardianCheck groundedness)
33+
(optional: guardian_check groundedness)
3434
```
3535

3636
---
@@ -168,32 +168,34 @@ answer = m.instruct(
168168

169169
## Step 5: Check groundedness (optional)
170170

171-
After generation, use [`GuardianCheck`](../reference/glossary#guardiancheck) with `GuardianRisk.GROUNDEDNESS` to
172-
verify the answer does not hallucinate beyond the retrieved documents:
171+
After generation, use [`guardian_check()`](../how-to/safety-guardrails) with
172+
`criteria="groundedness"` to verify the answer does not hallucinate beyond the
173+
retrieved documents. This requires `pip install "mellea[hf]"`:
173174

174175
```python
175-
from mellea.stdlib.requirements.safety.guardian import GuardianCheck, GuardianRisk
176+
from mellea.backends.huggingface import LocalHFBackend
177+
from mellea.stdlib.components import Message
178+
from mellea.stdlib.components.intrinsic import guardian
179+
from mellea.stdlib.context import ChatContext
176180

177-
groundedness_check = GuardianCheck(
178-
GuardianRisk.GROUNDEDNESS,
179-
backend_type="ollama",
180-
ollama_url="http://localhost:11434",
181-
context_text="\n\n".join(relevant),
181+
guardian_backend = LocalHFBackend(model_id="ibm-granite/granite-4.0-micro")
182+
183+
eval_ctx = (
184+
ChatContext()
185+
.add(Message("user", f"Document: {chr(10).join(relevant)}"))
186+
.add(Message("assistant", str(answer)))
182187
)
183188

184-
results = m.validate([groundedness_check])
185-
if results[0]._result:
186-
print("Grounded answer:", str(answer))
189+
score = guardian.guardian_check(eval_ctx, guardian_backend, criteria="groundedness")
190+
if score < 0.5:
191+
print(f"Grounded answer (score: {score:.4f}):", str(answer))
187192
else:
188-
print("Answer may contain hallucinated content:", results[0]._reason)
193+
print(f"Groundedness risk detected (score: {score:.4f})")
189194
```
190195

191-
Pass the same text to `context_text` that you used in `grounding_context`
192-
this ensures the groundedness model evaluates the answer against exactly what
193-
the generator was given.
194-
195-
> **Backend note:** `GuardianCheck` requires `granite3-guardian:2b` pulled in Ollama.
196-
> Run `ollama pull granite3-guardian:2b` before using it.
196+
Include the same documents in the evaluation context that you passed to
197+
`grounding_context` — this ensures the groundedness model evaluates the answer
198+
against exactly what the generator was given.
197199

198200
---
199201

@@ -204,8 +206,11 @@ from faiss import IndexFlatIP
204206
from sentence_transformers import SentenceTransformer
205207

206208
from mellea import generative, start_session
209+
from mellea.backends.huggingface import LocalHFBackend
210+
from mellea.stdlib.components import Message
211+
from mellea.stdlib.components.intrinsic import guardian
212+
from mellea.stdlib.context import ChatContext
207213
from mellea.stdlib.requirements import req, simple_validate
208-
from mellea.stdlib.requirements.safety.guardian import GuardianCheck, GuardianRisk
209214

210215

211216
@generative
@@ -226,6 +231,9 @@ def search(query: str, docs: list[str], index: IndexFlatIP,
226231
return [docs[i] for i in indices[0]]
227232

228233

234+
guardian_backend = LocalHFBackend(model_id="ibm-granite/granite-4.0-micro")
235+
236+
229237
def rag(docs: list[str], query: str) -> str | None:
230238
embedding_model = SentenceTransformer("all-MiniLM-L6-v2")
231239
index = build_index(docs, embedding_model)
@@ -245,14 +253,14 @@ def rag(docs: list[str], query: str) -> str | None:
245253
requirements=[req("Answer only from the provided documents.")],
246254
)
247255

248-
results = m.validate([GuardianCheck(
249-
GuardianRisk.GROUNDEDNESS,
250-
backend_type="ollama",
251-
ollama_url="http://localhost:11434",
252-
context_text="\n\n".join(relevant),
253-
)])
254-
if not results[0]._result:
255-
print("Warning: groundedness check failed:", results[0]._reason)
256+
eval_ctx = (
257+
ChatContext()
258+
.add(Message("user", f"Document: {chr(10).join(relevant)}"))
259+
.add(Message("assistant", str(answer)))
260+
)
261+
score = guardian.guardian_check(eval_ctx, guardian_backend, criteria="groundedness")
262+
if score >= 0.5:
263+
print(f"Warning: groundedness risk detected (score: {score:.4f})")
256264

257265
return str(answer)
258266
```
@@ -267,7 +275,7 @@ def rag(docs: list[str], query: str) -> str | None:
267275
| `is_relevant` docstring | How strictly the filter interprets relevance | Adjust phrasing to match your domain |
268276
| `grounding_context` key names | Tracing and debugging in spans | Use descriptive names in production |
269277
| `requirements` on `m.instruct()` | Answer length, citation, tone | Add after baseline quality is good |
270-
| GuardianCheck `context_text` | What the groundedness model checks against | Match exactly what you pass to `grounding_context` |
278+
| `guardian_check` document context | What the groundedness model checks against | Match exactly what you pass to `grounding_context` |
271279

272280
---
273281

0 commit comments

Comments
 (0)