以编程方式使用文档

LangChain 通过 @langchain/openai package.

> OpenAI 是美国人工智能(AI)研究实验室 > 包括非营利组织 OpenAI Incorporated > 及其营利性子公司 OpenAI Limited Partnership. > OpenAI 开展 AI 研究,宣称目的是促进和发展友好的 AI。 > OpenAI 系统运行在 Azure超级计算平台上 Microsoft.

> OpenAI API 由具有不同能力和价格的各种模型提供支持。 > > ChatGPT 是由 OpenAI.

安装和设置

  • - 获取 OpenAI API 密钥并将其设置为环境变量(OPENAI_API_KEY)

聊天模型

查看 使用示例.

LLM

查看 使用示例.

npm install @langchain/openai @langchain/core

文本嵌入模型

查看 使用示例

中间件

专为 OpenAI 模型设计的中间件。了解更多关于 中间件.

中间件描述
内容审核使用 OpenAI 的审核端点来审核代理流量

内容审核

使用 OpenAI 的审核端点来审核代理流量(用户输入、模型输出和工具结果),以检测和处理不安全的内容。内容审核适用于以下情况:

  • - 需要内容安全和合规性的应用程序
  • - 过滤有害、仇恨或不当内容
  • - 需要安全防护的客户面向代理
  • - 满足平台审核要求

API 参考: openAIModerationMiddleware

const agent = createAgent({
  model: "openai:gpt-5.5",
  tools: [searchTool, databaseTool],
  middleware: [
    openAIModerationMiddleware({
      model: "openai:gpt-5.5",
      moderationModel: "omni-moderation-latest",
      checkInput: true,
      checkOutput: true,
      exitBehavior: "end",
    }),
  ],
});

Configuration options

用于审核的 OpenAI 模型。可以是模型名称字符串(例如, "openai:gpt-5.5") or a BaseChatModel 实例。中间件将使用此模型的客户端访问审核端点。

要使用的 OpenAI 审核模型。选项: 'omni-moderation-latest', 'omni-moderation-2024-09-26', 'text-moderation-latest', 'text-moderation-stable'

是否在调用模型之前检查用户输入消息

是否在调用模型之后检查模型输出消息

是否在调用模型之前检查工具结果消息

如何处理内容被标记时的违规行为。选项:

  • - 'end' - 立即终止代理执行并返回违规消息
  • - 'error' - 抛出 OpenAIModerationError 异常
  • - 'replace' - 用违规消息替换被标记的内容并继续执行

违规消息的自定义模板。支持模板变量:

  • - {categories} - 被标记类别的逗号分隔列表
  • - {category_scores} - 类别评分的 JSON 字符串
  • - {original_content} - 原始被标记的内容

Default: "I'm sorry, but I can't comply with that request. It was flagged for {categories}."

Full example

此中间件集成了 OpenAI 的内容审核端点,用于在不同阶段检查内容:

审核阶段: - checkInput - 模型调用前的用户消息 - checkOutput - 模型调用后的 AI 消息 - checkToolResults - 模型调用前的工具输出

退出行为: - 'end' (默认)- 停止执行并返回违规消息 - 'error' - 抛出异常供应用程序处理 - 'replace' - 替换被标记的内容并继续执行

// Basic moderation
const agent = createAgent({
  model: "openai:gpt-5.5",
  tools: [searchTool, customerDataTool],
  middleware: [
    openAIModerationMiddleware({
      model: "openai:gpt-5.5",
      moderationModel: "omni-moderation-latest",
      checkInput: true,
      checkOutput: true,
    }),
  ],
});

// Strict moderation with custom message
const agentStrict = createAgent({
  model: "openai:gpt-5.5",
  tools: [searchTool, customerDataTool],
  middleware: [
    openAIModerationMiddleware({
      model: "openai:gpt-5.5",
      moderationModel: "omni-moderation-latest",
      checkInput: true,
      checkOutput: true,
      checkToolResults: true,
      exitBehavior: "error",
      violationMessage:
        "Content policy violation detected: {categories}. " +
        "Please rephrase your request.",
    }),
  ],
});

// Moderation with replacement behavior
const agentReplace = createAgent({
  model: "openai:gpt-5.5",
  tools: [searchTool],
  middleware: [
    openAIModerationMiddleware({
      model: "openai:gpt-5.5",
      checkInput: true,
      exitBehavior: "replace",
      violationMessage: "[Content removed due to safety policies]",
    }),
  ],
});