rlm

package
v0.75.3 Latest Latest
Warning

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

Go to latest
Published: Feb 3, 2026 License: MIT Imports: 23 Imported by: 0

Documentation ¶

Overview ¶

Package rlm provides context structure pre-analysis for RLM. The analyzer detects the structure of input context (JSON, markdown, code, etc.) and provides hints for optimal processing strategies.

Package rlm provides a native Recursive Language Model implementation for dspy-go. RLM enables LLMs to explore large contexts programmatically through a Go REPL, making iterative queries to sub-LLMs until a final answer is reached.

Index ¶

Constants ¶

This section is empty.

Variables ¶

View Source
var (
	ErrMissingContext = errors.New("missing required input: context")
	ErrMissingQuery   = errors.New("missing or invalid required input: query")
)

Functions ¶

func ChunkAnalysisSignature ¶

func ChunkAnalysisSignature() core.Signature

ChunkAnalysisSignature for analyzing individual chunks of large contexts.

func ContextMetadata ¶

func ContextMetadata(payload any) string

ContextMetadata returns a string describing the context.

func FindCodeBlocks ¶

func FindCodeBlocks(text string) []string

FindCodeBlocks extracts all ```go or ```repl code blocks from the LLM response. Returns an empty slice if no code blocks are found.

func FormatExecutionResult ¶

func FormatExecutionResult(result *ExecutionResult) string

FormatExecutionResult formats an execution result for display.

func IterationDemos ¶

func IterationDemos() []core.Example

IterationDemos provides few-shot examples for the iteration module.

func IterationSignature ¶

func IterationSignature() core.Signature

IterationSignature defines the signature for each RLM iteration. This powers the inner loop where the LLM decides what to do next.

func RLMSignature ¶

func RLMSignature() core.Signature

RLMSignature creates the main RLM module signature. This is the outer interface: takes context + query, returns answer.

func SubQueryDemos ¶

func SubQueryDemos() []core.Example

SubQueryDemos provides few-shot examples for sub-LLM queries.

func SubQuerySignature ¶

func SubQuerySignature() core.Signature

SubQuerySignature defines the signature for sub-LLM queries. This is used by Query() and QueryBatched() internally.

func SynthesisSignature ¶

func SynthesisSignature() core.Signature

SynthesisSignature for combining results from multiple chunk analyses.

Types ¶

type AdaptiveIterationConfig ¶ added in v0.75.1

type AdaptiveIterationConfig struct {
	// Enabled turns on adaptive iteration (default: false).
	Enabled bool

	// BaseIterations is the base number of iterations before context scaling.
	// Default: 10.
	BaseIterations int

	// MaxIterations caps the total iterations regardless of context size.
	// Default: 50.
	MaxIterations int

	// ContextScaleFactor determines how much context size increases iterations.
	// iterations = BaseIterations + (contextSize / ContextScaleFactor)
	// Default: 100000 (100KB per additional iteration).
	ContextScaleFactor int

	// EnableEarlyTermination allows early exit when model signals confidence.
	// Default: true.
	EnableEarlyTermination bool

	// ConfidenceThreshold is the number of confidence signals needed for early termination.
	// Default: 1.
	ConfidenceThreshold int

	// ConfidenceDetector is a custom function to detect confidence signals in responses.
	// If nil, a default heuristic based on FINAL markers is used.
	// The function returns true if the response indicates high confidence.
	ConfidenceDetector func(response string) bool
}

AdaptiveIterationConfig configures adaptive iteration behavior.

type AnalyzerConfig ¶ added in v0.75.1

type AnalyzerConfig struct {
	// SmallContextThreshold is the size below which no chunking is needed.
	SmallContextThreshold int

	// TokenEstimateRatio is the character-to-token ratio estimate.
	TokenEstimateRatio float64
}

AnalyzerConfig holds analyzer configuration options.

func DefaultAnalyzerConfig ¶ added in v0.75.1

func DefaultAnalyzerConfig() AnalyzerConfig

DefaultAnalyzerConfig returns the default analyzer configuration.

type AsyncBatchHandle ¶ added in v0.75.1

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

AsyncBatchHandle represents a batch of pending async queries.

func (*AsyncBatchHandle) CompletedCount ¶ added in v0.75.1

func (bh *AsyncBatchHandle) CompletedCount() int

CompletedCount returns the number of completed queries.

func (*AsyncBatchHandle) Handles ¶ added in v0.75.1

func (bh *AsyncBatchHandle) Handles() []*AsyncQueryHandle

Handles returns the individual query handles.

func (*AsyncBatchHandle) Ready ¶ added in v0.75.1

func (bh *AsyncBatchHandle) Ready() bool

Ready returns true if all queries have completed.

func (*AsyncBatchHandle) TotalCount ¶ added in v0.75.1

func (bh *AsyncBatchHandle) TotalCount() int

TotalCount returns the total number of queries in the batch.

func (*AsyncBatchHandle) WaitAll ¶ added in v0.75.1

func (bh *AsyncBatchHandle) WaitAll() ([]string, error)

WaitAll blocks until all queries complete and returns all results.

type AsyncQueryHandle ¶ added in v0.75.1

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

AsyncQueryHandle represents a pending async query.

func (*AsyncQueryHandle) Duration ¶ added in v0.75.1

func (h *AsyncQueryHandle) Duration() time.Duration

Duration returns the time elapsed since the query was started.

func (*AsyncQueryHandle) Error ¶ added in v0.75.1

func (h *AsyncQueryHandle) Error() error

Error returns any error that occurred during the query.

func (*AsyncQueryHandle) ID ¶ added in v0.75.1

func (h *AsyncQueryHandle) ID() string

ID returns the unique identifier for this async query.

func (*AsyncQueryHandle) Ready ¶ added in v0.75.1

func (h *AsyncQueryHandle) Ready() bool

Ready returns true if the result is available.

func (*AsyncQueryHandle) Result ¶ added in v0.75.1

func (h *AsyncQueryHandle) Result() (string, bool)

Result returns the result if ready, or empty string if not.

func (*AsyncQueryHandle) Wait ¶ added in v0.75.1

func (h *AsyncQueryHandle) Wait() (string, error)

Wait blocks until the query completes and returns the result.

type Chunk ¶ added in v0.75.2

type Chunk struct {
	ID        int       // Unique identifier
	Content   string    // The actual text content
	StartLine int       // Starting line number (1-indexed)
	EndLine   int       // Ending line number (1-indexed)
	Summary   string    // Optional summary of the chunk
	Embedding []float32 // Embedding vector for semantic search
}

Chunk represents a segment of the context with metadata.

type ChunkConfig ¶ added in v0.75.2

type ChunkConfig struct {
	// MaxChunkSize is the maximum number of characters per chunk
	MaxChunkSize int
	// OverlapSize is the number of characters to overlap between chunks
	OverlapSize int
	// ChunkByLines if true, chunks by line count instead of character count
	ChunkByLines bool
	// LinesPerChunk when ChunkByLines is true
	LinesPerChunk int
}

ChunkConfig configures how content is chunked.

func DefaultChunkConfig ¶ added in v0.75.2

func DefaultChunkConfig() ChunkConfig

DefaultChunkConfig returns sensible defaults for chunking.

type ChunkStrategy ¶ added in v0.75.1

type ChunkStrategy string

ChunkStrategy represents the recommended chunking approach.

const (
	// StrategyNone means no chunking needed - context is small enough.
	StrategyNone ChunkStrategy = "none"

	// StrategyFixed means split into fixed-size chunks.
	StrategyFixed ChunkStrategy = "fixed"

	// StrategyDelimiter means split on natural delimiters.
	StrategyDelimiter ChunkStrategy = "delimiter"

	// StrategyHierarchical means split based on document structure.
	StrategyHierarchical ChunkStrategy = "hierarchical"

	// StrategySemantic means split based on semantic units.
	StrategySemantic ChunkStrategy = "semantic"
)

type CodeBlock ¶

type CodeBlock struct {
	Code   string
	Result ExecutionResult
}

CodeBlock represents an extracted and executed code block.

type CompletionResult ¶

type CompletionResult struct {
	Response   string
	Iterations int
	Duration   time.Duration
	Usage      core.TokenUsage
}

CompletionResult represents the final result of an RLM completion.

type Config ¶

type Config struct {
	// MaxIterations is the maximum number of iteration loops (default: 30).
	MaxIterations int

	// Verbose enables verbose logging.
	Verbose bool

	// Timeout is the maximum duration for the entire RLM completion.
	// Zero means no timeout (default).
	Timeout time.Duration

	// TraceDir is the directory for RLM trace logs (JSONL format compatible with rlm-viewer).
	// Empty string disables tracing.
	TraceDir string

	// HistoryCompression configures incremental history compression.
	// When enabled, older iterations are summarized to reduce context size.
	HistoryCompression *HistoryCompressionConfig

	// AdaptiveIteration configures adaptive iteration strategy.
	// When enabled, max iterations are dynamically calculated based on context size.
	AdaptiveIteration *AdaptiveIterationConfig

	// SubRLM configures nested sub-RLM behavior.
	// When enabled, allows spawning nested RLM loops that share REPL state.
	SubRLM *SubRLMConfig

	// OutputTruncation configures output truncation settings.
	// Controls max lengths for execution output, variable previews, and history entries.
	OutputTruncation *OutputTruncationConfig

	// OnProgress is called at the start of each iteration with progress info.
	// Can be used to display progress to users or implement custom termination logic.
	OnProgress func(progress IterationProgress)
}

Config holds RLM configuration.

func DefaultConfig ¶

func DefaultConfig() Config

DefaultConfig returns the default RLM configuration.

type ContextAnalysis ¶ added in v0.75.1

type ContextAnalysis struct {
	// Type is the detected primary content type.
	Type ContextType

	// SecondaryTypes are additional types detected in mixed content.
	SecondaryTypes []ContextType

	// Size is the total size in bytes.
	Size int

	// EstimatedTokens is the estimated token count (rough approximation).
	EstimatedTokens int

	// RecommendedStrategy is the suggested chunking strategy.
	RecommendedStrategy ChunkStrategy

	// RecommendedChunkSize is the suggested chunk size in bytes.
	RecommendedChunkSize int

	// Delimiters are natural delimiters found in the content.
	Delimiters []string

	// Structure provides hints about the content structure.
	Structure StructureHints

	// LLMHint is a string hint to prepend to LLM prompts about the context.
	LLMHint string
}

ContextAnalysis contains the analysis results for a context payload.

func AnalyzeContext ¶ added in v0.75.1

func AnalyzeContext(payload any) *ContextAnalysis

AnalyzeContext examines the context payload and returns analysis results.

func AnalyzeContextWithConfig ¶ added in v0.75.1

func AnalyzeContextWithConfig(payload any, cfg AnalyzerConfig) *ContextAnalysis

AnalyzeContextWithConfig performs analysis with custom configuration.

func (*ContextAnalysis) IsLargeContext ¶ added in v0.75.1

func (a *ContextAnalysis) IsLargeContext() bool

IsLargeContext returns true if the context is considered large and would benefit from chunking.

func (*ContextAnalysis) ShouldUseBatching ¶ added in v0.75.1

func (a *ContextAnalysis) ShouldUseBatching() bool

ShouldUseBatching returns true if the context is large enough to warrant batched queries.

type ContextIndex ¶ added in v0.75.2

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

ContextIndex provides efficient access to context slices. It supports both line-based access and semantic search via embeddings.

func NewContextIndex ¶ added in v0.75.2

func NewContextIndex(content string, config ChunkConfig) *ContextIndex

NewContextIndex creates a new context index from raw content.

func (*ContextIndex) AllChunks ¶ added in v0.75.2

func (idx *ContextIndex) AllChunks() []Chunk

AllChunks returns all chunks.

func (*ContextIndex) ChunkCount ¶ added in v0.75.2

func (idx *ContextIndex) ChunkCount() int

ChunkCount returns the number of chunks.

func (*ContextIndex) FindRelevant ¶ added in v0.75.2

func (idx *ContextIndex) FindRelevant(ctx context.Context, query string, topK int) ([]Chunk, error)

FindRelevant returns the top-k most relevant chunks for a query. If embeddings are not indexed, returns chunks based on keyword matching.

func (*ContextIndex) GetChunk ¶ added in v0.75.2

func (idx *ContextIndex) GetChunk(id int) (Chunk, bool)

GetChunk returns a chunk by its ID.

func (*ContextIndex) GetContext ¶ added in v0.75.2

func (idx *ContextIndex) GetContext(startLine, endLine int) string

GetContext returns content between start and end lines (1-indexed, inclusive).

func (*ContextIndex) GetRawContent ¶ added in v0.75.2

func (idx *ContextIndex) GetRawContent() string

GetRawContent returns the raw content string.

func (*ContextIndex) IndexEagerly ¶ added in v0.75.2

func (idx *ContextIndex) IndexEagerly(ctx context.Context) error

IndexEagerly computes embeddings for all chunks immediately.

func (*ContextIndex) IsIndexed ¶ added in v0.75.2

func (idx *ContextIndex) IsIndexed() bool

IsIndexed returns whether embeddings have been computed.

func (*ContextIndex) LineCount ¶ added in v0.75.2

func (idx *ContextIndex) LineCount() int

LineCount returns the number of lines.

func (*ContextIndex) SetChunkSummary ¶ added in v0.75.2

func (idx *ContextIndex) SetChunkSummary(chunkID int, summary string)

SetChunkSummary sets a summary for a chunk.

func (*ContextIndex) SetEmbeddingFunc ¶ added in v0.75.2

func (idx *ContextIndex) SetEmbeddingFunc(fn EmbeddingFunc)

SetEmbeddingFunc sets the function used to compute embeddings.

type ContextType ¶ added in v0.75.1

type ContextType string

ContextType represents the detected type of content.

const (
	// TypeUnknown is used when the content type cannot be determined.
	TypeUnknown ContextType = "unknown"

	// TypeJSON indicates JSON-structured content.
	TypeJSON ContextType = "json"

	// TypeMarkdown indicates markdown-formatted content.
	TypeMarkdown ContextType = "markdown"

	// TypeCode indicates source code content.
	TypeCode ContextType = "code"

	// TypePlainText indicates plain text content.
	TypePlainText ContextType = "plaintext"

	// TypeCSV indicates CSV/tabular data.
	TypeCSV ContextType = "csv"

	// TypeXML indicates XML-structured content.
	TypeXML ContextType = "xml"

	// TypeLog indicates log file content.
	TypeLog ContextType = "log"

	// TypeMixed indicates mixed content with multiple types.
	TypeMixed ContextType = "mixed"
)

