跳至主要内容

Layerup Security

Layerup 安全 集成允许您保护对任何 LangChain LLM、LLM 链或 LLM 代理的调用。LLM 对象围绕任何现有 LLM 对象进行包装,从而在用户和 LLM 之间提供安全的层。

虽然 Layerup 安全对象被设计为 LLM,但它本身并不是 LLM,它只是围绕 LLM 进行包装,使其能够适应与底层 LLM 相同的功能。

设置

首先,您需要从 Layerup 网站 获取 Layerup 安全帐户。

接下来,通过 仪表板 创建一个项目,并复制您的 API 密钥。建议将您的 API 密钥放在项目的环境中。

安装 Layerup 安全 SDK

npm install @layerup/layerup-security

并安装 LangChain 社区

npm install @langchain/community @langchain/core

现在,您可以开始使用 Layerup 安全来保护您的 LLM 调用!

import {
LayerupSecurity,
LayerupSecurityOptions,
} from "@langchain/community/llms/layerup_security";
import { GuardrailResponse } from "@layerup/layerup-security";
import { OpenAI } from "@langchain/openai";

// Create an instance of your favorite LLM
const openai = new OpenAI({
modelName: "gpt-3.5-turbo",
openAIApiKey: process.env.OPENAI_API_KEY,
});

// Configure Layerup Security
const layerupSecurityOptions: LayerupSecurityOptions = {
// Specify a LLM that Layerup Security will wrap around
llm: openai,

// Layerup API key, from the Layerup dashboard
layerupApiKey: process.env.LAYERUP_API_KEY,

// Custom base URL, if self hosting
layerupApiBaseUrl: "https://api.uselayerup.com/v1",

// List of guardrails to run on prompts before the LLM is invoked
promptGuardrails: [],

// List of guardrails to run on responses from the LLM
responseGuardrails: ["layerup.hallucination"],

// Whether or not to mask the prompt for PII & sensitive data before it is sent to the LLM
mask: false,

// Metadata for abuse tracking, customer tracking, and scope tracking.
metadata: { customer: "[email protected]" },

// Handler for guardrail violations on the response guardrails
handlePromptGuardrailViolation: (violation: GuardrailResponse) => {
if (violation.offending_guardrail === "layerup.sensitive_data") {
// Custom logic goes here
}

return {
role: "assistant",
content: `There was sensitive data! I cannot respond. Here's a dynamic canned response. Current date: ${Date.now()}`,
};
},

// Handler for guardrail violations on the response guardrails
handleResponseGuardrailViolation: (violation: GuardrailResponse) => ({
role: "assistant",
content: `Custom canned response with dynamic data! The violation rule was ${violation.offending_guardrail}.`,
}),
};

const layerupSecurity = new LayerupSecurity(layerupSecurityOptions);
const response = await layerupSecurity.invoke(
"Summarize this message: my name is Bob Dylan. My SSN is 123-45-6789."
);

API 参考


此页面是否有帮助?


您也可以留下详细的反馈 在 GitHub 上.