保障AI应用安全:OpenAI Agents SDK防护栏功能完整使用手册

【免费下载链接】openai-agents-js A lightweight, powerful framework for multi-agent workflows and voice agents 【免费下载链接】openai-agents-js 项目地址: https://gitcode.com/gh_mirrors/ope/openai-agents-js

OpenAI Agents SDK是一个轻量级且功能强大的多智能体工作流和语音智能体框架,其防护栏功能为AI应用提供了全面的安全保障。本文将详细介绍如何使用这一功能,帮助开发者轻松构建安全可靠的AI应用。

为什么AI应用安全防护至关重要 🛡️

随着AI技术的快速发展,AI应用在各个领域得到广泛应用。然而,AI应用也面临着诸多安全风险,如敏感信息泄露、不当内容生成等。OpenAI Agents SDK的防护栏功能正是为了解决这些问题而设计,它可以在AI应用的输入和输出环节进行安全检查,有效防范安全风险。

AI应用安全防护

OpenAI Agents SDK防护栏功能概述

OpenAI Agents SDK的防护栏功能主要包括输入防护栏和输出防护栏两种类型。输入防护栏用于检查和过滤输入到AI应用的数据,防止恶意输入或敏感信息进入;输出防护栏则用于检查AI应用生成的输出结果,确保输出内容符合安全规范。

输入防护栏:阻止恶意输入和敏感信息

输入防护栏是保障AI应用安全的第一道防线。通过定义输入防护栏,开发者可以对输入数据进行严格的检查和过滤。

定义输入防护栏的基本方法

使用defineToolInputGuardrail函数可以定义一个输入防护栏。以下是一个简单的示例,用于阻止包含敏感信息(如API密钥)的输入:

const blockSecrets = defineToolInputGuardrail({
  name: 'block_secrets',
  run: async ({ toolCall }) => {
    const args = JSON.parse(toolCall.arguments) as { text?: string };
    if (args.text?.includes('sk-')) {
      return ToolGuardrailFunctionOutputFactory.rejectContent(
        'Remove secrets before calling this tool.',
      );
    }
    return ToolGuardrailFunctionOutputFactory.allow();
  },
});

在这个示例中,blockSecrets防护栏会检查输入数据中是否包含sk-(通常是API密钥的开头),如果包含则拒绝该输入。

在工具中应用输入防护栏

定义好输入防护栏后,可以将其应用到具体的工具中。例如,在classify_text工具中应用blockSecrets防护栏:

const classifyTool = tool({
  name: 'classify_text',
  description: 'Classify text for internal routing.',
  parameters: z.object({
    text: z.string(),
  }),
  inputGuardrails: [blockSecrets],
  execute: ({ text }) => `length:${text.length}`,
});

这样,当调用classify_text工具时,会先经过blockSecrets防护栏的检查。

输出防护栏:确保输出内容安全合规

输出防护栏用于检查AI应用生成的输出结果,防止不当内容或敏感信息被输出。

定义输出防护栏的基本方法

使用defineToolOutputGuardrail函数可以定义一个输出防护栏。以下是一个示例,用于检查输出结果中是否包含敏感信息:

const redactOutput = defineToolOutputGuardrail({
  name: 'redact_output',
  run: async ({ output }) => {
    const text = String(output ?? '');
    if (text.includes('sk-')) {
      return ToolGuardrailFunctionOutputFactory.rejectContent(
        'Output contained sensitive data.',
      );
    }
    return ToolGuardrailFunctionOutputFactory.allow();
  },
});

在这个示例中,redactOutput防护栏会检查输出结果中是否包含sk-,如果包含则拒绝输出该内容。

在工具中应用输出防护栏

与输入防护栏类似,输出防护栏也可以应用到工具中。例如,在classify_text工具中同时应用输入防护栏和输出防护栏:

const classifyTool = tool({
  name: 'classify_text',
  description: 'Classify text for internal routing.',
  parameters: z.object({
    text: z.string(),
  }),
  inputGuardrails: [blockSecrets],
  outputGuardrails: [redactOutput],
  execute: ({ text }) => `length:${text.length}`,
});

这样,classify_text工具在执行过程中,既会检查输入数据,也会检查输出结果。

多防护栏组合使用:增强安全防护效果

为了提高AI应用的安全性,可以组合使用多个防护栏。例如,可以定义多个输入防护栏和输出防护栏,对输入和输出进行多维度的检查。

多输入防护栏的应用

定义多个输入防护栏,并将它们同时应用到工具中:

const firstInputGuardrail = defineToolInputGuardrail({
  name: 'first_input_guardrail',
  run: async ({ toolCall }) => {
    // 检查逻辑1
    return ToolGuardrailFunctionOutputFactory.allow();
  },
});

const secondInputGuardrail = defineToolInputGuardrail({
  name: 'second_input_guardrail',
  run: async ({ toolCall }) => {
    // 检查逻辑2
    return ToolGuardrailFunctionOutputFactory.allow();
  },
});

