processor

package
v0.3.6 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2025 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

ABOUTME: Structured output processing for LLMs with prompt enhancement. ABOUTME: Schema-guided generation with caching and optimization strategies. Package processor implements structured output processing functionality for LLM interactions. It provides prompt enhancement, schema-guided generation, response extraction, and performance optimizations through caching and pooling strategies.

Core features:

  • Prompt enhancement for structured outputs
  • JSON extraction from LLM responses
  • Schema validation and caching
  • String builder pooling for performance
  • Configuration-based processing options

Package processor implements structured output processing functionality

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EnhancePromptWithSchema

func EnhancePromptWithSchema(prompt string, schema *schemaDomain.Schema) (string, error)

EnhancePromptWithSchema is a convenience function that enhances a prompt with schema information Optimized to use the singleton enhancer instance for better performance

func EstimateSchemaCapacity

func EstimateSchemaCapacity(schema *schemaDomain.Schema, prompt string, includeSchemaJSON bool, schemaJSONLength int) int

EstimateSchemaCapacity calculates a more accurate initial capacity for a schema based on its complexity and structure, returning the estimated capacity in bytes

func ExtractJSON

func ExtractJSON(s string) string

ExtractJSON is an optimized version of JSON extraction that handles various formats It uses a tiered approach, starting with fast methods and falling back to more complex ones

func GenerateSchemaKey

func GenerateSchemaKey(schema *schemaDomain.Schema) uint64

GenerateSchemaKey creates a hash key for a schema This is used for cache lookups to avoid repeated JSON marshaling

func GetDefaultPromptEnhancer

func GetDefaultPromptEnhancer() schemaDomain.PromptEnhancer

GetDefaultPromptEnhancer returns the singleton instance of the PromptEnhancer This avoids repeatedly creating new enhancer instances

func ImprovedGenerateSchemaKey

func ImprovedGenerateSchemaKey(schema *schemaDomain.Schema) uint64

ImprovedGenerateSchemaKey creates a hash key for a schema with better coverage of schema structures This handles conditional schemas, enums, nested structures, and is less sensitive to property order

func NewPromptEnhancer

func NewPromptEnhancer() domain.PromptEnhancer

NewPromptEnhancer creates a new prompt enhancer

func NewStructuredProcessor

func NewStructuredProcessor(validator schemaDomain.Validator) domain.Processor

NewStructuredProcessor creates a new structured processor

Types

type CacheEntry

type CacheEntry struct {
	Value      []byte    // The JSON data
	LastAccess time.Time // Last time this entry was accessed
}

CacheEntry represents a single entry in the schema cache with expiration

type Hashtable64

type Hashtable64 interface {
	hash.Hash
	Sum64() uint64
}

Hashtable64 interface to make testing easier

type OptimizedStringBuilder

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

OptimizedStringBuilder provides an enhanced string builder with better capacity estimation for complex schemas and prompt enhancement operations

func NewOptimizedBuilder

func NewOptimizedBuilder(initialCapacity int) *OptimizedStringBuilder

NewOptimizedBuilder creates a new OptimizedStringBuilder with the given initial capacity

func NewSchemaPromptBuilder

func NewSchemaPromptBuilder(prompt string, schema *schemaDomain.Schema, schemaJSONLength int) *OptimizedStringBuilder

NewSchemaPromptBuilder creates an OptimizedStringBuilder with capacity optimized for a schema prompt

func NewSchemaWithOptionsBuilder

func NewSchemaWithOptionsBuilder(basePrompt string, options map[string]interface{}) *OptimizedStringBuilder

NewSchemaWithOptionsBuilder creates an OptimizedStringBuilder with capacity optimized for a schema prompt with options

func (*OptimizedStringBuilder) Cap

func (b *OptimizedStringBuilder) Cap() int

Cap returns the current capacity of the builder

func (*OptimizedStringBuilder) Grow

func (b *OptimizedStringBuilder) Grow(n int)

Grow increases the builder's capacity

func (*OptimizedStringBuilder) Len

func (b *OptimizedStringBuilder) Len() int

Len returns the current length of the content

func (*OptimizedStringBuilder) Reset

func (b *OptimizedStringBuilder) Reset()

Reset resets the builder to empty

func (*OptimizedStringBuilder) String

func (b *OptimizedStringBuilder) String() string

String returns the built string

func (*OptimizedStringBuilder) Write

func (b *OptimizedStringBuilder) Write(p []byte) (int, error)

Write adds a byte slice to the builder

func (*OptimizedStringBuilder) WriteString

func (b *OptimizedStringBuilder) WriteString(s string) (int, error)

WriteString adds a string to the builder

type PromptEnhancer

type PromptEnhancer struct {
}

PromptEnhancer adds schema information to prompts

func (*PromptEnhancer) Enhance

func (p *PromptEnhancer) Enhance(prompt string, schema *schemaDomain.Schema) (string, error)

Enhance adds schema information to a prompt - optimized version

func (*PromptEnhancer) EnhanceWithOptions

func (p *PromptEnhancer) EnhanceWithOptions(prompt string, schema *schemaDomain.Schema, options map[string]interface{}) (string, error)

EnhanceWithOptions adds schema information to a prompt with additional options - optimized version

type SchemaCache

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

SchemaCache provides caching for schema JSON to avoid repeated marshaling

func NewSchemaCache

func NewSchemaCache() *SchemaCache

NewSchemaCache creates a new schema cache with a default capacity

func NewSchemaCacheWithOptions

func NewSchemaCacheWithOptions(maxSize int, expirationTime time.Duration) *SchemaCache

NewSchemaCacheWithOptions creates a new schema cache with custom settings

func (*SchemaCache) Clear

func (c *SchemaCache) Clear()

Clear empties the cache

func (*SchemaCache) Get

func (c *SchemaCache) Get(key uint64) ([]byte, bool)

Get retrieves cached schema JSON for the given key Returns the cached JSON and a boolean indicating if it was found

func (*SchemaCache) GetAverageAccessTime

func (c *SchemaCache) GetAverageAccessTime() time.Duration

GetAverageAccessTime returns the average time taken to access the cache

func (*SchemaCache) GetHitRate

func (c *SchemaCache) GetHitRate() (hits int64, misses int64, total int64)

GetHitRate returns the current cache hit/miss statistics

func (*SchemaCache) GetHitRateValue

func (c *SchemaCache) GetHitRateValue() float64

GetHitRateValue returns the current cache hit rate as a float64

func (*SchemaCache) GetSize

func (c *SchemaCache) GetSize() int64

GetSize returns the number of entries in the cache

func (*SchemaCache) ResetMetrics

func (c *SchemaCache) ResetMetrics()

ResetMetrics resets only the metrics while keeping the cache contents

func (*SchemaCache) Set

func (c *SchemaCache) Set(key uint64, value []byte)

Set stores schema JSON in the cache

type StructuredProcessor

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

StructuredProcessor handles processing of structured LLM outputs

func (*StructuredProcessor) Process

func (p *StructuredProcessor) Process(schema *schemaDomain.Schema, output string) (interface{}, error)

Process processes a raw output string against a schema

func (*StructuredProcessor) ProcessTyped

func (p *StructuredProcessor) ProcessTyped(schema *schemaDomain.Schema, output string, target interface{}) error

ProcessTyped processes a raw output string against a schema and maps to a target type

func (*StructuredProcessor) ToJSON

func (p *StructuredProcessor) ToJSON(obj interface{}) (string, error)

ToJSON converts an object to a JSON string

Jump to

Keyboard shortcuts

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