Skip to content

[PY] fix: content safety public preview deprecation #2207

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ class AzureContentSafetyModeratorOptions(OpenAIModeratorOptions):
You could attach multiple lists name here.
"""

halt_on_blocklist_hit: Optional[bool] = False
"""
When set to true, further analyses of harmful content will not be performed
in cases where blocklists are hit.
When set to false, all analyses of harmful content will be performed, whether
or not blocklists are hit.
Default value is false.
"""


class AzureContentSafetyModerator(Generic[StateT], Moderator[StateT]):
"""
Expand Down Expand Up @@ -95,28 +104,24 @@ async def review_input(self, context: TurnContext, state: StateT) -> Optional[Pl
text=input,
categories=self._options.categories,
blocklist_names=self._options.blocklist_names,
halt_on_blocklist_hit=self._options.halt_on_blocklist_hit,
)
)

flagged: bool = False
categories: Dict[str, bool] = {}
category_scores: Dict[str, int] = {}

category_results = ["hateResult", "selfHarmResult", "sexualResult", "violenceResult"]

for category in category_results:
result = res[category] if category in res else None
if result is not None:
category = result["category"].lower()
if category == "selfharm":
category = "self_harm"
categories[category] = result["severity"] is not None and result["severity"] > 0
category_scores[category] = (
0 if result["severity"] is None else result["severity"]
)
if result["severity"] is not None and result["severity"] > 0:
flagged = True
categories_analysis = res["categoriesAnalysis"]

for result in categories_analysis:
category = result["category"].lower()
if category == "selfharm":
category = "self_harm"
categories[category] = result["severity"] is not None and result["severity"] > 0
category_scores[category] = 0 if result["severity"] is None else result["severity"]
if result["severity"] is not None and result["severity"] > 0:
flagged = True
return (
None
if not flagged
Expand Down Expand Up @@ -156,34 +161,28 @@ async def review_output(self, context: TurnContext, state: StateT, plan: Plan) -
),
categories=self._options.categories,
blocklist_names=self._options.blocklist_names,
halt_on_blocklist_hit=self._options.halt_on_blocklist_hit,
)
)

flagged: bool = False
categories: Dict[str, bool] = {}
category_scores: Dict[str, int] = {}

category_results = [
"hateResult",
"selfHarmResult",
"sexualResult",
"violenceResult",
]
categories_analysis = res["categoriesAnalysis"]

for category in category_results:
result = res[category] if category in res else None
if result is not None:
category = result["category"].lower()
if category == "selfharm":
category = "self_harm"
categories[category] = (
result["severity"] is not None and result["severity"] > 0
)
category_scores[category] = (
0 if result["severity"] is None else result["severity"]
)
if result["severity"] is not None and result["severity"] > 0:
flagged = True
for result in categories_analysis:
category = result["category"].lower()
if category == "selfharm":
category = "self_harm"
categories[category] = (
result["severity"] is not None and result["severity"] > 0
)
category_scores[category] = (
0 if result["severity"] is None else result["severity"]
)
if result["severity"] is not None and result["severity"] > 0:
flagged = True

if flagged:
return Plan(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,19 @@ def analyze_text(self, *_args, **_kwargs: Any):
class MockContentSafetyClientWithResults:
def analyze_text(self, *_args, **_kwargs: Any):
return {
"blocklistsMatchResults": [],
"hateResult": {"category": "Hate", "severity": 6},
"selfHarmResult": {"category": "SelfHarm", "severity": 0},
"sexualResult": {"category": "Sexual", "severity": 0},
"violenceResult": {"category": "Violence", "severity": 0},
"blocklistsMatch": [
{
"blocklistName": "string",
"blocklistItemId": "string",
"blocklistItemText": "bleed",
}
],
"categoriesAnalysis": [
{"category": "Hate", "severity": 6},
{"category": "SelfHarm", "severity": 0},
{"category": "Sexual", "severity": 0},
{"category": "Violence", "severity": 0},
],
}


Expand Down
6 changes: 3 additions & 3 deletions python/samples/05.chatModeration/src/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
model = OpenAIModel(
OpenAIModelOptions(
api_key=config.OPENAI_KEY,
default_model="gpt-3.5-turbo"
default_model="gpt-4o"
))
moderator = OpenAIModerator(
OpenAIModeratorOptions(
Expand All @@ -47,15 +47,15 @@
model = OpenAIModel(
AzureOpenAIModelOptions(
api_key=config.AZURE_OPENAI_KEY,
default_model="gpt-35-turbo",
default_model="gpt-4o",
api_version="2023-03-15-preview",
endpoint=config.AZURE_OPENAI_ENDPOINT
))
moderator = AzureContentSafetyModerator(
AzureContentSafetyModeratorOptions(
api_key=config.AZURE_CONTENT_SAFETY_KEY,
moderate="both",
api_version="2023-04-30-preview",
api_version="2023-10-01",
endpoint=config.AZURE_CONTENT_SAFETY_ENDPOINT
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "A bot that demonstrates content moderation.",
"type": "completion",
"completion": {
"model": "gpt-3.5-turbo",
"model": "gpt-4o",
"completion_type": "chat",
"include_history": true,
"include_input": true,
Expand Down