type EmbeddingFunc ¶ added in v0.75.2

type EmbeddingFunc func(ctx context.Context, texts []string) ([][]float32, error)

EmbeddingFunc computes embeddings for text inputs.

type ExecutionResult ¶

type ExecutionResult struct {
	Stdout   string
	Stderr   string
	Duration time.Duration
}

ExecutionResult represents the result of executing code in the REPL.

type FinalAnswer ¶

type FinalAnswer struct {
	Type    FinalAnswerType
	Content string
}

FinalAnswer represents a detected FINAL or FINAL_VAR signal.

func FindFinalAnswer ¶

func FindFinalAnswer(text string) *FinalAnswer

FindFinalAnswer detects FINAL() or FINAL_VAR() signals in the LLM response. Returns nil if no final answer is found. Note: Code blocks are filtered out first to avoid false positives when FINAL appears in code examples.

type FinalAnswerType ¶

type FinalAnswerType string

FinalAnswerType indicates whether the answer is direct or a variable reference.

const (
	// FinalTypeDirect indicates a direct value like FINAL(42).
	FinalTypeDirect FinalAnswerType = "FINAL"
	// FinalTypeVariable indicates a variable reference like FINAL_VAR(answer).
	FinalTypeVariable FinalAnswerType = "FINAL_VAR"
)

type HistoryCompressionConfig ¶ added in v0.75.1

type HistoryCompressionConfig struct {
	// Enabled turns on history compression (default: false).
	Enabled bool

	// VerbatimIterations is the number of recent iterations to keep verbatim.
	// Older iterations will be summarized. Default: 3.
	VerbatimIterations int

	// MaxSummaryTokens is the approximate maximum tokens for summarized history.
	// Default: 500.
	MaxSummaryTokens int
}

HistoryCompressionConfig configures how message history is compressed.

type HistoryEntry ¶ added in v0.75.2

type HistoryEntry struct {
	Iteration int       // 1-indexed iteration number
	Timestamp time.Time // When this iteration started
	Action    string    // Action type: explore, query, compute, final, subrlm
	Code      string    // Code that was executed (if any)
	Output    string    // Execution output (truncated)
	Duration  time.Duration
	SubRLM    *SubRLMEntry // Non-nil if this was a subrlm action
}

HistoryEntry represents a single iteration in the RLM history. This provides immutable, structured history for debugging/checkpointing.

type ImmutableHistory ¶ added in v0.75.2

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

ImmutableHistory provides append-only history for RLM iterations.

func NewImmutableHistory ¶ added in v0.75.2

func NewImmutableHistory() *ImmutableHistory

NewImmutableHistory creates a new empty history.

func (*ImmutableHistory) Append ¶ added in v0.75.2

func (h *ImmutableHistory) Append(entry HistoryEntry)

Append adds a new entry to the history.

func (*ImmutableHistory) Entries ¶ added in v0.75.2

func (h *ImmutableHistory) Entries() []HistoryEntry

Entries returns all history entries (immutable copy).

func (*ImmutableHistory) Len ¶ added in v0.75.2

func (h *ImmutableHistory) Len() int

Len returns the number of entries.

func (*ImmutableHistory) String ¶ added in v0.75.2

func (h *ImmutableHistory) String(maxEntryLen int) string

String converts history to a string for LLM prompts. Uses configurable truncation settings.

type IterationProgress ¶ added in v0.75.1

type IterationProgress struct {
	// CurrentIteration is the current iteration number (1-indexed).
	CurrentIteration int

	// MaxIterations is the computed maximum iterations for this request.
	MaxIterations int

	// ConfidenceSignals counts how many times the model has signaled confidence.
	ConfidenceSignals int

	// HasFinalAttempt indicates the model tried to give a final answer.
	HasFinalAttempt bool

	// ContextSize is the size of the input context in bytes.
	ContextSize int
}

IterationProgress tracks progress and confidence during iteration.

type LLMCall ¶

type LLMCall struct {
	Prompt           string        `json:"prompt"`
	Response         string        `json:"response"`
	Duration         time.Duration `json:"duration"`
	PromptTokens     int           `json:"prompt_tokens"`
	CompletionTokens int           `json:"completion_tokens"`
}

LLMCall represents a sub-LLM call made from within the REPL.

type LLMSubClient ¶

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

LLMSubClient adapts a core.LLM to the SubLLMClient interface. This allows any dspy-go LLM to be used for sub-queries in RLM.

func NewLLMSubClient ¶

func NewLLMSubClient(llm core.LLM) *LLMSubClient

NewLLMSubClient creates a SubLLMClient from a core.LLM.

func (*LLMSubClient) Query ¶

func (c *LLMSubClient) Query(ctx context.Context, prompt string) (QueryResponse, error)

Query implements SubLLMClient.

func (*LLMSubClient) QueryBatched ¶

func (c *LLMSubClient) QueryBatched(ctx context.Context, prompts []string) ([]QueryResponse, error)

QueryBatched implements SubLLMClient with concurrent queries.

