core

package
v0.19.0 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2025 License: MIT Imports: 14 Imported by: 5

Documentation

Index

Constants

This section is empty.

Variables

View Source
var GlobalConfig = &Config{

	ConcurrencyLevel: 1,
}
View Source
var ProviderModels = map[string][]ModelID{
	"anthropic": {ModelAnthropicSonnet, ModelAnthropicHaiku, ModelAnthropicOpus},
	"google":    {ModelGoogleGeminiFlash, ModelGoogleGeminiPro, ModelGoogleGeminiFlashThinking},
	"ollama":    {},
	"llamacpp":  {},
}

Functions

func ConfigureDefaultLLM added in v0.16.1

func ConfigureDefaultLLM(apiKey string, modelID ModelID) error

ConfigureDefaultLLM sets up the default LLM to be used across the package.

func ConfigureTeacherLLM added in v0.16.1

func ConfigureTeacherLLM(apiKey string, modelID ModelID) error

ConfigureTeacherLLM sets up the teacher LLM.

func EndSpan added in v0.9.0

func EndSpan(ctx context.Context)

EndSpan completes the current span.

func SetConcurrencyOptions added in v0.16.1

func SetConcurrencyOptions(level int)

func SetDefaultLLM

func SetDefaultLLM(llm LLM)

SetDefaultLLM sets the default LLM.

func ValidateEndpointConfig added in v0.14.0

func ValidateEndpointConfig(cfg *EndpointConfig) error

func WithExecutionState added in v0.9.0

func WithExecutionState(ctx context.Context) context.Context

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

func (b *BaseLLM) GetHTTPClient() *http.Client

GetHTTPClient returns the HTTP client.

func (*BaseLLM) ModelID added in v0.14.0

func (b *BaseLLM) ModelID() string

ModelID implements LLM interface.

func (*BaseLLM) ProviderName added in v0.14.0

func (b *BaseLLM) ProviderName() string

ProviderName implements LLM interface.

type BaseModule

type BaseModule struct {
	Signature Signature
	LLM       LLM
}

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.

func (*BaseOptimizer) Compile

func (bo *BaseOptimizer) Compile(ctx context.Context, program Program, dataset Dataset, metric Metric) (Program, error)

Compile is a placeholder implementation and should be overridden by specific optimizer implementations.

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.

func (*BootstrapFewShot) Compile

func (bfs *BootstrapFewShot) Compile(ctx context.Context, program Program, dataset Dataset, metric Metric) (Program, error)

Compile implements the optimization logic for BootstrapFewShot.

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

type CompileOptions struct {
	MaxTrials int
	Teacher   *Program
}

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 Config added in v0.16.1

type Config struct {
	DefaultLLM       LLM
	TeacherLLM       LLM
	ConcurrencyLevel int
}

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 EndpointConfig struct {
	BaseURL    string            // Base API URL
	Path       string            // Specific endpoint path
	Headers    map[string]string // Common headers
	TimeoutSec int               // Request timeout in seconds
}

type Example

type Example struct {
	Inputs  map[string]interface{}
	Outputs map[string]interface{}
}

Example represents a single training/evaluation example.

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

type Field struct {
	Name        string
	Description string
	Prefix      string
}

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 InputField

type InputField struct {
	Field
}

InputField represents an input field.

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 Chain added in v0.14.0

func Chain(base LLM, decorators ...func(LLM) LLM) LLM

Helper function to compose multiple decorators.

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 LLMResponse struct {
	Content string
	Usage   *TokenInfo
}

type Metric

type Metric func(expected, actual map[string]interface{}) float64

Metric is a function type that evaluates the performance of a program.

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.

func (*ModuleOptions) Clone added in v0.17.1

func (o *ModuleOptions) Clone() *ModuleOptions

Clone creates a copy of ModuleOptions.

func (*ModuleOptions) MergeWith added in v0.17.1

