StuffDocumentsChain class

Chain that combines documents by stuffing into context.

This chain takes a list of documents and first combines them into a single string. It does this by formatting each document into a string with the documentPrompt and then joining them together with documentSeparator. It then adds that new resulting string to the inputs with the variable name set by llmChainStuffedDocumentPromptVar. Those inputs are then passed to the llmChain who will format the prompt and call the model.

The content of each document is formatted using documentPrompt. By default, it just takes the content of the document.

Example:

final prompt = PromptTemplate.fromTemplate(
  'Print {foo}. Context: {context}',
);
final llm = OpenAI(apiKey: openaiApiKey);
final llmChain = LLMChain(prompt: prompt, llm: llm);
final stuffChain = StuffDocumentsChain(llmChain: llmChain)
const foo = 'Hello world!';
const docs = [
  Document(pageContent: 'Hello 1!'),
  Document(pageContent: 'Hello 2!'),
];
final res = await stuffChain.call({
  'foo': foo,
  'input_documents': docs,
});
Inheritance
Implementers
Available Extensions

Constructors

StuffDocumentsChain({required LLMChain<BaseLanguageModel<Object, LanguageModelOptions, LanguageModelResult<Object>>, LanguageModelOptions, BaseMemory> llmChain, String inputKey = defaultInputKey, String outputKey = defaultOutputKey, BasePromptTemplate documentPrompt = defaultDocumentPrompt, String documentSeparator = defaultDocumentSeparator, String llmChainStuffedDocumentPromptVar = defaultLlmChainStuffedDocumentPromptVar})
Chain that combines documents by stuffing into context.

Properties

chainType String
Return the string type key uniquely identifying this class of chain.
no setteroverride
defaultOptions ChainOptions
The default options to use when invoking the Runnable.
finalinherited
documentPrompt BasePromptTemplate
Prompt to use to format each document.
final
documentSeparator String
The string with which to join the formatted documents.
final
hashCode int
The hash code for this object.
no setterinherited
inputKey String
Key to use for input documents.
finalinherited
inputKeys Set<String>
Input keys for this chain.
no setteroverride
llmChain LLMChain<BaseLanguageModel<Object, LanguageModelOptions, LanguageModelResult<Object>>, LanguageModelOptions, BaseMemory>
LLM wrapper to use after formatting documents.
final
llmChainStuffedDocumentPromptVar String
The variable name in the LLMChain.prompt where to put the documents in. If only one variable in the llmChain, this doesn't need to be provided.
getter/setter pair
memory BaseMemory?
Memory to use for this chain.
finalinherited
outputKey String
Key to use for output text.
finalinherited
outputKeys Set<String>
Output keys for this chain.
no setterinherited
runOutputKey String
Output key from where the run method needs to take the return value.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

apply(List<ChainValues> inputs) Future<List<ChainValues>>
Call the chain on all inputs in the list.
inherited
batch(List<ChainValues> inputs, {List<ChainOptions>? options}) Future<List<ChainValues>>
Batches the invocation of the Runnable on the given inputs.
inherited
bind(ChainOptions options) RunnableBinding<ChainValues, ChainOptions, ChainValues>
Binds the Runnable to the given options.
inherited
call(dynamic input, {bool returnOnlyOutputs = false}) Future<ChainValues>
Runs the core logic of this chain with the given values. If memory is not null, it will be used to load and save values.
inherited
callInternal(ChainValues inputs) Future<ChainValues>
Call method to be implemented by subclasses (called by call). This is where the core logic of the chain should be implemented.
inherited
close() → void
Cleans up any resources associated with it the Runnable.
inherited
combineDocs(List<Document> docs, {InputValues inputs = const {}}) Future<ChainValues>
Stuff all documents into one prompt and pass to LLM.
override
formatDocument(Document doc, BasePromptTemplate prompt) String
Formats a document into a string based on a prompt template.
inherited
getCompatibleOptions(RunnableOptions? options) ChainOptions?
Returns the given options if they are compatible with the Runnable, otherwise returns null.
inherited
invoke(ChainValues input, {ChainOptions? options}) Future<ChainValues>
Runs the core logic of this chain with the given input.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
pipe<NewRunOutput extends Object?, NewCallOptions extends RunnableOptions>(Runnable<ChainValues, NewCallOptions, NewRunOutput> next) RunnableSequence<ChainValues, NewRunOutput>
Pipes the output of this Runnable into another Runnable using a RunnableSequence.
inherited
promptLength(List<Document> docs, {InputValues inputs = const {}}) Future<int?>
Returns the prompt length (number of tokens) given the documents passed in.
override
run(dynamic input) Future<String>
Convenience method for executing chain when there's a single string output.
inherited
stream(ChainValues input, {ChainOptions? options}) Stream<ChainValues>
Streams the output of invoking the Runnable on the given input.
inherited
streamFromInputStream(Stream<ChainValues> inputStream, {ChainOptions? options}) Stream<ChainValues>
Streams the output of invoking the Runnable on the given inputStream.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited

Constants

defaultDocumentPrompt → const PromptTemplate
Default documentPrompt value.
defaultDocumentSeparator → const String
Default value for documentSeparator.
defaultInputKey → const String
Default inputKey value.
defaultLlmChainStuffedDocumentPromptVar → const String
Default value for llmChainStuffedDocumentPromptVar.
defaultOutputKey → const String
Default outputKey value.
pageContentPromptVar → const String
Prompt variable to use for the page content.