-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
94 additions
and
7 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from .moderation import ModerationClassifier | ||
|
||
__all__ = [ | ||
"ModerationClassifier", | ||
] |
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,32 @@ | ||
from typing import Optional | ||
import os | ||
from openai import OpenAI | ||
from openai.types.moderation import Moderation | ||
|
||
from ...core import BaseClassifier, Score | ||
|
||
|
||
class ModerationClassifier(BaseClassifier[Moderation]): | ||
"""A classifier that uses the OpenAI Moderations API for scoring.""" | ||
|
||
def __init__( | ||
self, | ||
*, | ||
api_key: Optional[str] = None, | ||
) -> None: | ||
if not api_key: | ||
api_key = os.environ["OPENAI_API_KEY"] | ||
|
||
self._client = OpenAI(api_key=api_key) | ||
|
||
def score_text(self, text: str) -> Score[Moderation]: | ||
"""Score the text using the OpenAI Moderations API.""" | ||
response = self._client.moderations.create(input=text) | ||
output = response.results[0] | ||
|
||
return Score[Moderation]( | ||
flagged=output.flagged, | ||
value=output, | ||
description="Moderation score for the given text", | ||
explanation="Details about the moderation score", | ||
) |
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
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