Documentation
¶
Index ¶
- Variables
- func ConfigureDefaultLLM(apiKey string, modelID ModelID) error
- func ConfigureTeacherLLM(apiKey string, modelID ModelID) error
- func EndSpan(ctx context.Context)
- func SetConcurrencyOptions(level int)
- func SetDefaultLLM(llm LLM)
- func ValidateEndpointConfig(cfg *EndpointConfig) error
- func WithExecutionState(ctx context.Context) context.Context
- func WithMaxTrials(n int) func(*CompileOptions)
- func WithTeacher(teacher *Program) func(*CompileOptions)
- type BaseDecorator
- type BaseLLM
- type BaseModule
- func (bm *BaseModule) Clone() Module
- func (bm *BaseModule) FormatOutputs(outputs map[string]any) map[string]any
- func (bm *BaseModule) GetSignature() Signature
- func (bm *BaseModule) Process(ctx context.Context, inputs map[string]any, opts ...Option) (map[string]any, error)
- func (bm *BaseModule) SetLLM(llm LLM)
- func (bm *BaseModule) SetSignature(signature Signature)
- func (bm *BaseModule) ValidateInputs(inputs map[string]any) error
- type BaseOptimizer
- type BatchEmbeddingResult
- type BootstrapFewShot
- type Capability
- type CompileOptions
- type Composable
- type Config
- type Dataset
- type EmbeddingOption
- type EmbeddingOptions
- type EmbeddingResult
- type EndpointConfig
- type Example
- type ExecutionContextKey
- type ExecutionState
- func (s *ExecutionState) GetCurrentSpan() *Span
- func (s *ExecutionState) GetModelID() string
- func (s *ExecutionState) GetTokenUsage() *TokenUsage
- func (s *ExecutionState) GetTraceID() string
- func (s *ExecutionState) WithModelID(modelID string)
- func (s *ExecutionState) WithTokenUsage(usage *TokenUsage)
- type Field
- type FieldOption
- type GenerateOption
- type GenerateOptions
- type InputField
- type LLM
- type LLMFactory
- type LLMResponse
- type Metric
- type ModelContextDecorator
- type ModelID
- type Module
- type ModuleChain
- type ModuleOptions
- type Optimizer
- type OptimizerFactory
- type OptimizerRegistry
- type Option
- type OutputField
- type Program
- func (p *Program) AddModule(name string, module Module)
- func (p Program) Clone() Program
- func (p Program) Equal(other Program) bool
- func (p Program) Execute(ctx context.Context, inputs map[string]interface{}) (map[string]interface{}, error)
- func (p *Program) GetModules() []Module
- func (p Program) GetSignature() Signature
- func (p *Program) SetForward(...)
- type ProgressReporter
- type Signature
- type Span
- type TokenInfo
- type TokenUsage
Constants ¶
This section is empty.
Variables ¶
var GlobalConfig = &Config{
ConcurrencyLevel: 1,
}
Functions ¶
func ConfigureDefaultLLM ¶ added in v0.16.1
ConfigureDefaultLLM sets up the default LLM to be used across the package.
func ConfigureTeacherLLM ¶ added in v0.16.1
ConfigureTeacherLLM sets up the teacher LLM.
func SetConcurrencyOptions ¶ added in v0.16.1
func SetConcurrencyOptions(level int)
func ValidateEndpointConfig ¶ added in v0.14.0
func ValidateEndpointConfig(cfg *EndpointConfig) error
func WithExecutionState ¶ added in v0.9.0
WithExecutionState creates a new context with dspy-go execution state.
func WithMaxTrials ¶
func WithMaxTrials(n int) func(*CompileOptions)
WithMaxTrials sets the maximum number of trials for optimization.
func WithTeacher ¶
func WithTeacher(teacher *Program) func(*CompileOptions)
WithTeacher sets a teacher program for optimization.
Types ¶
type BaseDecorator ¶ added in v0.14.0
type BaseDecorator struct {
LLM
}
BaseDecorator provides common functionality for all LLM decorators.
func (*BaseDecorator) Unwrap ¶ added in v0.14.0
func (d *BaseDecorator) Unwrap() LLM
type BaseLLM ¶
type BaseLLM struct {
// contains filtered or unexported fields
}
BaseLLM provides a base implementation of the LLM interface.
func NewBaseLLM ¶ added in v0.14.0
func NewBaseLLM(providerName string, modelID ModelID, capabilities []Capability, endpoint *EndpointConfig) *BaseLLM
func (*BaseLLM) Capabilities ¶ added in v0.14.0
func (b *BaseLLM) Capabilities() []Capability
Capabilities implements LLM interface.
func (*BaseLLM) GetEndpointConfig ¶ added in v0.14.0
func (b *BaseLLM) GetEndpointConfig() *EndpointConfig
GetEndpointConfig returns the current endpoint configuration.
func (*BaseLLM) GetHTTPClient ¶ added in v0.14.0
GetHTTPClient returns the HTTP client.
func (*BaseLLM) ProviderName ¶ added in v0.14.0
ProviderName implements LLM interface.
type BaseModule ¶
BaseModule provides a basic implementation of the Module interface.
func NewModule ¶
func NewModule(signature Signature) *BaseModule
NewModule creates a new base module with the given signature.
func (*BaseModule) Clone ¶
func (bm *BaseModule) Clone() Module
Clone creates a deep copy of the BaseModule.
func (*BaseModule) FormatOutputs ¶
func (bm *BaseModule) FormatOutputs(outputs map[string]any) map[string]any
FormatOutputs ensures that the output map contains all fields specified in the output signature.
func (*BaseModule) GetSignature ¶
func (bm *BaseModule) GetSignature() Signature
GetSignature returns the module's signature.
func (*BaseModule) Process ¶
func (bm *BaseModule) Process(ctx context.Context, inputs map[string]any, opts ...Option) (map[string]any, error)
Process is a placeholder implementation and should be overridden by specific modules.
func (*BaseModule) SetLLM ¶
func (bm *BaseModule) SetLLM(llm LLM)
SetLLM sets the language model for the module.
func (*BaseModule) SetSignature ¶ added in v0.1.0
func (bm *BaseModule) SetSignature(signature Signature)
func (*BaseModule) ValidateInputs ¶
func (bm *BaseModule) ValidateInputs(inputs map[string]any) error
ValidateInputs checks if the provided inputs match the module's input signature.
type BaseOptimizer ¶
type BaseOptimizer struct {
Name string
}
BaseOptimizer provides a basic implementation of the Optimizer interface.
type BatchEmbeddingResult ¶ added in v0.16.1
type BatchEmbeddingResult struct {
// Embeddings for each input
Embeddings []EmbeddingResult
// Any error that occurred during processing
Error error
// Input index that caused the error (if applicable)
ErrorIndex int
}
BatchEmbeddingResult represents results for multiple inputs.
type BootstrapFewShot ¶
type BootstrapFewShot struct {
BaseOptimizer
MaxExamples int
}
BootstrapFewShot implements a basic few-shot learning optimizer.
func NewBootstrapFewShot ¶
func NewBootstrapFewShot(maxExamples int) *BootstrapFewShot
NewBootstrapFewShot creates a new BootstrapFewShot optimizer.
type Capability ¶ added in v0.14.0
type Capability string
const ( // Core capabilities. CapabilityCompletion Capability = "completion" CapabilityChat Capability = "chat" CapabilityEmbedding Capability = "embedding" // Advanced capabilities. CapabilityJSON Capability = "json" CapabilityStreaming Capability = "streaming" CapabilityToolCalling Capability = "tool-calling" )
type CompileOptions ¶
CompileOptions represents options for the compilation process.
type Composable ¶
type Composable interface {
Module
Compose(next Module) Module
GetSubModules() []Module
SetSubModules([]Module)
}
Composable is an interface for modules that can be composed with other modules.
type Dataset ¶
type Dataset interface {
// Next returns the next example in the dataset
Next() (Example, bool)
// Reset resets the dataset iterator
Reset()
}
Dataset represents a collection of examples for training/evaluation.
type EmbeddingOption ¶ added in v0.16.1
type EmbeddingOption func(*EmbeddingOptions)
EmbeddingOption allows for optional parameters.
func WithBatchSize ¶ added in v0.16.1
func WithBatchSize(size int) EmbeddingOption
func WithModel ¶ added in v0.16.1
func WithModel(model string) EmbeddingOption
func WithParams ¶ added in v0.16.1
func WithParams(params map[string]interface{}) EmbeddingOption
type EmbeddingOptions ¶ added in v0.16.1
type EmbeddingOptions struct {
// Model-specific options for embedding
Model string
// Optional batch size for bulk embeddings
BatchSize int
// Additional model-specific parameters
Params map[string]interface{}
}
func NewEmbeddingOptions ¶ added in v0.16.1
func NewEmbeddingOptions() *EmbeddingOptions
Default options for embeddings.
type EmbeddingResult ¶ added in v0.16.1
type EmbeddingResult struct {
// The generated embedding vector
Vector []float32
// Token count and other metadata
TokenCount int
// Any model-specific metadata
Metadata map[string]interface{}
}
EmbeddingResult represents the result of embedding generation.
type EndpointConfig ¶ added in v0.14.0
type ExecutionContextKey ¶ added in v0.9.0
type ExecutionContextKey struct {
// contains filtered or unexported fields
}
ExecutionContextKey is the type for context keys specific to dspy-go.
type ExecutionState ¶ added in v0.9.0
type ExecutionState struct {
// contains filtered or unexported fields
}
ExecutionState holds the mutable state for an execution context.
func GetExecutionState ¶ added in v0.9.0
func GetExecutionState(ctx context.Context) *ExecutionState
GetExecutionState retrieves the execution state from a context.
func (*ExecutionState) GetCurrentSpan ¶ added in v0.9.0
func (s *ExecutionState) GetCurrentSpan() *Span
func (*ExecutionState) GetModelID ¶ added in v0.9.0
func (s *ExecutionState) GetModelID() string
State access methods.
func (*ExecutionState) GetTokenUsage ¶ added in v0.9.0
func (s *ExecutionState) GetTokenUsage() *TokenUsage
func (*ExecutionState) GetTraceID ¶ added in v0.9.0
func (s *ExecutionState) GetTraceID() string
func (*ExecutionState) WithModelID ¶ added in v0.9.0
func (s *ExecutionState) WithModelID(modelID string)
State modification methods.
func (*ExecutionState) WithTokenUsage ¶ added in v0.9.0
func (s *ExecutionState) WithTokenUsage(usage *TokenUsage)
type Field ¶
Field represents a single field in a signature.
func NewField ¶ added in v0.4.0
func NewField(name string, opts ...FieldOption) Field
NewField creates a new Field with smart defaults.
type FieldOption ¶ added in v0.4.0
type FieldOption func(*Field)
FieldOption allows customization of Field creation.
func WithCustomPrefix ¶ added in v0.4.0
func WithCustomPrefix(prefix string) FieldOption
WithCustomPrefix overrides the default prefix.
func WithDescription ¶ added in v0.4.0
func WithDescription(desc string) FieldOption
WithDescription sets a custom description.
func WithNoPrefix ¶ added in v0.4.0
func WithNoPrefix() FieldOption
WithNoPrefix removes the prefix entirely.
type GenerateOption ¶
type GenerateOption func(*GenerateOptions)
GenerateOption represents an option for text generation.
func WithFrequencyPenalty ¶
func WithFrequencyPenalty(p float64) GenerateOption
WithFrequencyPenalty sets the frequency penalty.
func WithMaxTokens ¶
func WithMaxTokens(n int) GenerateOption
WithMaxTokens sets the maximum number of tokens to generate.
func WithPresencePenalty ¶
func WithPresencePenalty(p float64) GenerateOption
WithPresencePenalty sets the presence penalty.
func WithStopSequences ¶
func WithStopSequences(sequences ...string) GenerateOption
WithStopSequences sets the stop sequences.
func WithTemperature ¶
func WithTemperature(t float64) GenerateOption
WithTemperature sets the sampling temperature.
func WithTopP ¶
func WithTopP(p float64) GenerateOption
WithTopP sets the nucleus sampling probability.
type GenerateOptions ¶
type GenerateOptions struct {
MaxTokens int
Temperature float64
TopP float64
PresencePenalty float64
FrequencyPenalty float64
Stop []string
}
GenerateOptions holds configuration for text generation.
func NewGenerateOptions ¶
func NewGenerateOptions() *GenerateOptions
NewGenerateOptions creates a new GenerateOptions with default values.
type LLM ¶
type LLM interface {
// Generate produces text completions based on the given prompt
Generate(ctx context.Context, prompt string, options ...GenerateOption) (*LLMResponse, error)
// GenerateWithJSON produces structured JSON output based on the given prompt
GenerateWithJSON(ctx context.Context, prompt string, options ...GenerateOption) (map[string]interface{}, error)
CreateEmbedding(ctx context.Context, input string, options ...EmbeddingOption) (*EmbeddingResult, error)
CreateEmbeddings(ctx context.Context, inputs []string, options ...EmbeddingOption) (*BatchEmbeddingResult, error)
ProviderName() string
ModelID() string
Capabilities() []Capability
}
LLM represents an interface for language models.
var DefaultLLM LLM
DefaultLLM represents the default LLM to be used when none is specified.
func GetDefaultLLM ¶ added in v0.16.1
func GetDefaultLLM() LLM
GetDefaultLLM returns the default LLM.
func GetTeacherLLM ¶ added in v0.16.1
func GetTeacherLLM() LLM
GetTeacherLLM returns the teacher LLM.
type LLMFactory ¶
type LLMFactory interface {
// CreateLLM creates a new LLM instance. It uses the global configuration
// from core.GlobalConfig for client settings.
CreateLLM(apiKey string, modelID ModelID) (LLM, error)
}
LLMFactory defines a simple interface for creating LLM instances. This maintains compatibility with existing code while allowing for configuration.
var DefaultFactory LLMFactory
DefaultFactory is the global factory instance used by the configuration system.
type LLMResponse ¶ added in v0.6.0
type ModelContextDecorator ¶ added in v0.14.0
type ModelContextDecorator struct {
BaseDecorator
}
ModelContextDecorator adds model context tracking.
func NewModelContextDecorator ¶ added in v0.14.0
func NewModelContextDecorator(base LLM) *ModelContextDecorator
func (*ModelContextDecorator) Generate ¶ added in v0.14.0
func (d *ModelContextDecorator) Generate(ctx context.Context, prompt string, options ...GenerateOption) (*LLMResponse, error)
type ModelID ¶
type ModelID string
ModelID represents the available model IDs.
const ( // Anthropic models. ModelAnthropicHaiku ModelID = ModelID(anthropic.ModelHaiku) ModelAnthropicSonnet ModelID = ModelID(anthropic.ModelSonnet) ModelAnthropicOpus ModelID = ModelID(anthropic.ModelOpus) ModelGoogleGeminiFlash ModelID = "gemini-2.0-flash" ModelGoogleGeminiPro ModelID = "gemini-2.0-pro-exp" ModelGoogleGeminiFlashThinking ModelID = "gemini-2.0-flash-thinking-exp" )
type Module ¶
type Module interface {
// Process executes the module's logic
Process(ctx context.Context, inputs map[string]any, opts ...Option) (map[string]any, error)
// GetSignature returns the module's input and output signature
GetSignature() Signature
// SetLLM sets the language model for the module
SetLLM(llm LLM)
// Clone creates a deep copy of the module
Clone() Module
}
Module represents a basic unit of computation in DSPy.
type ModuleChain ¶
type ModuleChain struct {
BaseModule
Modules []Module
}
ModuleChain represents a chain of modules.
func NewModuleChain ¶
func NewModuleChain(modules ...Module) *ModuleChain
NewModuleChain creates a new module chain.
type ModuleOptions ¶ added in v0.17.0
type ModuleOptions struct {
// LLM generation options
GenerateOptions []GenerateOption
}
ModuleOptions holds configuration that can be passed to modules.
type Optimizer ¶
type Optimizer interface {
// Compile optimizes a given program using the provided dataset and metric
Compile(ctx context.Context, program Program, dataset Dataset, metric Metric) (Program, error)
}
Optimizer represents an interface for optimizing DSPy programs.
type OptimizerFactory ¶
OptimizerFactory is a function type for creating Optimizer instances.
type OptimizerRegistry ¶
type OptimizerRegistry struct {
// contains filtered or unexported fields
}
OptimizerRegistry maintains a registry of available Optimizer implementations.
func NewOptimizerRegistry ¶
func NewOptimizerRegistry() *OptimizerRegistry
NewOptimizerRegistry creates a new OptimizerRegistry.
func (*OptimizerRegistry) Create ¶
func (r *OptimizerRegistry) Create(name string) (Optimizer, error)
Create instantiates a new Optimizer based on the given name.
func (*OptimizerRegistry) Register ¶
func (r *OptimizerRegistry) Register(name string, factory OptimizerFactory)
Register adds a new Optimizer factory to the registry.
type Option ¶ added in v0.17.0
type Option func(*ModuleOptions)
func WithGenerateOptions ¶ added in v0.17.0
func WithGenerateOptions(opts ...GenerateOption) Option
WithGenerateOptions adds LLM generation options.
type Program ¶
type Program struct {
Modules map[string]Module
Forward func(ctx context.Context, inputs map[string]interface{}) (map[string]interface{}, error)
}
Program represents a complete DSPy pipeline or workflow.
func NewProgram ¶
func NewProgram(modules map[string]Module, forward func(context.Context, map[string]interface{}) (map[string]interface{}, error)) Program
NewProgram creates a new Program with the given modules and forward function.
func (Program) Execute ¶
func (p Program) Execute(ctx context.Context, inputs map[string]interface{}) (map[string]interface{}, error)
Execute runs the program with the given inputs.
func (*Program) GetModules ¶ added in v0.1.0
func (Program) GetSignature ¶
GetSignature returns the overall signature of the program This would need to be defined based on the Forward function's expected inputs and outputs.
type ProgressReporter ¶
type Signature ¶
type Signature struct {
Inputs []InputField
Outputs []OutputField
Instruction string
}
Signature represents the input and output specification of a module.
func NewSignature ¶
func NewSignature(inputs []InputField, outputs []OutputField) Signature
NewSignature creates a new Signature with the given inputs and outputs.
func ParseSignature ¶
ParseSignature parses a signature string into a Signature struct.
func ShorthandNotation ¶
ShorthandNotation creates a Signature from a shorthand notation string.
func (Signature) WithInstruction ¶
WithInstruction adds an instruction to the Signature.
type Span ¶ added in v0.9.0
type Span struct {
ID string
ParentID string
Operation string
StartTime time.Time
EndTime time.Time
Error error
Annotations map[string]interface{}
}
Span represents a single operation within the execution.
func CollectSpans ¶ added in v0.9.0
Helper method to collect all spans.