func (o *ModuleOptions) MergeWith(other *ModuleOptions) *ModuleOptions

MergeWith merges this options with other options, with other taking precedence.

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

type OptimizerFactory func() (Optimizer, error)

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 OutputField

type OutputField struct {
	Field
}

OutputField represents an output field.

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) AddModule

func (p *Program) AddModule(name string, module Module)

AddModule adds a new module to the Program.

func (Program) Clone

func (p Program) Clone() Program

Clone creates a deep copy of the Program.

func (Program) Equal

func (p Program) Equal(other Program) bool

Equal checks if two Programs are equivalent.

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 (p *Program) GetModules() []Module

func (Program) GetSignature

func (p Program) GetSignature() Signature

GetSignature returns the overall signature of the program This would need to be defined based on the Forward function's expected inputs and outputs.

func (*Program) SetForward

func (p *Program) SetForward(forward func(context.Context, map[string]interface{}) (map[string]interface{}, error))

SetForward sets the forward function for the Program.

type ProgressReporter

type ProgressReporter interface {
	Report(stage string, processed, total int)
}

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

func ParseSignature(signatureStr string) (Signature, error)

ParseSignature parses a signature string into a Signature struct.

func ShorthandNotation

func ShorthandNotation(notation string) (Signature, error)

ShorthandNotation creates a Signature from a shorthand notation string.

func (Signature) String

func (s Signature) String() string

String returns a string representation of the Signature.

func (Signature) WithInstruction

func (s Signature) WithInstruction(instruction string) Signature

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

func CollectSpans(ctx context.Context) []*Span

Helper method to collect all spans.

func StartSpan added in v0.9.0

func StartSpan(ctx context.Context, operation string) (context.Context, *Span)

StartSpan begins a new operation span.

func (*Span) WithAnnotation added in v0.9.0

func (s *Span) WithAnnotation(key string, value interface{})

func (*Span) WithError added in v0.9.0

func (s *Span) WithError(err error)

Span methods.

type TokenInfo added in v0.6.0

type TokenInfo struct {
	PromptTokens     int
	CompletionTokens int
	TotalTokens      int
}

type TokenUsage added in v0.9.0

type TokenUsage struct {
	PromptTokens     int
	CompletionTokens int
	TotalTokens      int
	Cost             float64
}

TokenUsage tracks token consumption.

type Tool added in v0.18.0

type Tool interface {
	// Metadata returns the tool's metadata
	Metadata() *ToolMetadata

	// CanHandle checks if the tool can handle a specific action/intent
	CanHandle(ctx context.Context, intent string) bool

	// Execute runs the tool with provided parameters
	Execute(ctx context.Context, params map[string]interface{}) (ToolResult, error)

	// Validate checks if the parameters match the expected schema
	Validate(params map[string]interface{}) error
}

Tool represents a capability that can be used by both agents and modules.

type ToolMetadata added in v0.18.0

type ToolMetadata struct {
	Name          string            // Unique identifier for the tool
	Description   string            // Human-readable description
	InputSchema   map[string]string // Expected input parameter types
	OutputSchema  map[string]string // Expected output types
	Capabilities  []string          // List of supported capabilities
	ContextNeeded []string          // Required context keys
	Version       string            // Tool version for compatibility
}

ToolMetadata contains information about a tool's capabilities and requirements.

type ToolRegistry added in v0.18.0

type ToolRegistry interface {
	Register(tool Tool) error
	Get(name string) (Tool, error)
	List() []Tool
	Match(intent string) []Tool
}

ToolRegistry manages available tools.

type ToolResult added in v0.18.0

type ToolResult struct {
	Data        interface{}            // The actual result data
	Metadata    map[string]interface{} // Execution metadata (timing, resources used, etc)
	Annotations map[string]interface{} // Additional context for result interpretation
}

ToolResult wraps tool execution results with metadata.

Jump to

Keyboard shortcuts

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