type Option ¶

type Option func(*Config)

Option configures the RLM.

func WithAdaptiveIteration ¶ added in v0.75.1

func WithAdaptiveIteration() Option

WithAdaptiveIteration enables adaptive iteration strategy with default configuration. This dynamically adjusts max iterations based on context size and enables early termination when the model signals confidence.

func WithAdaptiveIterationConfig ¶ added in v0.75.1

func WithAdaptiveIterationConfig(cfg AdaptiveIterationConfig) Option

WithAdaptiveIterationConfig enables adaptive iteration with custom configuration.

func WithHistoryCompression ¶ added in v0.75.1

func WithHistoryCompression(verbatimIterations, maxSummaryTokens int) Option

WithHistoryCompression enables incremental history compression. verbatimIterations is how many recent iterations to keep in full (default: 3). maxSummaryTokens is the approximate max tokens for the summary (default: 500).

func WithMaxIterations ¶

func WithMaxIterations(n int) Option

WithMaxIterations sets the maximum number of iterations. Values <= 0 are ignored and the default is used.

func WithOutputTruncation ¶ added in v0.75.2

func WithOutputTruncation() Option

WithOutputTruncation enables output truncation with default configuration.

func WithOutputTruncationConfig ¶ added in v0.75.2

func WithOutputTruncationConfig(cfg OutputTruncationConfig) Option

WithOutputTruncationConfig enables output truncation with custom configuration.

func WithProgressHandler ¶ added in v0.75.1

func WithProgressHandler(handler func(IterationProgress)) Option

WithProgressHandler sets a callback for iteration progress updates.

func WithSubRLM ¶ added in v0.75.2

func WithSubRLM() Option

WithSubRLM enables sub-RLM support with default configuration. Sub-RLMs allow nested RLM loops that share REPL state for complex multi-step analysis.

func WithSubRLMConfig ¶ added in v0.75.2

func WithSubRLMConfig(cfg SubRLMConfig) Option

WithSubRLMConfig enables sub-RLM support with custom configuration.

func WithTimeout ¶

func WithTimeout(d time.Duration) Option

WithTimeout sets the maximum duration for the completion.

func WithTraceDir ¶

func WithTraceDir(dir string) Option

WithTraceDir enables JSONL tracing to the specified directory. The trace files are compatible with rlm-go's rlm-viewer command.

func WithVerbose ¶

func WithVerbose(v bool) Option

WithVerbose enables verbose logging.

type OutputFieldSpec ¶ added in v0.75.2

type OutputFieldSpec struct {
	Type        string // Expected type: "string", "int", "float", "bool", "[]string", "map"
	Required    bool   // Whether the field is required
	Description string // Description of what this field should contain
}

OutputFieldSpec defines expected type and validation for SUBMIT output fields.

type OutputTruncationConfig ¶ added in v0.75.2

type OutputTruncationConfig struct {
	// Enabled turns on output truncation (default: true).
	Enabled bool

	// MaxOutputLen is the maximum characters in execution output (default: 5000).
	MaxOutputLen int

	// MaxVarPreviewLen is the maximum characters in variable preview (default: 100).
	MaxVarPreviewLen int

	// MaxHistoryEntryLen is the maximum characters per history entry (default: 1000).
	MaxHistoryEntryLen int
}

OutputTruncationConfig configures output truncation settings.

func DefaultOutputTruncationConfig ¶ added in v0.75.2

func DefaultOutputTruncationConfig() OutputTruncationConfig

DefaultOutputTruncationConfig returns the default output truncation configuration.

type QueryResponse ¶

type QueryResponse struct {
	Response         string
	PromptTokens     int
	CompletionTokens int
}

QueryResponse contains the LLM response with usage metadata.

type REPLEnvironment ¶

type REPLEnvironment interface {
	// LoadContext loads the context payload into the REPL environment.
	LoadContext(payload any) error

	// Execute runs Go code in the interpreter and returns the result.
	Execute(ctx context.Context, code string) (*ExecutionResult, error)

	// GetVariable retrieves a variable value from the interpreter.
	GetVariable(name string) (string, error)

	// SetVariable sets a variable in the interpreter.
	SetVariable(name, value string) error

	// Reset clears the interpreter state.
	Reset() error

	// ContextInfo returns metadata about the loaded context.
	ContextInfo() string

	// GetLocals extracts commonly used variables from the interpreter.
	GetLocals() map[string]any

	// GetVariableMetadata returns rich metadata about REPL variables.
	GetVariableMetadata() []REPLVariable

	// HasFinal returns true if FINAL() or FINAL_VAR() has been called.
	// This provides state-verified completion detection (Nightjar Algorithm 1 Gap 1).
	HasFinal() bool

	// Final returns the value passed to FINAL() or FINAL_VAR().
	// Returns empty string if not set.
	Final() string

	// ClearFinal resets the final state.
	ClearFinal()

	// HasSubmit returns true if SUBMIT() has been called with valid output.
	HasSubmit() bool

	// GetSubmitOutput returns the submitted output fields.
	GetSubmitOutput() map[string]any

	// SetSubmitSchema sets the expected output schema for SUBMIT validation.
	SetSubmitSchema(schema map[string]OutputFieldSpec)

	// GetLLMCalls returns the LLM calls made during code execution.
	GetLLMCalls() []LLMCall
}

