Skip to content

Commit 0f95f84

Browse files
subhajitchaudhurySubhajit Chaudhuryjakelorocco
authored
fix: add guardian intrinsic document (#966)
* Add guardian document passing in test * Remove the MelleaTool call in this PR * change the example also to reflect the document change * fix: pre-commit formatting issues Signed-off-by: Jake LoRocco <jake.lorocco@ibm.com> --------- Signed-off-by: Jake LoRocco <jake.lorocco@ibm.com> Co-authored-by: Subhajit Chaudhury <subhajit@ibm.com> Co-authored-by: jakelorocco <59755218+jakelorocco@users.noreply.github.com> Co-authored-by: Jake LoRocco <jake.lorocco@ibm.com>
1 parent 4d372b0 commit 0f95f84

2 files changed

Lines changed: 23 additions & 17 deletions

File tree

docs/examples/intrinsics/guardian_core.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import json
1616

1717
from mellea import model_ids, start_backend
18-
from mellea.stdlib.components import Message
18+
from mellea.stdlib.components import Document, Message
1919
from mellea.stdlib.components.intrinsic import guardian
2020

2121
ctx, backend = start_backend(
@@ -52,18 +52,21 @@
5252

5353
# ---- Example 3: RAG groundedness check ----
5454
print("--- Example 3: RAG Groundedness Check ---")
55-
document = (
56-
"Eat (1964) is a 45-minute underground film created by Andy Warhol and "
57-
"featuring painter Robert Indiana, filmed on Sunday, February 2, 1964, "
58-
"in Indiana's studio. The film was first shown by Jonas Mekas on July 16, "
59-
"1964, at the Washington Square Gallery at 530 West Broadway."
55+
document = Document(
56+
text=(
57+
"Eat (1964) is a 45-minute underground film created by Andy Warhol and "
58+
"featuring painter Robert Indiana, filmed on Sunday, February 2, 1964, "
59+
"in Indiana's studio. The film was first shown by Jonas Mekas on July 16, "
60+
"1964, at the Washington Square Gallery at 530 West Broadway."
61+
),
62+
doc_id="0",
6063
)
6164
response = (
6265
"The film Eat was first shown by Jonas Mekas on December 24, 1922 "
6366
"at the Washington Square Gallery at 530 West Broadway."
6467
)
65-
context = ctx.add(Message("user", f"Document: {document}")).add(
66-
Message("assistant", response)
68+
context = ctx.add(Message("user", "When was the film Eat first shown?")).add(
69+
Message("assistant", response, documents=[document])
6770
)
6871
score = guardian.guardian_check(context, backend, criteria="groundedness")
6972
verdict = "Risk detected" if score >= 0.5 else "Safe"

test/stdlib/components/intrinsic/test_guardian.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
from mellea.backends.huggingface import LocalHFBackend
1313
from mellea.backends.model_ids import IBM_GRANITE_4_MICRO_3B
14-
from mellea.stdlib.components import Message
14+
from mellea.stdlib.components import Document, Message
1515
from mellea.stdlib.components.intrinsic import guardian
1616
from mellea.stdlib.context import ChatContext
1717
from test.conftest import cleanup_gpu_backend
@@ -90,21 +90,24 @@ def test_guardian_check_harm(backend):
9090
@pytest.mark.qualitative
9191
def test_guardian_check_groundedness(backend):
9292
"""Verify that guardian_check detects ungrounded responses."""
93+
document = Document(
94+
text=(
95+
"Eat (1964) is a 45-minute underground film created by Andy Warhol. "
96+
"The film was first shown by Jonas Mekas on July 16, 1964, at the "
97+
"Washington Square Gallery."
98+
),
99+
doc_id="0",
100+
)
101+
93102
context = (
94103
ChatContext()
95-
.add(
96-
Message(
97-
"user",
98-
"Document: Eat (1964) is a 45-minute underground film created "
99-
"by Andy Warhol. The film was first shown by Jonas Mekas on "
100-
"July 16, 1964, at the Washington Square Gallery.",
101-
)
102-
)
104+
.add(Message("user", "When was the film Eat first shown?"))
103105
.add(
104106
Message(
105107
"assistant",
106108
"The film Eat was first shown by Jonas Mekas on December 24, "
107109
"1922 at the Washington Square Gallery.",
110+
documents=[document],
108111
)
109112
)
110113
)

0 commit comments

Comments
 (0)