core

package
v0.0.13 Latest Latest
Warning

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

Go to latest
Published: Dec 7, 2025 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const Version = "0.0.0-dev"

Version is a placeholder for the dspy-go version.

Variables

View Source
var GlobalConfig = &Config{

	ConcurrencyLevel: 1,
}
View Source
var ProviderModels = map[string][]ModelID{
	"anthropic": {
		ModelAnthropicSonnet, ModelAnthropicHaiku, ModelAnthropicOpus,
		ModelAnthropicClaude4Opus, ModelAnthropicClaude4Sonnet, ModelAnthropicClaude45Sonnet,
	},
	"google": {
		ModelGoogleGeminiFlash, ModelGoogleGeminiPro, ModelGoogleGeminiFlashLite,
		ModelGoogleGemini3ProPreview,
		ModelGoogleGemini20Flash, ModelGoogleGemini20FlashLite,
	},
	"openai": {
		ModelOpenAIGPT4, ModelOpenAIGPT4Turbo, ModelOpenAIGPT35Turbo, ModelOpenAIGPT4o, ModelOpenAIGPT4oMini,
		ModelOpenAIGPT41, ModelOpenAIGPT41Mini, ModelOpenAIGPT41Nano,
		ModelOpenAIO1, ModelOpenAIO1Pro, ModelOpenAIO1Mini,
		ModelOpenAIO3, ModelOpenAIO3Mini,
		ModelOpenAIGPT5, ModelOpenAIGPT5Mini, ModelOpenAIGPT5Nano,
	},
	"ollama": {
		ModelOllamaLlama3_8B, ModelOllamaLlama3_70B, ModelOllamaLlama3_1_8B, ModelOllamaLlama3_1_70B,
		ModelOllamaLlama3_2_3B, ModelOllamaCodeLlama13B, ModelOllamaCodeLlama34B, ModelOllamaMistral7B,
		ModelOllamaGemma2B, ModelOllamaGemma7B, ModelOllamaQwen2_5_7B, ModelOllamaQwen2_5_14B,
		ModelOllamaNomicEmbed, ModelOllamaMxbaiEmbed, ModelOllamaAllMiniLM,
	},
	"llamacpp":          {},
	"litellm":           {ModelLiteLLMGPT4, ModelLiteLLMClaude3, ModelLiteLLMLlama2, ModelLiteLLMGemini},
	"localai":           {ModelLocalAILlama2, ModelLocalAICodeLlama, ModelLocalAIAlpaca, ModelLocalAIVicuna},
	"fastchat":          {ModelFastChatVicuna, ModelFastChatAlpaca, ModelFastChatCodeLlama, ModelFastChatLlama2},
	"openai_compatible": {ModelCompatibleGPT4, ModelCompatibleGPT35, ModelCompatibleClaude, ModelCompatibleLlama2},
}

Functions

func ConfigureDefaultLLM

func ConfigureDefaultLLM(apiKey string, modelID ModelID) error

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

func ConfigureDefaultLLMFromRegistry

func ConfigureDefaultLLMFromRegistry(ctx context.Context, apiKey string, modelID ModelID) error

ConfigureDefaultLLMFromRegistry sets up the default LLM using the registry.

func ConfigureFromRegistryConfig

func ConfigureFromRegistryConfig(ctx context.Context, config RegistryConfig) error

ConfigureFromRegistryConfig initializes the global configuration with registry settings.

func ConfigureTeacherLLM

func ConfigureTeacherLLM(apiKey string, modelID ModelID) error

ConfigureTeacherLLM sets up the teacher LLM.

func ConfigureTeacherLLMFromRegistry

func ConfigureTeacherLLMFromRegistry(ctx context.Context, apiKey string, modelID ModelID) error

ConfigureTeacherLLMFromRegistry sets up the teacher LLM using the registry.

func EndSpan

func EndSpan(ctx context.Context)

EndSpan completes the current span.

func GetSupportedModels

func GetSupportedModels() map[string][]string

GetSupportedModels returns all models supported by all providers in the registry.

func InitializeDefaultFactory

func InitializeDefaultFactory()

InitializeDefaultFactory initializes the default factory with the registry system.

func InitializeRegistry

func InitializeRegistry(ctx context.Context, config RegistryConfig) error

InitializeRegistry initializes the global registry with the given configuration.

func IsModelSupportedInRegistry

func IsModelSupportedInRegistry(modelID ModelID) bool

IsModelSupportedInRegistry checks if a model is supported by the registry.

func IsMultimodalContent

func IsMultimodalContent(signature Signature, inputs map[string]any) bool

IsMultimodalContent checks if the inputs contain any non-text content.

func LoadProgram

func LoadProgram(p *Program, filepath string) error

LoadProgram loads program state (demos and tuned parameters) from a JSON file into an existing Program instance. It assumes the Program `p` has already been constructed with the correct architecture (modules and signatures) and necessary LLMs configured.

func ProcessTyped

func ProcessTyped[TInput, TOutput any](ctx context.Context, module Module, inputs TInput, opts ...Option) (TOutput, error)

ProcessTyped provides type-safe module processing using generics.

func ProcessTypedWithValidation

func ProcessTypedWithValidation[TInput, TOutput any](ctx context.Context, module Module, inputs TInput, opts ...Option) (TOutput, error)

ProcessTypedWithValidation provides type-safe processing with signature validation.

func RefreshRegistryProvider

func RefreshRegistryProvider(ctx context.Context, providerName string, config ProviderConfig) error

RefreshRegistryProvider reloads a provider configuration.

func RegisterProviderFactory

func RegisterProviderFactory(name string, factory ProviderFactory) error

RegisterProviderFactory is a convenience function to register a provider factory.

func SaveProgram

func SaveProgram(p *Program, filepath string) error

SaveProgram serializes the current state of the Program's modules to a JSON file.

func SetConcurrencyOptions

func SetConcurrencyOptions(level int)

func SetDefaultLLM

func SetDefaultLLM(llm LLM)

SetDefaultLLM sets the default LLM.

func ValidateEndpointConfig

func ValidateEndpointConfig(cfg *EndpointConfig) error

func WithExecutionState

func WithExecutionState(ctx context.Context) context.Context

WithExecutionState creates a new context with dspy-go execution state.

func WithFreshExecutionState

func WithFreshExecutionState(ctx context.Context) context.Context

WithFreshExecutionState creates a new context with a fresh ExecutionState, even if the parent context already has one. This is useful for parallel workers that need isolated execution state to avoid mutex contention while still inheriting other context values (deadlines, trace IDs, etc.) from the parent.

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 AgentHandler