REPLEnvironment defines the interface for a REPL that can execute code and make LLM queries.

type REPLVariable ¶ added in v0.75.2

type REPLVariable struct {
	Name        string // Variable name
	Value       any    // The actual value
	Type        string // Type description (string, int, []string, map, etc.)
	Length      int    // Length/size for strings and collections (-1 if N/A)
	Preview     string // Truncated representation for display
	IsImportant bool   // True for key variables (result, answer, etc.)
}

REPLVariable provides rich metadata about a variable in the REPL.

type RLM ¶

type RLM struct {
	core.BaseModule
	// contains filtered or unexported fields
}

RLM is the main Recursive Language Model module implementation. It enables LLMs to explore large contexts programmatically through a Go REPL, making iterative queries to sub-LLMs until a final answer is reached.

func New ¶

func New(rootLLM core.LLM, subLLMClient SubLLMClient, opts ...Option) *RLM

New creates a new RLM module instance with separate LLMs. rootLLM is used for the main orchestration loop. subLLMClient is used for Query/QueryBatched calls from within the REPL. For most cases, use NewFromLLM instead which uses the same LLM for both.

func NewFromLLM ¶

func NewFromLLM(llm core.LLM, opts ...Option) *RLM

NewFromLLM creates a new RLM module using a single core.LLM for both root orchestration and sub-queries. This is the recommended constructor for most use cases.

func (*RLM) Clone ¶

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

Clone creates a copy of the RLM module.

func (*RLM) Complete ¶

func (r *RLM) Complete(ctx context.Context, contextPayload any, query string) (*CompletionResult, error)

Complete runs an RLM completion. contextPayload is the context data (string, map, or slice). query is the user's question.

func (*RLM) GetTokenTracker ¶

func (r *RLM) GetTokenTracker() *TokenTracker

GetTokenTracker returns the token tracker for inspecting usage.

func (*RLM) Process ¶

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

Process implements the core.Module interface. It takes inputs with "context" and "query" fields and returns the answer.

func (*RLM) ProcessWithInterceptors ¶

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

ProcessWithInterceptors executes the RLM module's logic with interceptor support.

func (*RLM) SetLLM ¶

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

SetLLM sets the root LLM for orchestration and updates the iteration module.

func (*RLM) WithOptions ¶

func (r *RLM) WithOptions(opts ...Option) *RLM

WithOptions applies additional options to the RLM module.

type StructureHints ¶ added in v0.75.1

type StructureHints struct {
	// HasHeaders indicates if the content has section headers.
	HasHeaders bool

	// HeaderCount is the number of headers detected.
	HeaderCount int

	// HasCodeBlocks indicates if code blocks are present.
	HasCodeBlocks bool

	// CodeBlockCount is the number of code blocks detected.
	CodeBlockCount int

	// HasLists indicates if lists are present.
	HasLists bool

	// ListCount is the number of lists detected.
	ListCount int

	// HasTables indicates if tables are present.
	HasTables bool

	// NestingDepth is the maximum nesting depth for structured content.
	NestingDepth int

	// LineCount is the total number of lines.
	LineCount int

	// AvgLineLength is the average line length.
	AvgLineLength int
}

StructureHints provides detailed structure information.

type SubLLMClient ¶

type SubLLMClient interface {
	// Query makes a single LLM query.
	Query(ctx context.Context, prompt string) (QueryResponse, error)
	// QueryBatched makes concurrent LLM queries.
	QueryBatched(ctx context.Context, prompts []string) ([]QueryResponse, error)
}

SubLLMClient defines the interface for making LLM calls from within the REPL.

type SubRLMCall ¶ added in v0.75.2

type SubRLMCall struct {
	Query            string        `json:"query"`
	Result           string        `json:"result"`
	Iterations       int           `json:"iterations"`
	Depth            int           `json:"depth"`
	Duration         time.Duration `json:"duration"`
	PromptTokens     int           `json:"prompt_tokens"`
	CompletionTokens int           `json:"completion_tokens"`
}

SubRLMCall represents a nested sub-RLM invocation.

type SubRLMConfig ¶ added in v0.75.2

type SubRLMConfig struct {
	// MaxDepth is the maximum nesting depth for sub-RLM calls (default: 3).
	// A value of 1 means no nesting allowed, 2 means one level of nesting, etc.
	MaxDepth int

	// CurrentDepth tracks the current nesting level (0 = root RLM).
	// This is set internally and should not be configured by users.
	CurrentDepth int

	// MaxIterationsPerSubRLM limits iterations for each sub-RLM call.
	// Default: 10. Use 0 to inherit parent's max iterations.
	MaxIterationsPerSubRLM int
}

