RetrievalQAChain class
Chain for question-answering against the documents retrieved by the retriever.
It retrieves the documents using the retriever and then combines them using the combineDocumentsChain.
For convenience, you can instantiate this chain using the factory constructor RetrievalQAChain.fromLlm. By default, it uses a prompt template optimized for question answering that includes the retrieved documents and the question to answer. The documents are inserted in the prompt using a StuffDocumentsQAChain.
The chain returns two outputs:
result
(or the output key specified in the constructor): the answer to the question.source_documents
: the documents used to answer the question.
Note: as the chain returns two outputs you can only call it using the call method. The run method is not supported.
Example:
final retriever = VectorStoreRetriever(vectorStore: vectorStore);
final retrievalQA = RetrievalQAChain.fromLlm(
llm: llm,
retriever: retriever,
);
final res = await retrievalQA({
RetrievalQAChain.defaultInputKey: 'What did I say?',
});
final answer = res[RetrievalQAChain.defaultOutputKey];
final docs = res[RetrievalQAChain.sourceDocumentsOutputKey];
If you need more flexibility, you can use the primary constructor which
allows you to specify the retriever and the combineDocumentsChain.
Your prompt should include the {context}
and {question}
variables to
be replaced by the documents and the question respectively.
Example:
final llmChain = LLMChain(prompt: prompt, llm: llm);
final stuffChain = StuffDocumentsChain(llmChain: llmChain);
final retrievalQA = RetrievalQAChain(
retriever: retriever,
combineDocumentsChain: stuffChain,
);
final res = await retrievalQA({
RetrievalQAChain.defaultInputKey: 'What did I say?',
});
- Inheritance
-
- Object
- Runnable<
ChainValues, ChainOptions, ChainValues> - BaseLangChain<
ChainValues, ChainOptions, ChainValues> - BaseChain<
BaseMemory> - RetrievalQAChain
- Available Extensions
Constructors
-
RetrievalQAChain({required Retriever<
RetrieverOptions> retriever, required BaseCombineDocumentsChain combineDocumentsChain, String inputKey = defaultInputKey, String outputKey = defaultOutputKey, String combineDocumentsChainInputKey = BaseCombineDocumentsChain.defaultInputKey}) -
Chain for question-answering against the documents retrieved by the
retriever.
const
-
RetrievalQAChain.fromLlm({required BaseLanguageModel<
Object, LanguageModelOptions, LanguageModelResult< llm, required Retriever<Object> >RetrieverOptions> retriever, PromptTemplate? prompt}) -
Creates a RetrievalQAChain from a BaseLanguageModel and a
Retriever.
factory
Properties
- chainType → String
-
Return the string type key uniquely identifying this class of chain.
no setteroverride
- combineDocumentsChain → BaseCombineDocumentsChain
-
Chain to use to combine the documents.
final
- combineDocumentsChainInputKey → String
-
Key to use for inputting the documents to combineDocumentsChain.
final
- defaultOptions → ChainOptions
-
The default options to use when invoking the Runnable.
finalinherited
- hashCode → int
-
The hash code for this object.
no setterinherited
- inputKey → String
-
Key to use for the input query.
final
-
inputKeys
→ Set<
String> -
Input keys for this chain.
no setteroverride
- memory → BaseMemory?
-
Memory to use for this chain.
finalinherited
- outputKey → String
-
Key to use for output text.
final
-
outputKeys
→ Set<
String> -
Output keys for this chain.
no setteroverride
-
retriever
→ Retriever<
RetrieverOptions> -
Retriever to use.
final
- 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.
override
-
close(
) → void -
Cleans up any resources associated with it the Runnable.
inherited
-
getCompatibleOptions(
RunnableOptions? options) → ChainOptions? -
Returns the given
options
if they are compatible with the Runnable, otherwise returnsnull
.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
-
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
- defaultInputKey → const String
- Default input key for the query.
- defaultOutputKey → const String
- Default output key for the output of the chain.
- questionPromptVar → const String
- Prompt variable to use for the question.
- sourceDocumentsOutputKey → const String
- Output key to use for returning the source documents.