type AgentHandler func(ctx context.Context, input map[string]interface{}) (map[string]interface{}, error)

AgentHandler represents the actual agent execute function.

type AgentInfo

type AgentInfo struct {
	// AgentID is the unique identifier for the agent instance
	AgentID string

	// AgentType is the category/type of the agent (e.g., "ReactiveAgent", "PlannerAgent")
	AgentType string

	// Capabilities lists the tools/capabilities available to this agent
	Capabilities []Tool

	// Version is the agent version for compatibility tracking
	Version string

	// Metadata contains additional agent-specific information
	Metadata map[string]interface{}
}

AgentInfo contains metadata about an agent for interceptor use.

func NewAgentInfo

func NewAgentInfo(agentID, agentType string, capabilities []Tool) *AgentInfo

NewAgentInfo creates an AgentInfo with the provided details. This is a convenience function to ensure consistent AgentInfo creation.

func (*AgentInfo) WithMetadata

func (ai *AgentInfo) WithMetadata(key string, value interface{}) *AgentInfo

WithMetadata adds metadata to AgentInfo.

func (*AgentInfo) WithVersion

func (ai *AgentInfo) WithVersion(version string) *AgentInfo

WithVersion sets the version for AgentInfo.

type AgentInterceptor

type AgentInterceptor func(ctx context.Context, input map[string]interface{}, info *AgentInfo, handler AgentHandler) (map[string]interface{}, error)

AgentInterceptor wraps Agent.Execute() calls with additional functionality. Similar to ModuleInterceptor but for agent operations.

func ChainAgentInterceptors

func ChainAgentInterceptors(interceptors ...AgentInterceptor) AgentInterceptor

ChainAgentInterceptors combines multiple agent interceptors into a single interceptor. The interceptors are applied in order: first interceptor in the slice is the outermost layer. If no interceptors are provided, returns a pass-through interceptor.

type BaseDecorator

type BaseDecorator struct {
	LLM
}

BaseDecorator provides common functionality for all LLM decorators.

func (*BaseDecorator) Unwrap

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

func NewBaseLLM(providerName string, modelID ModelID, capabilities []Capability, endpoint *EndpointConfig, opts ...BaseLLMOption) *BaseLLM

func (*BaseLLM) Capabilities

func (b *BaseLLM) Capabilities() []Capability

Capabilities implements LLM interface.

func (*BaseLLM) GenerateWithContent

func (b *BaseLLM) GenerateWithContent(ctx context.Context, content []ContentBlock, options ...GenerateOption) (*LLMResponse, error)

Concrete LLM implementations should override this if they support multimodal content.

func (*BaseLLM) GetEndpointConfig

func (b *BaseLLM) GetEndpointConfig() *EndpointConfig

GetEndpointConfig returns the current endpoint configuration.

func (*BaseLLM) GetHTTPClient

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

GetHTTPClient returns the HTTP client.

func (*BaseLLM) IsLocal

func (b *BaseLLM) IsLocal() bool

IsLocal returns true if this LLM runs locally (CPU-bound inference).

func (*BaseLLM) ModelID

func (b *BaseLLM) ModelID() string

ModelID implements LLM interface.

func (*BaseLLM) ProviderName

func (b *BaseLLM) ProviderName() string

ProviderName implements LLM interface.

func (*BaseLLM) StreamGenerateWithContent

func (b *BaseLLM) StreamGenerateWithContent(ctx context.Context, content []ContentBlock, options ...GenerateOption) (*StreamResponse, error)

Concrete LLM implementations should override this if they support multimodal streaming.

func (*BaseLLM) WorkloadType

func (b *BaseLLM) WorkloadType() WorkloadType

WorkloadType returns the workload classification based on provider. Remote APIs are I/O-bound (network latency), local models are CPU-bound.

type BaseLLMOption

type BaseLLMOption func(*BaseLLM)

BaseLLMOption configures BaseLLM behavior.

func WithTransportConfig

func WithTransportConfig(config TransportConfig) BaseLLMOption

WithTransportConfig sets custom HTTP transport configuration. Use this for high-throughput scenarios that need more concurrent connections.

type BaseModule

type BaseModule struct {
	Signature   Signature
	LLM         LLM
	DisplayName string
	ModuleType  string
	// contains filtered or unexported fields
}

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

func (bm *BaseModule) ClearInterceptors()

ClearInterceptors removes all interceptors from this module.

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

func (bm *BaseModule) GetDisplayName() string

GetDisplayName returns the human-readable name for this module instance.

func (*BaseModule) GetInterceptors

func (bm *BaseModule) GetInterceptors() []ModuleInterceptor

GetInterceptors returns the current interceptors for this module.

func (*BaseModule) GetModuleType

func (bm *BaseModule) GetModuleType() string

GetModuleType returns the category/type of this module.

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

func (bm *BaseModule) ProcessWithInterceptors(ctx context.Context, inputs map[string]any, interceptors []ModuleInterceptor, opts ...Option) (map[string]any, error)

ProcessWithInterceptors executes the module's logic with interceptor support. If no interceptors are provided, it falls back to using the module's default interceptors. Note: This method should be called on the concrete module type, not BaseModule directly.

func (*BaseModule) ProcessWithInterceptorsImpl

func (bm *BaseModule) ProcessWithInterceptorsImpl(ctx context.Context, inputs map[string]any, interceptors []ModuleInterceptor, processFunc func(context.Context, map[string]any, ...Option) (map[string]any, error), opts ...Option) (map[string]any, error)

ProcessWithInterceptorsImpl is a helper method that implements the interceptor logic. It accepts a process function as a parameter to allow concrete types to pass their own Process method.

func (*BaseModule) SetInterceptors

func (bm *BaseModule) SetInterceptors(interceptors []ModuleInterceptor)

SetInterceptors sets the default interceptors for this module instance.

func (*BaseModule) SetLLM

func (bm *BaseModule) SetLLM(llm LLM)

SetLLM sets the language model for the module.

func (*BaseModule) SetSignature

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

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

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"
	// New multimodal capabilities.
	CapabilityMultimodal Capability = "multimodal"
	CapabilityVision     Capability = "vision"
	CapabilityAudio      Capability = "audio"
)

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

type Config struct {
	DefaultLLM       LLM
	TeacherLLM       LLM
	ConcurrencyLevel int

	// Registry configuration
	Registry *RegistryConfig `json:"registry,omitempty" yaml:"registry,omitempty"`
}

type ContentBlock

