Skip to main content

Layerup Security

Layerup Security 集成允许您保护对任何 LangChain LLM、LLM chain 或 LLM agent 的调用。LLM 对象包装任何现有的 LLM 对象,为您的用户与 LLM 之间提供了一个安全层。

虽然 Layerup Security 对象被设计为 LLM,但它实际上并不是一个 LLM,它只是包装了一个 LLM,使其能够适应与基础 LLM 相同的功能。

设置

首先,您需要从 Layerup 网站 创建一个 Layerup Security 账户。

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

安装 Layerup Security SDK:

pip install LayerupSecurity

并安装 LangChain Community:

pip install langchain-community

现在,您准备好使用 Layerup Security 保护您的 LLM 调用!

from langchain_community.llms.layerup_security import LayerupSecurity
from langchain_openai import OpenAI

# 创建您最喜欢的 LLM 的实例
openai = OpenAI(
model_name="gpt-3.5-turbo",
openai_api_key="OPENAI_API_KEY",
)

# 配置 Layerup Security
layerup_security = LayerupSecurity(
# 指定 Layerup Security 将包装的 LLM
llm=openai,

# Layerup API 密钥,来自 Layerup 仪表板
layerup_api_key="LAYERUP_API_KEY",

# 自托管时的自定义基础 URL
layerup_api_base_url="https://api.uselayerup.com/v1",

# 在调用 LLM 之前在提示上运行的护栏列表
prompt_guardrails=[],

# 在 LLM 响应中运行的护栏列表
response_guardrails=["layerup.hallucination"],

# 在发送到 LLM 之前是否掩盖 PII 和敏感数据
mask=False,

# 用于滥用跟踪、客户跟踪和范围跟踪的元数据
metadata={"customer": "[email protected]"},

# 提示护栏违规的处理程序
handle_prompt_guardrail_violation=(
lambda violation: {
"role": "assistant",
"content": (
"有敏感数据!我无法回应。"
"这是一个动态的 canned 响应。当前日期:{}"
).format(datetime.now())
}
if violation["offending_guardrail"] == "layerup.sensitive_data"
else None
),

# 响应护栏违规的处理程序
handle_response_guardrail_violation=(
lambda violation: {
"role": "assistant",
"content": (
"带有动态数据的自定义 canned 响应!"
"违规规则是 {}."
).format(violation["offending_guardrail"])
}
),
)

response = layerup_security.invoke(
"总结这个消息:我的名字是 Bob Dylan。我的社会安全号码是 123-45-6789。"
)

相关


此页面是否有帮助?


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