Documentation
¶
Index ¶
- func Initialize(c *config.Config) error
- func ValidateCompletion(next http.Handler) http.Handler
- func ValidateOptions(opts *Options) error
- type APIError
- type CacheOptions
- type CompletionRequest
- type Message
- type Options
- type RedisOptions
- type RetryOptions
- type TokenCounter
- type Tokenizer
- type ValidationErrorDetail
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Initialize ¶
Initialize initializes the validation middleware with configuration
func ValidateCompletion ¶
ValidateCompletion validates completion request bodies
func ValidateOptions ¶
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)
}