7.4. 配置内置检测器和保护rails 网关


内置检测器和保护rails 网关是您可以使用 GuardrailsOrchestrator 服务一起部署的 sidecar 容器。使用 GuardrailsOrchestrator 自定义资源(CR)启用它们。这个示例使用 regex 内置检测器来演示进程。

先决条件

  • 具有 OpenShift AI 集群的集群管理员特权。
  • 您已下载并安装 OpenShift AI 命令行界面(CLI)。如需更多信息 ,请参阅安装 OpenShift CLI
  • 您熟悉如何创建 ConfigMap 来监控用户定义的工作流。您在此过程中执行类似的步骤。如需更多信息,请参阅了解配置映射
  • 您已将 KServe 配置为使用 RawDeployment 模式。如需更多信息,请参阅在 单模式服务平台上部署模型
  • 在 OpenShift AI DataScienceCluster 中将 TrustyAI 组件设置为 Managed
  • 您有一个大型语言模型(LLM),用于 chat 生成或文本分类,或两者都部署在命名空间中。

流程

  1. 在 YAML 文件中定义 ConfigMap 对象,以指定 regexDetectorImage。例如,使用以下内容创建一个名为 regex_image_cm.yaml 的 YAML 文件:

    regex_gateway_images_cm.yaml示例

    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: gorch-regex-gateway-image-config
    data:
      regexDetectorImage: 'quay.io/repository/trustyai/regex-detector@sha256:efab6cd8b637b9c35d311aaf639dfedee7d28de3ee07b412ab473deadecd3606'            
    1
    
      GatewayImage: 'quay.io/repository/trustyai/vllm-orchestrator-gateway@sha256:c511b386d61a728acdfe8a1ac7a16b3774d072dd053718e5b9c5fab0f025ac3b' 
    2

    1
    regex detector 是一个 sidecar 镜像,它提供基于 regex 的检测。
    2
    guardrails 网关是一个 sidecar 镜像,它模拟 vLLM chat completions API 并保存预设置的检测器配置。
  2. 部署 regex_gateway_images_cm.yaml 配置映射:

    $ oc apply -f regex_gateway_images_cm.yaml -n <TEST_NAMESPACE>
  3. 定义 guardrails 网关 ConfigMap 对象,以指定 检测器 和路由。例如,使用以下内容创建一个名为 detectors_cm.yaml 的 YAML 文件:

    detectors_cm.yaml示例

    kind: ConfigMap
    apiVersion: v1
    metadata:
      name: fms-orchestr8-config-gateway
      labels:
        app: fmstack-nlp
    data:
      config.yaml: |
        orchestrator:   
    1
    
          host: "localhost"
          port: 8032
        detectors:      
    2
    
          - name: regex_language
            input: true 
    3
    
            output: true
            detector_params:
              regex:
                - email
                - us-social-security-number
                - credit-card
                - ipv4
                - ipv6
                - us-phone-number
                - uk-post-code
                - $CUSTOM_REGEX
          - name: hap
            detector_params: {}
        routes:         
    4
    
          - name: all
            detectors:
              - regex_language
              - hap
          - name: passthrough
            detectors:

    1
    编配器服务。
    2
    用于常见检测操作的预配置正则表达式列表。这些正则表达式会检测个人识别信息,如电子邮件信用卡
    3
    detector 将用于输入和输出。
    4
    为检测器生成的端点。例如,pii 由 $GUARDRAILS_GATEWAY_URL/pii/v1/chat/completions 提供,并使用 regex detector。passthrough preset 不使用任何 detectors。
  4. 部署 guardrails 网关 detectors_cm.yaml 配置映射:

    $ oc apply -f detectors_cm.yaml -n <TEST_NAMESPACE>
  5. 指定您在 GuardrailsOrchestrator 自定义资源(CR)中创建的 ConfigMap 对象。例如,使用以下内容创建一个名为 orchestrator_cr.yaml 的 YAML 文件:

    orchestrator_cr.yaml CR 示例

    apiVersion: trustyai.opendatahub.io/v1alpha1
    kind: GuardrailsOrchestrator
    metadata:
      name: gorch-sample
    spec:
      orchestratorConfig: "fms-orchestr8-config-nlp"
      enableBuiltInDetectors: True  
    1
    
      enableGuardrailsGateway: True  
    2
    
      guardrailsGatewayConfig: "fms-orchestr8-config-gateway" 
    3
    
      replicas: 1

    1
    enableBuiltInDetectors 字段(如果设为 True )会将内置检测器注入编配器 pod 中。
    2
    enableGuardrailsGateway 字段(如果设为 True )将 guardrails 网关注入编配器 pod。
    3
    guardrailsGatewayConfig 字段指定 ConfigMap 资源的名称,用于重新路由编配器和 regex detector 路由到特定路径。
  6. 部署编配器自定义资源。此步骤在命名空间中创建服务帐户、部署、服务和路由对象。

    oc apply -f orchestrator_cr.yaml -n <TEST_NAMESPACE>

