guardrails

package
v0.5.10 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 13, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package guardrails 提供 Agent 输出质量护栏

与 security/guard 不同,guardrails 关注的是输出质量而非安全性:

  • 函数式检查:长度、关键词、格式、自定义规则
  • LLM-as-Judge:使用 LLM 评估输出质量
  • 幻觉检测:检测输出是否包含虚构信息
  • 相关性检查:检测输出是否与输入相关

使用示例:

rail := NewGuardrail(
    WithLengthCheck(50, 2000),
    WithKeywordCheck([]string{"错误", "无法"}),
    WithLLMJudge(llmProvider, "model-name"),
)
result, err := rail.Check(ctx, input, output)
if !result.Passed {
    // 输出质量不合格,需要重新生成
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CheckDetail

type CheckDetail struct {
	// Name 检查项名称
	Name string `json:"name"`

	// Passed 是否通过
	Passed bool `json:"passed"`

	// Score 分数
	Score float64 `json:"score"`

	// Message 说明信息
	Message string `json:"message,omitempty"`
}

CheckDetail 单项检查详情

type CheckResult

type CheckResult struct {
	// Passed 是否通过
	Passed bool `json:"passed"`

	// Score 质量分数 (0-1,越高质量越好)
	Score float64 `json:"score"`

	// Reason 不通过的原因
	Reason string `json:"reason,omitempty"`

	// Suggestions 改进建议
	Suggestions []string `json:"suggestions,omitempty"`

	// Details 详细检查结果
	Details []CheckDetail `json:"details,omitempty"`
}

CheckResult 检查结果

type Checker

type Checker interface {
	// Name 检查器名称
	Name() string

	// Check 执行检查
	// input 是用户输入,output 是 Agent 输出
	Check(ctx context.Context, input, output string) (*CheckDetail, error)
}

Checker 单项检查器接口

type Guardrail

type Guardrail struct {
	// contains filtered or unexported fields
}

Guardrail 输出护栏 组合多个 Checker 对 Agent 输出进行质量检查

func NewGuardrail

func NewGuardrail(opts ...GuardrailOption) *Guardrail

NewGuardrail 创建输出护栏

func (*Guardrail) Check

func (g *Guardrail) Check(ctx context.Context, input, output string) (*CheckResult, error)

Check 执行所有检查

type GuardrailOption

type GuardrailOption func(*Guardrail)

GuardrailOption 护栏配置选项

func WithChecker

func WithChecker(checker Checker) GuardrailOption

WithChecker 添加自定义检查器

func WithFailFast

func WithFailFast(failFast bool) GuardrailOption

WithFailFast 设置失败快速返回

func WithFormatCheck

func WithFormatCheck(format OutputFormat) GuardrailOption

WithFormatCheck 添加格式检查

func WithHallucinationCheck

func WithHallucinationCheck(provider llm.Provider, model string, context string) GuardrailOption

WithHallucinationCheck 添加幻觉检测

func WithKeywordCheck

func WithKeywordCheck(forbidden []string) GuardrailOption

WithKeywordCheck 添加关键词检查(输出不应包含这些关键词)

func WithLLMJudge

func WithLLMJudge(provider llm.Provider, model string) GuardrailOption

WithLLMJudge 添加 LLM-as-Judge 检查

func WithLengthCheck

func WithLengthCheck(minLen, maxLen int) GuardrailOption

WithLengthCheck 添加长度检查

func WithRelevanceCheck

func WithRelevanceCheck(provider llm.Provider, model string) GuardrailOption

WithRelevanceCheck 添加相关性检查

func WithThreshold

func WithThreshold(threshold float64) GuardrailOption

WithThreshold 设置通过阈值(0-1)

type OutputFormat

type OutputFormat int

OutputFormat 输出格式类型

const (
	// FormatJSON JSON 格式
	FormatJSON OutputFormat = iota

	// FormatMarkdown Markdown 格式
	FormatMarkdown

	// FormatPlainText 纯文本格式
	FormatPlainText
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL