护栏配置
在使用 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)Guardrails 配置是一个类似 JSON 的字符串,由一系列策略组成。在上面的示例中,它在代码块中定义,其中 <guardrailsName> 和 <guardrailsDescription> 是用户定义的名称和说明:
guardrails_config = {
"name" : "<guardrailsName>",
"description" : "<guardrailsDescription>",
"policies" : [ ]
}每个策略都有以下键:
| 关键信息 | 必需 | 说明 | 数据类型 | 默认值 |
|---|---|---|---|---|
policyName
|
无 | 策略的定制名称 | 字符串 | 不适用 |
policyType
|
是 | 要应用的护栏策略的类型。
允许的值包括:
|
ENUM | |
policyDescription
|
无 | 策略的说明 | 字符串 | |
scope
|
无 | 该范围定义了如何应用护栏。
允许的值包括:
|
ENUM | |
action
|
无 | 违反策略时要执行的操作
允许的值包括:
|
ENUM | |
threshold
|
无 | 用于检测的阈值。
范围是介于 0 和 1 之间的概率。 |
浮点型 | |
piiCategories
|
是 | 要检测的 PII 数据及其操作和启用的类别。 | Array |
piiCategories 也是使用以下键的类似 JSON 的对象的数组:
| 关键信息 | 必需 | 说明 | 数据类型 | 默认值 |
|---|---|---|---|---|
category
|
是 | 要检测的 PII 类别。
允许的值包括:
|
字符串 | 不适用 |
isEnabled
|
无 | 启用 PII 类别的检测。
允许的值包括:
|
ENUM | |
action
|
无 | 检测到 PII 类别时要执行的操作。覆盖上面的操作。
允许的值包括:
|
字符串 |
示例:完整的 Guardrails 配置
在这种情况下,我们应用所有三个策略:
- 内容审核仅应用于代理响应,
- 如果检测到提示注入,将阻止用户请求,
- 在代理响应和用户请求上检测到 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"
} ]
} ]
}