chains

package
v0.23.1 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LLMChain added in v0.15.0

type LLMChain[T any] struct {
	LLM         llms.Model
	Prompt      prompts.PromptTemplate
	Parser      schema.OutputParser[T]
	CallOptions []llms.CallOption
}

LLMChain combines a prompt template, an LLM, and an output parser into a single callable unit. It renders the prompt, calls the LLM, and parses the output into a typed result.

func NewLLMChain added in v0.15.0

func NewLLMChain[T any](llm llms.Model, prompt prompts.PromptTemplate, opts ...LLMChainOption[T]) *LLMChain[T]

NewLLMChain creates a new LLMChain. If no parser is provided via options, a StringParser is used (only valid when T is string).

func (*LLMChain[T]) Call added in v0.15.0

func (c *LLMChain[T]) Call(ctx context.Context, vars map[string]string) (T, error)

Call renders the prompt with the provided variables, calls the LLM, and parses the response. Returns an error if no parser is configured and T is not string.

func (*LLMChain[T]) GetPrompt added in v0.15.0

func (c *LLMChain[T]) GetPrompt(vars map[string]string) string

GetPrompt renders the prompt without calling the LLM. Useful for debugging.

type LLMChainOption added in v0.15.0

type LLMChainOption[T any] func(*LLMChain[T])

LLMChainOption configures an LLMChain.

func WithLLMCallOptions added in v0.15.0

func WithLLMCallOptions[T any](opts ...llms.CallOption) LLMChainOption[T]

WithLLMCallOptions sets LLM call options (temperature, max tokens, etc.)

func WithOutputParser added in v0.15.0

func WithOutputParser[T any](parser schema.OutputParser[T]) LLMChainOption[T]

WithOutputParser sets a custom output parser for the chain.

type MapReduceChain added in v0.15.0

type MapReduceChain[In, Mid, Out any] struct {
	MapFunc        func(ctx context.Context, input In) (Mid, error)
	ReduceFunc     func(ctx context.Context, results []Mid) (Out, error)
	MaxConcurrency int
	Timeout        time.Duration // per-task timeout (0 = no timeout)
	QuorumFraction float64       // e.g. 0.66 = return when 2/3 succeed
}

MapReduceChain runs a map function concurrently over inputs, then reduces the collected results into a final output. Supports concurrency limits, per-task timeouts, and quorum-based early return.

func NewMapReduceChain added in v0.15.0

func NewMapReduceChain[In, Mid, Out any](
	mapFn func(ctx context.Context, input In) (Mid, error),
	reduceFn func(ctx context.Context, results []Mid) (Out, error),
	opts ...MapReduceOption[In, Mid, Out],
) *MapReduceChain[In, Mid, Out]

func (*MapReduceChain[In, Mid, Out]) Call added in v0.15.0

func (c *MapReduceChain[In, Mid, Out]) Call(ctx context.Context, inputs []In) (Out, error)

Call runs the map function over all inputs concurrently (with bounded concurrency), waits for quorum, then calls ReduceFunc with the results.

type MapReduceOption added in v0.15.0

type MapReduceOption[In, Mid, Out any] func(*MapReduceChain[In, Mid, Out])

func WithMapTimeout added in v0.15.0

func WithMapTimeout[In, Mid, Out any](d time.Duration) MapReduceOption[In, Mid, Out]

WithMapTimeout sets a timeout for each individual map task.

func WithMaxConcurrency added in v0.15.0

func WithMaxConcurrency[In, Mid, Out any](n int) MapReduceOption[In, Mid, Out]

WithMaxConcurrency limits the number of map tasks running in parallel.

func WithQuorum added in v0.15.0

func WithQuorum[In, Mid, Out any](fraction float64) MapReduceOption[In, Mid, Out]

WithQuorum sets the fraction of map tasks that must succeed before the chain proceeds to the reduce step. For example, 0.66 means at least 2 out of 3 tasks must succeed.

type RetrievalQA

type RetrievalQA struct {
	Retriever     schema.Retriever
	LLM           llms.Model
	PromptBuilder func(query string, docs []schema.Document) (string, error)
}

func NewRetrievalQA

func NewRetrievalQA(retriever schema.Retriever, llm llms.Model, opts ...RetrievalQAOption) RetrievalQA

func (RetrievalQA) Call

func (c RetrievalQA) Call(ctx context.Context, query string) (string, error)

type RetrievalQAOption added in v0.15.0

type RetrievalQAOption func(*RetrievalQA)

func WithPromptBuilder added in v0.15.0

func WithPromptBuilder(pb func(query string, docs []schema.Document) (string, error)) RetrievalQAOption

WithPromptBuilder allows passing a custom function to format the retrieved documents and query into a final string prompt.

type ValidatingRetrievalQA added in v0.2.0

type ValidatingRetrievalQA struct {
	Retriever    schema.Retriever
	GeneratorLLM llms.Model
	ValidatorLLM llms.Model
	// contains filtered or unexported fields
}

ValidatingRetrievalQA validates the relevance of retrieved context before generation.

func NewValidatingRetrievalQA added in v0.2.0

func NewValidatingRetrievalQA(retriever schema.Retriever, generator llms.Model, opts ...ValidatingRetrievalQAOption) (ValidatingRetrievalQA, error)

NewValidatingRetrievalQA creates a new ValidatingRetrievalQA chain.

func (*ValidatingRetrievalQA) Call added in v0.2.0

func (c *ValidatingRetrievalQA) Call(ctx context.Context, query string) (string, error)

type ValidatingRetrievalQAOption added in v0.2.0

type ValidatingRetrievalQAOption func(*ValidatingRetrievalQA)

func WithLogger added in v0.2.0

func WithLogger(logger *slog.Logger) ValidatingRetrievalQAOption

func WithValidator added in v0.2.0

func WithValidator(llm llms.Model) ValidatingRetrievalQAOption

Jump to

Keyboard shortcuts

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