type ContentBlock struct {
	Type     FieldType `json:"type"`
	Text     string    `json:"text,omitempty"`
	Data     []byte    `json:"-"` // Raw binary data for images/audio
	MimeType string    `json:"mime_type,omitempty"`
	// Optional metadata for extensibility
	Metadata map[string]interface{} `json:"metadata,omitempty"`
}

It's provider-agnostic - each LLM provider handles its own format conversion.

func ConvertInputsToContentBlocks

func ConvertInputsToContentBlocks(signature Signature, inputs map[string]any) []ContentBlock

This enables backward compatibility while supporting multimodal content. It creates a structured prompt that includes field instructions and prefix information.

func NewAudioBlock

func NewAudioBlock(data []byte, mimeType string) ContentBlock

NewAudioBlock creates an audio content block.

func NewAudioFromBase64

func NewAudioFromBase64(data string, mimeType string) (*ContentBlock, error)

NewAudioFromBase64 creates an audio ContentBlock from base64 encoded data. It decodes the base64 data and creates a properly formatted ContentBlock.

func NewAudioFromFile

func NewAudioFromFile(path string) (*ContentBlock, error)

NewAudioFromFile creates an audio ContentBlock from a file path. It reads the file, detects the MIME type, and creates a properly formatted ContentBlock.

func NewAudioFromURL

func NewAudioFromURL(url string) (*ContentBlock, error)

NewAudioFromURL creates an audio ContentBlock from a URL. It downloads the audio, detects the MIME type, and creates a properly formatted ContentBlock.

func NewImageBlock

func NewImageBlock(data []byte, mimeType string) ContentBlock

NewImageBlock creates an image content block.

func NewImageFromBase64

func NewImageFromBase64(data string, mimeType string) (*ContentBlock, error)

NewImageFromBase64 creates an image ContentBlock from base64 encoded data. It decodes the base64 data and creates a properly formatted ContentBlock.

func NewImageFromFile

func NewImageFromFile(path string) (*ContentBlock, error)

NewImageFromFile creates an image ContentBlock from a file path. It reads the file, detects the MIME type, and creates a properly formatted ContentBlock.

func NewImageFromURL

func NewImageFromURL(url string) (*ContentBlock, error)

NewImageFromURL creates an image ContentBlock from a URL. It downloads the image, detects the MIME type, and creates a properly formatted ContentBlock.

func NewTextBlock

func NewTextBlock(text string) ContentBlock

NewTextBlock creates a text content block.

func (ContentBlock) String

func (cb ContentBlock) String() string

String returns a string representation of the content block.

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 DefaultLLMRegistry

type DefaultLLMRegistry struct {
	// contains filtered or unexported fields
}

DefaultLLMRegistry is the default implementation of LLMRegistry.

func NewLLMRegistry

func NewLLMRegistry() *DefaultLLMRegistry

NewLLMRegistry creates a new instance of DefaultLLMRegistry.

func (*DefaultLLMRegistry) CreateLLM

func (r *DefaultLLMRegistry) CreateLLM(ctx context.Context, apiKey string, modelID ModelID) (LLM, error)

CreateLLM creates an LLM instance using the registry with backward compatibility.

func (*DefaultLLMRegistry) CreateLLMWithConfig

func (r *DefaultLLMRegistry) CreateLLMWithConfig(ctx context.Context, config ProviderConfig, modelID ModelID) (LLM, error)

CreateLLMWithConfig creates an LLM instance with specific configuration.

func (*DefaultLLMRegistry) GetModelProvider

func (r *DefaultLLMRegistry) GetModelProvider(modelID ModelID) (string, bool)

GetModelProvider returns the provider name for a model.

func (*DefaultLLMRegistry) GetProviderConfig

func (r *DefaultLLMRegistry) GetProviderConfig(name string) (ProviderConfig, bool)

GetProviderConfig returns the configuration for a provider.

func (*DefaultLLMRegistry) IsModelSupported

func (r *DefaultLLMRegistry) IsModelSupported(modelID ModelID) bool

IsModelSupported checks if a model is supported by any provider.

func (*DefaultLLMRegistry) ListProviders

func (r *DefaultLLMRegistry) ListProviders() []string

ListProviders returns all registered provider names.

func (*DefaultLLMRegistry) LoadFromConfig

func (r *DefaultLLMRegistry) LoadFromConfig(ctx context.Context, configs map[string]ProviderConfig) error

LoadFromConfig loads provider configurations from a config structure.

func (*DefaultLLMRegistry) RefreshProvider

func (r *DefaultLLMRegistry) RefreshProvider(ctx context.Context, name string, config ProviderConfig) error

RefreshProvider reloads a provider configuration.

func (*DefaultLLMRegistry) RegisterProvider

func (r *DefaultLLMRegistry) RegisterProvider(name string, factory ProviderFactory) error

RegisterProvider registers a new provider factory.

func (*DefaultLLMRegistry) SetDefaultProvider

func (r *DefaultLLMRegistry) SetDefaultProvider(name string) error

SetDefaultProvider sets the default provider for models.

func (*DefaultLLMRegistry) UnregisterProvider

func (r *DefaultLLMRegistry) UnregisterProvider(name string) error

UnregisterProvider removes a provider.

type DemoConsumer

type DemoConsumer interface {
	SetDemos([]Example)
}

DemoConsumer is an interface that modules can optionally implement to allow their demos to be loaded.

type DemoProvider

type DemoProvider interface {
	GetDemos() []Example
}

DemoProvider is an interface that modules can optionally implement to allow their demos to be retrieved.

type EmbeddingOption

type EmbeddingOption func(*EmbeddingOptions)

EmbeddingOption allows for optional parameters.

func WithBatchSize

func WithBatchSize(size int) EmbeddingOption

func WithModel

func WithModel(model string) EmbeddingOption

func WithParams

func WithParams(params map[string]interface{}) EmbeddingOption

type EmbeddingOptions

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

func NewEmbeddingOptions() *EmbeddingOptions

Default options for embeddings.

type EmbeddingResult

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

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
	SchemaConfig *SchemaConfig
}

type Example

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

Example represents a single training/evaluation example.

type ExecutionContextKey

type ExecutionContextKey struct {
	// contains filtered or unexported fields
}

ExecutionContextKey is the type for context keys specific to dspy-go.

type ExecutionState

type ExecutionState struct {
	// contains filtered or unexported fields
}

ExecutionState holds the mutable state for an execution context.

func GetExecutionState

func GetExecutionState(ctx context.Context) *ExecutionState

GetExecutionState retrieves the execution state from a context.

func (*ExecutionState) GetCurrentSpan

