modules

package
v0.34.0 Latest Latest
Warning

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

Go to latest
Published: Jun 25, 2025 License: MIT Imports: 14 Imported by: 1

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

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

func (*ChainOfThought) Compose added in v0.1.0

func (c *ChainOfThought) Compose(next core.Module) core.Module

func (*ChainOfThought) GetSignature

func (c *ChainOfThought) GetSignature() core.Signature

func (*ChainOfThought) GetSubModules added in v0.1.0

func (c *ChainOfThought) GetSubModules() []core.Module

func (*ChainOfThought) Process

func (c *ChainOfThought) Process(ctx context.Context, inputs map[string]any, opts ...core.Option) (map[string]any, error)

func (*ChainOfThought) SetLLM

func (c *ChainOfThought) SetLLM(llm core.LLM)

func (*ChainOfThought) SetSignature added in v0.28.0

func (c *ChainOfThought) SetSignature(signature core.Signature)

SetSignature implements the core.Module interface.

func (*ChainOfThought) SetSubModules added in v0.1.0

func (c *ChainOfThought) SetSubModules(modules []core.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.

type Module

type Module interface {
	Forward(inputs map[string]interface{}) (Predict, error)
}

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.

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) Clone added in v0.30.0

func (p *Parallel) Clone() core.Module

Clone creates a deep copy of the Parallel module.

func (*Parallel) GetInnerModule added in v0.30.0

func (p *Parallel) GetInnerModule() core.Module

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.

func (*Parallel) SetLLM added in v0.30.0

func (p *Parallel) SetLLM(llm core.LLM)

SetLLM sets the LLM for both this module and the inner module.

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 NewPredict(signature core.Signature) *Predict

func (*Predict) Clone

func (p *Predict) Clone() core.Module

func (*Predict) FormatOutputs

func (p *Predict) FormatOutputs(outputs map[string]interface{}) map[string]interface{}

func (*Predict) GetDemos

func (p *Predict) GetDemos() []core.Example

func (*Predict) GetLLMIdentifier added in v0.27.0

func (p *Predict) GetLLMIdentifier() map[string]string

GetLLMIdentifier implements the LMConfigProvider interface.

func (*Predict) GetSignature

func (p *Predict) GetSignature() core.Signature

func (*Predict) Process

func (p *Predict) Process(ctx context.Context, inputs map[string]interface{}, opts ...core.Option) (map[string]interface{}, error)

func (*Predict) SetDemos

func (p *Predict) SetDemos(demos []core.Example)

func (*Predict) SetLLM

func (p *Predict) SetLLM(llm core.LLM)

func (*Predict) ValidateInputs

func (p *Predict) ValidateInputs(inputs map[string]interface{}) error

func (*Predict) WithDefaultOptions added in v0.17.1

func (p *Predict) WithDefaultOptions(opts ...core.Option) *Predict

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

func NewReAct(signature core.Signature, registry *tools.InMemoryToolRegistry, maxIters int) *ReAct

NewReAct creates a new ReAct module. It takes a signature (which it modifies), a tool registry pointer, and max iterations.

func (*ReAct) Clone

func (r *ReAct) Clone() core.Module

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.

func (*ReAct) SetLLM

func (r *ReAct) SetLLM(llm core.LLM)

SetLLM sets the language model for both the base module and the internal Predict module.

func (*ReAct) WithDefaultOptions added in v0.17.1

func (r *ReAct) WithDefaultOptions(opts ...core.Option) *ReAct

WithDefaultOptions sets default options by configuring the underlying Predict module.

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) Clone added in v0.30.0

func (r *Refine) Clone() core.Module

Clone creates a deep copy of the Refine module.

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

func (r *Refine) GetSignature() core.Signature

GetSignature returns the module's signature.

func (*Refine) GetWrappedModule added in v0.30.0

func (r *Refine) GetWrappedModule() core.Module

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) SetLLM added in v0.30.0

func (r *Refine) SetLLM(llm core.LLM)

SetLLM sets the language model for the wrapped module.

func (*Refine) SetSignature added in v0.30.0

func (r *Refine) SetSignature(signature core.Signature)

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

func (r *Refine) WithDefaultOptions(opts ...core.Option) *Refine

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

type RewardFunction func(inputs map[string]interface{}, outputs map[string]interface{}) float64

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.

Jump to

Keyboard shortcuts

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