guardrails

package
v0.1.13 Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Action

type Action string

Action represents the action to take when a guardrail is triggered

const (
	// BlockAction blocks the request or response
	BlockAction Action = "block"

	// RedactAction redacts the sensitive content
	RedactAction Action = "redact"

	// WarnAction allows the content but logs a warning
	WarnAction Action = "warn"
)

type ContentFilter

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

ContentFilter implements a guardrail that filters inappropriate content

func NewContentFilter

func NewContentFilter(blockedWords []string, action Action) *ContentFilter

NewContentFilter creates a new content filter guardrail

func (*ContentFilter) Action

func (c *ContentFilter) Action() Action

Action returns the action to take when the guardrail is triggered

func (*ContentFilter) CheckRequest

func (c *ContentFilter) CheckRequest(ctx context.Context, request string) (bool, string, error)

CheckRequest checks if a request violates the guardrail

func (*ContentFilter) CheckResponse

func (c *ContentFilter) CheckResponse(ctx context.Context, response string) (bool, string, error)

CheckResponse checks if a response violates the guardrail

func (*ContentFilter) Type

func (c *ContentFilter) Type() GuardrailType

Type returns the type of guardrail

type Guardrail

type Guardrail interface {
	// Type returns the type of guardrail
	Type() GuardrailType

	// CheckRequest checks if a request violates the guardrail
	CheckRequest(ctx context.Context, request string) (bool, string, error)

	// CheckResponse checks if a response violates the guardrail
	CheckResponse(ctx context.Context, response string) (bool, string, error)

	// Action returns the action to take when the guardrail is triggered
	Action() Action
}

Guardrail represents a guardrail that can be applied to requests and responses

type GuardrailType

type GuardrailType string

GuardrailType represents the type of guardrail

const (
	// ContentFilterGuardrail filters content for inappropriate material
	ContentFilterGuardrail GuardrailType = "content_filter"

	// TokenLimitGuardrail limits the number of tokens in a request or response
	TokenLimitGuardrail GuardrailType = "token_limit"

	// PiiFilterGuardrail filters personally identifiable information
	PiiFilterGuardrail GuardrailType = "pii_filter"

	// ToolRestrictionGuardrail restricts which tools can be used
	ToolRestrictionGuardrail GuardrailType = "tool_restriction"

	// RateLimitGuardrail limits the rate of requests
	RateLimitGuardrail GuardrailType = "rate_limit"
)

type LLMMiddleware

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

LLMMiddleware implements middleware for LLM calls

func NewLLMMiddleware

func NewLLMMiddleware(llm interfaces.LLM, pipeline *Pipeline) *LLMMiddleware

NewLLMMiddleware creates a new LLM middleware

func (*LLMMiddleware) Generate

func (m *LLMMiddleware) Generate(ctx context.Context, prompt string, options map[string]interface{}) (string, error)

Generate generates text from a prompt

type PiiFilter

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

PiiFilter implements a guardrail that filters personally identifiable information

func NewPiiFilter

func NewPiiFilter(action Action) *PiiFilter

NewPiiFilter creates a new PII filter guardrail

func (*PiiFilter) Action

func (p *PiiFilter) Action() Action

Action returns the action to take when the guardrail is triggered

func (*PiiFilter) CheckRequest

func (p *PiiFilter) CheckRequest(ctx context.Context, request string) (bool, string, error)

CheckRequest checks if a request violates the guardrail

func (*PiiFilter) CheckResponse

func (p *PiiFilter) CheckResponse(ctx context.Context, response string) (bool, string, error)

CheckResponse checks if a response violates the guardrail

func (*PiiFilter) Type

func (p *PiiFilter) Type() GuardrailType

Type returns the type of guardrail

type Pipeline

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

Pipeline represents a pipeline of guardrails

func NewPipeline

func NewPipeline(guardrails []Guardrail, logger logging.Logger) *Pipeline

NewPipeline creates a new guardrails pipeline

func (*Pipeline) AddGuardrail

func (p *Pipeline) AddGuardrail(guardrail Guardrail)

AddGuardrail adds a guardrail to the pipeline

func (*Pipeline) ProcessRequest

