forked from langchain-ai/langchain
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit tests and required dependencies
- Loading branch information
1 parent
61a815f
commit 2afbff5
Showing
3 changed files
with
74 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
libs/community/tests/unit_tests/tools/azure_ai_services/test_content_safety.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
"""Tests for the Azure AI Content Safety Text Tool.""" | ||
|
||
from typing import Any | ||
|
||
import pytest | ||
|
||
from langchain_community.tools.azure_ai_services.content_safety import ( | ||
AzureContentSafetyTextTool, | ||
) | ||
|
||
|
||
@pytest.mark.requires("azure.ai.contentsafety") | ||
def test_content_safety(mocker: Any) -> None: | ||
mocker.patch("azure.ai.contentsafety.ContentSafetyClient", autospec=True) | ||
mocker.patch("azure.core.credentials.AzureKeyCredential", autospec=True) | ||
|
||
key = "key" | ||
endpoint = "endpoint" | ||
|
||
tool = AzureContentSafetyTextTool( | ||
content_safety_key=key, content_safety_endpoint=endpoint | ||
) | ||
assert tool.content_safety_key == key | ||
assert tool.content_safety_endpoint == endpoint | ||
|
||
|
||
@pytest.mark.requires("azure.ai.contentsafety") | ||
def test_harmful_content_detected(mocker: Any) -> None: | ||
key = "key" | ||
endpoint = "endpoint" | ||
|
||
mocker.patch("azure.core.credentials.AzureKeyCredential", autospec=True) | ||
mocker.patch("azure.ai.contentsafety.ContentSafetyClient", autospec=True) | ||
tool = AzureContentSafetyTextTool( | ||
content_safety_key=key, content_safety_endpoint=endpoint | ||
) | ||
|
||
mock_content_client = mocker.Mock() | ||
mock_content_client.analyze_text.return_value.categories_analysis = [ | ||
{"category": "Harm", "severity": 1} | ||
] | ||
|
||
tool.content_safety_client = mock_content_client | ||
|
||
input = "This text contains harmful content" | ||
output = "Harm: 1\n" | ||
|
||
result = tool._run(input) | ||
assert result == output | ||
|
||
|
||
@pytest.mark.requires("azure.ai.contentsafety") | ||
def test_no_harmful_content_detected(mocker: Any) -> None: | ||
key = "key" | ||
endpoint = "endpoint" | ||
|
||
tool = AzureContentSafetyTextTool( | ||
content_safety_key=key, content_safety_endpoint=endpoint | ||
) | ||
|
||
mock_content_client = mocker.Mock() | ||
mock_content_client.analyze_text.return_value.categories_analysis = [ | ||
{"category": "Harm", "severity": 0} | ||
] | ||
|
||
tool.content_safety_client = mock_content_client | ||
|
||
input = "This text contains harmful content" | ||
output = "Harm: 0\n" | ||
|
||
result = tool._run(input) | ||
assert result == output |