Documentation
¶
Overview ¶
Package guardrails provides built-in ProviderHook implementations that replace the legacy validator system with hook-based guardrails.
Index ¶
- func NewGuardrailHook(typeName string, params map[string]any) (hooks.ProviderHook, error)
- type BannedWordsHook
- func (h *BannedWordsHook) AfterCall(_ context.Context, _ *hooks.ProviderRequest, resp *hooks.ProviderResponse) hooks.Decision
- func (h *BannedWordsHook) BeforeCall(_ context.Context, _ *hooks.ProviderRequest) hooks.Decision
- func (h *BannedWordsHook) Name() string
- func (h *BannedWordsHook) OnChunk(_ context.Context, chunk *providers.StreamChunk) hooks.Decision
- type LengthHook
- func (h *LengthHook) AfterCall(_ context.Context, _ *hooks.ProviderRequest, resp *hooks.ProviderResponse) hooks.Decision
- func (h *LengthHook) BeforeCall(_ context.Context, _ *hooks.ProviderRequest) hooks.Decision
- func (h *LengthHook) Name() string
- func (h *LengthHook) OnChunk(_ context.Context, chunk *providers.StreamChunk) hooks.Decision
- type MaxSentencesHook
- type RequiredFieldsHook
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewGuardrailHook ¶
NewGuardrailHook creates a guardrail ProviderHook from a validator config type name and params map. This is used by ProviderStage to instantiate pack-defined guardrails from metadata.
Types ¶
type BannedWordsHook ¶
type BannedWordsHook struct {
// contains filtered or unexported fields
}
BannedWordsHook denies provider responses that contain any of the configured banned words. It implements both ProviderHook and ChunkInterceptor for streaming support.
func NewBannedWordsHook ¶
func NewBannedWordsHook(words []string) *BannedWordsHook
NewBannedWordsHook creates a guardrail that rejects responses containing any of the given words. Matching is case-insensitive with word boundaries.
func (*BannedWordsHook) AfterCall ¶
func (h *BannedWordsHook) AfterCall( _ context.Context, _ *hooks.ProviderRequest, resp *hooks.ProviderResponse, ) hooks.Decision
AfterCall checks the completed response for banned words.
func (*BannedWordsHook) BeforeCall ¶
func (h *BannedWordsHook) BeforeCall( _ context.Context, _ *hooks.ProviderRequest, ) hooks.Decision
BeforeCall is a no-op — banned words are checked after generation.
func (*BannedWordsHook) Name ¶
func (h *BannedWordsHook) Name() string
Name returns the guardrail type identifier.
func (*BannedWordsHook) OnChunk ¶
func (h *BannedWordsHook) OnChunk( _ context.Context, chunk *providers.StreamChunk, ) hooks.Decision
OnChunk checks accumulated streaming content for banned words.
type LengthHook ¶
type LengthHook struct {
// contains filtered or unexported fields
}
LengthHook denies provider responses that exceed character or token limits. It implements both ProviderHook and ChunkInterceptor for streaming support.
func NewLengthHook ¶
func NewLengthHook(maxCharacters, maxTokens int) *LengthHook
NewLengthHook creates a guardrail that rejects responses exceeding the given character and/or token limits. Pass 0 to disable a limit.
func (*LengthHook) AfterCall ¶
func (h *LengthHook) AfterCall( _ context.Context, _ *hooks.ProviderRequest, resp *hooks.ProviderResponse, ) hooks.Decision
AfterCall checks the completed response against length limits.
func (*LengthHook) BeforeCall ¶
func (h *LengthHook) BeforeCall( _ context.Context, _ *hooks.ProviderRequest, ) hooks.Decision
BeforeCall is a no-op — length is checked after generation.
func (*LengthHook) Name ¶
func (h *LengthHook) Name() string
Name returns the guardrail type identifier.
func (*LengthHook) OnChunk ¶
func (h *LengthHook) OnChunk( _ context.Context, chunk *providers.StreamChunk, ) hooks.Decision
OnChunk checks accumulated streaming content against length limits.
type MaxSentencesHook ¶
type MaxSentencesHook struct {
// contains filtered or unexported fields
}
MaxSentencesHook denies provider responses that contain more than a configured number of sentences. It does not support streaming because sentence boundaries can only be reliably counted on complete content.
func NewMaxSentencesHook ¶
func NewMaxSentencesHook(maxSentences int) *MaxSentencesHook
NewMaxSentencesHook creates a guardrail that rejects responses exceeding the given sentence count.
func (*MaxSentencesHook) AfterCall ¶
func (h *MaxSentencesHook) AfterCall( _ context.Context, _ *hooks.ProviderRequest, resp *hooks.ProviderResponse, ) hooks.Decision
AfterCall checks the completed response for sentence count.
func (*MaxSentencesHook) BeforeCall ¶
func (h *MaxSentencesHook) BeforeCall( _ context.Context, _ *hooks.ProviderRequest, ) hooks.Decision
BeforeCall is a no-op — sentence count is checked after generation.
func (*MaxSentencesHook) Name ¶
func (h *MaxSentencesHook) Name() string
Name returns the guardrail type identifier.
type RequiredFieldsHook ¶
type RequiredFieldsHook struct {
// contains filtered or unexported fields
}
RequiredFieldsHook denies provider responses that do not contain all required field strings. It does not support streaming because all content must be available to check for field presence.
func NewRequiredFieldsHook ¶
func NewRequiredFieldsHook(fields []string) *RequiredFieldsHook
NewRequiredFieldsHook creates a guardrail that rejects responses missing any of the given field strings.
func (*RequiredFieldsHook) AfterCall ¶
func (h *RequiredFieldsHook) AfterCall( _ context.Context, _ *hooks.ProviderRequest, resp *hooks.ProviderResponse, ) hooks.Decision
AfterCall checks the completed response for required fields.
func (*RequiredFieldsHook) BeforeCall ¶
func (h *RequiredFieldsHook) BeforeCall( _ context.Context, _ *hooks.ProviderRequest, ) hooks.Decision
BeforeCall is a no-op — field presence is checked after generation.
func (*RequiredFieldsHook) Name ¶
func (h *RequiredFieldsHook) Name() string
Name returns the guardrail type identifier.