Skip to content

Commit 744d568

Browse files
committed
review comment
Signed-off-by: Akihiko Kuroda <akihikokuroda2020@gmail.com>
1 parent 5914136 commit 744d568

3 files changed

Lines changed: 42 additions & 10 deletions

File tree

AGENTS.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,8 @@ Intrinsics are specialized LoRA adapters that add task-specific capabilities (RA
180180
| `rag` | `find_citations(response, documents, context, backend)` | Document sentences supporting the response |
181181
| `rag` | `check_context_relevance(question, document, context, backend)` | Whether a document is relevant (0–1); only supported for granite-4.0, not granite-4.1 |
182182
| `rag` | `flag_hallucinated_content(response, documents, context, backend)` | Flag potentially hallucinated sentences |
183-
| `guardian` | `factuality_detection(response, context, backend, *, documents=None, model_options=None)` | Determine if the last response is factually incorrect ("yes"/"no") |
184-
| `guardian` | `factuality_correction(response, context, backend, *, documents=None, model_options=None)` | Correct the last response to be factually accurate |
183+
| `guardian` | `factuality_detection(context, backend, *, documents=None, model_options=None)` | Determine if the last response is factually incorrect ("yes"/"no") |
184+
| `guardian` | `factuality_correction(context, backend, *, documents=None, model_options=None)` | Correct the last response to be factually accurate |
185185

186186
```python
187187
from mellea.backends.huggingface import LocalHFBackend

mellea/stdlib/components/intrinsic/guardian.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,15 @@ def guardian_check(
179179
180180
Returns:
181181
Risk score as a float between 0.0 (no risk) and 1.0 (risk detected).
182+
183+
Raises:
184+
ValueError: If documents are provided but target_role is not "assistant".
182185
"""
186+
if documents is not None and target_role != "assistant":
187+
raise ValueError(
188+
"documents parameter is only supported when target_role='assistant'"
189+
)
190+
183191
if documents is not None and target_role == "assistant":
184192
response, context, resolved_docs = _resolve_response(None, context)
185193
explicit_docs = _coerce_to_documents(documents)

test/stdlib/components/intrinsic/test_guardian.py

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -182,12 +182,24 @@ def test_factuality_detection(backend):
182182

183183
@pytest.mark.qualitative
184184
def test_factuality_detection_from_context(backend):
185-
"""Verify factuality detection works when documents are in the last message."""
185+
"""Verify factuality detection works when documents are already in the last message."""
186186
context, documents = _read_guardian_input("factuality_detection.json")
187187

188-
# Test with documents passed as argument to factuality_detection
189-
# The function should handle extracting the response and attaching documents
190-
result = guardian.factuality_detection(context, backend, documents=documents)
188+
# Rebuild context with documents attached to the last assistant message
189+
last_turn = context.last_turn()
190+
if last_turn and last_turn.model_input:
191+
context = ChatContext().add(Message("user", "What is the question?"))
192+
if isinstance(last_turn.model_input, Message):
193+
context = context.add(
194+
Message(
195+
last_turn.model_input.role,
196+
last_turn.model_input.content,
197+
documents=documents,
198+
)
199+
)
200+
201+
# Call without documents= argument; documents should be picked up from context
202+
result = guardian.factuality_detection(context, backend)
191203
assert result == "yes" or result == "no"
192204

193205

@@ -203,12 +215,24 @@ def test_factuality_correction(backend):
203215

204216
@pytest.mark.qualitative
205217
def test_factuality_correction_from_context(backend):
206-
"""Verify factuality correction works when documents are in the last message."""
218+
"""Verify factuality correction works when documents are already in the last message."""
207219
context, documents = _read_guardian_input("factuality_correction.json")
208220

209-
# Test with documents passed as argument to factuality_correction
210-
# The function should handle extracting the response and attaching documents
211-
result = guardian.factuality_correction(context, backend, documents=documents)
221+
# Rebuild context with documents attached to the last assistant message
222+
last_turn = context.last_turn()
223+
if last_turn and last_turn.model_input:
224+
context = ChatContext().add(Message("user", "What is the question?"))
225+
if isinstance(last_turn.model_input, Message):
226+
context = context.add(
227+
Message(
228+
last_turn.model_input.role,
229+
last_turn.model_input.content,
230+
documents=documents,
231+
)
232+
)
233+
234+
# Call without documents= argument; documents should be picked up from context
235+
result = guardian.factuality_correction(context, backend)
212236
assert isinstance(result, str)
213237

214238

0 commit comments

Comments
 (0)