validation

package
v0.0.26 Latest Latest
Warning

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

Go to latest
Published: Jan 3, 2025 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Initialize

func Initialize(c *config.Config) error

Initialize initializes the validation middleware with configuration

func ValidateCompletion

func ValidateCompletion(next http.Handler) http.Handler

ValidateCompletion validates completion request bodies

func ValidateOptions

func ValidateOptions(opts *Options) error

ValidateOptions performs comprehensive validation of request options

Types

type APIError added in v0.0.19

type APIError struct {
	Type       string                  `json:"type"`                 // Error type (e.g., "validation_error")
	Message    string                  `json:"message"`              // High-level error message
	RequestID  string                  `json:"request_id"`           // For error tracking
	Code       int                     `json:"code"`                 // HTTP status code
	Details    []ValidationErrorDetail `json:"details,omitempty"`    // Detailed validation errors
	Suggestion string                  `json:"suggestion,omitempty"` // Helpful suggestion for fixing the error
}

type CacheOptions

type CacheOptions struct {
	Enable  bool          `json:"enable"`
	Type    string        `json:"type" validate:"omitempty,oneof=memory redis file"`
	TTL     time.Duration `json:"ttl" validate:"omitempty,gt=0"`
	MaxSize int64         `json:"max_size" validate:"omitempty,gt=0"`
	Dir     string        `json:"dir" validate:"omitempty,required_if=Type file,dir"`
	Redis   *RedisOptions `json:"redis" validate:"omitempty,required_if=Type redis"`
}

CacheOptions represents caching configuration for requests

type CompletionRequest

type CompletionRequest struct {
	Messages []Message `json:"messages,omitempty" validate:"omitempty,dive"`
	Input    string    `json:"input,omitempty" validate:"omitempty"`
	Options  *Options  `json:"options,omitempty" validate:"omitempty"`
}

CompletionRequest represents the expected schema for completion requests

type Message

type Message struct {
	Role    string `json:"role" validate:"required,oneof=user assistant system"`
	Content string `json:"content" validate:"required,min=1"`
}

Message represents a single message in a completion request

type Options

type Options struct {
	Temperature      float64       `json:"temperature,omitempty" validate:"omitempty,gte=0,lte=1"`
	MaxTokens        int           `json:"max_tokens,omitempty" validate:"omitempty,gt=0"`
	TopP             float64       `json:"top_p,omitempty" validate:"omitempty,gt=0,lte=1"`
	FrequencyPenalty float64       `json:"frequency_penalty,omitempty" validate:"omitempty,gte=-2,lte=2"`
	PresencePenalty  float64       `json:"presence_penalty,omitempty" validate:"omitempty,gte=-2,lte=2"`
	Cache            *CacheOptions `json:"cache,omitempty" validate:"omitempty"`
	Retry            *RetryOptions `json:"retry,omitempty" validate:"omitempty"`
}

Options represents optional parameters for completion requests

type RedisOptions

type RedisOptions struct {
	Address  string `json:"address" validate:"required,hostname_port"`
	Password string `json:"password" validate:"omitempty"`
	DB       int    `json:"db" validate:"gte=0"`
}

RedisOptions represents Redis-specific configuration

type RetryOptions

type RetryOptions struct {
	MaxRetries      int           `json:"max_retries" validate:"gt=0"`
	InitialDelay    time.Duration `json:"initial_delay" validate:"required,gt=0"`
	MaxDelay        time.Duration `json:"max_delay" validate:"required,gtfield=InitialDelay"`
	Multiplier      float64       `json:"multiplier" validate:"gt=1"`
	RetryableErrors []string      `json:"retryable_errors" validate:"required,min=1,dive,oneof=rate_limit timeout server_error"`
}

RetryOptions represents retry configuration for failed requests

type TokenCounter

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

TokenCounter handles token counting for messages using tiktoken

func NewTokenCounter

func NewTokenCounter(model string) (*TokenCounter, error)

NewTokenCounter creates a new token counter for the specified model

func (*TokenCounter) CountRequestTokens

func (tc *TokenCounter) CountRequestTokens(req CompletionRequest) int

CountRequestTokens counts the total number of tokens in a completion request

func (*TokenCounter) CountTokens

func (tc *TokenCounter) CountTokens(msg Message) int

CountTokens counts the total number of tokens in a message

func (*TokenCounter) ValidateTokens

func (tc *TokenCounter) ValidateTokens(req CompletionRequest, maxContextTokens int) error

ValidateTokens checks if the request's token count is within limits

type Tokenizer

type Tokenizer interface {
	Encode(text string, allowedSpecial, disallowedSpecial []string) []int
	Decode(tokens []int) string
	CountTokens(text string) int
}

Tokenizer defines the interface for token counting

type ValidationErrorDetail added in v0.0.19

type ValidationErrorDetail struct {
	Field   string `json:"field"`           // The field that failed validation
	Message string `json:"message"`         // Human-readable error message
	Code    string `json:"code"`            // Machine-readable error code
	Value   string `json:"value,omitempty"` // The invalid value (if safe to return)
}

Jump to

Keyboard shortcuts

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