SubRLMConfig configures nested sub-RLM behavior.

type SubRLMEntry ¶ added in v0.75.2

type SubRLMEntry struct {
	Query      string        // The sub-RLM query
	Result     string        // The sub-RLM result
	Iterations int           // How many iterations the sub-RLM took
	Duration   time.Duration // How long the sub-RLM ran
}

SubRLMEntry captures sub-RLM invocation details.

type TokenTracker ¶

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

TokenTracker aggregates token usage across root LLM and sub-LLM calls.

func NewTokenTracker ¶

func NewTokenTracker() *TokenTracker

NewTokenTracker creates a new token tracker.

func (*TokenTracker) AddRootUsage ¶

func (t *TokenTracker) AddRootUsage(promptTokens, completionTokens int)

AddRootUsage adds token usage from a root LLM call.

func (*TokenTracker) AddSubCall ¶

func (t *TokenTracker) AddSubCall(call LLMCall)

AddSubCall adds a sub-LLM call with its token usage.

func (*TokenTracker) AddSubCalls ¶

func (t *TokenTracker) AddSubCalls(calls []LLMCall)

AddSubCalls adds multiple sub-LLM calls.

func (*TokenTracker) AddSubRLMCall ¶ added in v0.75.2

func (t *TokenTracker) AddSubRLMCall(call SubRLMCall)

AddSubRLMCall adds a sub-RLM call with its token usage.

func (*TokenTracker) ClearSubCalls ¶

func (t *TokenTracker) ClearSubCalls()

ClearSubCalls clears the recorded sub-LLM calls but preserves the counts.

func (*TokenTracker) GetRootUsage ¶

func (t *TokenTracker) GetRootUsage() core.TokenUsage

GetRootUsage returns token usage from root LLM calls only.

func (*TokenTracker) GetSubCalls ¶

func (t *TokenTracker) GetSubCalls() []LLMCall

GetSubCalls returns a copy of all sub-LLM calls.

func (*TokenTracker) GetSubRLMCalls ¶ added in v0.75.2

func (t *TokenTracker) GetSubRLMCalls() []SubRLMCall

GetSubRLMCalls returns a copy of all sub-RLM calls.

func (*TokenTracker) GetSubRLMUsage ¶ added in v0.75.2

func (t *TokenTracker) GetSubRLMUsage() core.TokenUsage

GetSubRLMUsage returns token usage from sub-RLM calls only.

func (*TokenTracker) GetSubUsage ¶

func (t *TokenTracker) GetSubUsage() core.TokenUsage

GetSubUsage returns token usage from sub-LLM calls only.

func (*TokenTracker) GetTotalUsage ¶

func (t *TokenTracker) GetTotalUsage() core.TokenUsage

GetTotalUsage returns the total aggregated token usage.

func (*TokenTracker) Reset ¶

func (t *TokenTracker) Reset()

Reset clears all tracked usage.

type YaegiREPL ¶

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

YaegiREPL is a Yaegi-based Go interpreter with RLM capabilities.

SECURITY NOTE: The interpreter is sandboxed by restricting imports to a safe subset of the standard library (no os, net, syscall, etc.). However, it does NOT protect against resource exhaustion attacks. LLM-generated code could potentially allocate large amounts of memory or create infinite loops that exceed the execution timeout. If running untrusted code in production, consider additional OS-level resource limits (e.g., cgroups, containers) or running the interpreter in a separate process with strict memory limits.

func NewYaegiREPL ¶

func NewYaegiREPL(client SubLLMClient) (*YaegiREPL, error)

NewYaegiREPL creates a new YaegiREPL instance. Returns an error if initialization fails (e.g., stdlib loading or builtin injection).

func (*YaegiREPL) ClearAsyncQueries ¶ added in v0.75.1

func (r *YaegiREPL) ClearAsyncQueries()

ClearAsyncQueries clears all tracked async queries.

func (*YaegiREPL) ClearFinal ¶ added in v0.75.2

func (r *YaegiREPL) ClearFinal()

ClearFinal resets the final state. Call this at the start of each iteration or when reusing the REPL.

func (*YaegiREPL) ClearLLMCalls ¶

func (r *YaegiREPL) ClearLLMCalls()

ClearLLMCalls clears the recorded LLM calls.

func (*YaegiREPL) ClearSubmit ¶ added in v0.75.2

func (r *YaegiREPL) ClearSubmit()

ClearSubmit resets the submit state.

func (*YaegiREPL) ContextInfo ¶

func (r *YaegiREPL) ContextInfo() string

ContextInfo returns metadata about the loaded context.

func (*YaegiREPL) Execute ¶

func (r *YaegiREPL) Execute(ctx context.Context, code string) (result *ExecutionResult, err error)

Execute runs Go code in the interpreter. Execution errors are captured in stderr rather than returned, allowing the caller to inspect all output. The mutex is held for the entire duration to ensure thread safety, as the yaegi interpreter is not safe for concurrent use. Panics from the Yaegi interpreter are recovered and reported as stderr errors.