func (s *ExecutionState) GetCurrentSpan() *Span

func (*ExecutionState) GetModelID

func (s *ExecutionState) GetModelID() string

State access methods.

func (*ExecutionState) GetTokenUsage

func (s *ExecutionState) GetTokenUsage() *TokenUsage

func (*ExecutionState) GetTraceID

func (s *ExecutionState) GetTraceID() string

func (*ExecutionState) WithModelID

func (s *ExecutionState) WithModelID(modelID string)

State modification methods.

func (*ExecutionState) WithTokenUsage

func (s *ExecutionState) WithTokenUsage(usage *TokenUsage)

type Field

type Field struct {
	Name        string
	Description string
	Prefix      string
	Type        FieldType         // Data type for the field
	Items       *Field            // For array types, this represents the item type
	Properties  map[string]*Field // For object types, this holds nested fields
}

Field represents a single field in a signature.

func NewArrayField added in v0.0.3

func NewArrayField(name string, itemType *Field, opts ...FieldOption) Field

NewArrayField creates a new array field of a specific item type.

func NewBoolField added in v0.0.3

func NewBoolField(name string, opts ...FieldOption) Field

NewBoolField creates a new field of type bool.

func NewField

func NewField(name string, opts ...FieldOption) Field

NewField creates a new Field with smart defaults and customizable options.

func NewIntField added in v0.0.3

func NewIntField(name string, opts ...FieldOption) Field

NewIntField creates a new field of type int.

func NewObjectField added in v0.0.3

func NewObjectField(name string, properties map[string]*Field, opts ...FieldOption) Field

NewObjectField creates a new object field with nested fields.

func NewStringField added in v0.0.3

func NewStringField(name string, opts ...FieldOption) Field

NewStringField creates a new field of type string.

type FieldMetadata

type FieldMetadata struct {
	Name        string
	GoFieldName string
	Required    bool
	Description string
	Prefix      string
	Type        FieldType
	GoType      reflect.Type

	// NEW
	Item       *FieldMetadata            // for arrays
	Properties map[string]*FieldMetadata // for nested objects
}

type FieldOption

type FieldOption func(*Field)

FieldOption allows customization of Field creation.

func WithArrayType added in v0.0.3

func WithArrayType(itemType *Field) FieldOption

WithArrayType sets the field type as array and assigns the item type.

func WithCustomPrefix

func WithCustomPrefix(prefix string) FieldOption

WithCustomPrefix overrides the default prefix.

func WithDescription

func WithDescription(desc string) FieldOption

WithDescription sets a custom description for the field.

func WithFieldType

func WithFieldType(fieldType FieldType) FieldOption

WithFieldType sets the field type.

func WithNoPrefix

func WithNoPrefix() FieldOption

WithNoPrefix removes the prefix entirely.

func WithObjectType added in v0.0.3

func WithObjectType(properties map[string]*Field) FieldOption

WithObjectType sets the field type as object and assigns nested fields.

type FieldType

type FieldType string

FieldType represents the type of data a field can contain.

const (
	FieldTypeText   FieldType = "text"
	FieldTypeImage  FieldType = "image"
	FieldTypeAudio  FieldType = "audio"
	FieldTypeInt    FieldType = "int"
	FieldTypeBool   FieldType = "bool"
	FieldTypeString FieldType = "string"
	FieldTypeArray  FieldType = "array"
	FieldTypeObject FieldType = "object"
)

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 WithRequestJSONSchema added in v0.0.4

func WithRequestJSONSchema(raw interface{}) GenerateOption

WithRequestJSONSchema sets raw JSON schema for input.

func WithRequestSchema added in v0.0.4

func WithRequestSchema(schema *utils.TypeSchema) GenerateOption

func WithResponseJSONSchema added in v0.0.4

func WithResponseJSONSchema(raw interface{}) GenerateOption

WithResponseJSONSchema sets the raw JSON-schema for output.

func WithResponseMIMEType added in v0.0.4

func WithResponseMIMEType(mime string) GenerateOption

WithResponseMIMEType manually sets MIME type (advanced)

func WithResponseSchema added in v0.0.4

func WithResponseSchema(schema *utils.TypeSchema) GenerateOption

WithResponseSchema sets the structured output schema.

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

	Parameters           *utils.TypeSchema
	ParametersJSONSchema interface{}
	ResponseSchema       *utils.TypeSchema
	ResponseJSONSchema   interface{}
	ResponseMIMEType     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 InterceptableModule

type InterceptableModule interface {
	Module

	// ProcessWithInterceptors executes the module's logic with interceptor support
	ProcessWithInterceptors(ctx context.Context, inputs map[string]any, interceptors []ModuleInterceptor, opts ...Option) (map[string]any, error)

	// SetInterceptors sets the default interceptors for this module instance
	SetInterceptors(interceptors []ModuleInterceptor)

	// GetInterceptors returns the current interceptors for this module
	GetInterceptors() []ModuleInterceptor

	// ClearInterceptors removes all interceptors from this module
	ClearInterceptors()
}

InterceptableModule extends Module with interceptor support. This interface provides backward-compatible enhancement for modules that support interceptors.

type InterceptorChain

type InterceptorChain struct {
	// contains filtered or unexported fields
}

InterceptorChain manages a collection of interceptors for a specific component type.

func NewInterceptorChain

func NewInterceptorChain() *InterceptorChain

NewInterceptorChain creates a new empty interceptor chain.

func (*InterceptorChain) AddAgentInterceptor

func (ic *InterceptorChain) AddAgentInterceptor(interceptor AgentInterceptor) *InterceptorChain

AddAgentInterceptor adds an agent interceptor to the chain. Interceptors are applied in the order they are added (first added, first executed).

func (*InterceptorChain) AddModuleInterceptor

func (ic *InterceptorChain) AddModuleInterceptor(interceptor ModuleInterceptor) *InterceptorChain

AddModuleInterceptor adds a module interceptor to the chain. Interceptors are applied in the order they are added (first added, first executed).

func (*InterceptorChain) AddToolInterceptor

func (ic *InterceptorChain) AddToolInterceptor(interceptor ToolInterceptor) *InterceptorChain

AddToolInterceptor adds a tool interceptor to the chain. Interceptors are applied in the order they are added (first added, first executed).

func (*InterceptorChain) Clear

func (ic *InterceptorChain) Clear() *InterceptorChain

Clear removes all interceptors from the chain.

func (*InterceptorChain) Compose

func (ic *InterceptorChain) Compose(other *InterceptorChain) *InterceptorChain