验证

  1. 使用编配器的 /info 端点检查编配器 pod 的健康状况:

    GORCH_ROUTE=$(oc get routes guardrails-orchestrator-health -o jsonpath='{.spec.host}')
    curl -s https://$GORCH_ROUTE/info | jq

    响应示例

    {
      "services": {
        "chat_generation": {
          "status": "HEALTHY"
        },
        "regex": {
          "status": "HEALTHY"
        }
      }
    }

    在本例中,Guardrails Orchestrator 通过单个 chat_generation LLM 协调来自 regex detector 的请求。

7.4.1. 将请求发送到正则表达式检测器

您可以使用 Guardrails Orchestrator API 将请求发送到 regex detector。正则表达式检测器通过标记与指定正则表达式模式匹配的内容来过滤对话。

先决条件

  • 您已配置了 regex detector 镜像。

流程

  • 向您配置的 regex detector 发送请求。以下示例向名为 regex 的正则表达式发送请求,以标记个人识别信息。

    GORCH_ROUTE=$(oc get routes guardrails-orchestrator -o jsonpath='{.spec.host}')
    curl -X 'POST' "https://$GORCH_ROUTE/api/v2/text/detection/content" \
      -H 'accept: application/json' \
      -H 'Content-Type: application/json' \
      -d '{
      "detectors": {
        "regex": {"regex": ["email"]}
      },
      "content": "my email is test@domain.com"
    }' | jq

    响应示例

    {
      "detections": [
        {
          "start": 12,
          "end": 27,
          "text": "test@domain.com",
          "detection": "EmailAddress",
          "detection_type": "pii",
          "detector_id": "regex",
          "score": 1.0
        }
      ]
    }

7.4.2. 使用 guardrails 网关查询

guardrails 网关是一个 sidecar 镜像,可用于 GuardrailsOrchestrator 服务。它提供 OpenAI v1/chat/completions API,允许您指定您要用来访问该服务的检测器和端点。

先决条件

  • 您已配置了 guardrails 网关镜像。

流程

  1. 为检测器设置端点:

    GUARDRAILS_GATEWAY=https://$(oc get routes guardrails-gateway -o jsonpath='{.spec.host}')

    根据 配置 内置检测器和保护rails 网关 中提供的配置示例,使用 guardrails 的模型的可用端点为 $GUARDRAILS_GATEWAY/pii

  2. 使用 guardrails pii 端点查询模型:

    curl -v $GUARDRAILS_GATEWAY/pii/v1/chat/completions \
    -H "Content-Type: application/json" \
    -d '{
        "model": $MODEL,
        "messages": [
            {
                "role": "user",
                "content": "btw here is my social 123456789"
            }
        ]
    }'

    响应示例

    Warning: Unsuitable input detected. Please check the detected entities on your input and try again with the unsuitable input removed.
    Input Detections:
       0) The regex detector flagged the following text: "123-45-6789"

Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2026 Red Hat
返回顶部