Documentation
¶
Index ¶
- Variables
- func AnalyzeComparisonResults[T any](results []ComparisonResult[T]) string
- func ChainOfThought(ctx context.Context, l LLM, question string, opts ...PromptOption) (string, error)
- func CleanResponse(response string) string
- func ExtractStructuredData[T any](ctx context.Context, l LLM, text string, opts ...PromptOption) (*T, error)
- func GenerateJSONSchema(v interface{}) ([]byte, error)
- func QuestionAnswer(ctx context.Context, l LLM, question string, opts ...PromptOption) (string, error)
- func Summarize(ctx context.Context, l LLM, text string, opts ...PromptOption) (string, error)
- func Validate(s interface{}) error
- type ComparisonResult
- type Config
- type ConfigOption
- func SetAPIKey(apiKey string) ConfigOption
- func SetDebugLevel(level LogLevel) ConfigOption
- func SetMaxRetries(maxRetries int) ConfigOption
- func SetMaxTokens(maxTokens int) ConfigOption
- func SetMemory(maxTokens int) ConfigOption
- func SetModel(model string) ConfigOption
- func SetOllamaEndpoint(endpoint string) ConfigOption
- func SetProvider(provider string) ConfigOption
- func SetRetryDelay(retryDelay time.Duration) ConfigOption
- func SetTemperature(temperature float64) ConfigOption
- func SetTimeout(timeout time.Duration) ConfigOption
- type Example
- type GenerateOption
- type IterationCallback
- type LLM
- type LogLevel
- type MemoryOption
- type Metric
- type OptimizationEntry
- type OptimizerOption
- func WithCustomMetrics(metrics ...Metric) OptimizerOption
- func WithIterationCallback(callback IterationCallback) OptimizerOption
- func WithIterations(iterations int) OptimizerOption
- func WithMaxRetries(maxRetries int) OptimizerOption
- func WithMemorySize(size int) OptimizerOption
- func WithOptimizationGoal(goal string) OptimizerOption
- func WithRatingSystem(system string) OptimizerOption
- func WithRetryDelay(delay time.Duration) OptimizerOption
- func WithThreshold(threshold float64) OptimizerOption
- type Prompt
- type PromptOptimizer
- type PromptOption
- type PromptTemplate
- type PromptTemplateOption
- type SchemaOption
- type ValidateFunc
Constants ¶
This section is empty.
Variables ¶
var QuestionAnswerTemplate = NewPromptTemplate( "QuestionAnswer", "Answer the given question", "Answer the following question:\n\n{{.Question}}", WithPromptOptions( WithDirectives("Provide a clear and concise answer"), WithOutput("Answer:"), ), )
Functions ¶
func AnalyzeComparisonResults ¶
func AnalyzeComparisonResults[T any](results []ComparisonResult[T]) string
func ChainOfThought ¶
func ChainOfThought(ctx context.Context, l LLM, question string, opts ...PromptOption) (string, error)
ChainOfThought performs a chain of thought reasoning
func CleanResponse ¶
CleanResponse removes markdown code block syntax and trims the JSON response
func ExtractStructuredData ¶
func ExtractStructuredData[T any](ctx context.Context, l LLM, text string, opts ...PromptOption) (*T, error)
ExtractStructuredData extracts structured data from text based on a given struct type
func GenerateJSONSchema ¶
GenerateJSONSchema generates a JSON schema for the given struct
func QuestionAnswer ¶
func QuestionAnswer(ctx context.Context, l LLM, question string, opts ...PromptOption) (string, error)
QuestionAnswer performs question answering
Types ¶
type ComparisonResult ¶
type ComparisonResult[T any] struct { Provider string Model string Response string Data T Error error Attempts int }
func CompareModels ¶
func CompareModels[T any](ctx context.Context, prompt string, validateFunc ValidateFunc[T], configs ...*Config) ([]ComparisonResult[T], error)
CompareModels now uses a generic type T and accepts a validation function
type Config ¶
type Config struct {
Provider string
Model string
OllamaEndpoint string
Temperature float64
MaxTokens int
Timeout time.Duration
MaxRetries int
RetryDelay time.Duration
APIKey string
DebugLevel LogLevel
MemoryOption *MemoryOption
}
Config holds the configuration for the LLM
func LoadConfig ¶
LoadConfig loads the configuration from environment variables
type ConfigOption ¶
type ConfigOption func(*Config)
ConfigOption is a function type for modifying Config
func SetAPIKey ¶
func SetAPIKey(apiKey string) ConfigOption
SetAPIKey sets the API key for the current provider in the Config
func SetDebugLevel ¶
func SetDebugLevel(level LogLevel) ConfigOption
SetDebugLevel sets the debug level in the Config
func SetMaxRetries ¶
func SetMaxRetries(maxRetries int) ConfigOption
SetMaxRetries sets the maximum number of retries in the Config
func SetMaxTokens ¶
func SetMaxTokens(maxTokens int) ConfigOption
SetMaxTokens sets the max tokens in the Config
func SetMemory ¶ added in v0.0.8
func SetMemory(maxTokens int) ConfigOption
SetMemory sets the memory option for the LLM
func SetOllamaEndpoint ¶
func SetOllamaEndpoint(endpoint string) ConfigOption
SetOllamaEndpoint sets the Ollama endpoint in the Config
func SetProvider ¶
func SetProvider(provider string) ConfigOption
SetProvider sets the provider in the Config
func SetRetryDelay ¶
func SetRetryDelay(retryDelay time.Duration) ConfigOption
SetRetryDelay sets the delay between retries in the Config
func SetTemperature ¶
func SetTemperature(temperature float64) ConfigOption
SetTemperature sets the temperature in the Config
func SetTimeout ¶
func SetTimeout(timeout time.Duration) ConfigOption
SetTimeout sets the timeout in the Config
type GenerateOption ¶
type GenerateOption func(*generateConfig)
GenerateOption is a function type for configuring generate options
func WithJSONSchemaValidation ¶
func WithJSONSchemaValidation() GenerateOption
WithJSONSchemaValidation returns a GenerateOption that enables JSON schema validation
type IterationCallback ¶ added in v0.0.9
type IterationCallback func(iteration int, entry OptimizationEntry)
IterationCallback is a function type for the iteration callback
type LLM ¶
type LLM interface {
// Generate produces a response given a context, prompt, and optional generate options
Generate(ctx context.Context, prompt *Prompt, opts ...GenerateOption) (string, error)
// SetOption sets an option for the LLM
SetOption(key string, value interface{})
// GetPromptJSONSchema returns the JSON schema for the prompt
GetPromptJSONSchema(opts ...SchemaOption) ([]byte, error)
// GetProvider returns the provider of the LLM
GetProvider() string
// GetModel returns the model of the LLM
GetModel() string
// UpdateDebugLevel updates the debug level of the LLM
UpdateDebugLevel(level LogLevel)
// Debug logs a debug message with optional key-value pairs
Debug(msg string, keysAndValues ...interface{})
// GetDebugLevel returns the current debug level of the LLM
GetDebugLevel() LogLevel
SetOllamaEndpoint(endpoint string) error
}
LLM is the interface that wraps the basic LLM operations
func NewLLM ¶
func NewLLM(opts ...ConfigOption) (LLM, error)
NewLLM creates a new LLM instance, potentially with memory if the option is set
type MemoryOption ¶ added in v0.0.8
type MemoryOption struct {
MaxTokens int
}
type OptimizationEntry ¶ added in v0.0.9
type OptimizationEntry = llm.OptimizationEntry
type OptimizerOption ¶ added in v0.0.9
type OptimizerOption func(*PromptOptimizer)
OptimizerOption is a function type for configuring the PromptOptimizer
func WithCustomMetrics ¶ added in v0.0.9
func WithCustomMetrics(metrics ...Metric) OptimizerOption
WithCustomMetrics adds custom metrics to the PromptOptimizer
func WithIterationCallback ¶ added in v0.0.9
func WithIterationCallback(callback IterationCallback) OptimizerOption
Modify the existing WithIterationCallback function
func WithIterations ¶ added in v0.0.91
func WithIterations(iterations int) OptimizerOption
func WithMaxRetries ¶ added in v0.0.9
func WithMaxRetries(maxRetries int) OptimizerOption
WithMaxRetries sets the maximum number of retries for the PromptOptimizer
func WithMemorySize ¶ added in v0.0.91
func WithMemorySize(size int) OptimizerOption
WithMemorySize sets the memory size for the PromptOptimizer
func WithOptimizationGoal ¶ added in v0.0.9
func WithOptimizationGoal(goal string) OptimizerOption
WithOptimizationGoal sets the optimization goal for the PromptOptimizer
func WithRatingSystem ¶ added in v0.0.9
func WithRatingSystem(system string) OptimizerOption
WithRatingSystem sets the rating system for the PromptOptimizer
func WithRetryDelay ¶ added in v0.0.9
func WithRetryDelay(delay time.Duration) OptimizerOption
WithRetryDelay sets the delay between retries for the PromptOptimizer
func WithThreshold ¶ added in v0.0.9
func WithThreshold(threshold float64) OptimizerOption
WithThreshold sets the threshold for the PromptOptimizer
type Prompt ¶
type Prompt struct {
Input string `json:"input" jsonschema:"required,description=The main input text for the LLM" validate:"required"`
Output string `json:"output,omitempty" jsonschema:"description=Specification for the expected output format"`
Directives []string `json:"directives,omitempty" jsonschema:"description=List of directives to guide the LLM"`
Context string `json:"context,omitempty" jsonschema:"description=Additional context for the LLM"`
MaxLength int `` /* 128-byte string literal not displayed */
Examples []string `json:"examples,omitempty" jsonschema:"description=List of examples to guide the LLM"`
}
Prompt represents a structured prompt for an LLM
func NewPrompt ¶
func NewPrompt(input string, opts ...PromptOption) *Prompt
NewPrompt creates a new Prompt with the given input and applies any provided options
func (*Prompt) Apply ¶
func (p *Prompt) Apply(opts ...PromptOption)
Apply applies the given options to the Prompt
func (*Prompt) GenerateJSONSchema ¶
func (p *Prompt) GenerateJSONSchema(opts ...SchemaOption) ([]byte, error)
GenerateJSONSchema returns the JSON Schema for the Prompt struct
type PromptOptimizer ¶ added in v0.0.9
type PromptOptimizer struct {
// contains filtered or unexported fields
}
PromptOptimizer is the public interface for the prompt optimization system
func NewPromptOptimizer ¶ added in v0.0.9
func NewPromptOptimizer(l LLM, initialPrompt string, taskDesc string, opts ...OptimizerOption) *PromptOptimizer
NewPromptOptimizer creates a new PromptOptimizer
func (*PromptOptimizer) GetOptimizationHistory ¶ added in v0.0.9
func (po *PromptOptimizer) GetOptimizationHistory() []OptimizationEntry
GetOptimizationHistory returns the history of optimization attempts
func (*PromptOptimizer) OptimizePrompt ¶ added in v0.0.9
func (po *PromptOptimizer) OptimizePrompt(ctx context.Context) (string, error)
OptimizePrompt runs the optimization process
type PromptOption ¶
type PromptOption func(*Prompt)
PromptOption is a function type that modifies a Prompt
func WithContext ¶
func WithContext(context string) PromptOption
WithContext adds context to the Prompt
func WithDirectives ¶
func WithDirectives(directives ...string) PromptOption
WithDirectives adds directives to the Prompt
func WithExamples ¶
func WithExamples(examples ...string) PromptOption
WithExamples adds examples to the Prompt
func WithMaxLength ¶
func WithMaxLength(length int) PromptOption
WithMaxLength sets the maximum length for the output
func WithOutput ¶
func WithOutput(output string) PromptOption
WithOutput adds an output specification to the Prompt
type PromptTemplate ¶
type PromptTemplate struct {
Name string
Description string
Template string
Options []PromptOption
}
PromptTemplate represents a template for generating prompts
func NewPromptTemplate ¶
func NewPromptTemplate(name, description, template string, opts ...PromptTemplateOption) *PromptTemplate
NewPromptTemplate creates a new PromptTemplate with the given name, description, and template
type PromptTemplateOption ¶
type PromptTemplateOption func(*PromptTemplate)
PromptTemplateOption is a function type that modifies a PromptTemplate
func WithPromptOptions ¶
func WithPromptOptions(options ...PromptOption) PromptTemplateOption
WithPromptOptions adds PromptOptions to the PromptTemplate
type SchemaOption ¶
type SchemaOption func(*jsonschema.Reflector)
SchemaOption is a function type for schema generation options
func WithExpandedStruct ¶
func WithExpandedStruct(expanded bool) SchemaOption
WithExpandedStruct sets the ExpandedStruct option for schema generation
type ValidateFunc ¶
ValidateFunc is a function type for custom validation logic
