#github.com/xulang-org/packages/guardrails
Xu 0.16.0 API
import: guardrails #github.com/xulang-org/packages/guardrails
counts: 15 functions, 0 methods, 5 types, 0 constants, 0 method sets
External package `guardrails #github.com/xulang-org/packages/guardrails`, provided by github.com/xulang-org/packages.
github.com/xulang-org/packages · 1.17.2 · Install and use
struct Finding
pub struct Finding {
pub rule string
pub severity string
pub snippet string
}
Purpose
Structure encapsulating rule, severity, snippet.
Public fields
rule string- field (string)
severity string- field (string)
snippet string- field (string)
struct CheckResult
pub struct CheckResult {
pub allowed bool
pub sanitized_text string
pub findings []Finding
pub reason string
}
Purpose
Structure encapsulating allowed, sanitized_text, findings, reason.
Public fields
allowed bool- field (bool)
sanitized_text string- field (string)
findings []Finding- field ([]Finding)
reason string- field (string)
struct Policy
pub struct Policy {
pub block_injection bool
pub block_secrets bool
pub redact_pii bool
pub blocked_terms []string
pub strict_mode bool
}
Purpose
Structure encapsulating block_injection, block_secrets, redact_pii, blocked_terms, strict_mode.
Public fields
block_injection bool- field (bool)
block_secrets bool- field (bool)
redact_pii bool- field (bool)
blocked_terms []string- field ([]string)
strict_mode bool- field (bool)
struct RedTeamCase
pub struct RedTeamCase {
pub id string
pub kind string
pub payload string
}
Purpose
Structure encapsulating id, kind, payload.
Public fields
id string- field (string)
kind string- kind tag
payload string- field (string)
struct RedTeamReport
pub struct RedTeamReport {
pub total int
pub blocked int
pub passed_ids []string
pub missed_ids []string
pub block_rate f64
}
Purpose
Structure encapsulating total, blocked, passed_ids, missed_ids, block_rate.
Public fields
total int- field (int)
blocked int- field (int)
passed_ids []string- field ([]string)
missed_ids []string- field ([]string)
block_rate f64- field (f64)
default_policy
pub fn default_policy() Policy
Purpose
Default policy.
Parameters
No arguments are required.
Returns
Policy — a value of this type
strict_policy
pub fn strict_policy() Policy
Purpose
Strict policy.
Parameters
No arguments are required.
Returns
Policy — a value of this type
normalize_for_detection
pub fn normalize_for_detection(text &string) string
Purpose
Normalize for detection.
Parameters
text &string- text (read-only borrow; caller keeps the original; type &string)
Returns
string — result string
redact_pii
pub fn redact_pii(text &string) CheckResult
Purpose
Redact pii.
Parameters
text &string- text (read-only borrow; caller keeps the original; type &string)
Returns
CheckResult — a value of this type
detect_prompt_injection
pub fn detect_prompt_injection(text &string) []Finding
Purpose
Detect prompt injection.
Parameters
text &string- text (read-only borrow; caller keeps the original; type &string)
Returns
[]Finding — slice result
check_input
pub fn check_input(policy &Policy, text &string) CheckResult
Purpose
Check input.
Parameters
policy &Policy- policy (read-only borrow; caller keeps the original; type &Policy)
text &string- text (read-only borrow; caller keeps the original; type &string)
Returns
CheckResult — a value of this type
check_output
pub fn check_output(policy &Policy, text &string) CheckResult
Purpose
Check output.
Parameters
policy &Policy- policy (read-only borrow; caller keeps the original; type &Policy)
text &string- text (read-only borrow; caller keeps the original; type &string)
Returns
CheckResult — a value of this type
check_tool_output
pub fn check_tool_output(policy &Policy, tool_name &string, output &string) CheckResult
Purpose
Check tool output.
Parameters
policy &Policy- policy (read-only borrow; caller keeps the original; type &Policy)
tool_name &string- tool_name (read-only borrow; caller keeps the original; type &string)
output &string- output (read-only borrow; caller keeps the original; type &string)
Returns
CheckResult — a value of this type
check_groundedness
pub fn check_groundedness(answer &string, context_chunks &[]string) f64
Purpose
Check groundedness.
Parameters
answer &string- answer (read-only borrow; caller keeps the original; type &string)
context_chunks &[]string- context_chunks (read-only borrow; caller keeps the original; type &[]string)
Returns
f64 — a value of this type
run_red_team
pub fn run_red_team(policy &Policy, cases &[]RedTeamCase) RedTeamReport
Purpose
Run red team.
Parameters
policy &Policy- policy (read-only borrow; caller keeps the original; type &Policy)
cases &[]RedTeamCase- cases (read-only borrow; caller keeps the original; type &[]RedTeamCase)
Returns
RedTeamReport — a value of this type
inject_canary
pub fn inject_canary(system_prompt string, canary_token string) string
Purpose
Inject canary.
Parameters
system_prompt string- system_prompt (string)
canary_token string- canary_token (string)
Returns
string — result string
detect_canary_leak
pub fn detect_canary_leak(output &string, canary_token &string) CheckResult
Purpose
Detect canary leak.
Parameters
output &string- output (read-only borrow; caller keeps the original; type &string)
canary_token &string- canary_token (read-only borrow; caller keeps the original; type &string)
Returns
CheckResult — a value of this type
sanitize_rag_chunks
pub fn sanitize_rag_chunks(policy &Policy, chunks &[]string) []string
Purpose
Sanitize rag chunks.
Parameters
policy &Policy- policy (read-only borrow; caller keeps the original; type &Policy)
chunks &[]string- chunks (read-only borrow; caller keeps the original; type &[]string)
Returns
[]string — slice result
check_safety_openai
pub fn check_safety_openai(client &openai.Client, model string, text &string) CheckResult
Purpose
Check safety openai. Throws on failure; handle the error or propagate it to the caller.
Parameters
client &openai.Client- client (read-only borrow; caller keeps the original; type &openai.Client)
model string- model (string)
text &string- text (read-only borrow; caller keeps the original; type &string)
Returns
CheckResult — a value of this type
check_input_strict
pub fn check_input_strict(policy &Policy, client &openai.Client, model string, text &string) CheckResult
Purpose
Check input strict. Throws on failure; handle the error or propagate it to the caller.
Parameters
policy &Policy- policy (read-only borrow; caller keeps the original; type &Policy)
client &openai.Client- client (read-only borrow; caller keeps the original; type &openai.Client)
model string- model (string)
text &string- text (read-only borrow; caller keeps the original; type &string)
Returns
CheckResult — a value of this type