界限組態

您可以在使用 OCIAIConf() 選取基礎模型時,使用輔助功能設定監護人。

從 OCI Generative 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 編號 原則的自訂名稱 String
policyType 要套用的監護人政策類型。
允許的值包括:
  • CONTENT_MODERATION
  • PROMPT_ATTACKS_PREVENTION
  • PII_DETECTION
列舉  
policyDescription 編號 保單的說明 String  
scope 編號 此範圍定義套用界限的位置。
允許的值包括:
  • USER_REQUEST
  • AGENT_RESPONSE
  • BOTH
列舉  
action 編號 違反原則時要採取的動作
允許的值包括:
  • INFORM
  • BLOCK
  • ALLOW MASK

    (僅適用於 PII_DETECTION)

列舉  
threshold 編號 偵測臨界值 。

範圍為介於 0 與 1 之間的機率。

浮點數  
piiCategories 要偵測的 PII 資料類目及其動作與啟用。 陣列  

piiCategories 也是使用下列索引鍵的類似 JSON 物件陣列:

要點 這是必要欄位。 描述 資料類型 預設值
category 要偵測的 PII 類別。
允許的值包括:
  • PERSON
  • ADDRESS
  • TELEPHONE_NUMBER
  • EMAIL
String
isEnabled 編號 啟用 PII 分類的偵測。
允許的值包括:
  • True
  • False
列舉  
action 編號 偵測到個人識別資訊類目時要採取的動作。覆寫上面的動作。
允許的值包括:
  • INFORM
  • BLOCK
  • ALLOW
  • MASK
String  

範例:完成界限組態

在這種情況下,我們應用了這三種政策:
  • 內容審核僅適用於代理程式回應,
  • 偵測到提示插入將會封鎖使用者要求,
  • 在代理程式回應和使用者要求上偵測到 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" 
      } ] 
    } ] 
  }