Compose combines multiple interceptor chains into a single chain. This is useful for merging different sources of interceptors (e.g., global + module-specific).

func (*InterceptorChain) Count

func (ic *InterceptorChain) Count() int

Count returns the total number of interceptors in the chain.

func (*InterceptorChain) CountByType

func (ic *InterceptorChain) CountByType() (modules, agents, tools int)

CountByType returns the count of interceptors by type.

func (*InterceptorChain) GetAgentInterceptors

func (ic *InterceptorChain) GetAgentInterceptors() []AgentInterceptor

GetAgentInterceptors returns a copy of the agent interceptor slice.

func (*InterceptorChain) GetModuleInterceptors

func (ic *InterceptorChain) GetModuleInterceptors() []ModuleInterceptor

GetModuleInterceptors returns a copy of the module interceptor slice.

func (*InterceptorChain) GetToolInterceptors

func (ic *InterceptorChain) GetToolInterceptors() []ToolInterceptor

GetToolInterceptors returns a copy of the tool interceptor slice.

func (*InterceptorChain) IsEmpty

func (ic *InterceptorChain) IsEmpty() bool

IsEmpty returns true if the chain contains no interceptors.

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)

	GenerateWithFunctions(ctx context.Context, prompt string, functions []map[string]interface{}, 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)

	StreamGenerate(ctx context.Context, prompt string, options ...GenerateOption) (*StreamResponse, error)

	// Multimodal methods - new additions that don't break existing interface
	GenerateWithContent(ctx context.Context, content []ContentBlock, options ...GenerateOption) (*LLMResponse, error)
	StreamGenerateWithContent(ctx context.Context, content []ContentBlock, options ...GenerateOption) (*StreamResponse, error)

	ProviderName() string
	ModelID() string
	Capabilities() []Capability
}

LLM represents an interface for language models.

func Chain

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

Helper function to compose multiple decorators.

func CreateLLMFromRegistry

func CreateLLMFromRegistry(ctx context.Context, apiKey string, modelID ModelID) (LLM, error)

CreateLLMFromRegistry creates an LLM instance using the global registry.

func CreateLLMWithTimeout

func CreateLLMWithTimeout(apiKey string, modelID ModelID, timeout time.Duration) (LLM, error)

CreateLLMWithTimeout creates an LLM with a specified timeout.

func GetDefaultLLM

func GetDefaultLLM() LLM

GetDefaultLLM returns the default LLM.

func GetTeacherLLM

func GetTeacherLLM() LLM

GetTeacherLLM returns the teacher LLM.

func LoadLLMFromConfig

func LoadLLMFromConfig(ctx context.Context, providerConfig ProviderConfig, modelID ModelID) (LLM, error)

LoadLLMFromConfig creates an LLM instance from a complete provider configuration.

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.

func GetDefaultFactory

func GetDefaultFactory() LLMFactory

GetDefaultFactory returns the default factory, initializing it if necessary.

type LLMRegistry

type LLMRegistry interface {
	// RegisterProvider registers a new provider factory
	RegisterProvider(name string, factory ProviderFactory) error

	// UnregisterProvider removes a provider
	UnregisterProvider(name string) error

	// CreateLLM creates an LLM instance using the registry
	CreateLLM(ctx context.Context, apiKey string, modelID ModelID) (LLM, error)

	// CreateLLMWithConfig creates an LLM instance with specific configuration
	CreateLLMWithConfig(ctx context.Context, config ProviderConfig, modelID ModelID) (LLM, error)

	// LoadFromConfig loads provider configurations from a config structure
	LoadFromConfig(ctx context.Context, configs map[string]ProviderConfig) error

	// ListProviders returns all registered provider names
	ListProviders() []string

	// GetProviderConfig returns the configuration for a provider
	GetProviderConfig(name string) (ProviderConfig, bool)

	// IsModelSupported checks if a model is supported by any provider
	IsModelSupported(modelID ModelID) bool

	// GetModelProvider returns the provider name for a model
	GetModelProvider(modelID ModelID) (string, bool)

	// SetDefaultProvider sets the default provider for models
	SetDefaultProvider(name string) error

	// RefreshProvider reloads a provider configuration
	RefreshProvider(ctx context.Context, name string, config ProviderConfig) error
}

LLMRegistry manages dynamic registration and creation of LLM providers.

var (
	GlobalRegistry LLMRegistry
)

GlobalRegistry is the global registry instance.

func GetRegistry

func GetRegistry() LLMRegistry

GetRegistry returns the global registry instance.

type LLMResponse

type LLMResponse struct {
	Content  string
	Usage    *TokenInfo
	Metadata map[string]interface{}
}

type LMConfigProvider

type LMConfigProvider interface {
	GetLLMIdentifier() map[string]string // Returns map like {"provider": "OpenAI", "model": "gpt-4"}
}

LMConfigProvider is an interface that modules can optionally implement to allow their LM configuration identifiers to be saved and checked.

type Metric

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

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

type ModelConfig

type ModelConfig struct {
	// Model identifier
	ID string `json:"id" yaml:"id"`

	// Display name
	Name string `json:"name,omitempty" yaml:"name,omitempty"`

	// Model capabilities
	Capabilities []string `json:"capabilities,omitempty" yaml:"capabilities,omitempty"`

	// Model-specific parameters
	Params map[string]interface{} `json:"params,omitempty" yaml:"params,omitempty"`

	// Default generation options
	DefaultOptions *GenerateOptions `json:"default_options,omitempty" yaml:"default_options,omitempty"`
}

ModelConfig represents configuration for a specific model.

type ModelContextDecorator

type ModelContextDecorator struct {
	BaseDecorator
}

ModelContextDecorator adds model context tracking.

func NewModelContextDecorator

func NewModelContextDecorator(base LLM) *ModelContextDecorator