const multiGuardrailTool = tool({
  name: 'multi_guardrail_tool',
  description: 'Tool with multiple input guardrails',
  parameters: z.object({ text: z.string() }),
  inputGuardrails: [firstInputGuardrail, secondInputGuardrail],
  execute: ({ text }) => text,
});

当调用multiGuardrailTool工具时,会依次执行firstInputGuardrailsecondInputGuardrail防护栏的检查,只有所有防护栏都允许通过,工具才会执行。

多输出防护栏的应用

同样,也可以定义多个输出防护栏并组合使用:

const firstOutputGuardrail = defineToolOutputGuardrail({
  name: 'first_output_guardrail',
  run: async ({ output }) => {
    // 检查逻辑1
    return ToolGuardrailFunctionOutputFactory.allow();
  },
});

const secondOutputGuardrail = defineToolOutputGuardrail({
  name: 'second_output_guardrail',
  run: async ({ output }) => {
    // 检查逻辑2
    return ToolGuardrailFunctionOutputFactory.allow();
  },
});

const multiOutputGuardrailTool = tool({
  name: 'multi_output_guardrail_tool',
  description: 'Tool with multiple output guardrails',
  parameters: z.object({ text: z.string() }),
  outputGuardrails: [firstOutputGuardrail, secondOutputGuardrail],
  execute: ({ text }) => text,
});

防护栏在智能体中的应用

防护栏不仅可以应用到工具中,还可以在智能体层面进行配置,对整个智能体的输入和输出进行安全防护。

创建带有防护栏的智能体

const agent = new Agent({
  name: 'Classifier',
  instructions: 'Classify incoming text.',
  tools: [classifyTool],
});

在这个示例中,智能体Classifier使用了带有输入和输出防护栏的classifyTool工具,从而确保在处理文本分类任务时的安全性。

实际应用案例:文本分类工具的安全防护

以下是一个完整的文本分类工具安全防护案例,展示了如何结合输入防护栏和输出防护栏来保障AI应用的安全。

import {
  Agent,
  ToolGuardrailFunctionOutputFactory,
  defineToolInputGuardrail,
  defineToolOutputGuardrail,
  tool,
} from '@openai/agents';
import { z } from 'zod';

// 定义输入防护栏,阻止包含敏感信息的输入
const blockSecrets = defineToolInputGuardrail({
  name: 'block_secrets',
  run: async ({ toolCall }) => {
    const args = JSON.parse(toolCall.arguments) as { text?: string };
    if (args.text?.includes('sk-')) {
      return ToolGuardrailFunctionOutputFactory.rejectContent(
        'Remove secrets before calling this tool.',
      );
    }
    return ToolGuardrailFunctionOutputFactory.allow();
  },
});

// 定义输出防护栏,检查输出结果中是否包含敏感信息
const redactOutput = defineToolOutputGuardrail({
  name: 'redact_output',
  run: async ({ output }) => {
    const text = String(output ?? '');
    if (text.includes('sk-')) {
      return ToolGuardrailFunctionOutputFactory.rejectContent(
        'Output contained sensitive data.',
      );
    }
    return ToolGuardrailFunctionOutputFactory.allow();
  },
});

// 创建带有防护栏的文本分类工具
const classifyTool = tool({
  name: 'classify_text',
  description: 'Classify text for internal routing.',
  parameters: z.object({
    text: z.string(),
  }),
  inputGuardrails: [blockSecrets],
  outputGuardrails: [redactOutput],
  execute: ({ text }) => `length:${text.length}`,
});

// 创建智能体并使用该工具
const agent = new Agent({
  name: 'Classifier',
  instructions: 'Classify incoming text.',
  tools: [classifyTool],
});

通过这个案例,我们可以看到防护栏功能如何有效地阻止敏感信息的输入和输出,保障AI应用的安全。

AI安全防护案例

总结:构建安全可靠的AI应用

OpenAI Agents SDK的防护栏功能为AI应用提供了全面的安全保障。通过定义输入防护栏和输出防护栏,开发者可以有效地防范敏感信息泄露、恶意输入等安全风险。在实际应用中,建议根据具体需求组合使用多个防护栏,以增强安全防护效果。

要开始使用OpenAI Agents SDK的防护栏功能,你可以克隆仓库:https://gitcode.com/gh_mirrors/ope/openai-agents-js,然后参考examples/docs/guardrails/toolGuardrails.ts中的示例代码进行开发。

希望本文能够帮助你更好地理解和使用OpenAI Agents SDK的防护栏功能,构建安全可靠的AI应用!

【免费下载链接】openai-agents-js A lightweight, powerful framework for multi-agent workflows and voice agents 【免费下载链接】openai-agents-js 项目地址: https://gitcode.com/gh_mirrors/ope/openai-agents-js

Logo

小龙虾开发者社区是 CSDN 旗下专注 OpenClaw 生态的官方阵地,聚焦技能开发、插件实践与部署教程,为开发者提供可直接落地的方案、工具与交流平台,助力高效构建与落地 AI 应用

更多推荐