func (p *Pipeline) ProcessRequest(ctx context.Context, request string) (string, error)

ProcessRequest processes a request through the guardrails pipeline

func (*Pipeline) ProcessResponse

func (p *Pipeline) ProcessResponse(ctx context.Context, response string) (string, error)

ProcessResponse processes a response through the guardrails pipeline

type RateLimit

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

RateLimit implements a guardrail that limits the rate of requests

func NewRateLimit

func NewRateLimit(requestsPerMinute int, action Action) *RateLimit

NewRateLimit creates a new rate limit guardrail

func (*RateLimit) Action

func (r *RateLimit) Action() Action

Action returns the action to take when the guardrail is triggered

func (*RateLimit) CheckRequest

func (r *RateLimit) CheckRequest(ctx context.Context, request string) (bool, string, error)

CheckRequest checks if a request violates the guardrail

func (*RateLimit) CheckResponse

func (r *RateLimit) CheckResponse(ctx context.Context, response string) (bool, string, error)

CheckResponse checks if a response violates the guardrail

func (*RateLimit) Type

func (r *RateLimit) Type() GuardrailType

Type returns the type of guardrail

type SimpleTokenCounter

type SimpleTokenCounter struct{}

SimpleTokenCounter implements a simple token counter

func (*SimpleTokenCounter) CountTokens

func (s *SimpleTokenCounter) CountTokens(text string) (int, error)

CountTokens counts tokens in text (simple approximation)

type TokenCounter

type TokenCounter interface {
	CountTokens(text string) (int, error)
}

TokenCounter is an interface for counting tokens in text

type TokenLimit

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

TokenLimit implements a guardrail that limits the number of tokens

func NewTokenLimit

func NewTokenLimit(maxTokens int, counter TokenCounter, action Action, truncateMode string) *TokenLimit

NewTokenLimit creates a new token limit guardrail

func (*TokenLimit) Action

func (t *TokenLimit) Action() Action

Action returns the action to take when the guardrail is triggered

func (*TokenLimit) CheckRequest

func (t *TokenLimit) CheckRequest(ctx context.Context, request string) (bool, string, error)

CheckRequest checks if a request violates the guardrail

func (*TokenLimit) CheckResponse

func (t *TokenLimit) CheckResponse(ctx context.Context, response string) (bool, string, error)

CheckResponse checks if a response violates the guardrail

func (*TokenLimit) Type

func (t *TokenLimit) Type() GuardrailType

Type returns the type of guardrail

type ToolMiddleware

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

ToolMiddleware implements middleware for tool calls

func NewToolMiddleware

func NewToolMiddleware(tool interfaces.Tool, pipeline *Pipeline) *ToolMiddleware

NewToolMiddleware creates a new tool middleware

func (*ToolMiddleware) Description

func (m *ToolMiddleware) Description() string

Description returns a description of what the tool does

func (*ToolMiddleware) Name

func (m *ToolMiddleware) Name() string

Name returns the name of the tool

func (*ToolMiddleware) Parameters

func (m *ToolMiddleware) Parameters() map[string]interfaces.ParameterSpec

Parameters returns the parameters that the tool accepts

func (*ToolMiddleware) Run

func (m *ToolMiddleware) Run(ctx context.Context, input string) (string, error)

Run executes the tool with the given input

type ToolRestriction

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

ToolRestriction implements a guardrail that restricts which tools can be used

func NewToolRestriction

func NewToolRestriction(allowedTools []string, action Action) *ToolRestriction

NewToolRestriction creates a new tool restriction guardrail

func (*ToolRestriction) Action

func (t *ToolRestriction) Action() Action

Action returns the action to take when the guardrail is triggered

func (*ToolRestriction) CheckRequest

func (t *ToolRestriction) CheckRequest(ctx context.Context, request string) (bool, string, error)

CheckRequest checks if a request violates the guardrail

func (*ToolRestriction) CheckResponse

func (t *ToolRestriction) CheckResponse(ctx context.Context, response string) (bool, string, error)

CheckResponse checks if a response violates the guardrail

func (*ToolRestriction) Type

func (t *ToolRestriction) Type() GuardrailType

Type returns the type of guardrail

Jump to

Keyboard shortcuts

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