func (*ModelContextDecorator) Generate

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 - Claude 3.x and newer series.
	ModelAnthropicHaiku          ModelID = ModelID(anthropic.ModelClaude_3_Haiku_20240307)
	ModelAnthropicSonnet         ModelID = ModelID(anthropic.ModelClaudeSonnet4_5_20250929) // For backwards compatibility
	ModelAnthropicSonnet35       ModelID = ModelID(anthropic.ModelClaudeSonnet4_5_20250929)
	ModelAnthropicOpus           ModelID = ModelID(anthropic.ModelClaudeOpus4_1_20250805)
	ModelAnthropicClaude4Opus    ModelID = ModelID(anthropic.ModelClaudeOpus4_1_20250805)
	ModelAnthropicClaude4Sonnet  ModelID = ModelID(anthropic.ModelClaudeSonnet4_5_20250929)
	ModelAnthropicClaude45Sonnet ModelID = ModelID(anthropic.ModelClaudeSonnet4_5_20250929)

	// Google Gemini models (existing).
	ModelGoogleGeminiFlash     ModelID = "gemini-2.5-flash"
	ModelGoogleGeminiPro       ModelID = "gemini-2.5-pro"
	ModelGoogleGeminiFlashLite ModelID = "gemini-2.5-flash-lite"
	// Google Gemini 3 series (new).
	ModelGoogleGemini3ProPreview ModelID = "gemini-3-pro-preview"
	// Google Gemini 2.0 series (new).
	ModelGoogleGemini20Flash     ModelID = "gemini-2.0-flash"
	ModelGoogleGemini20FlashLite ModelID = "gemini-2.0-flash-lite"

	// OpenAI models - GPT-4 series.
	ModelOpenAIGPT4       ModelID = "gpt-4"
	ModelOpenAIGPT4Turbo  ModelID = "gpt-4-turbo"
	ModelOpenAIGPT35Turbo ModelID = "gpt-3.5-turbo"
	ModelOpenAIGPT4o      ModelID = "gpt-4o"
	ModelOpenAIGPT4oMini  ModelID = "gpt-4o-mini"
	// OpenAI models - GPT-4.1 series.
	ModelOpenAIGPT41     ModelID = "gpt-4.1"
	ModelOpenAIGPT41Mini ModelID = "gpt-4.1-mini"
	ModelOpenAIGPT41Nano ModelID = "gpt-4.1-nano"
	// OpenAI models - o1 reasoning series.
	ModelOpenAIO1     ModelID = "o1"
	ModelOpenAIO1Pro  ModelID = "o1-pro"
	ModelOpenAIO1Mini ModelID = "o1-mini"
	// OpenAI models - o3 reasoning series.
	ModelOpenAIO3     ModelID = "o3"
	ModelOpenAIO3Mini ModelID = "o3-mini"
	// OpenAI models - GPT-5 series.
	ModelOpenAIGPT5     ModelID = "gpt-5"
	ModelOpenAIGPT5Mini ModelID = "gpt-5-mini"
	ModelOpenAIGPT5Nano ModelID = "gpt-5-nano"

	// LiteLLM models (can proxy to any provider).
	ModelLiteLLMGPT4    ModelID = "gpt-4"
	ModelLiteLLMClaude3 ModelID = "claude-3-sonnet-20240229"
	ModelLiteLLMLlama2  ModelID = "llama-2-70b-chat"
	ModelLiteLLMGemini  ModelID = "gemini-pro"

	// Ollama models (OpenAI-compatible mode).
	ModelOllamaLlama3_8B    ModelID = "llama3:8b"
	ModelOllamaLlama3_70B   ModelID = "llama3:70b"
	ModelOllamaLlama3_1_8B  ModelID = "llama3.1:8b"
	ModelOllamaLlama3_1_70B ModelID = "llama3.1:70b"
	ModelOllamaLlama3_2_3B  ModelID = "llama3.2:3b"
	ModelOllamaCodeLlama13B ModelID = "codellama:13b"
	ModelOllamaCodeLlama34B ModelID = "codellama:34b"
	ModelOllamaMistral7B    ModelID = "mistral:7b"
	ModelOllamaGemma2B      ModelID = "gemma:2b"
	ModelOllamaGemma7B      ModelID = "gemma:7b"
	ModelOllamaQwen2_5_7B   ModelID = "qwen2.5:7b"
	ModelOllamaQwen2_5_14B  ModelID = "qwen2.5:14b"

	// Ollama embedding models.
	ModelOllamaNomicEmbed ModelID = "nomic-embed-text"
	ModelOllamaMxbaiEmbed ModelID = "mxbai-embed-large"
	ModelOllamaAllMiniLM  ModelID = "all-minilm"

	// LocalAI models.
	ModelLocalAILlama2    ModelID = "llama-2-7b-chat"
	ModelLocalAICodeLlama ModelID = "codellama-13b-instruct"
	ModelLocalAIAlpaca    ModelID = "alpaca-7b"
	ModelLocalAIVicuna    ModelID = "vicuna-7b"

	// FastChat models.
	ModelFastChatVicuna    ModelID = "vicuna-7b-v1.5"
	ModelFastChatAlpaca    ModelID = "alpaca-13b"
	ModelFastChatCodeLlama ModelID = "codellama-7b-instruct"
	ModelFastChatLlama2    ModelID = "llama-2-7b-chat"

	// Generic OpenAI-compatible models.
	ModelCompatibleGPT4   ModelID = "gpt-4"
	ModelCompatibleGPT35  ModelID = "gpt-3.5-turbo"
	ModelCompatibleClaude ModelID = "claude-3-sonnet"
	ModelCompatibleLlama2 ModelID = "llama-2-chat"
)

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

	SetSignature(signature Signature)

	// SetLLM sets the language model for the module
	SetLLM(llm LLM)

	// Clone creates a deep copy of the module
	Clone() Module

	// GetDisplayName returns a human-readable name for this module instance
	GetDisplayName() string

	// GetModuleType returns the category/type of this module
	GetModuleType() string
}

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 ModuleHandler

type ModuleHandler func(ctx context.Context, inputs map[string]any, opts ...Option) (map[string]any, error)

ModuleHandler represents the actual module process function.

type ModuleInfo

type ModuleInfo struct {
	// ModuleName is the human-readable name of the module instance
	ModuleName string

	// ModuleType is the category/type of the module (e.g., "ChainOfThought", "React")
	ModuleType string

	// Signature contains the input/output structure definition
	Signature Signature

	// Version is the module version for compatibility tracking
	Version string

	// Metadata contains additional module-specific information
	Metadata map[string]interface{}
}

ModuleInfo contains metadata about a module for interceptor use.

func NewModuleInfo

func NewModuleInfo(moduleName, moduleType string, signature Signature) *ModuleInfo

NewModuleInfo creates a ModuleInfo with the provided details. This is a convenience function to ensure consistent ModuleInfo creation.

func (*ModuleInfo) WithMetadata

func (mi *ModuleInfo) WithMetadata(key string, value interface{}) *ModuleInfo

WithMetadata adds metadata to ModuleInfo.

func (*ModuleInfo) WithVersion

func (mi *ModuleInfo) WithVersion(version string) *ModuleInfo

WithVersion sets the version for ModuleInfo.

type ModuleInterceptor

type ModuleInterceptor func(ctx context.Context, inputs map[string]any, info *ModuleInfo, handler ModuleHandler, opts ...Option) (map[string]any, error)

