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 生成或文本分类,或两者都部署在命名空间中。
流程
在 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 部署
regex_gateway_images_cm.yaml配置映射:$ oc apply -f regex_gateway_images_cm.yaml -n <TEST_NAMESPACE>定义 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: true3 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:部署 guardrails 网关
detectors_cm.yaml配置映射:$ oc apply -f detectors_cm.yaml -n <TEST_NAMESPACE>指定您在
GuardrailsOrchestrator自定义资源(CR)中创建的ConfigMap对象。例如,使用以下内容创建一个名为orchestrator_cr.yaml的 YAML 文件:orchestrator_cr.yamlCR 示例apiVersion: trustyai.opendatahub.io/v1alpha1 kind: GuardrailsOrchestrator metadata: name: gorch-sample spec: orchestratorConfig: "fms-orchestr8-config-nlp" enableBuiltInDetectors: True1 enableGuardrailsGateway: True2 guardrailsGatewayConfig: "fms-orchestr8-config-gateway"3 replicas: 1部署编配器自定义资源。此步骤在命名空间中创建服务帐户、部署、服务和路由对象。
oc apply -f orchestrator_cr.yaml -n <TEST_NAMESPACE>
验证
使用编配器的
/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_generationLLM 协调来自regexdetector 的请求。
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 网关镜像。
流程
为检测器设置端点:
GUARDRAILS_GATEWAY=https://$(oc get routes guardrails-gateway -o jsonpath='{.spec.host}')根据 配置 内置检测器和保护rails 网关 中提供的配置示例,使用 guardrails 的模型的可用端点为
$GUARDRAILS_GATEWAY/pii。使用 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"