ガードレール構成

ガードレールは、OCIAIConf()を使用して基本モデルの選択の一部として、支援ユーティリティを使用して構成できます。

ガードレール構成は、OCI生成AIサービスから基本モデルを選択するときに提供されます。この例では、xai.grok-4モデルを選択します。

from aidputils.agents.toolkit.configs import OCIAIConf 
guardrails_config = { 
    "name" : "<guardrailsName>", 
    "description" : "<guardrailsDescription>", 
    "policies" : [ ] 
  } 
model_args = {} 
llm_conf = OCIAIConf(model_provider='generic', 
                     compartment_id='<compartment_ocid>', 
                     model_args=model_args, 
                     endpoint='https://inference.generativeai.<oci-region>.oci.oraclecloud.com', 
                     model_id='xai.grok-4', 
                     guardrails_config=guardrails_config)

ガードレール構成は、ポリシーの配列で構成されるJSONのような文字列です。前述の例では、<guardrailsName>および<guardrailsDescription>がユーザー定義の名前および説明である次のコード・ブロックで定義されています。


guardrails_config = { 
    "name" : "<guardrailsName>", 
    "description" : "<guardrailsDescription>", 
    "policies" : [ ] 
  }

各ポリシーには次のキーがあります。

ヒント 必須 説明 データ・タイプ デフォルト値
policyName × ポリシーのカスタム名 文字列 N/A
policyType 適用するガードレール・ポリシーのタイプ。
指定できる値:
  • CONTENT_MODERATION
  • PROMPT_ATTACKS_PREVENTION
  • PII_DETECTION
ENUM  
policyDescription × ポリシーの説明です。 文字列  
scope × スコープは、ガードレールの適用場所を定義します。
指定できる値:
  • USER_REQUEST
  • AGENT_RESPONSE
  • BOTH
ENUM  
action × ポリシー違反時に実行するアクション
指定できる値:
  • INFORM
  • BLOCK
  • ALLOW MASK

    (PII_DETECTIONの場合のみ)

ENUM  
threshold × 検出のしきい値。

範囲は0から1までの確率です。

浮動小数  
piiCategories 検出されるPIIデータのカテゴリとそのアクションおよび有効化。 配列  

piiCategoriesは、次のキーを使用するJSONのようなオブジェクトの配列でもあります。

ヒント 必須 説明 データ・タイプ デフォルト値
category 検出するPIIカテゴリ。
指定できる値:
  • PERSON
  • ADDRESS
  • TELEPHONE_NUMBER
  • EMAIL
文字列 N/A
isEnabled × PIIカテゴリの検出を有効にします。
指定できる値:
  • True
  • False
ENUM  
action × PIIカテゴリが検出された場合に実行する処理。前述の処理を上書きします。
指定できる値:
  • INFORM
  • BLOCK
  • ALLOW
  • MASK
文字列  

例: ガードレール構成の完了

この場合、次の3つのポリシーすべてを適用します。
  • コンテンツのモデレーションは、エージェントの応答にのみ適用されます。
  • プロンプトインジェクションは、検出された場合、ユーザーの要求をブロックします。
  • PIIはエージェント・レスポンスとユーザー・リクエストの両方で検出されます。各PIIカテゴリは異なる方法で処理されます。
guardrails_config = { 
    "policies" : [ { 
      "policyType" : "CONTENT_MODERATION", 
      "policyName" : "Content Moderation prevention", 
      "policyDescription" : "Choose an action to take when hate, sexual, violence, toxic, derogatory, or harassment content is detected in either the user input query or the agent response.", 
      "scope" : "AGENT_RESPONSE", 
      "action" : "INFORM", 
      "threshold" : 0.5, 
      "categories" : [ ] 
    }, { 
      "policyType" : "PROMPT_ATTACKS_PREVENTION", 
      "policyName" : "Prompt Injection prevention", 
      "policyDescription" : "Choose action when prompt injection is detected on the user query.", 
      "scope" : "USER_REQUEST", 
      "action" : "BLOCK", 
      "threshold" : 0.5 
    }, { 
      "policyType" : "PII_DETECTION", 
      "policyName" : "Personally Identifiable Information (PII) detection", 
      "policyDescription" : "Choose an action to take when PII entities are detected in either the user input query or the agent response.", 
      "scope" : "AGENT_RESPONSE", 
      "action" : "INFORM", 
      "threshold" : 0.5, 
      "piiCategories" : [ { 
        "category" : "PERSON", 
        "isEnabled" : False, 
        "action" : "INFORM" 
      }, { 
        "category" : "ADDRESS", 
        "isEnabled" : False, 
        "action" : "INFORM" 
      }, { 
        "category" : "TELEPHONE_NUMBER", 
        "isEnabled" : True, 
        "action" : "MASK" 
      }, { 
        "category" : "EMAIL", 
        "isEnabled" : True, 
        "action" : "MASK" 
      } ] 
    }, { 
      "policyType" : "PII_DETECTION", 
      "policyName" : "Personally Identifiable Information (PII) detection", 
      "policyDescription" : "Choose an action to take when PII entities are detected in either the user input query or the agent response.", 
      "scope" : "USER_REQUEST", 
      "action" : "INFORM", 
      "threshold" : 0.5, 
      "piiCategories" : [ { 
        "category" : "PERSON", 
        "isEnabled" : True, 
        "action" : "INFORM" 
      }, { 
        "category" : "ADDRESS", 
        "isEnabled" : True, 
        "action" : "INFORM" 
      }, { 
        "category" : "TELEPHONE_NUMBER", 
        "isEnabled" : True, 
        "action" : "BLOCK" 
      }, { 
        "category" : "EMAIL", 
        "isEnabled" : False, 
        "action" : "INFORM" 
      } ] 
    } ] 
  }