Skip to main content

Chains

Chains enable you to combine language models with other sources of information, third-party APIs, or even other language models. This allows you to create powerful, multi-step applications that go beyond single LLM calls.

LangChainGo provides a standard interface for chains, along with several built-in implementations for common patterns. You can also create custom chains by implementing the Chain interface.

Key Concepts

  • Sequential Processing: Chains execute steps in sequence, passing outputs between stages
  • Flexible Input/Output: Chains work with map-based inputs and outputs for maximum flexibility
  • Memory Integration: Chains can maintain conversation state across calls
  • Composability: Chains can be combined to create complex workflows

Built-in Chain Types

LLM Chain

The simplest chain that calls an LLM with a prompt template.

Sequential Chain

Chains multiple steps together, where each step's output feeds into the next.

Map-Reduce Chain

Processes large documents by mapping operations across chunks and reducing results.

Conversation Chain

Maintains conversation memory while processing new inputs.

Retrieval QA Chain

Combines document retrieval with question answering capabilities.

Executing chains

In LangChain there are multiple functions ment to execute chains.

Call

Call is the standard function used for executing an chain. The function takes a context, the chain to be executed and the input values of the chain. The input values is a map with string keys and any value. The function returns the output values of the chain and a potential error.

res, err := chains.Call(
context.Background(),
chain,
map[string]any{
"product": "colorful socks",
},
)
if err != nil {
log.Fatal(err)
}
fmt.Println(res)
map[text:

Socktastic!]

Run

If a chain only expects one input and returns a string the run function can be used to execute the chain. The privious example could therefore be written like this:

text, err := chains.Run(
context.Background(),
chain,
"colorful socks",
)
if err != nil {
log.Fatal(err)
}
fmt.Println(text)
Socktastic!

Predict

Many chains expect multiple input values and returns one string. For these cases the predict function is handy.

text, err := chains.Predict(
context.Background(),
chain,
map[string]any{
"product": "colorful socks",
"description": "The company is based in California"
}
)
if err != nil {
log.Fatal(err)
}
fmt.Println(text)

Advanced

To implement your own custom chain you must create an struct that implements the chain interface.

// Chain is the interface all chains must implement.
type Chain interface {
// Call runs the logic of the chain and returns the output. This method should
// not be called directly. Use rather the Call, Run or Predict functions that
// handles the memory and other aspects of the chain.
Call(ctx context.Context, inputs map[string]any, options ...ChainCallOption) (map[string]any, error)
// GetMemory gets the memory of the chain.
GetMemory() schema.Memory
// InputKeys returns the input keys the chain expects.
GetInputKeys() []string
// OutputKeys returns the output keys the chain expects.
GetOutputKeys() []string
}