TypeScript
使用 LangChain JavaScript 集成 Arcjet 脱敏聊天模型。
npm install @arcjet/redact
npm install @langchain/community @langchain/core
import { ArcjetRedact, ArcjetSensitiveInfoType, } from "@langchain/community/chat_models/arcjet"; import { ChatOpenAI } from "@langchain/openai"; // 创建另一个聊天模型实例供 Arcjet 包装 const openai = new ChatOpenAI({ temperature: 0.8, model: "gpt-3.5-turbo-0125", }); const arcjetRedactOptions = { // 指定一个 LLM,Arcjet Redact 将在对输入进行脱敏后调用它。 chatModel: openai, // 指定应进行脱敏的实体列表。 // 如果未指定,则所有实体都将被脱敏。 entities: ["email", "phone-number", "ip-address", "custom-entity"] as ArcjetSensitiveInfoType[], // 您可以提供自定义检测函数来检测我们尚不支持的实体。 // 它接受一个 token 列表,并返回已识别类型的列表或 undefined。 // 如果使用了返回的 undefined 类型,则应将其添加到实体列表中。 detect: (tokens: string[]) => { return tokens.map((t) => t === "some-sensitive-info" ? "custom-entity" : undefined) }, // 提供给自定义检测函数的 token 数量。默认为 1。 // 它可用于在检测自定义实体类型时提供额外的上下文。 contextWindowSize: 1, // 这允许您在脱敏时提供自定义替换。请确保 // 替换是唯一的,以便反脱敏按预期工作。 replace: (identifiedType: string) => { return identifiedType === "email" ? "redacted@example.com" : undefined; }, }; const arcjetRedact = new ArcjetRedact(arcjetRedactOptions); const response = await arcjetRedact.invoke( "My email address is test@example.com, here is some-sensitive-info" );