第 8 章 使用带有 TrustyAI 的 Llama Stack
本节包含在 TrustyAI 中使用 Llama Stack 的教程。这些教程演示了如何使用各种 Llama Stack 组件和提供程序来评估和使用语言模型。
以下小节介绍了如何使用 Llama Stack 并提供示例用例:
- 使用带有 TrustyAI 中的 lm-evaluation-harness 的 Llama Stack 外部评估供应商
- 使用 LM-Eval Llama Stack 外部评估供应商运行自定义评估
- 使用带有 Llama Stack 的 trustyai-fms Guardrails Orchestrator
include::upstream-modules/ using-llama-stack-external-evaluation-with-lm-evaluation-harness-in-TrustyAI.adoc[leveloffset=+1]
include::upstream-modules/ running-custom-evaluations-with-LMEval-and-llama-stack.adoc[leveloffset=+1]
8.1. 在 Llama Stack 中使用 Guardrails Orchestrator 复制链接链接已复制到粘贴板!
trustyai_fms 编配器服务器是 Llama Stack 的外部供应商,允许您通过 Llama Stack API 配置和使用 Guardrails Orchestrator 和兼容检测模型。这种 Llama Stack 的实现将 Guardrails Orchestrator 与一组社区开发的检测器相结合,以提供强大的内容过滤和安全监控。
本例演示了如何使用内置的 Guardrails Regex Detector 和 Guardrails Orchestrator 作为 Llama Stack Security guardrails,使用 LlamaStack Operator 在您的 Red Hat OpenShift AI 命名空间中部署分发。
先决条件
- 具有集群管理员特权。
- 您已下载并安装 OpenShift 命令行界面 (CLI)。如需更多信息 ,请参阅安装 OpenShift CLI。
- 您有一个大型语言模型(LLM),用于 chat 生成或文本分类,或两者都部署在命名空间中。
集群管理员已在 OpenShift 中安装以下 Operator:
- Red Hat OpenShift Service Mesh 版本 2.6.7-0 或更高版本。
- Red Hat OpenShift Serverless 版本 1.35.1 或更高版本。
- Red Hat Authorino Operator,版本 1.2.1 或更高版本。
流程
使用
DataScienceCluster中的以下配置,配置 OpenShift AI 环境。请注意,您必须手动将spec.llamastack.managementState字段更新为Managed:spec: trustyai: managementState: Managed llamastack: managementState: Managed kserve: defaultDeploymentMode: RawDeployment managementState: Managed nim: managementState: Managed rawDeploymentServiceConfig: Headless serving: ingressGateway: certificate: type: OpenshiftDefaultIngress managementState: Removed name: knative-serving serviceMesh: managementState: Removed在 OpenShift AI 命名空间中创建项目:
PROJECT_NAME="lls-minimal-example" oc new-project $PROJECT_NAME通过为基于 regex 的 PII 检测应用编配器配置,使用 regex 检测器部署 Guardrails Orchestrator:
cat <<EOF | oc apply -f - kind: ConfigMap apiVersion: v1 metadata: name: fms-orchestr8-config-nlp data: config.yaml: | detectors: regex: type: text_contents service: hostname: "127.0.0.1" port: 8080 chunker_id: whole_doc_chunker default_threshold: 0.5 --- apiVersion: trustyai.opendatahub.io/v1alpha1 kind: GuardrailsOrchestrator metadata: name: guardrails-orchestrator spec: orchestratorConfig: "fms-orchestr8-config-nlp" enableBuiltInDetectors: true enableGuardrailsGateway: false replicas: 1 EOF在同一命名空间中,创建一个 Llama Stack 发行版:
apiVersion: llamastack.io/v1alpha1 kind: LlamaStackDistribution metadata: name: llamastackdistribution-sample namespace: <PROJECT_NAMESPACE> spec: replicas: 1 server: containerSpec: env: - name: VLLM_URL value: '${VLLM_URL}' - name: INFERENCE_MODEL value: '${INFERENCE_MODEL}' - name: MILVUS_DB_PATH value: '~/.llama/milvus.db' - name: VLLM_TLS_VERIFY value: 'false' - name: FMS_ORCHESTRATOR_URL value: '${FMS_ORCHESTRATOR_URL}' name: llama-stack port: 8321 distribution: name: rh-dev storage: size: 20Gi
部署了 LlamaStackDistribution CR 后,会在同一命名空间中创建新 pod。此 pod 为您的分发运行 LlamaStack 服务器。to to-
-
Llama Stack 服务器运行后,使用
/v1/shields端点动态注册屏蔽。例如,注册使用正则表达式模式检测个人可识别的信息(PII)的 shield。 打开 port-forward 以在本地访问它:
oc -n $PROJECT_NAME port-forward svc/llama-stack 8321:8321使用
/v1/shields端点动态注册 shield。例如,注册使用正则表达式模式检测个人可识别的信息(PII)的 shield:curl -X POST http://localhost:8321/v1/shields \ -H 'Content-Type: application/json' \ -d '{ "shield_id": "regex_detector", "provider_shield_id": "regex_detector", "provider_id": "trustyai_fms", "params": { "type": "content", "confidence_threshold": 0.5, "message_types": ["system", "user"], "detectors": { "regex": { "detector_params": { "regex": ["email", "us-social-security-number", "credit-card"] } } } } }'验证 shield 是否已注册:
curl -s http://localhost:8321/v1/shields | jq '.'以下输出表示 shield 已被成功注册:
{ "data": [ { "identifier": "regex_detector", "provider_resource_id": "regex_detector", "provider_id": "trustyai_fms", "type": "shield", "params": { "type": "content", "confidence_threshold": 0.5, "message_types": [ "system", "user" ], "detectors": { "regex": { "detector_params": { "regex": [ "email", "us-social-security-number", "credit-card" ] } } } } } ] }注册 shield 后,通过将包含 PII 的信息发送到
/v1/safety/run-shield端点来验证它是否正常工作:电子邮件检测示例:
curl -X POST http://localhost:8321/v1/safety/run-shield \ -H "Content-Type: application/json" \ -d '{ "shield_id": "regex_detector", "messages": [ { "content": "My email is test@example.com", "role": "user" } ] }' | jq '.'这应该返回一个响应,表示检测到电子邮件:
{ "violation": { "violation_level": "error", "user_message": "Content violation detected by shield regex_detector (confidence: 1.00, 1/1 processed messages violated)", "metadata": { "status": "violation", "shield_id": "regex_detector", "confidence_threshold": 0.5, "summary": { "total_messages": 1, "processed_messages": 1, "skipped_messages": 0, "messages_with_violations": 1, "messages_passed": 0, "message_fail_rate": 1.0, "message_pass_rate": 0.0, "total_detections": 1, "detector_breakdown": { "active_detectors": 1, "total_checks_performed": 1, "total_violations_found": 1, "violations_per_message": 1.0 } }, "results": [ { "message_index": 0, "text": "My email is test@example.com", "status": "violation", "score": 1.0, "detection_type": "pii", "individual_detector_results": [ { "detector_id": "regex", "status": "violation", "score": 1.0, "detection_type": "pii" } ] } ] } } }社交安全号码(SSN)检测示例:
curl -X POST http://localhost:8321/v1/safety/run-shield \ -H "Content-Type: application/json" \ -d '{ "shield_id": "regex_detector", "messages": [ { "content": "My SSN is 123-45-6789", "role": "user" } ] }' | jq '.'这应该会返回一个响应,表示检测到 SSN:
{ "violation": { "violation_level": "error", "user_message": "Content violation detected by shield regex_detector (confidence: 1.00, 1/1 processed messages violated)", "metadata": { "status": "violation", "shield_id": "regex_detector", "confidence_threshold": 0.5, "summary": { "total_messages": 1, "processed_messages": 1, "skipped_messages": 0, "messages_with_violations": 1, "messages_passed": 0, "message_fail_rate": 1.0, "message_pass_rate": 0.0, "total_detections": 1, "detector_breakdown": { "active_detectors": 1, "total_checks_performed": 1, "total_violations_found": 1, "violations_per_message": 1.0 } }, "results": [ { "message_index": 0, "text": "My SSN is 123-45-6789", "status": "violation", "score": 1.0, "detection_type": "pii", "individual_detector_results": [ { "detector_id": "regex", "status": "violation", "score": 1.0, "detection_type": "pii" } ] } ] } } }信用卡检测示例:
curl -X POST http://localhost:8321/v1/safety/run-shield \ -H "Content-Type: application/json" \ -d '{ "shield_id": "regex_detector", "messages": [ { "content": "My credit card number is 4111-1111-1111-1111", "role": "user" } ] }' | jq '.'这应该返回一个响应,表示检测到信用卡号码:
{ "violation": { "violation_level": "error", "user_message": "Content violation detected by shield regex_detector (confidence: 1.00, 1/1 processed messages violated)", "metadata": { "status": "violation", "shield_id": "regex_detector", "confidence_threshold": 0.5, "summary": { "total_messages": 1, "processed_messages": 1, "skipped_messages": 0, "messages_with_violations": 1, "messages_passed": 0, "message_fail_rate": 1.0, "message_pass_rate": 0.0, "total_detections": 1, "detector_breakdown": { "active_detectors": 1, "total_checks_performed": 1, "total_violations_found": 1, "violations_per_message": 1.0 } }, "results": [ { "message_index": 0, "text": "My credit card number is 4111-1111-1111-1111", "status": "violation", "score": 1.0, "detection_type": "pii", "individual_detector_results": [ { "detector_id": "regex", "status": "violation", "score": 1.0, "detection_type": "pii" } ] } ] } } }