mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-09 08:08:32 +00:00
Add HallucinationGuardrail no-op implementation with tests (#2869)
Some checks failed
Notify Downstream / notify-downstream (push) Has been cancelled
Some checks failed
Notify Downstream / notify-downstream (push) Has been cancelled
- Add `HallucinationGuardrail` class as enterprise feature placeholder - Update LLM guardrail events to support `HallucinationGuardrail` instances - Add comprehensive tests for `HallucinationGuardrail` initialization and behavior - Add integration tests for `HallucinationGuardrail` with task execution system - Ensure no-op behavior always returns True
This commit is contained in:
108
tests/test_hallucination_guardrail.py
Normal file
108
tests/test_hallucination_guardrail.py
Normal file
@@ -0,0 +1,108 @@
|
||||
from unittest.mock import Mock
|
||||
|
||||
import pytest
|
||||
|
||||
from crewai.llm import LLM
|
||||
from crewai.tasks.hallucination_guardrail import HallucinationGuardrail
|
||||
from crewai.tasks.task_output import TaskOutput
|
||||
|
||||
|
||||
def test_hallucination_guardrail_initialization():
|
||||
"""Test that the hallucination guardrail initializes correctly with all parameters."""
|
||||
mock_llm = Mock(spec=LLM)
|
||||
|
||||
guardrail = HallucinationGuardrail(context="Test reference context", llm=mock_llm)
|
||||
|
||||
assert guardrail.context == "Test reference context"
|
||||
assert guardrail.llm == mock_llm
|
||||
assert guardrail.threshold is None
|
||||
assert guardrail.tool_response == ""
|
||||
|
||||
guardrail = HallucinationGuardrail(
|
||||
context="Test reference context",
|
||||
llm=mock_llm,
|
||||
threshold=8.5,
|
||||
tool_response="Sample tool response",
|
||||
)
|
||||
|
||||
assert guardrail.context == "Test reference context"
|
||||
assert guardrail.llm == mock_llm
|
||||
assert guardrail.threshold == 8.5
|
||||
assert guardrail.tool_response == "Sample tool response"
|
||||
|
||||
|
||||
def test_hallucination_guardrail_no_op_behavior():
|
||||
"""Test that the guardrail always returns True in the open-source version."""
|
||||
mock_llm = Mock(spec=LLM)
|
||||
guardrail = HallucinationGuardrail(
|
||||
context="Test reference context",
|
||||
llm=mock_llm,
|
||||
threshold=9.0,
|
||||
)
|
||||
|
||||
task_output = TaskOutput(
|
||||
raw="Sample task output",
|
||||
description="Test task",
|
||||
expected_output="Expected output",
|
||||
agent="Test Agent",
|
||||
)
|
||||
|
||||
result, output = guardrail(task_output)
|
||||
|
||||
assert result is True
|
||||
assert output == "Sample task output"
|
||||
|
||||
|
||||
def test_hallucination_guardrail_description():
|
||||
"""Test that the guardrail provides the correct description for event logging."""
|
||||
guardrail = HallucinationGuardrail(
|
||||
context="Test reference context", llm=Mock(spec=LLM)
|
||||
)
|
||||
|
||||
assert guardrail.description == "HallucinationGuardrail (no-op)"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"context,task_output_text,threshold,tool_response",
|
||||
[
|
||||
(
|
||||
"Earth orbits the Sun once every 365.25 days.",
|
||||
"It takes Earth approximately one year to go around the Sun.",
|
||||
None,
|
||||
"",
|
||||
),
|
||||
(
|
||||
"Python was created by Guido van Rossum in 1991.",
|
||||
"Python is a programming language developed by Guido van Rossum.",
|
||||
7.5,
|
||||
"",
|
||||
),
|
||||
(
|
||||
"The capital of France is Paris.",
|
||||
"Paris is the largest city and capital of France.",
|
||||
9.0,
|
||||
"Geographic API returned: France capital is Paris",
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_hallucination_guardrail_always_passes(
|
||||
context, task_output_text, threshold, tool_response
|
||||
):
|
||||
"""Test that the guardrail always passes regardless of configuration in open-source version."""
|
||||
mock_llm = Mock(spec=LLM)
|
||||
|
||||
guardrail = HallucinationGuardrail(
|
||||
context=context, llm=mock_llm, threshold=threshold, tool_response=tool_response
|
||||
)
|
||||
|
||||
task_output = TaskOutput(
|
||||
raw=task_output_text,
|
||||
description="Test task",
|
||||
expected_output="Expected output",
|
||||
agent="Test Agent",
|
||||
)
|
||||
|
||||
result, output = guardrail(task_output)
|
||||
|
||||
assert result is True
|
||||
assert output == task_output_text
|
||||
@@ -1,9 +1,10 @@
|
||||
from unittest.mock import ANY, Mock, patch
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
from crewai import Agent, Task
|
||||
from crewai.llm import LLM
|
||||
from crewai.tasks.hallucination_guardrail import HallucinationGuardrail
|
||||
from crewai.tasks.llm_guardrail import LLMGuardrail
|
||||
from crewai.tasks.task_output import TaskOutput
|
||||
from crewai.utilities.events import (
|
||||
@@ -267,3 +268,37 @@ def test_guardrail_when_an_error_occurs(sample_agent, task_output):
|
||||
max_retries=0,
|
||||
)
|
||||
task.execute_sync(agent=sample_agent)
|
||||
|
||||
|
||||
def test_hallucination_guardrail_integration():
|
||||
"""Test that HallucinationGuardrail integrates properly with the task system."""
|
||||
agent = Mock()
|
||||
agent.role = "test_agent"
|
||||
agent.execute_task.return_value = "test result"
|
||||
agent.crew = None
|
||||
|
||||
mock_llm = Mock(spec=LLM)
|
||||
guardrail = HallucinationGuardrail(
|
||||
context="Test reference context for validation", llm=mock_llm, threshold=8.0
|
||||
)
|
||||
|
||||
task = Task(
|
||||
description="Test task with hallucination guardrail",
|
||||
expected_output="Valid output",
|
||||
guardrail=guardrail,
|
||||
)
|
||||
|
||||
result = task.execute_sync(agent=agent)
|
||||
assert isinstance(result, TaskOutput)
|
||||
assert result.raw == "test result"
|
||||
|
||||
|
||||
def test_hallucination_guardrail_description_in_events():
|
||||
"""Test that HallucinationGuardrail description appears correctly in events."""
|
||||
mock_llm = Mock(spec=LLM)
|
||||
guardrail = HallucinationGuardrail(context="Test context", llm=mock_llm)
|
||||
|
||||
assert guardrail.description == "HallucinationGuardrail (no-op)"
|
||||
|
||||
event = LLMGuardrailStartedEvent(guardrail=guardrail, retry_count=0)
|
||||
assert event.guardrail == "HallucinationGuardrail (no-op)"
|
||||
|
||||
Reference in New Issue
Block a user