func (*YaegiREPL) Final ¶ added in v0.75.2

func (r *YaegiREPL) Final() string

Final returns the value passed to FINAL() or FINAL_VAR(). Returns empty string if not set.

func (*YaegiREPL) GetAsyncQuery ¶ added in v0.75.1

func (r *YaegiREPL) GetAsyncQuery(handleID string) (*AsyncQueryHandle, bool)

GetAsyncQuery returns the async query handle by ID.

func (*YaegiREPL) GetContextIndex ¶ added in v0.75.2

func (r *YaegiREPL) GetContextIndex() *ContextIndex

GetContextIndex returns the current context index (for external access).

func (*YaegiREPL) GetLLMCalls ¶

func (r *YaegiREPL) GetLLMCalls() []LLMCall

GetLLMCalls returns and clears the recorded LLM calls. Returns a copy of the calls slice to prevent external modification.

func (*YaegiREPL) GetLocals ¶

func (r *YaegiREPL) GetLocals() map[string]any

GetLocals extracts commonly used variables from the interpreter.

func (*YaegiREPL) GetSubmitOutput ¶ added in v0.75.2

func (r *YaegiREPL) GetSubmitOutput() map[string]any

GetSubmitOutput returns the submitted output fields.

func (*YaegiREPL) GetVariable ¶

func (r *YaegiREPL) GetVariable(name string) (string, error)

GetVariable retrieves a variable value from the interpreter.

func (*YaegiREPL) GetVariableMetadata ¶ added in v0.75.2

func (r *YaegiREPL) GetVariableMetadata() []REPLVariable

GetVariableMetadata returns rich metadata about REPL variables. This provides type info, length, and preview for better LLM context.

func (*YaegiREPL) HasFinal ¶ added in v0.75.2

func (r *YaegiREPL) HasFinal() bool

HasFinal returns true if FINAL() or FINAL_VAR() has been called. This provides state-verified completion detection (Nightjar Algorithm 1 Gap 1).

func (*YaegiREPL) HasSubmit ¶ added in v0.75.2

func (r *YaegiREPL) HasSubmit() bool

HasSubmit returns true if SUBMIT() has been called with valid output.

func (*YaegiREPL) IndexContext ¶ added in v0.75.2

func (r *YaegiREPL) IndexContext(ctx context.Context) error

IndexContext eagerly indexes the loaded context for semantic search. Call this after LoadContext if you want embedding-based FindRelevant.

func (*YaegiREPL) LoadContext ¶

func (r *YaegiREPL) LoadContext(payload any) error

LoadContext injects the context payload into the interpreter as the `context` variable.

func (*YaegiREPL) PendingAsyncQueries ¶ added in v0.75.1

func (r *YaegiREPL) PendingAsyncQueries() int

PendingAsyncQueries returns the number of pending async queries.

func (*YaegiREPL) QueryAsync ¶ added in v0.75.1

func (r *YaegiREPL) QueryAsync(prompt string) *AsyncQueryHandle

QueryAsync starts an async query and returns a handle. This is the Go API for async queries.

func (*YaegiREPL) QueryBatchedAsync ¶ added in v0.75.1

func (r *YaegiREPL) QueryBatchedAsync(prompts []string) *AsyncBatchHandle

QueryBatchedAsync starts batch async queries and returns a batch handle. This is the Go API for batch async queries.

func (*YaegiREPL) Reset ¶

func (r *YaegiREPL) Reset() error

Reset clears the interpreter state and creates a fresh instance.

func (*YaegiREPL) SetChunkConfig ¶ added in v0.75.2

func (r *YaegiREPL) SetChunkConfig(config ChunkConfig)

SetChunkConfig sets the chunking configuration for context indexing.

func (*YaegiREPL) SetContext ¶

func (r *YaegiREPL) SetContext(ctx context.Context)

SetContext sets the execution context for LLM calls.

func (*YaegiREPL) SetEmbeddingFunc ¶ added in v0.75.2

func (r *YaegiREPL) SetEmbeddingFunc(fn EmbeddingFunc)

SetEmbeddingFunc sets the function used for semantic search. If not set, FindRelevant falls back to keyword matching.

func (*YaegiREPL) SetSubmitSchema ¶ added in v0.75.2

func (r *YaegiREPL) SetSubmitSchema(schema map[string]OutputFieldSpec)

SetSubmitSchema sets the expected output schema for SUBMIT validation.

func (*YaegiREPL) SetVariable ¶ added in v0.75.2

func (r *YaegiREPL) SetVariable(name, value string) error

SetVariable sets a variable in the interpreter that can be accessed in subsequent code executions. This is used by sub-RLM to store results that the parent RLM can access.

func (*YaegiREPL) WaitAllAsyncQueries ¶ added in v0.75.1

func (r *YaegiREPL) WaitAllAsyncQueries()

WaitAllAsyncQueries waits for all pending async queries to complete.

Jump to

Keyboard shortcuts

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