生成AIのモデレーション¶
モデレーション設定は、プロンプトと回答をフィルターすることで、生成AIアプリケーションが安全で適切なコンテンツを生成するようにします。 モデレーションは、生成プロセスのさまざまな段階で、問題のあるコンテンツをブロックしたり、警告を表示したりできます。
モデレーションテンプレートのリスト¶
利用可能なすべてのモデレーションテンプレートを取得するには:
import datarobot as dr
templates = dr.ModerationTemplate.list()
for template in templates:
print(f"Template: {template.name}")
print(f" Description: {template.description}")
モデレーション設定の作成¶
テンプレートからモデレーション設定を作成します。 モデレーション設定を作成する際は、以下を指定する必要があります。
template_id:この設定のベースとなるテンプレートのID。name:設定のわかりやすい名前。description:設定の説明。stages:このガードが有効なモデレーションのステージ(例:PROMPT、RESPONSE)。entity_id:この設定が適用されるカスタムモデルバージョンまたはプレイグラウンドのID。entity_type:関連付けられたエンティティのタイプ(CUSTOM_MODEL_VERSIONまたはPLAYGROUND)。intervention:モデレーションに失敗した場合に実行するアクション(BLOCKまたはWARN)。llm_type:このガードが使用する基盤LLM。
templates = dr.ModerationTemplate.list()
template = templates[0]
custom_model_version = dr.CustomModelVersion.get(version_id)
moderation_config = dr.ModerationConfiguration.create(
template_id=template.id,
name="Content Safety Guard",
description="Filters inappropriate content",
stages=[dr.ModerationGuardStage.PROMPT, dr.ModerationGuardStage.RESPONSE],
entity_id=custom_model_version.id,
entity_type=dr.ModerationGuardEntityType.CUSTOM_MODEL_VERSION,
intervention=dr.ModerationIntervention.BLOCK,
llm_type=dr.ModerationGuardLlmType.DATAROBOT_LLM
)
moderation_config
プレイグラウンドのモデレーションを作成することもできます。
playground = dr.genai.Playground.get(playground_id)
moderation_config = dr.ModerationConfiguration.create(
template_id=template.id,
name="Playground Safety Guard",
description="Filters content in playground",
stages=[dr.ModerationGuardStage.PROMPT, dr.ModerationGuardStage.RESPONSE],
entity_id=playground.id,
entity_type=dr.ModerationGuardEntityType.PLAYGROUND,
intervention=dr.ModerationIntervention.WARN,
llm_type=dr.ModerationGuardLlmType.DATAROBOT_LLM
)
モデレーション設定のリスト¶
エンティティの設定を取得するには:
custom_model_version = dr.CustomModelVersion.get(version_id)
configs = dr.ModerationConfiguration.list(
entity_id=custom_model_version.id,
entity_type=dr.ModerationGuardEntityType.CUSTOM_MODEL_VERSION
)
for config in configs:
print(f"Config: {config.name}")
print(f" Stages: {config.stages}")
print(f" Intervention: {config.intervention}")
モデレーション設定の取得¶
特定の設定を取得するには:
config = dr.ModerationConfiguration.get(config_id)
print(f"Name: {config.name}")
print(f"Description: {config.description}")
print(f"Stages: {config.stages}")
モデレーション設定の更新¶
モデレーション設定を更新するには:
config = dr.ModerationConfiguration.get(config_id)
config.update(
name="Updated Safety Guard",
description="Enhanced content filtering",
intervention=dr.ModerationIntervention.WARN
)
全体的なモデレーション設定の取得¶
エンティティの全体的なモデレーション設定を取得します。
custom_model_version = dr.CustomModelVersion.get(version_id)
overall_config = dr.OverallModerationConfig.get(
entity_id=custom_model_version.id,
entity_type=dr.ModerationGuardEntityType.CUSTOM_MODEL_VERSION
)
if overall_config:
print(f"Moderation enabled: {overall_config.is_enabled}")
print(f"Configurations: {len(overall_config.configurations)}")
全体的なモデレーション設定のリスト¶
すべての全体的なモデレーション設定を取得するには:
overall_configs = dr.OverallModerationConfig.list()
for config in overall_configs:
print(f"Entity: {config.entity_id}")
print(f" Enabled: {config.is_enabled}")
全体的なモデレーション設定の更新¶
全体的なモデレーション設定を変更するには:
overall_config = dr.OverallModerationConfig.get(
entity_id=custom_model_version.id,
entity_type=dr.ModerationGuardEntityType.CUSTOM_MODEL_VERSION
)
overall_config.update(is_enabled=True)