Documentation
¶
Overview ¶
Package scorer provides a production-ready Go library for scoring text content using OpenAI's GPT API to determine relevance for various use cases.
The library provides batch processing, concurrent execution, and flexible prompt customization with comprehensive error handling and resilience patterns.
Features:
- Batch processing of text items (10 items per API call for efficiency)
- Concurrent processing with configurable parallelism
- Interface-first design for testing and extensibility
- Custom prompt templates with Go template support
- Circuit breaker pattern for resilience
- Retry logic with exponential backoff
- Prometheus metrics integration
- Content validation and sanitization utilities
Basic usage:
cfg := scorer.Config{
OpenAIKey: os.Getenv("OPENAI_API_KEY"),
MaxConcurrent: 5,
}
s, err := scorer.NewScorer(cfg)
if err != nil {
log.Fatal(err)
}
results, err := s.ScoreTexts(ctx, items)
Index ¶
- Variables
- func CalculateRetryDelay(attempt int, config *RetryConfig) time.Duration
- func GetMetricsHandler() http.Handler
- func GetRetryStats(err error) (attempts int, finalError error)
- func IsRetryableError(err error) bool
- func RegisterCustomMetrics(collector prometheus.Collector) error
- func SanitizeContent(content string) string
- func ShouldTripCircuit(err error) bool
- func ValidateAndSanitize(items []TextItem, opts ValidationOptions) ([]TextItem, []ValidationResult, error)
- type CircuitBreakerConfig
- type CircuitBreakerWrapper
- func (w *CircuitBreakerWrapper) Counts() gobreaker.Counts
- func (w *CircuitBreakerWrapper) CreateChatCompletion(ctx context.Context, req openai.ChatCompletionRequest) (openai.ChatCompletionResponse, error)
- func (w *CircuitBreakerWrapper) GetHealth() HealthStatus
- func (w *CircuitBreakerWrapper) State() gobreaker.State
- type Config
- func (c Config) Validate() error
- func (c Config) WithCircuitBreaker() Config
- func (c Config) WithCircuitBreakerConfig(config *CircuitBreakerConfig) Config
- func (c Config) WithMaxConcurrent(max int) Config
- func (c Config) WithModel(model string) Config
- func (c Config) WithPromptTemplate(templateText string) Config
- func (c Config) WithRetry() Config
- func (c Config) WithRetryConfig(config *RetryConfig) Config
- func (c Config) WithRetryStrategy(strategy RetryStrategy, maxAttempts int) Config
- func (c Config) WithTimeout(timeout time.Duration) Config
- type HealthStatus
- type IntegratedScorer
- func (s *IntegratedScorer) GetHealth(ctx context.Context) HealthStatus
- func (s *IntegratedScorer) ScoreTexts(ctx context.Context, items []TextItem, opts ...ScoringOption) ([]ScoredItem, error)
- func (s *IntegratedScorer) ScoreTextsWithOptions(ctx context.Context, items []TextItem, opts ...ScoringOption) ([]ScoredItem, error)
- type MetricsRecorder
- func (m *MetricsRecorder) RecordAPICall(endpoint string, status string, seconds float64)
- func (m *MetricsRecorder) RecordBatchSize(size int)
- func (m *MetricsRecorder) RecordCircuitBreakerState(name string, state int)
- func (m *MetricsRecorder) RecordCircuitBreakerTrip(name string)
- func (m *MetricsRecorder) RecordConcurrentRequests(delta float64)
- func (m *MetricsRecorder) RecordError(errorType string)
- func (m *MetricsRecorder) RecordItemsScored(count int)
- func (m *MetricsRecorder) RecordQueuedRequests(delta float64)
- func (m *MetricsRecorder) RecordRequest(status string, model string)
- func (m *MetricsRecorder) RecordRequestDuration(seconds float64, model string)
- func (m *MetricsRecorder) RecordRetry(reason string)
- func (m *MetricsRecorder) RecordRetryAttempt(attempts int)
- func (m *MetricsRecorder) RecordScore(score int)
- func (m *MetricsRecorder) RecordTokensUsed(tokenType string, count int)
- type OpenAIClient
- type RetryConfig
- type RetryStrategy
- type RetryWrapper
- type ScoredItem
- type Scorer
- func BuildCustomScorer(cfg Config) (Scorer, error)
- func BuildProductionScorer(apiKey string) (Scorer, error)
- func CombineWithCircuitBreaker(scorer Scorer, retryConfig *RetryConfig, cbConfig *CircuitBreakerConfig) Scorer
- func NewCircuitBreakerScorer(scorer Scorer, config *CircuitBreakerConfig) Scorer
- func NewIntegratedScorer(cfg Config) (Scorer, error)
- func NewRetryScorer(scorer Scorer, config *RetryConfig) Scorer
- func NewScorer(cfg Config) (Scorer, error)
- func WithMetrics(scorer Scorer, metrics *MetricsRecorder) Scorer
- func WrapWithCircuitBreaker(scorer Scorer, config *CircuitBreakerConfig) Scorer
- type ScoringOption
- type ScoringOptions
- type TextItem
- type ValidationOptions
- type ValidationResult
Constants ¶
This section is empty.
Variables ¶
var ( ErrMissingAPIKey = errors.New("OpenAI API key is required") ErrInvalidConfig = errors.New("invalid configuration") ErrEmptyInput = errors.New("input items cannot be empty") ErrContentTooLong = errors.New("content exceeds maximum length") ErrContentTooShort = errors.New("content is too short") ErrContentWhitespace = errors.New("content contains only whitespace") )
Error definitions
Functions ¶
func CalculateRetryDelay ¶
func CalculateRetryDelay(attempt int, config *RetryConfig) time.Duration
CalculateRetryDelay calculates the delay for a given retry attempt
func GetMetricsHandler ¶
GetMetricsHandler returns an HTTP handler for Prometheus metrics
func GetRetryStats ¶
GetRetryStats returns statistics about retry operations
func IsRetryableError ¶
IsRetryableError determines if an error should trigger a retry
func RegisterCustomMetrics ¶
func RegisterCustomMetrics(collector prometheus.Collector) error
RegisterCustomMetrics allows registration of custom metrics
func SanitizeContent ¶ added in v0.12.0
SanitizeContent cleans and normalizes text content
func ShouldTripCircuit ¶
ShouldTripCircuit determines if an error should cause the circuit to trip
func ValidateAndSanitize ¶ added in v0.12.0
func ValidateAndSanitize(items []TextItem, opts ValidationOptions) ([]TextItem, []ValidationResult, error)
ValidateAndSanitize performs both validation and sanitization
Types ¶
type CircuitBreakerConfig ¶
type CircuitBreakerConfig struct {
MaxRequests uint32 // Max requests in half-open state
Interval time.Duration // Interval for closed state
Timeout time.Duration // Timeout for open state
ReadyToTrip func(counts gobreaker.Counts) bool // Custom trip condition
OnStateChange func(name string, from, to gobreaker.State) // State change callback
}
CircuitBreakerConfig holds circuit breaker settings
type CircuitBreakerWrapper ¶
type CircuitBreakerWrapper struct {
// contains filtered or unexported fields
}
CircuitBreakerWrapper wraps an OpenAI client with circuit breaker functionality
func NewCircuitBreakerWrapper ¶
func NewCircuitBreakerWrapper(client OpenAIClient, config *CircuitBreakerConfig) *CircuitBreakerWrapper
NewCircuitBreakerWrapper creates a new circuit breaker wrapper around an OpenAI client
func (*CircuitBreakerWrapper) Counts ¶
func (w *CircuitBreakerWrapper) Counts() gobreaker.Counts
Counts returns the current counts of the circuit breaker
func (*CircuitBreakerWrapper) CreateChatCompletion ¶
func (w *CircuitBreakerWrapper) CreateChatCompletion(ctx context.Context, req openai.ChatCompletionRequest) (openai.ChatCompletionResponse, error)
CreateChatCompletion executes the API call through the circuit breaker
func (*CircuitBreakerWrapper) GetHealth ¶
func (w *CircuitBreakerWrapper) GetHealth() HealthStatus
GetHealth returns the health status of the circuit breaker
func (*CircuitBreakerWrapper) State ¶
func (w *CircuitBreakerWrapper) State() gobreaker.State
State returns the current state of the circuit breaker
type Config ¶
type Config struct {
APIKey string // OpenAI API key (required)
Model string // OpenAI model to use
PromptText string // Custom prompt template
MaxConcurrent int // Maximum concurrent API calls
MaxContentLength int // Maximum content length per text item (0 = use default)
EnableCircuitBreaker bool // Enable circuit breaker pattern
EnableRetry bool // Enable retry with backoff
Timeout time.Duration // Request timeout
CircuitBreakerConfig *CircuitBreakerConfig // Circuit breaker configuration
RetryConfig *RetryConfig // Retry configuration
}
Config holds the configuration for the scorer
func NewDefaultConfig ¶
NewDefaultConfig creates a config with sensible defaults
func NewProductionConfig ¶
NewProductionConfig creates a production-ready config with all resilience features
func (Config) WithCircuitBreaker ¶
WithCircuitBreaker enables circuit breaker with default settings
func (Config) WithCircuitBreakerConfig ¶
func (c Config) WithCircuitBreakerConfig(config *CircuitBreakerConfig) Config
WithCircuitBreakerConfig enables circuit breaker with custom settings
func (Config) WithMaxConcurrent ¶
WithMaxConcurrent sets the maximum concurrent requests
func (Config) WithPromptTemplate ¶
WithPromptTemplate sets a custom prompt template
func (Config) WithRetryConfig ¶
func (c Config) WithRetryConfig(config *RetryConfig) Config
WithRetryConfig enables retry with custom settings
func (Config) WithRetryStrategy ¶
func (c Config) WithRetryStrategy(strategy RetryStrategy, maxAttempts int) Config
WithRetryStrategy enables retry with specified strategy
type HealthStatus ¶
type HealthStatus struct {
Healthy bool // Overall health status
Status string // Human-readable status message
Details map[string]interface{} // Additional health details
}
HealthStatus represents the health state of the scorer
type IntegratedScorer ¶
type IntegratedScorer struct {
// contains filtered or unexported fields
}
IntegratedScorer combines all resilience patterns and features
func (*IntegratedScorer) GetHealth ¶
func (s *IntegratedScorer) GetHealth(ctx context.Context) HealthStatus
GetHealth returns comprehensive health status
func (*IntegratedScorer) ScoreTexts ¶
func (s *IntegratedScorer) ScoreTexts(ctx context.Context, items []TextItem, opts ...ScoringOption) ([]ScoredItem, error)
ScoreTexts implements TextScorer with full integration
func (*IntegratedScorer) ScoreTextsWithOptions ¶
func (s *IntegratedScorer) ScoreTextsWithOptions(ctx context.Context, items []TextItem, opts ...ScoringOption) ([]ScoredItem, error)
ScoreTextsWithOptions implements TextScorer with metrics and monitoring
type MetricsRecorder ¶
type MetricsRecorder struct {
// contains filtered or unexported fields
}
MetricsRecorder provides methods to record metrics
func NewMetricsRecorder ¶
func NewMetricsRecorder(enabled bool) *MetricsRecorder
NewMetricsRecorder creates a new metrics recorder
func (*MetricsRecorder) RecordAPICall ¶
func (m *MetricsRecorder) RecordAPICall(endpoint string, status string, seconds float64)
RecordAPICall records an API call duration
func (*MetricsRecorder) RecordBatchSize ¶
func (m *MetricsRecorder) RecordBatchSize(size int)
RecordBatchSize records the size of a batch
func (*MetricsRecorder) RecordCircuitBreakerState ¶
func (m *MetricsRecorder) RecordCircuitBreakerState(name string, state int)
RecordCircuitBreakerState records circuit breaker state
func (*MetricsRecorder) RecordCircuitBreakerTrip ¶
func (m *MetricsRecorder) RecordCircuitBreakerTrip(name string)
RecordCircuitBreakerTrip records a circuit breaker trip
func (*MetricsRecorder) RecordConcurrentRequests ¶
func (m *MetricsRecorder) RecordConcurrentRequests(delta float64)
RecordConcurrentRequests updates concurrent request count
func (*MetricsRecorder) RecordError ¶
func (m *MetricsRecorder) RecordError(errorType string)
RecordError records an error
func (*MetricsRecorder) RecordItemsScored ¶
func (m *MetricsRecorder) RecordItemsScored(count int)
RecordItemsScored records the number of items scored
func (*MetricsRecorder) RecordQueuedRequests ¶
func (m *MetricsRecorder) RecordQueuedRequests(delta float64)
RecordQueuedRequests updates queued request count
func (*MetricsRecorder) RecordRequest ¶
func (m *MetricsRecorder) RecordRequest(status string, model string)
RecordRequest records a request metric
func (*MetricsRecorder) RecordRequestDuration ¶
func (m *MetricsRecorder) RecordRequestDuration(seconds float64, model string)
RecordRequestDuration records request duration
func (*MetricsRecorder) RecordRetry ¶
func (m *MetricsRecorder) RecordRetry(reason string)
RecordRetry records a retry
func (*MetricsRecorder) RecordRetryAttempt ¶
func (m *MetricsRecorder) RecordRetryAttempt(attempts int)
RecordRetryAttempt records retry attempts
func (*MetricsRecorder) RecordScore ¶
func (m *MetricsRecorder) RecordScore(score int)
RecordScore records a score
func (*MetricsRecorder) RecordTokensUsed ¶
func (m *MetricsRecorder) RecordTokensUsed(tokenType string, count int)
RecordTokensUsed records tokens used
type OpenAIClient ¶
type OpenAIClient interface {
CreateChatCompletion(context.Context, openai.ChatCompletionRequest) (openai.ChatCompletionResponse, error)
}
OpenAIClient defines the interface for interacting with OpenAI API
type RetryConfig ¶
type RetryConfig struct {
MaxAttempts int // Maximum number of retry attempts
Strategy RetryStrategy // Backoff strategy to use
InitialDelay time.Duration // Initial delay between retries
MaxDelay time.Duration // Maximum delay between retries
}
RetryConfig holds retry settings
type RetryStrategy ¶
type RetryStrategy string
RetryStrategy defines the backoff strategy for retries
const ( RetryStrategyExponential RetryStrategy = "exponential" RetryStrategyConstant RetryStrategy = "constant" RetryStrategyFibonacci RetryStrategy = "fibonacci" // Content length limits DefaultMaxContentLength = 10000 // Default maximum content length in characters MinContentLength = 1 // Minimum content length to be valid )
type RetryWrapper ¶
type RetryWrapper struct {
// contains filtered or unexported fields
}
RetryWrapper wraps an OpenAI client with retry logic
func NewRetryWrapper ¶
func NewRetryWrapper(client OpenAIClient, config *RetryConfig) *RetryWrapper
NewRetryWrapper creates a new retry wrapper around an OpenAI client
func (*RetryWrapper) CreateChatCompletion ¶
func (w *RetryWrapper) CreateChatCompletion(ctx context.Context, req openai.ChatCompletionRequest) (openai.ChatCompletionResponse, error)
CreateChatCompletion executes the API call with retry logic
type ScoredItem ¶
type ScoredItem struct {
Item TextItem // Original text item
Score int // Score between 0-100
Reason string // AI explanation for the score
}
ScoredItem represents a text item with its AI-generated score
type Scorer ¶
type Scorer interface {
// ScoreTexts scores a slice of text items
ScoreTexts(ctx context.Context, items []TextItem, opts ...ScoringOption) ([]ScoredItem, error)
// ScoreTextsWithOptions scores text items with runtime options
ScoreTextsWithOptions(ctx context.Context, items []TextItem, opts ...ScoringOption) ([]ScoredItem, error)
// GetHealth returns the current health status of the scorer
GetHealth(ctx context.Context) HealthStatus
}
Scorer provides methods to score generic text items
func BuildCustomScorer ¶
BuildCustomScorer creates a scorer with custom configuration
func BuildProductionScorer ¶
BuildProductionScorer creates a production-ready scorer with all features
func CombineWithCircuitBreaker ¶
func CombineWithCircuitBreaker(scorer Scorer, retryConfig *RetryConfig, cbConfig *CircuitBreakerConfig) Scorer
CombineWithCircuitBreaker creates a scorer with both retry and circuit breaker
func NewCircuitBreakerScorer ¶
func NewCircuitBreakerScorer(scorer Scorer, config *CircuitBreakerConfig) Scorer
NewCircuitBreakerScorer creates a new circuit breaker wrapper for a Scorer
func NewIntegratedScorer ¶
NewIntegratedScorer creates a fully integrated scorer with all features
func NewRetryScorer ¶
func NewRetryScorer(scorer Scorer, config *RetryConfig) Scorer
NewRetryScorer creates a new retry wrapper for a Scorer
func WithMetrics ¶
func WithMetrics(scorer Scorer, metrics *MetricsRecorder) Scorer
WithMetrics wraps any TextScorer with metrics recording
func WrapWithCircuitBreaker ¶
func WrapWithCircuitBreaker(scorer Scorer, config *CircuitBreakerConfig) Scorer
WrapWithCircuitBreaker wraps an existing Scorer with circuit breaker functionality
type ScoringOption ¶
type ScoringOption func(*scoringOptions)
ScoringOption is a functional option for configuring scoring behavior
func WithExtraContext ¶
func WithExtraContext(context map[string]interface{}) ScoringOption
WithExtraContext adds extra context data for template substitution
func WithModel ¶
func WithModel(model string) ScoringOption
WithModel sets the model for this scoring request
func WithPromptTemplate ¶
func WithPromptTemplate(prompt string) ScoringOption
WithPromptTemplate sets a custom prompt template for this scoring request
type ScoringOptions ¶
type ScoringOptions struct {
Model string // Model to use for this request
PromptText string // Custom prompt for this request
ExtraContext map[string]interface{} // Additional context data
}
ScoringOptions is the exported version for testing (uppercase)
type TextItem ¶
type TextItem struct {
ID string // Unique identifier for the text item
Content string // The text content to be scored
Metadata map[string]interface{} // Optional metadata for context
}
TextItem represents a generic text item to be scored
func SanitizeTextItems ¶ added in v0.12.0
SanitizeTextItems sanitizes content for a batch of text items
type ValidationOptions ¶ added in v0.12.0
type ValidationOptions struct {
MaxLength int
MinLength int
AllowEmpty bool
AllowWhitespace bool
TrimWhitespace bool
}
ValidationOptions configures content validation behavior
func DefaultValidationOptions ¶ added in v0.12.0
func DefaultValidationOptions() ValidationOptions
DefaultValidationOptions returns sensible defaults for content validation
type ValidationResult ¶ added in v0.12.0
ValidationResult contains the results of content validation
func ValidateContent ¶ added in v0.12.0
func ValidateContent(content string, opts ValidationOptions) ValidationResult
ValidateContent validates a single text item's content
func ValidateTextItems ¶ added in v0.12.0
func ValidateTextItems(items []TextItem, opts ValidationOptions) ([]ValidationResult, error)
ValidateTextItems validates a batch of text items