高橋かずひとのプログラミング、その他、備忘録。

日々調べてたことや、作ってみたものをメモしているブログ。 お決まりの断り文句ですが、このブログに書かれている内容は個人の見解であり、所属する組織の公式見解ではありません。チラ裏。

OpenAI の Moderation API(問題発言検出 ※暴力とかセクシャルとか) を味見👀

ちょっと興味あって、OpenAI の Moderation API を味見しています👀

「The moderations endpoint is a tool you can use to check whether text is potentially harmful. Developers can use it to identify content that might be harmful and take action, for instance by filtering it.」
ざっくり言うとテキストが有害なものかどうか判定する。というところでしょうか。


ただ、ちょっと動かした感じ、かなり直接的な表現(殺す とか)じゃないと True にならない印象です。
あと、全部平仮名にしたりすると(ころす とか)、サクッと False になってしまったり、、、🤔
sexual カテゴリで True 判定する入力も分かりませんでした、、、🦔

以下をColaboratoryで動かしました。
今回もGitHubに上げるほどじゃないので、ブログに試したコードを貼り付けます👀

!pip install openai
from openai import OpenAI
from google.colab import userdata

client = OpenAI(api_key=userdata.get('OPENAI_API_KEY'))  # 【OpenAIのAPIキーを指定】
input_text = ""
response = client.moderations.create(input=input_text)

output = response.results[0]

print(output.flagged)
print()
for _, category_score in zip(output.categories, output.category_scores):
    print(category_score)