Layerup Security
Layerup Security 整合讓您可以保護對任何 LangChain LLM、LLM 鏈或 LLM 代理程式的呼叫。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
# Create an instance of your favorite LLM
openai = OpenAI(
model_name="gpt-3.5-turbo",
openai_api_key="OPENAI_API_KEY",
)
# Configure Layerup Security
layerup_security = LayerupSecurity(
# Specify a LLM that Layerup Security will wrap around
llm=openai,
# Layerup API key, from the Layerup dashboard
layerup_api_key="LAYERUP_API_KEY",
# Custom base URL, if self hosting
layerup_api_base_url="https://api.uselayerup.com/v1",
# List of guardrails to run on prompts before the LLM is invoked
prompt_guardrails=[],
# List of guardrails to run on responses from the LLM
response_guardrails=["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": "example@uselayerup.com"},
# Handler for guardrail violations on the prompt guardrails
handle_prompt_guardrail_violation=(
lambda violation: {
"role": "assistant",
"content": (
"There was sensitive data! I cannot respond. "
"Here's a dynamic canned response. Current date: {}"
).format(datetime.now())
}
if violation["offending_guardrail"] == "layerup.sensitive_data"
else None
),
# Handler for guardrail violations on the response guardrails
handle_response_guardrail_violation=(
lambda violation: {
"role": "assistant",
"content": (
"Custom canned response with dynamic data! "
"The violation rule was {}."
).format(violation["offending_guardrail"])
}
),
)
response = layerup_security.invoke(
"Summarize this message: my name is Bob Dylan. My SSN is 123-45-6789."
)
API 參考:LayerupSecurity | OpenAI