Documentation
¶
Overview ¶
Package modules provides the Parallel execution wrapper for DSPy-Go.
The Parallel module enables concurrent execution of any DSPy module across multiple inputs, providing significant performance improvements for batch processing.
Example usage:
predict := modules.NewPredict(signature)
parallel := modules.NewParallel(predict,
modules.WithMaxWorkers(4),
modules.WithReturnFailures(true))
batchInputs := []map[string]interface{}{
{"input": "first example"},
{"input": "second example"},
{"input": "third example"},
}
result, err := parallel.Process(ctx, map[string]interface{}{
"batch_inputs": batchInputs,
})
results := result["results"].([]map[string]interface{})
The parallel module automatically manages worker pools, error handling, and result collection while maintaining the order of inputs in outputs.
Index ¶
- type ChainOfThought
- func (c *ChainOfThought) Clone() core.Module
- func (c *ChainOfThought) Compose(next core.Module) core.Module
- func (c *ChainOfThought) GetDisplayName() string
- func (c *ChainOfThought) GetModuleType() string
- func (c *ChainOfThought) GetSignature() core.Signature
- func (c *ChainOfThought) GetSubModules() []core.Module
- func (c *ChainOfThought) Process(ctx context.Context, inputs map[string]any, opts ...core.Option) (map[string]any, error)
- func (c *ChainOfThought) SetLLM(llm core.LLM)
- func (c *ChainOfThought) SetSignature(signature core.Signature)
- func (c *ChainOfThought) SetSubModules(modules []core.Module)
- func (c *ChainOfThought) WithDefaultOptions(opts ...core.Option) *ChainOfThought
- func (c *ChainOfThought) WithName(name string) *ChainOfThought
- type Module
- type MultiChainComparison
- func (m *MultiChainComparison) Clone() core.Module
- func (m *MultiChainComparison) GetSignature() core.Signature
- func (m *MultiChainComparison) Process(ctx context.Context, inputs map[string]interface{}, opts ...core.Option) (map[string]interface{}, error)
- func (m *MultiChainComparison) SetLLM(llm core.LLM)
- func (m *MultiChainComparison) WithName(name string) *MultiChainComparison
- type OfferFeedback
- type Parallel
- func (p *Parallel) Clone() core.Module
- func (p *Parallel) GetInnerModule() core.Module
- func (p *Parallel) Process(ctx context.Context, inputs map[string]interface{}, opts ...core.Option) (map[string]interface{}, error)
- func (p *Parallel) SetLLM(llm core.LLM)
- func (p *Parallel) WithName(name string) *Parallel
- type ParallelOption
- type ParallelOptions
- type ParallelResult
- type Predict
- func (p *Predict) Clone() core.Module
- func (p *Predict) FormatOutputs(outputs map[string]interface{}) map[string]interface{}
- func (p *Predict) GetDemos() []core.Example
- func (p *Predict) GetLLMIdentifier() map[string]string
- func (p *Predict) GetSignature() core.Signature
- func (p *Predict) Process(ctx context.Context, inputs map[string]interface{}, opts ...core.Option) (map[string]interface{}, error)
- func (p *Predict) SetDemos(demos []core.Example)
- func (p *Predict) SetLLM(llm core.LLM)
- func (p *Predict) ValidateInputs(inputs map[string]interface{}) error
- func (p *Predict) WithDefaultOptions(opts ...core.Option) *Predict
- func (p *Predict) WithName(name string) *Predict
- type ReAct
- type Refine
- func (r *Refine) Clone() core.Module
- func (r *Refine) GetConfig() RefineConfig
- func (r *Refine) GetSignature() core.Signature
- func (r *Refine) GetWrappedModule() core.Module
- func (r *Refine) Process(ctx context.Context, inputs map[string]interface{}, opts ...core.Option) (map[string]interface{}, error)
- func (r *Refine) SetLLM(llm core.LLM)
- func (r *Refine) SetSignature(signature core.Signature)
- func (r *Refine) UpdateConfig(config RefineConfig) *Refine
- func (r *Refine) WithDefaultOptions(opts ...core.Option) *Refine
- func (r *Refine) WithName(name string) *Refine
- type RefineConfig
- type RewardFunction
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ChainOfThought ¶
type ChainOfThought struct {
Predict *Predict
}
func NewChainOfThought ¶
func NewChainOfThought(signature core.Signature) *ChainOfThought
func (*ChainOfThought) Clone ¶
func (c *ChainOfThought) Clone() core.Module
Clone creates a deep copy of the ChainOfThought module.
func (*ChainOfThought) Compose ¶ added in v0.1.0
func (c *ChainOfThought) Compose(next core.Module) core.Module
Compose creates a new module that chains this module with the next module.
func (*ChainOfThought) GetDisplayName ¶ added in v0.36.0
func (c *ChainOfThought) GetDisplayName() string
GetDisplayName returns the display name for this ChainOfThought module.
func (*ChainOfThought) GetModuleType ¶ added in v0.36.0
func (c *ChainOfThought) GetModuleType() string
GetModuleType returns "ChainOfThought".
func (*ChainOfThought) GetSignature ¶
func (c *ChainOfThought) GetSignature() core.Signature
GetSignature returns the signature from the internal Predict module.
func (*ChainOfThought) GetSubModules ¶ added in v0.1.0
func (c *ChainOfThought) GetSubModules() []core.Module
GetSubModules returns the sub-modules of this ChainOfThought.
func (*ChainOfThought) SetLLM ¶
func (c *ChainOfThought) SetLLM(llm core.LLM)
SetLLM sets the LLM on the internal Predict module.
func (*ChainOfThought) SetSignature ¶ added in v0.28.0
func (c *ChainOfThought) SetSignature(signature core.Signature)
SetSignature sets the signature on the internal Predict module with rationale field.
func (*ChainOfThought) SetSubModules ¶ added in v0.1.0
func (c *ChainOfThought) SetSubModules(modules []core.Module)
SetSubModules sets the sub-modules (expects exactly one Predict module).
func (*ChainOfThought) WithDefaultOptions ¶ added in v0.17.1
func (c *ChainOfThought) WithDefaultOptions(opts ...core.Option) *ChainOfThought
WithDefaultOptions sets default options by configuring the underlying Predict module.
func (*ChainOfThought) WithName ¶ added in v0.36.0
func (c *ChainOfThought) WithName(name string) *ChainOfThought
WithName sets a semantic name for this ChainOfThought instance.
type MultiChainComparison ¶ added in v0.30.0
type MultiChainComparison struct {
core.BaseModule
M int // Number of attempts to compare
// contains filtered or unexported fields
}
MultiChainComparison implements the multi-chain comparison module that compares multiple reasoning attempts and produces a holistic evaluation.
func NewMultiChainComparison ¶ added in v0.30.0
func NewMultiChainComparison(signature core.Signature, M int, temperature float64, opts ...core.Option) *MultiChainComparison
NewMultiChainComparison creates a new MultiChainComparison module.
func (*MultiChainComparison) Clone ¶ added in v0.30.0
func (m *MultiChainComparison) Clone() core.Module
Clone creates a deep copy of the MultiChainComparison module.
func (*MultiChainComparison) GetSignature ¶ added in v0.30.0
func (m *MultiChainComparison) GetSignature() core.Signature
GetSignature returns the signature of the module.
func (*MultiChainComparison) Process ¶ added in v0.30.0
func (m *MultiChainComparison) Process(ctx context.Context, inputs map[string]interface{}, opts ...core.Option) (map[string]interface{}, error)
Process implements the core.Module interface. It takes completions and processes them into reasoning attempts for comparison.
func (*MultiChainComparison) SetLLM ¶ added in v0.30.0
func (m *MultiChainComparison) SetLLM(llm core.LLM)
SetLLM sets the LLM for the internal predict module.
func (*MultiChainComparison) WithName ¶ added in v0.36.0
func (m *MultiChainComparison) WithName(name string) *MultiChainComparison
WithName sets a semantic name for this MultiChainComparison instance.
type OfferFeedback ¶ added in v0.30.0
type OfferFeedback struct {
core.BaseModule
// contains filtered or unexported fields
}
OfferFeedback represents a module for generating advice to improve module performance. This is a simplified version of the Python implementation's OfferFeedback signature.
func NewOfferFeedback ¶ added in v0.30.0
func NewOfferFeedback() *OfferFeedback
NewOfferFeedback creates a new OfferFeedback module.
func (*OfferFeedback) Clone ¶ added in v0.30.0
func (of *OfferFeedback) Clone() core.Module
Clone creates a deep copy of the OfferFeedback module.
func (*OfferFeedback) Process ¶ added in v0.30.0
func (of *OfferFeedback) Process(ctx context.Context, inputs map[string]interface{}, opts ...core.Option) (map[string]interface{}, error)
Process generates feedback and advice for improving module performance.
func (*OfferFeedback) SetLLM ¶ added in v0.30.0
func (of *OfferFeedback) SetLLM(llm core.LLM)
SetLLM sets the language model for feedback generation.
type Parallel ¶ added in v0.30.0
type Parallel struct {
core.BaseModule
// contains filtered or unexported fields
}
Parallel executes a module against multiple inputs concurrently.
func NewParallel ¶ added in v0.30.0
func NewParallel(module core.Module, opts ...ParallelOption) *Parallel
NewParallel creates a new parallel execution wrapper around a module.
func (*Parallel) GetInnerModule ¶ added in v0.30.0
GetInnerModule returns the wrapped module.
func (*Parallel) Process ¶ added in v0.30.0
func (p *Parallel) Process(ctx context.Context, inputs map[string]interface{}, opts ...core.Option) (map[string]interface{}, error)
Process executes the inner module against multiple inputs in parallel.
type ParallelOption ¶ added in v0.30.0
type ParallelOption func(*ParallelOptions)
ParallelOption is a function that configures ParallelOptions.
func WithMaxWorkers ¶ added in v0.30.0
func WithMaxWorkers(count int) ParallelOption
WithMaxWorkers sets the maximum number of concurrent workers.
func WithReturnFailures ¶ added in v0.30.0
func WithReturnFailures(returnFailures bool) ParallelOption
WithReturnFailures configures whether to return failed results.
func WithStopOnFirstError ¶ added in v0.30.0
func WithStopOnFirstError(stopOnError bool) ParallelOption
WithStopOnFirstError configures whether to stop on first error.
type ParallelOptions ¶ added in v0.30.0
type ParallelOptions struct {
// MaxWorkers sets the maximum number of concurrent workers
MaxWorkers int
// ReturnFailures determines if failed results should be included in output
ReturnFailures bool
// StopOnFirstError stops execution on first error encountered
StopOnFirstError bool
}
ParallelOptions configures parallel execution behavior.
type ParallelResult ¶ added in v0.30.0
type ParallelResult struct {
Index int // Original index in the input batch
Success bool // Whether execution succeeded
Output map[string]interface{} // The actual output
Error error // Error if execution failed
}
ParallelResult contains the result of a parallel execution.
type Predict ¶
type Predict struct {
core.BaseModule
Demos []core.Example
LLM core.LLM
// contains filtered or unexported fields
}
func NewPredict ¶
func (*Predict) FormatOutputs ¶
func (*Predict) GetLLMIdentifier ¶ added in v0.27.0
GetLLMIdentifier implements the LMConfigProvider interface.
func (*Predict) GetSignature ¶
func (*Predict) ValidateInputs ¶
func (*Predict) WithDefaultOptions ¶ added in v0.17.1
type ReAct ¶
type ReAct struct {
core.BaseModule
Predict *Predict
Registry *tools.InMemoryToolRegistry
MaxIters int
}
ReAct implements the ReAct agent loop (Reason, Action, Observation). It uses a Predict module to generate thoughts and actions, and executes tools.
func NewReAct ¶
NewReAct creates a new ReAct module. It takes a signature (which it modifies), a tool registry pointer, and max iterations.
func (*ReAct) Clone ¶
Clone creates a copy of the ReAct module. Note: Predict module is cloned, but the LLM instance and ToolRegistry are shared. Cloning the registry itself might be complex and depends on the registry implementation. Sharing the registry is usually acceptable.
func (*ReAct) Process ¶
func (r *ReAct) Process(ctx context.Context, inputs map[string]any, opts ...core.Option) (map[string]any, error)
Process executes the ReAct loop.
type Refine ¶ added in v0.30.0
type Refine struct {
core.BaseModule
// contains filtered or unexported fields
}
Refine implements a refinement module that runs predictions multiple times with varying temperatures to improve quality based on a reward function.
func NewRefine ¶ added in v0.30.0
func NewRefine(module core.Module, config RefineConfig) *Refine
NewRefine creates a new Refine module with the specified configuration.
func (*Refine) GetConfig ¶ added in v0.30.0
func (r *Refine) GetConfig() RefineConfig
GetConfig returns the current refinement configuration.
func (*Refine) GetSignature ¶ added in v0.30.0
GetSignature returns the module's signature.
func (*Refine) GetWrappedModule ¶ added in v0.30.0
GetWrappedModule returns the underlying module being refined.
func (*Refine) Process ¶ added in v0.30.0
func (r *Refine) Process(ctx context.Context, inputs map[string]interface{}, opts ...core.Option) (map[string]interface{}, error)
Process executes the refinement logic by running the module multiple times with different temperatures and selecting the best result based on the reward function.
func (*Refine) SetSignature ¶ added in v0.30.0
SetSignature updates the signature for both this module and the wrapped module.
func (*Refine) UpdateConfig ¶ added in v0.30.0
func (r *Refine) UpdateConfig(config RefineConfig) *Refine
UpdateConfig allows updating the refinement configuration.
func (*Refine) WithDefaultOptions ¶ added in v0.30.0
WithDefaultOptions sets default options for the module.
type RefineConfig ¶ added in v0.30.0
type RefineConfig struct {
// Number of refinement attempts
N int
// Reward function to evaluate predictions
RewardFn RewardFunction
// Minimum threshold for acceptable predictions
Threshold float64
// Number of failed attempts before giving up (optional)
FailCount *int
}
RefineConfig holds configuration options for the Refine module.
type RewardFunction ¶ added in v0.30.0
RewardFunction represents a function that evaluates the quality of a prediction. It takes the inputs used and the outputs produced, and returns a reward score. Higher scores indicate better predictions.