Documentation
¶
Index ¶
- func EndSpan(ctx context.Context)
- 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) (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 BootstrapFewShot
- type Capability
- type CompileOptions
- type Composable
- type Dataset
- 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 LLMRegistry
- type LLMResponse
- type Metric
- type ModelContextDecorator
- type ModelID
- type Module
- type ModuleChain
- type Optimizer
- type OptimizerFactory
- type OptimizerRegistry
- 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 ¶
This section is empty.
Functions ¶
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 ¶
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 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 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)
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.
type LLMFactory ¶
LLMFactory is a function type for creating LLM instances.
type LLMRegistry ¶
type LLMRegistry struct {
// contains filtered or unexported fields
}
LLMRegistry maintains a registry of available LLM implementations.
func (*LLMRegistry) Create ¶
func (r *LLMRegistry) Create(name string) (LLM, error)
Create instantiates a new LLM based on the given name.
func (*LLMRegistry) Register ¶
func (r *LLMRegistry) Register(name string, factory LLMFactory)
Register adds a new LLM factory to the registry.
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-exp" ModelGoogleGeminiPro ModelID = "gemini-1.5-pro" )
type Module ¶
type Module interface {
// Process executes the module's logic
Process(ctx context.Context, inputs map[string]any) (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 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 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.