ModuleInterceptor wraps Module.Process() calls with additional functionality. It follows the gRPC interceptor pattern: the interceptor can inspect/modify the request, call the handler, and inspect/modify the response.

func ChainModuleInterceptors

func ChainModuleInterceptors(interceptors ...ModuleInterceptor) ModuleInterceptor

ChainModuleInterceptors combines multiple module interceptors into a single interceptor. The interceptors are applied in order: first interceptor in the slice is the outermost layer. If no interceptors are provided, returns a pass-through interceptor.

type ModuleOptions

type ModuleOptions struct {
	// LLM generation options
	GenerateOptions []GenerateOption

	StreamHandler StreamHandler
}

ModuleOptions holds configuration that can be passed to modules.

func (*ModuleOptions) Clone

func (o *ModuleOptions) Clone() *ModuleOptions

Clone creates a copy of ModuleOptions.

func (*ModuleOptions) MergeWith

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

type Option func(*ModuleOptions)

func WithGenerateOptions

func WithGenerateOptions(opts ...GenerateOption) Option

WithGenerateOptions adds LLM generation options.

func WithOptions

func WithOptions(opts ...Option) Option

func WithStreamHandler

func WithStreamHandler(handler StreamHandler) Option

WithStreamHandler returns an option to enable streaming.

type OutputField

type OutputField struct {
	Field
}

OutputField represents an output field.

type ParameterConsumer

type ParameterConsumer interface {
	SetTunedParameters(params map[string]interface{}) error // Return error for validation
}

ParameterConsumer is an interface that modules can optionally implement to allow their tuned parameters to be loaded.

type ParameterProvider

type ParameterProvider interface {
	GetTunedParameters() map[string]interface{}
}

ParameterProvider is an interface that modules can optionally implement to allow their tuned parameters to be saved.

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

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

func (p *Program) Predictors() []Module

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 ProviderConfig

type ProviderConfig struct {
	// Provider name (e.g., "anthropic", "google", "ollama")
	Name string `json:"name" yaml:"name"`

	// API configuration
	APIKey  string `json:"api_key,omitempty" yaml:"api_key,omitempty"`
	BaseURL string `json:"base_url,omitempty" yaml:"base_url,omitempty"`

	// Model-specific configuration
	Models map[string]ModelConfig `json:"models,omitempty" yaml:"models,omitempty"`

	// Provider-specific parameters
	Params map[string]interface{} `json:"params,omitempty" yaml:"params,omitempty"`

	// Endpoint configuration
	Endpoint *EndpointConfig `json:"endpoint,omitempty" yaml:"endpoint,omitempty"`
}

ProviderConfig represents configuration for a specific provider.

type ProviderFactory

type ProviderFactory func(ctx context.Context, config ProviderConfig, modelID ModelID) (LLM, error)

ProviderFactory creates LLM instances for a specific provider.

type RegistryBasedFactory

type RegistryBasedFactory struct {
	// contains filtered or unexported fields
}

RegistryBasedFactory implements LLMFactory using the registry system.

func NewRegistryBasedFactory

func NewRegistryBasedFactory(registry LLMRegistry) *RegistryBasedFactory

NewRegistryBasedFactory creates a new factory that uses the registry system.

func (*RegistryBasedFactory) CreateLLM

func (f *RegistryBasedFactory) CreateLLM(apiKey string, modelID ModelID) (LLM, error)

CreateLLM creates an LLM instance using the registry.

type RegistryConfig

type RegistryConfig struct {
	// Providers configuration
	Providers map[string]ProviderConfig `json:"providers,omitempty" yaml:"providers,omitempty"`

	// Default provider
	DefaultProvider string `json:"default_provider,omitempty" yaml:"default_provider,omitempty"`

	// Cache settings
	CacheEnabled bool          `json:"cache_enabled,omitempty" yaml:"cache_enabled,omitempty"`
	CacheTTL     time.Duration `json:"cache_ttl,omitempty" yaml:"cache_ttl,omitempty"`
}

RegistryConfig represents the configuration for the registry system.

type SavedExample

type SavedExample struct {
	Inputs  map[string]interface{} `json:"inputs"`
	Outputs map[string]interface{} `json:"outputs"`
}

SavedExample represents a serializable dspy.Example.

type SavedModuleState

type SavedModuleState struct {
	Signature       string                 `json:"signature"`                  // Module's signature string representation (READ-ONLY during load)
	Demos           []SavedExample         `json:"demos,omitempty"`            // Saved demos, if the module provides them
	LMIdentifier    map[string]string      `json:"lm_identifier,omitempty"`    // Identifying info for the LM used (e.g., provider, model)
	TunedParameters map[string]interface{} `json:"tuned_parameters,omitempty"` // Module-specific tuned parameters (e.g., k for retriever)
	ModuleType      string                 `json:"module_type"`                // Concrete type name (e.g., "Predict")

}

SavedModuleState represents the serializable state of a single DSPy module. Specific fields might vary depending on the module type (Predict, CoT, etc.).

type SavedProgramState

type SavedProgramState struct {
	Modules  map[string]SavedModuleState `json:"modules"`  // Map module ID/name to its state
	Metadata map[string]string           `json:"metadata"` // e.g., {"dspy_go_version": "..."}
}

SavedProgramState represents the serializable state of an entire DSPy program.

type SchemaConfig added in v0.0.4

type SchemaConfig struct {
	RequestSchema      *utils.TypeSchema
	RequestJSONSchema  interface{}
	ResponseSchema     *utils.TypeSchema
	ResponseJSONSchema interface{}
	ResponseMIMEType   string
}

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

func (s Signature) AppendInput(name string, prefix string, description string) Signature

AppendInput adds an input field to the signature.

func (Signature) PrependOutput

func (s Signature) PrependOutput(name string, prefix string, description string) Signature

PrependOutput adds an output field to the beginning of the outputs.

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 SignatureMetadata

type SignatureMetadata struct {
	Inputs      []FieldMetadata
	Outputs     []FieldMetadata
	Instruction string
}

type Span

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

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

Helper method to collect all spans.

func StartSpan

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

StartSpan begins a new operation span.

func StartSpanWithContext

func StartSpanWithContext(ctx context.Context, operation string, moduleName string, metadata map[string]interface{}) (context.Context, *Span)

StartSpanWithContext begins a new operation span with additional context information.

func (*Span) WithAnnotation

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

func (*Span) WithError

func (s *Span) WithError(err error)

Span methods.

type StreamChunk

type StreamChunk struct {
	Content string     // The text content of this chunk
	Done    bool       // Indicates if this is the final chunk
	Error   error      // Any error that occurred during streaming
	Usage   *TokenInfo // Optional token usage information (may be nil)
}

type StreamHandler

type StreamHandler func(chunk StreamChunk) error

type StreamResponse

type StreamResponse struct {
	ChunkChannel <-chan StreamChunk // Channel receiving response chunks
	Cancel       func()             // Function to cancel the stream
}

StreamResponse encapsulates a streaming response.

type TokenInfo

type TokenInfo struct {
	PromptTokens     int
	CompletionTokens int
	TotalTokens      int
}

type TokenUsage

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

TokenUsage tracks token consumption.

type Tool

type Tool interface {
	// Name returns the tool's identifier
	Name() string

	// Description returns human-readable explanation of the tool's purpose
	Description() string

	// 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

	// InputSchema returns the expected parameter structure
	InputSchema() models.InputSchema
}

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

type ToolHandler

type ToolHandler func(ctx context.Context, args map[string]interface{}) (ToolResult, error)

ToolHandler represents the actual tool execute function.

type ToolInfo

type ToolInfo struct {
	// Name is the unique identifier for the tool
	Name string

	// Description is a human-readable explanation of the tool's purpose
	Description string

	// InputSchema defines the expected parameter structure
	InputSchema models.InputSchema

	// ToolType is the category/type of the tool (e.g., "MCPTool", "FunctionTool")
	ToolType string

	// Version is the tool version for compatibility tracking
	Version string

	// Capabilities lists the specific capabilities this tool provides
	Capabilities []string

	// Metadata contains additional tool-specific information
	Metadata map[string]interface{}
}

ToolInfo contains metadata about a tool for interceptor use.

func NewToolInfo

func NewToolInfo(name, description, toolType string, inputSchema models.InputSchema) *ToolInfo

NewToolInfo creates a ToolInfo with the provided details. This is a convenience function to ensure consistent ToolInfo creation.

func (*ToolInfo) WithCapabilities

func (ti *ToolInfo) WithCapabilities(capabilities ...string) *ToolInfo

WithCapabilities sets the capabilities for ToolInfo.

func (*ToolInfo) WithMetadata

func (ti *ToolInfo) WithMetadata(key string, value interface{}) *ToolInfo

WithMetadata adds metadata to ToolInfo.

func (*ToolInfo) WithVersion

func (ti *ToolInfo) WithVersion(version string) *ToolInfo

WithVersion sets the version for ToolInfo.

type ToolInterceptor

type ToolInterceptor func(ctx context.Context, args map[string]interface{}, info *ToolInfo, handler ToolHandler) (ToolResult, error)

ToolInterceptor wraps Tool.Execute() calls with additional functionality. This allows interception of tool invocations for logging, validation, security, etc.

func ChainToolInterceptors

func ChainToolInterceptors(interceptors ...ToolInterceptor) ToolInterceptor

ChainToolInterceptors combines multiple tool interceptors into a single interceptor. The interceptors are applied in order: first interceptor in the slice is the outermost layer. If no interceptors are provided, returns a pass-through interceptor.

type ToolMetadata

type ToolMetadata struct {
	Name          string             // Unique identifier for the tool
	Description   string             // Human-readable description
	InputSchema   models.InputSchema // Rich schema from MCP-Go - now consistent with Tool interface
	OutputSchema  map[string]string  // Keep this field for backward compatibility
	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

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

ToolRegistry manages available tools.

type ToolResult

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.

type TransportConfig

type TransportConfig struct {
	MaxIdleConns        int           // Total idle connections across all hosts (default: 100)
	MaxIdleConnsPerHost int           // Idle connections per host (default: 100)
	MaxConnsPerHost     int           // Max concurrent connections per host (default: 100)
	IdleConnTimeout     time.Duration // How long idle connections stay open (default: 90s)
	TLSHandshakeTimeout time.Duration // TLS handshake timeout (default: 10s)
}

TransportConfig configures HTTP connection pooling behavior for LLM requests. Tuning these values can significantly improve performance for parallel workloads.

func DefaultTransportConfig

func DefaultTransportConfig() TransportConfig

DefaultTransportConfig returns optimized defaults for parallel LLM workloads. These values support up to 100 concurrent requests to the same API endpoint.

func (TransportConfig) ToTransport

func (tc TransportConfig) ToTransport() *http.Transport

ToTransport converts the config to an http.Transport.

type TypedSignature

type TypedSignature[TInput, TOutput any] interface {
	// GetInputType returns the reflect.Type for the input struct
	GetInputType() reflect.Type

	// GetOutputType returns the reflect.Type for the output struct
	GetOutputType() reflect.Type

	// ValidateInput performs compile-time and runtime validation of input
	ValidateInput(input TInput) error

	// ValidateOutput performs compile-time and runtime validation of output
	ValidateOutput(output TOutput) error

	// GetFieldMetadata returns parsed struct tag metadata
	GetFieldMetadata() SignatureMetadata

	// ToLegacySignature converts to the existing Signature interface for backward compatibility
	ToLegacySignature() Signature

	// WithInstruction returns a new TypedSignature with the specified instruction
	WithInstruction(instruction string) TypedSignature[TInput, TOutput]
}

TypedSignature provides compile-time type safety for module inputs and outputs.

func FromLegacySignature

func FromLegacySignature(sig Signature) TypedSignature[map[string]any, map[string]any]

Backward compatibility: convert legacy signature to typed.

func NewTypedSignature

func NewTypedSignature[TInput, TOutput any]() TypedSignature[TInput, TOutput]

NewTypedSignature creates a new typed signature for the given input/output types.

func NewTypedSignatureCached

func NewTypedSignatureCached[TInput, TOutput any]() TypedSignature[TInput, TOutput]

NewTypedSignatureCached creates a cached typed signature for the given input/output types. This function provides better performance for repeated calls with the same types.

type WorkloadType

type WorkloadType int

WorkloadType indicates whether LLM calls are I/O-bound or CPU-bound. This affects the optimal number of parallel workers.

const (
	// WorkloadIOBound indicates remote API calls (Gemini, OpenAI, Anthropic).
	// I/O-bound workloads benefit from many concurrent workers (default: 100).
	WorkloadIOBound WorkloadType = iota

	// WorkloadCPUBound indicates local model inference (Ollama, llama.cpp).
	// CPU-bound workloads should use NumCPU() workers to avoid thrashing.
	WorkloadCPUBound
)

Jump to

Keyboard shortcuts

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